Browse Source

add ProcessUtils UT

pull/3/MERGE
Eights-LI 4 years ago
parent
commit
2cad271749
  1. 17
      dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java
  2. 50
      dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java

17
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java

@ -65,7 +65,7 @@ public class ProcessUtils {
*/ */
public static String buildCommandStr(List<String> commandList) { public static String buildCommandStr(List<String> commandList) {
String cmdstr; String cmdstr;
String[] cmd = (String[]) commandList.toArray(); String[] cmd = commandList.toArray(new String[0]);
SecurityManager security = System.getSecurityManager(); SecurityManager security = System.getSecurityManager();
boolean allowAmbiguousCommands = false; boolean allowAmbiguousCommands = false;
if (security == null) { if (security == null) {
@ -158,7 +158,7 @@ public class ProcessUtils {
while (regexMatcher.find()) { while (regexMatcher.find()) {
matchList.add(regexMatcher.group()); matchList.add(regexMatcher.group());
} }
return (String[]) matchList.toArray(); return matchList.toArray(new String[0]);
} }
/** /**
@ -365,19 +365,24 @@ public class ProcessUtils {
*/ */
public static String getPidsStr(int processId) throws Exception { public static String getPidsStr(int processId) throws Exception {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Matcher mat; Matcher mat = null;
// pstree pid get sub pids // pstree pid get sub pids
if (OSUtils.isMacOS()) { if (OSUtils.isMacOS()) {
String pids = OSUtils.exeCmd("pstree -sp " + processId); String pids = OSUtils.exeCmd("pstree -sp " + processId);
mat = MACPATTERN.matcher(pids); if (null != pids) {
mat = MACPATTERN.matcher(pids);
}
} else { } else {
String pids = OSUtils.exeCmd("pstree -p " + processId); String pids = OSUtils.exeCmd("pstree -p " + processId);
mat = WINDOWSATTERN.matcher(pids); mat = WINDOWSATTERN.matcher(pids);
} }
while (mat.find()) { if (null != mat) {
sb.append(mat.group(1)).append(" "); while (mat.find()) {
sb.append(mat.group(1)).append(" ");
}
} }
return sb.toString().trim(); return sb.toString().trim();
} }

50
dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java

@ -14,34 +14,66 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.server.utils; package org.apache.dolphinscheduler.server.utils;
import org.junit.Assert; import static org.powermock.api.mockito.PowerMockito.when;
import org.junit.Test;
import org.slf4j.Logger; import org.apache.dolphinscheduler.common.utils.OSUtils;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({System.class, OSUtils.class})
public class ProcessUtilsTest { public class ProcessUtilsTest {
private static final Logger logger = LoggerFactory.getLogger(ProcessUtilsTest.class); @Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test @Test
public void getPidsStr() throws Exception { public void getPidsStr() throws Exception {
String pidList = ProcessUtils.getPidsStr(1); int processId = 1;
String pidList = ProcessUtils.getPidsStr(processId);
Assert.assertNotEquals("The child process of process 1 should not be empty", pidList, ""); Assert.assertNotEquals("The child process of process 1 should not be empty", pidList, "");
logger.info("Sub process list : {}", pidList);
PowerMockito.mockStatic(OSUtils.class);
when(OSUtils.isMacOS()).thenReturn(true);
when(OSUtils.exeCmd("pstree -sp " + processId)).thenReturn(null);
String pidListMac = ProcessUtils.getPidsStr(processId);
Assert.assertEquals(pidListMac, "");
} }
@Test @Test
public void testBuildCommandStr() { public void testBuildCommandStr() {
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
commands.add("sudo"); commands.add("sudo");
Assert.assertEquals(ProcessUtils.buildCommandStr(commands), "sudo"); commands.add("-u");
commands.add("tenantCode");
//allowAmbiguousCommands false
Assert.assertEquals(ProcessUtils.buildCommandStr(commands), "sudo -u tenantCode");
//quota
commands.clear();
commands.add("\"sudo\"");
Assert.assertEquals(ProcessUtils.buildCommandStr(commands), "\"sudo\"");
//allowAmbiguousCommands true
commands.clear();
commands.add("sudo");
System.setProperty("jdk.lang.Process.allowAmbiguousCommands", "false");
Assert.assertEquals(ProcessUtils.buildCommandStr(commands), "\"sudo\"");
} }
} }

Loading…
Cancel
Save