SQOOP-3283: Fixing MySQL 3rd party test hanging issue by getting username
from System env/property instead of depending on whoami and Process#waitFor
(Daniel Voros by Attila Szabo)
diff --git a/src/test/org/apache/sqoop/manager/mysql/MySQLTestUtils.java b/src/test/org/apache/sqoop/manager/mysql/MySQLTestUtils.java
index 25dbe9d..75ecc35 100644
--- a/src/test/org/apache/sqoop/manager/mysql/MySQLTestUtils.java
+++ b/src/test/org/apache/sqoop/manager/mysql/MySQLTestUtils.java
@@ -18,15 +18,11 @@
package org.apache.sqoop.manager.mysql;
-import org.apache.sqoop.SqoopOptions;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.sqoop.SqoopOptions;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.util.ArrayList;
/**
@@ -95,50 +91,19 @@
return moreArgs;
}
- public static String getCurrentUser() {
+ private static String getCurrentUser() {
// First, check the $USER environment variable.
String envUser = System.getenv("USER");
if (null != envUser) {
return envUser;
}
- // Try `whoami`
- String[] whoamiArgs = new String[1];
- whoamiArgs[0] = "whoami";
- Process p = null;
- BufferedReader r = null;
- try {
- p = Runtime.getRuntime().exec(whoamiArgs);
- InputStream is = p.getInputStream();
- r = new BufferedReader(new InputStreamReader(is));
- return r.readLine();
- } catch (IOException ioe) {
- LOG.error("IOException reading from `whoami`: " + ioe.toString());
- return null;
- } finally {
- // close our stream.
- if (null != r) {
- try {
- r.close();
- } catch (IOException ioe) {
- LOG.warn("IOException closing input stream from `whoami`: "
- + ioe.toString());
- }
- }
- // wait for whoami to exit.
- while (p != null) {
- try {
- int ret = p.waitFor();
- if (0 != ret) {
- LOG.error("whoami exited with error status " + ret);
- // suppress original return value from this method.
- return null;
- }
- } catch (InterruptedException ie) {
- continue; // loop around.
- }
- }
-
+ // Fall back to user.name system property
+ envUser = System.getProperty("user.name");
+ if (null != envUser) {
+ return envUser;
}
+ throw new RuntimeException("MySQL username not set and unable to get system user. Please set it"
+ + " with '-Dsqoop.test.mysql.username=...' or USER environment variable!");
}
public void addPasswordIfIsSet(ArrayList<String> args) {