From 2cad271749474af505357e7f21cdd1402ebc3921 Mon Sep 17 00:00:00 2001 From: Eights-LI Date: Wed, 11 Nov 2020 22:58:51 +0800 Subject: [PATCH] add ProcessUtils UT --- .../server/utils/ProcessUtils.java | 17 ++++--- .../server/utils/ProcessUtilsTest.java | 50 +++++++++++++++---- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java index a057060d8b..16acbb1143 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java @@ -65,7 +65,7 @@ public class ProcessUtils { */ public static String buildCommandStr(List commandList) { String cmdstr; - String[] cmd = (String[]) commandList.toArray(); + String[] cmd = commandList.toArray(new String[0]); SecurityManager security = System.getSecurityManager(); boolean allowAmbiguousCommands = false; if (security == null) { @@ -158,7 +158,7 @@ public class ProcessUtils { while (regexMatcher.find()) { 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 { StringBuilder sb = new StringBuilder(); - Matcher mat; + Matcher mat = null; // pstree pid get sub pids if (OSUtils.isMacOS()) { String pids = OSUtils.exeCmd("pstree -sp " + processId); - mat = MACPATTERN.matcher(pids); + if (null != pids) { + mat = MACPATTERN.matcher(pids); + } } else { String pids = OSUtils.exeCmd("pstree -p " + processId); mat = WINDOWSATTERN.matcher(pids); } - while (mat.find()) { - sb.append(mat.group(1)).append(" "); + if (null != mat) { + while (mat.find()) { + sb.append(mat.group(1)).append(" "); + } } + return sb.toString().trim(); } diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java index ace5cd8471..8a2eec3d41 100644 --- a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java +++ b/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 * limitations under the License. */ + package org.apache.dolphinscheduler.server.utils; -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import static org.powermock.api.mockito.PowerMockito.when; + +import org.apache.dolphinscheduler.common.utils.OSUtils; -import java.io.IOException; import java.util.ArrayList; 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 { - private static final Logger logger = LoggerFactory.getLogger(ProcessUtilsTest.class); + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } @Test 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, ""); - 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 public void testBuildCommandStr() { List commands = new ArrayList<>(); 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\""); } }