aloyszhang commented on code in PR #9916:
URL: https://github.com/apache/inlong/pull/9916#discussion_r1547203105


##########
inlong-agent/agent-installer/src/test/java/installer/TestModuleManager.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package installer;
+
+import org.apache.inlong.agent.installer.ModuleManager;
+import org.apache.inlong.common.pojo.agent.installer.ConfigResult;
+import org.apache.inlong.common.pojo.agent.installer.ModuleConfig;
+import org.apache.inlong.common.pojo.agent.installer.ModuleStateEnum;
+import org.apache.inlong.common.pojo.agent.installer.PackageConfig;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+import static org.awaitility.Awaitility.await;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ModuleManager.class)
+@PowerMockIgnore({"javax.management.*"})
+public class TestModuleManager {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TestModuleManager.class);
+    private static BaseTestsHelper helper;
+    private static final ClassLoader LOADER = 
TestModuleManager.class.getClassLoader();
+    private static ModuleManager manager;
+    private static List<Action> realActionList = new ArrayList<>();
+    private static List<Action> expectedActionList = new ArrayList<>();
+    private static String OLD_MD5 = "95648c83b45971dce503d5d844496cfc";
+    private static String NEW_MD5 = "e573f399da60ddeff09904bb95bdc307";
+
+    @Data
+    @Builder
+    @AllArgsConstructor
+    @NoArgsConstructor
+    private static class Action {
+
+        public ModuleActionTypeEnum type;
+        public String md5;
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            Action action = (Action) o;
+            return Objects.equals(type, action.type) &&
+                    Objects.equals(md5, action.md5);
+        }
+    }
+
+    @BeforeClass
+    public static void setup() {
+        helper = new 
BaseTestsHelper(TestModuleManager.class.getName()).setupAgentHome();
+        manager = PowerMockito.spy(new ModuleManager());
+    }
+
+    @AfterClass
+    public static void teardown() throws Exception {
+        manager.stop();
+    }
+
+    private void fillExpectedActionList() {
+        expectedActionList.add(new Action(ModuleActionTypeEnum.STOP, OLD_MD5));
+        expectedActionList.add(new Action(ModuleActionTypeEnum.UNINSTALL, 
OLD_MD5));
+        expectedActionList.add(new Action(ModuleActionTypeEnum.DOWNLOAD, 
NEW_MD5));
+        expectedActionList.add(new Action(ModuleActionTypeEnum.INSTALL, 
NEW_MD5));
+        expectedActionList.add(new Action(ModuleActionTypeEnum.START, 
NEW_MD5));
+    }
+
+    private void mockFunctions() {
+        try {
+            PowerMockito.doAnswer(invocation -> {
+                ModuleConfig module = invocation.getArgument(0);
+                realActionList.add(new Action(ModuleActionTypeEnum.DOWNLOAD, 
module.getPackageConfig().getMd5()));
+                return true;
+            }).when(manager, "downloadModule", Mockito.any());
+
+            PowerMockito.doAnswer(invocation -> {
+                ModuleConfig module = invocation.getArgument(0);
+                realActionList.add(new Action(ModuleActionTypeEnum.INSTALL, 
module.getPackageConfig().getMd5()));
+                return true;
+            }).when(manager, "installModule", Mockito.any());
+
+            PowerMockito.doAnswer(invocation -> {
+                ModuleConfig module = invocation.getArgument(0);
+                realActionList.add(new Action(ModuleActionTypeEnum.UNINSTALL, 
module.getPackageConfig().getMd5()));
+                return true;
+            }).when(manager, "uninstallModule", Mockito.any());
+
+            PowerMockito.doAnswer(invocation -> {
+                ModuleConfig module = invocation.getArgument(0);
+                realActionList.add(new Action(ModuleActionTypeEnum.START, 
module.getPackageConfig().getMd5()));
+                return true;
+            }).when(manager, "startModule", Mockito.any());
+
+            PowerMockito.doAnswer(invocation -> {
+                ModuleConfig module = invocation.getArgument(0);
+                realActionList.add(new Action(ModuleActionTypeEnum.STOP, 
module.getPackageConfig().getMd5()));
+                return true;
+            }).when(manager, "stopModule", Mockito.any());
+
+            PowerMockito.doAnswer(invocation -> {
+                ModuleConfig module = invocation.getArgument(0);
+                return true;
+            }).when(manager, "isProcessAllStarted", Mockito.any());
+
+            PowerMockito.doReturn(null).when(manager, "getHttpManager", 
Mockito.any());
+        } catch (Exception e) {
+            LOGGER.error("mock downloadModule error", e);
+            Assert.assertTrue(false);
+            return;
+        }
+    }
+
+    @Test
+    public void testModuleManager() {
+        fillExpectedActionList();
+        mockFunctions();
+        String confPath = LOADER.getResource("conf/").getPath();
+        manager.restoreFromLocalFile(confPath);
+        
Assert.assertTrue(manager.getModule(1).getPackageConfig().getMd5().equals(OLD_MD5));
+        manager.submitConfig(getConfig());
+        try {
+            manager.start();
+        } catch (Exception e) {
+            LOGGER.error("start module manager error", e);
+            Assert.assertTrue("start module manager error", false);

Review Comment:
   ```suggestion
               Assert.fail("start module manager error");
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to