From 7aa187e82e782c619e3a78a7c8af522286ca6c3f Mon Sep 17 00:00:00 2001 From: TLPC <470193496@qq.com> Date: Tue, 19 Sep 2023 14:23:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?AgentCore=E5=8A=A8=E6=80=81=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E5=8D=B8=E8=BD=BD=E6=B5=8B=E8=AF=95:=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => agentcore-common}/action.yml | 6 +- .../scenarios/agentcore/dynamic/action.yml | 92 +++++++++++++++++ .../workflows/agentcore_integration_test.yml | 27 ++++- .../agentcore-integration-test/pom.xml | 20 ++-- .../dynamic/DynamicInstallPluginTest.java | 38 +++++++ .../dynamic/DynamicReInstallAgentTest.java | 38 +++++++ .../dynamic/DynamicUninstallAgentTest.java | 38 +++++++ .../dynamic/DynamicUninstallPluginTest.java | 38 +++++++ .../dynamicconfig/DynamicConfigTest.java | 38 +++++++ .../RequestUtils.java} | 28 +++--- .../application/AgentCoreTestApplication.java | 72 ++------------ .../controller/TestController.java | 55 +++++++++++ .../application/results/DynamicResults.java | 87 ++++++++++++++++ .../application/router/ControllerHandler.java | 98 +++++++++++++++++++ .../test/application/router/RouterPath.java | 58 +++++++++++ .../dynamic-test-first-plugin/pom.xml | 47 +++++++++ .../plugin/declarer/TestDynamicDeclarer.java | 44 +++++++++ .../interceptor/RepeatEnhanceInterceptor.java | 46 +++++++++ ....core.plugin.agent.declarer.PluginDeclarer | 1 + .../dynamic-test-second-plugin/pom.xml | 47 +++++++++ .../plugin/declarer/TestDynamicDeclarer.java | 44 +++++++++ .../interceptor/RepeatEnhanceInterceptor.java | 46 +++++++++ ....core.plugin.agent.declarer.PluginDeclarer | 1 + .../agentcore-test/pom.xml | 4 + .../scripts/AgentLoader.java | 65 ++++++++++++ suppressions.xml | 3 + 26 files changed, 984 insertions(+), 97 deletions(-) rename .github/actions/scenarios/agentcore/{ => agentcore-common}/action.yml (82%) create mode 100644 .github/actions/scenarios/agentcore/dynamic/action.yml create mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java rename sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/{AgentcoreTest.java => utils/RequestUtils.java} (84%) create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/ControllerHandler.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/pom.xml create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/declarer/TestDynamicDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/interceptor/RepeatEnhanceInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/pom.xml create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/declarer/TestDynamicDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/interceptor/RepeatEnhanceInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer create mode 100644 sermant-integration-tests/scripts/AgentLoader.java diff --git a/.github/actions/scenarios/agentcore/action.yml b/.github/actions/scenarios/agentcore/agentcore-common/action.yml similarity index 82% rename from .github/actions/scenarios/agentcore/action.yml rename to .github/actions/scenarios/agentcore/agentcore-common/action.yml index fd43deb2ee..c2415fcb0e 100644 --- a/.github/actions/scenarios/agentcore/action.yml +++ b/.github/actions/scenarios/agentcore/agentcore-common/action.yml @@ -1,12 +1,12 @@ name: "Agent-core Common Test" -description: "Auto test for spring common, include nacos dynamic config" +description: "Auto test for agent-core common, include nacos dynamic config" runs: using: "composite" steps: - name: entry uses: ./.github/actions/common/entry with: - log-dir: ./logs/agentcore-test + log-dir: ./logs/agentcore-test/dynamic-config - name: start applications shell: bash env: @@ -23,7 +23,7 @@ runs: bash ./sermant-integration-tests/scripts/checkService.sh http://127.0.0.1:8915/ping 120 - name: agentcore test module start shell: bash - run: mvn test --file sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml + run: mvn test -Dagentcore.test.type=DYNAMIC_CONFIG --file sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml - name: exit if: always() uses: ./.github/actions/common/exit diff --git a/.github/actions/scenarios/agentcore/dynamic/action.yml b/.github/actions/scenarios/agentcore/dynamic/action.yml new file mode 100644 index 0000000000..9d25c7faec --- /dev/null +++ b/.github/actions/scenarios/agentcore/dynamic/action.yml @@ -0,0 +1,92 @@ +name: "Agent-core Dynamic Test" +description: "Auto test for agent-core dynamic" +runs: + using: "composite" + steps: + - name: entry + uses: ./.github/actions/common/entry + with: + log-dir: ./logs/agentcore-test/dynamic + - name: compile AgentLoader + shell: bash + run: | + cp sermant-integration-tests/scripts/AgentLoader.java ./ + javac -classpath ./:${{ env.JAVA_HOME}}/lib/tools.jar AgentLoader.java + - name: start applications + shell: bash + env: + dynamic.config.serverAddress: 127.0.0.1:2181 + dynamic.config.dynamicConfigType: ZOOKEEPER + service.meta.project: TestAgentCore + run: | + nohup java -jar \ + sermant-agent-${{ env.sermantVersion }}/agent/agentcore-test-application-1.0.0-jar-with-dependencies.jar > ${{ env.logDir + }}/agentcore-test.log 2>&1 & + - name: waiting for agentcore services start + shell: bash + run: | + bash ./sermant-integration-tests/scripts/checkService.sh http://127.0.0.1:8915/ping 120 + - name: (install agent) modify plugins file + shell: bash + run: | + sed -i '/dynamic-test-first-plugin/d' sermant-agent-${{ env.sermantVersion }}/agent/config/plugins.yaml + sed -i '/dynamic-test-second-plugin/d' sermant-agent-${{ env.sermantVersion }}/agent/config/plugins.yaml + sed -i '/ passive:/a \ - dynamic-test-first-plugin' sermant-agent-${{ env.sermantVersion }}/agent/config/plugins.yaml + sed -i '/ passive:/a \ - dynamic-test-second-plugin' sermant-agent-${{ env.sermantVersion }}/agent/config/plugins.yaml + - name: (install agent) dynamic install agent + shell: bash + run: java -classpath ./:${{ env.JAVA_HOME}}/lib/tools.jar AgentLoader sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar + - name: Sleep for 5 Seconds + shell: bash + run: sleep 5 + - name: (install plugin) dynamic install plugin + shell: bash + run: java -classpath ./:${{ env.JAVA_HOME}}/lib/tools.jar AgentLoader sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar command=INSTALL-PLUGINS:dynamic-test-first-plugin/dynamic-test-second-plugin + - name: Sleep for 5 Seconds + shell: bash + run: sleep 5 + - name: (install plugin) test install plugin + shell: bash + run: mvn test -Dagentcore.test.type=INSTALL_PLUGIN --file sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml + - name: (uninstall plugin) dynamic uninstall plugin + shell: bash + run: java -classpath ./:${{ env.JAVA_HOME}}/lib/tools.jar AgentLoader sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar command=UNINSTALL-PLUGINS:dynamic-test-first-plugin + - name: Sleep for 5 Seconds + shell: bash + run: sleep 5 + - name: (uninstall plugin) test uninstall plugin + shell: bash + run: mvn test -Dagentcore.test.type=UNINSTALL_PLUGIN --file sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml + - name: (uninstall agent) dynamic uninstall agent + shell: bash + run: java -classpath ./:${{ env.JAVA_HOME}}/lib/tools.jar AgentLoader sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar command=UNINSTALL-AGENT + - name: Sleep for 5 Seconds + shell: bash + run: sleep 5 + - name: (uninstall agent) test uninstall agent + shell: bash + run: mvn test -Dagentcore.test.type=UNINSTALL_AGENT --file sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml + - name: (reinstall agent) dynamic reinstall agent + shell: bash + run: java -classpath ./:${{ env.JAVA_HOME}}/lib/tools.jar AgentLoader sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar command=INSTALL-PLUGINS:dynamic-test-first-plugin/dynamic-test-second-plugin + - name: Sleep for 5 Seconds + shell: bash + run: sleep 5 + - name: (reinstall agent) test reinstall agent + shell: bash + run: mvn test -Dagentcore.test.type=REINSTALL_AGENT --file sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml + - name: exit + if: always() + uses: ./.github/actions/common/exit + with: + processor-keyword: agentcore + - name: if failure then upload error log + uses: actions/upload-artifact@v3 + if: ${{ failure() || cancelled() }} + with: + name: agentcore-test-logs + path: | + ./*.log + ./logs/** + if-no-files-found: warn + retention-days: 2 diff --git a/.github/workflows/agentcore_integration_test.yml b/.github/workflows/agentcore_integration_test.yml index 27d395cf70..d391540ef2 100644 --- a/.github/workflows/agentcore_integration_test.yml +++ b/.github/workflows/agentcore_integration_test.yml @@ -77,11 +77,15 @@ jobs: - name: move test plugin run: | mkdir -p sermant-agent-${{ env.sermantVersion }}/agent/pluginPackage/agentcore-test-plugin/plugin + mkdir -p sermant-agent-${{ env.sermantVersion }}/agent/pluginPackage/dynamic-test-first-plugin/plugin + mkdir -p sermant-agent-${{ env.sermantVersion }}/agent/pluginPackage/dynamic-test-second-plugin/plugin cp ./sermant-integration-tests/agentcore-test/agentcore-test-plugin/target/agentcore-test-plugin-1.0.0.jar sermant-agent-${{ env.sermantVersion }}/agent/pluginPackage/agentcore-test-plugin/plugin/ + cp ./sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/target/dynamic-test-first-plugin-1.0.0.jar sermant-agent-${{ env.sermantVersion }}/agent/pluginPackage/dynamic-test-first-plugin/plugin/ + cp ./sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/target/dynamic-test-second-plugin-1.0.0.jar sermant-agent-${{ env.sermantVersion }}/agent/pluginPackage/dynamic-test-second-plugin/plugin/ cp ./sermant-integration-tests/agentcore-test/agentcore-test-application/target/agentcore-test-application-1.0.0-jar-with-dependencies.jar sermant-agent-${{ env.sermantVersion }}/agent/ sed -i '/plugins:/a \ - agentcore-test-plugin' sermant-agent-${{ env.sermantVersion }}/agent/config/plugins.yaml - test-for-agentcore: - name: Test for agentcore + test-for-agentcore-common: + name: Test for agentcore common runs-on: ubuntu-latest needs: [ build-agent-and-cache, download-midwares-and-cache ] steps: @@ -93,7 +97,22 @@ jobs: echo "javaVersion=8" >> $GITHUB_ENV - name: common operations uses: ./.github/actions/common/agentcore - - name: start agentcore test - uses: ./.github/actions/scenarios/agentcore + - name: start agentcore common test + uses: ./.github/actions/scenarios/agentcore/agentcore-common + test-for-agentcore-dynamic: + name: Test for agentcore dynamic + runs-on: ubuntu-latest + needs: [build-agent-and-cache, download-midwares-and-cache] + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 100 + - name: set java version to environment + run: | + echo "javaVersion=8" >> $GITHUB_ENV + - name: common operations + uses: ./.github/actions/common/agentcore + - name: start dynamic test + uses: ./.github/actions/scenarios/agentcore/dynamic diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml b/sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml index 9d93878d3f..73f7cf0e0e 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/pom.xml @@ -14,6 +14,7 @@ 8 8 + 3.0.0 @@ -55,15 +56,14 @@ - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven.surefire.plugin.version} + + + \ No newline at end of file diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java new file mode 100644 index 0000000000..b635ab308b --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; + +import com.huaweicloud.agentcore.test.request.utils.RequestUtils; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +import java.io.IOException; + +/** + * 动态安装插件测试方法,采用http请求调用方式测试 + * + * @author tangle + * @since 2023-09-26 + */ +@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "INSTALL_PLUGIN") +public class DynamicInstallPluginTest { + @Test + public void testInstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testInstallPlugin"); + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java new file mode 100644 index 0000000000..8f0ee59932 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; + +import com.huaweicloud.agentcore.test.request.utils.RequestUtils; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +import java.io.IOException; + +/** + * 动态重装Agent测试方法,采用http请求调用方式测试 + * + * @author tangle + * @since 2023-09-26 + */ +@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "REINSTALL_AGENT") +public class DynamicReInstallAgentTest { + @Test + public void testInstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testReInstallAgent"); + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java new file mode 100644 index 0000000000..9c28e492b5 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; + +import com.huaweicloud.agentcore.test.request.utils.RequestUtils; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +import java.io.IOException; + +/** + * 动态卸载Agent测试方法,采用http请求调用方式测试 + * + * @author tangle + * @since 2023-09-26 + */ +@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "UNINSTALL_AGENT") +public class DynamicUninstallAgentTest { + @Test + public void testInstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testUninstallAgent"); + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java new file mode 100644 index 0000000000..8066f1b093 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; + +import com.huaweicloud.agentcore.test.request.utils.RequestUtils; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +import java.io.IOException; + +/** + * 动态卸载插件测试方法,采用http请求调用方式测试 + * + * @author tangle + * @since 2023-09-26 + */ +@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "UNINSTALL_PLUGIN") +public class DynamicUninstallPluginTest { + @Test + public void testInstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testUninstallPlugin"); + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java new file mode 100644 index 0000000000..86ae5cf6cf --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.request.dynamicconfig; + +import com.huaweicloud.agentcore.test.request.utils.RequestUtils; + +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +/** + * 动态配置测试方法,采用http请求调用方式测试 + * + * @author tangle + * @since 2023-09-07 + */ +@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "DYNAMIC_CONFIG") +public class DynamicConfigTest { + @Test + public void testDynamicConfig() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testDynamicConfig"); + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/AgentcoreTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/utils/RequestUtils.java similarity index 84% rename from sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/AgentcoreTest.java rename to sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/utils/RequestUtils.java index 9c4e644bd8..f4b1dea46e 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/AgentcoreTest.java +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/utils/RequestUtils.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package com.huaweicloud.agentcore.test.request; +package com.huaweicloud.agentcore.test.request.utils; + +import com.huaweicloud.agentcore.test.request.dynamicconfig.DynamicConfigTest; import com.alibaba.fastjson.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -27,32 +27,28 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** - * agentcore测试方法,采用http请求调用方式测试 + * http请求工具类 * * @author tangle - * @since 2023-09-07 + * @since 2023-09-26 */ -public class AgentcoreTest { - private static final Logger LOGGER = LoggerFactory.getLogger(AgentcoreTest.class); - - @Test - public void testAgentcore() throws IOException { - testRequest("http://127.0.0.1:8915/testDynamicConfig"); - } +public class RequestUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtils.class); /** * 对测试请求结果进行断言判断 * * @param url 测试请求接口的url */ - public void testRequest(String url) throws IOException { + public static void testRequest(String url) throws IOException { String response = doGet(url); Assertions.assertNotEquals("", response, url + " Request Error. "); Map resultMap = convertHttpEntityToMap(response); @@ -67,7 +63,7 @@ public void testRequest(String url) throws IOException { * @param url http请求url * @return 响应体body */ - private String doGet(String url) { + private static String doGet(String url) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { RequestConfig requestConfig = RequestConfig.custom() .build(); @@ -93,7 +89,7 @@ private String doGet(String url) { * @param response JSON数据 * @return map数据 */ - private Map convertHttpEntityToMap(String response) throws IOException { + private static Map convertHttpEntityToMap(String response) throws IOException { Map result = new HashMap<>(); JSONObject jsonObject = JSONObject.parseObject(response); for (String key : jsonObject.keySet()) { diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/AgentCoreTestApplication.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/AgentCoreTestApplication.java index 09f0b4d9ab..c5dcfe787d 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/AgentCoreTestApplication.java +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/AgentCoreTestApplication.java @@ -16,16 +16,14 @@ package com.huaweicloud.agentcore.test.application; -import com.huaweicloud.agentcore.test.application.controller.TestController; +import com.huaweicloud.agentcore.test.application.router.ControllerHandler; +import com.huaweicloud.agentcore.test.application.router.RouterPath; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import java.io.IOException; -import java.io.OutputStream; +import java.lang.reflect.Field; import java.net.InetSocketAddress; -import java.nio.charset.StandardCharsets; /** * 测试应用启动类 @@ -36,76 +34,22 @@ public class AgentCoreTestApplication { private static final int SERVER_PORT = 8915; - private static final int REQUEST_SUCCESS_CODE = 200; - - private static final int REQUEST_FAILED_CODE = 404; - - private static final String REQUEST_URL_NOT_FOUND = "No such request path."; - - private static final String REQUEST_PATH_PING = "/ping"; - - private static final String REQUEST_PATH_DYNAMIC_CONFIG = "/testDynamicConfig"; - /** * 启动main方法 * * @param args * @throws IOException + * @throws IllegalAccessException */ - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, IllegalAccessException { HttpServer server = HttpServer.create(new InetSocketAddress(SERVER_PORT), 0); // 添加URL路由 - server.createContext(REQUEST_PATH_PING, new ControllerHandler()); - server.createContext(REQUEST_PATH_DYNAMIC_CONFIG, new ControllerHandler()); + for (Field field : RouterPath.class.getDeclaredFields()) { + server.createContext(String.valueOf(field.get(null)), new ControllerHandler()); + } // 启动服务器 server.start(); } - - /** - * 封装的http服务端 - * - * @author tangle - * @since 2023-09-08 - */ - static class ControllerHandler implements HttpHandler { - private final TestController testController; - - ControllerHandler() { - this.testController = new TestController(); - } - - @Override - public void handle(HttpExchange exchange) throws IOException { - // 获取输出流 - OutputStream os = exchange.getResponseBody(); - String response = setResponse(exchange); - int responseCode = response.equals(REQUEST_URL_NOT_FOUND) ? REQUEST_FAILED_CODE : REQUEST_SUCCESS_CODE; - - // 构建响应消息 - exchange.sendResponseHeaders(responseCode, response.length()); - - // 发送响应消息 - os.write(response.getBytes(StandardCharsets.UTF_8)); - os.close(); - } - - /** - * 根据请求url分配执行方法 - * - * @param exchange - * @return - */ - private String setResponse(HttpExchange exchange) { - switch (exchange.getRequestURI().getPath()) { - case REQUEST_PATH_PING: - return testController.ping(); - case REQUEST_PATH_DYNAMIC_CONFIG: - return testController.testDynamicConfig(); - default: - return REQUEST_URL_NOT_FOUND; - } - } - } } diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java index 2be3e19c22..464fef9ee4 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java @@ -17,6 +17,7 @@ package com.huaweicloud.agentcore.test.application.controller; import com.huaweicloud.agentcore.test.application.results.DynamicConfigResults; +import com.huaweicloud.agentcore.test.application.results.DynamicResults; import com.huaweicloud.agentcore.test.application.tests.dynamicconfig.DynamicConfigTest; import com.alibaba.fastjson.JSONObject; @@ -60,6 +61,60 @@ public String testDynamicConfig() { return jsonObject.toJSONString(); } + /** + * 测试动态安装插件 + * + * @return 测试结果 + */ + public String testInstallPlugin() { + Map resultMap = new HashMap<>(); + resultMap.put(DynamicResults.DYNAMIC_INSTALL_PLUGIN_REPEAT_ENHANCE.name(), + DynamicResults.DYNAMIC_INSTALL_PLUGIN_REPEAT_ENHANCE.getResult()); + JSONObject jsonObject = new JSONObject(resultMap); + return jsonObject.toJSONString(); + } + + /** + * 测试动态卸载插件 + * + * @return 测试结果 + */ + public String testUninstallPlugin() { + Map resultMap = new HashMap<>(); + resultMap.put(DynamicResults.DYNAMIC_UNINSTALL_PLUGIN_INTERCEPTOR_FAILURE.name(), + DynamicResults.DYNAMIC_UNINSTALL_PLUGIN_INTERCEPTOR_FAILURE.getResult()); + resultMap.put(DynamicResults.DYNAMIC_UNINSTALL_REPEAT_ENHANCE.name(), + DynamicResults.DYNAMIC_UNINSTALL_REPEAT_ENHANCE.getResult()); + JSONObject jsonObject = new JSONObject(resultMap); + return jsonObject.toJSONString(); + } + + /** + * 测试动态卸载agent + * + * @return 测试结果 + */ + public String testUninstallAgent() { + Map resultMap = new HashMap<>(); + resultMap.put(DynamicResults.DYNAMIC_UNINSTALL_AGENT_PLUGIN_FAILURE.name(), + DynamicResults.DYNAMIC_UNINSTALL_AGENT_PLUGIN_FAILURE.getResult()); + JSONObject jsonObject = new JSONObject(resultMap); + return jsonObject.toJSONString(); + } + + /** + * 测试动态重装agent + * + * @return 测试结果 + */ + public String testReInstallAgent() { + Map resultMap = new HashMap<>(); + resultMap.put(DynamicResults.DYNAMIC_REINSTALL_AGENT_PLUGIN_SUCCESS.name(), + DynamicResults.DYNAMIC_REINSTALL_AGENT_PLUGIN_SUCCESS.getResult()); + JSONObject jsonObject = new JSONObject(resultMap); + return jsonObject.toJSONString(); + } + private String buildExceptionKey(Exception e) { return "Unexpected exception occurs: " + e.getMessage(); } diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java new file mode 100644 index 0000000000..7f84262a8b --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.application.results; + +/** + * 动态安装卸载测试用例结果 + * + * @author tangle + * @since 2023-09-08 + */ +public enum DynamicResults { + /** + * 动态安装插件重复增强不干扰 + */ + DYNAMIC_INSTALL_PLUGIN_REPEAT_ENHANCE("Test dynamic install plugin repetitive enhancement."), + + /** + * 动态卸载插件,拦截点失效 + */ + DYNAMIC_UNINSTALL_PLUGIN_INTERCEPTOR_FAILURE("Test dynamic uninstall plugin, plugin failure."), + + /** + * 动态卸载插件,服务关闭 + */ + DYNAMIC_UNINSTALL_SERVICE_CLOSE("Test dynamic uninstall plugin service close."), + + /** + * 动态卸载插件,对已有拦截点不影响 + */ + DYNAMIC_UNINSTALL_REPEAT_ENHANCE("Test dynamic uninstall plugin not effect other interceptor."), + + /** + * 动态卸载AGENT,插件失效 + */ + DYNAMIC_UNINSTALL_AGENT_PLUGIN_FAILURE("Test dynamic uninstall, plugin failure."), + + /** + * 再次安装AGENT,插件生效 + */ + DYNAMIC_REINSTALL_AGENT_PLUGIN_SUCCESS("Test dynamic reinstall agent, plugin success."); + + /** + * 用例描述 + */ + private String description; + + /** + * 测试结果标识 + */ + private boolean result; + + /** + * 构造函数 + * + * @param description 用例描述 + */ + DynamicResults(String description) { + this.description = description; + this.result = true; + } + + public boolean getResult() { + return result; + } + + public void setResult(boolean result) { + this.result = result; + } + + public String getDescription() { + return description; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/ControllerHandler.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/ControllerHandler.java new file mode 100644 index 0000000000..8374d2c3b8 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/ControllerHandler.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.application.router; + +import com.huaweicloud.agentcore.test.application.controller.TestController; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; + +/** + * 封装的http服务端 + * + * @author tangle + * @since 2023-09-08 + */ +public class ControllerHandler implements HttpHandler { + private static final int REQUEST_SUCCESS_CODE = 200; + + private static final int REQUEST_NOT_FOUND_CODE = 404; + + private static final int REQUEST_FAILED_CODE = 500; + + private static final String REQUEST_URL_NOT_FOUND = "No such request path."; + + private static final String REQUEST_PARAMS_ERROR = "Request params error."; + + private final TestController testController; + + /** + * 构造函数 + */ + public ControllerHandler() { + this.testController = new TestController(); + } + + @Override + public void handle(HttpExchange exchange) throws IOException { + String response = setResponse(exchange); + int responseCode = REQUEST_SUCCESS_CODE; + if (response.equals(REQUEST_URL_NOT_FOUND)) { + responseCode = REQUEST_NOT_FOUND_CODE; + } + if (response.equals(REQUEST_PARAMS_ERROR)) { + responseCode = REQUEST_FAILED_CODE; + } + + // 构建响应消息 + exchange.sendResponseHeaders(responseCode, response.length()); + + // 发送响应消息 + OutputStream os = exchange.getResponseBody(); + os.write(response.getBytes(StandardCharsets.UTF_8)); + os.close(); + } + + /** + * 根据请求url分配执行方法 + * + * @param exchange + * @return + */ + private String setResponse(HttpExchange exchange) { + switch (exchange.getRequestURI().getPath()) { + case RouterPath.REQUEST_PATH_PING: + return testController.ping(); + case RouterPath.REQUEST_PATH_DYNAMIC_CONFIG: + return testController.testDynamicConfig(); + case RouterPath.REQUEST_PATH_INSTALL_PLUGIN: + return testController.testInstallPlugin(); + case RouterPath.REQUEST_PATH_UNINSTALL_PLUGIN: + return testController.testUninstallPlugin(); + case RouterPath.REQUEST_PATH_UNINSTALL_AGENT: + return testController.testUninstallAgent(); + case RouterPath.REQUEST_PATH_REINSTALL_AGENT: + return testController.testReInstallAgent(); + default: + return REQUEST_URL_NOT_FOUND; + } + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java new file mode 100644 index 0000000000..cbf7c109b5 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.application.router; + +/** + * http请求路由地址 + * + * @author tangle + * @since 2023-09-26 + */ +public class RouterPath { + /** + * 验证服务启动成功的地址 + */ + public static final String REQUEST_PATH_PING = "/ping"; + + /** + * 测试动态配置请求地址 + */ + public static final String REQUEST_PATH_DYNAMIC_CONFIG = "/testDynamicConfig"; + + /** + * 测试动态安装插件请求地址 + */ + public static final String REQUEST_PATH_INSTALL_PLUGIN = "/testInstallPlugin"; + + /** + * 测试动态卸载插件请求地址 + */ + public static final String REQUEST_PATH_UNINSTALL_PLUGIN = "/testUninstallPlugin"; + + /** + * 测试动态卸载Agent请求地址 + */ + public static final String REQUEST_PATH_UNINSTALL_AGENT = "/testUninstallAgent"; + + /** + * 测试动态重装Agent请求地址 + */ + public static final String REQUEST_PATH_REINSTALL_AGENT = "/testReInstallAgent"; + + private RouterPath() { + } +} diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/pom.xml b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/pom.xml new file mode 100644 index 0000000000..0328b1dd9e --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/pom.xml @@ -0,0 +1,47 @@ + + + agentcore-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + dynamic-test-first-plugin + + + ${project.artifactId} + ${project.version} + 8 + 8 + plugin + + + + + com.huaweicloud.sermant + sermant-agentcore-core + provided + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + + org.apache.maven.plugins + maven-jar-plugin + + + + ${package.plugin.name} + ${package.plugin.version} + + + + + + + diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/declarer/TestDynamicDeclarer.java b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/declarer/TestDynamicDeclarer.java new file mode 100644 index 0000000000..d0d7dcea65 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/declarer/TestDynamicDeclarer.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.dynamic.test.first.plugin.declarer; + +import com.huaweicloud.dynamic.test.first.plugin.interceptor.RepeatEnhanceInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 动态安装卸载测试first插件的增强类 + * + * @author tangle + * @since 2023-09-26 + */ +public class TestDynamicDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameEquals( + "com.huaweicloud.agentcore.test.application.tests.dynamic.DynamicTest"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.nameEquals("repeatEnhance"), new RepeatEnhanceInterceptor()) + }; + } +} diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/interceptor/RepeatEnhanceInterceptor.java b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/interceptor/RepeatEnhanceInterceptor.java new file mode 100644 index 0000000000..b880549cb9 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/java/com/huaweicloud/dynamic/test/first/plugin/interceptor/RepeatEnhanceInterceptor.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.dynamic.test.first.plugin.interceptor; + +import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * 动态安装卸载测试first插件的拦截器 + * + * @author tangle + * @since 2023-09-26 + */ +public class RepeatEnhanceInterceptor extends AbstractInterceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(); + + @Override + public ExecuteContext before(ExecuteContext context) { + context.getArguments()[0] = true; + LOGGER.log(Level.INFO, "Test repeat enhance, first plugin enhance success"); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer new file mode 100644 index 0000000000..bb4b782d2d --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-first-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -0,0 +1 @@ +com.huaweicloud.dynamic.test.first.plugin.declarer.TestDynamicDeclarer \ No newline at end of file diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/pom.xml b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/pom.xml new file mode 100644 index 0000000000..68955fe0d5 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/pom.xml @@ -0,0 +1,47 @@ + + + agentcore-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + dynamic-test-second-plugin + + + ${project.artifactId} + ${project.version} + 8 + 8 + plugin + + + + + com.huaweicloud.sermant + sermant-agentcore-core + provided + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + + org.apache.maven.plugins + maven-jar-plugin + + + + ${package.plugin.name} + ${package.plugin.version} + + + + + + + diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/declarer/TestDynamicDeclarer.java b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/declarer/TestDynamicDeclarer.java new file mode 100644 index 0000000000..cf0a42ea3b --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/declarer/TestDynamicDeclarer.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.dynamic.test.second.plugin.declarer; + +import com.huaweicloud.dynamic.test.second.plugin.interceptor.RepeatEnhanceInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 动态安装卸载测试second插件的增强类 + * + * @author tangle + * @since 2023-09-26 + */ +public class TestDynamicDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameEquals( + "com.huaweicloud.agentcore.test.application.tests.dynamic.DynamicTest"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.nameEquals("repeatEnhance"), new RepeatEnhanceInterceptor()) + }; + } +} diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/interceptor/RepeatEnhanceInterceptor.java b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/interceptor/RepeatEnhanceInterceptor.java new file mode 100644 index 0000000000..26d3e9a356 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/java/com/huaweicloud/dynamic/test/second/plugin/interceptor/RepeatEnhanceInterceptor.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.dynamic.test.second.plugin.interceptor; + +import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * 动态安装卸载测试second插件的拦截器 + * + * @author tangle + * @since 2023-09-26 + */ +public class RepeatEnhanceInterceptor extends AbstractInterceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(); + + @Override + public ExecuteContext before(ExecuteContext context) { + context.getArguments()[1] = true; + LOGGER.log(Level.INFO, "Test repeat enhance, second plugin enhance success"); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer new file mode 100644 index 0000000000..4e77456eda --- /dev/null +++ b/sermant-integration-tests/agentcore-test/dynamic-test-second-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -0,0 +1 @@ +com.huaweicloud.dynamic.test.second.plugin.declarer.TestDynamicDeclarer \ No newline at end of file diff --git a/sermant-integration-tests/agentcore-test/pom.xml b/sermant-integration-tests/agentcore-test/pom.xml index 97a9efc0a5..3e1260c60d 100644 --- a/sermant-integration-tests/agentcore-test/pom.xml +++ b/sermant-integration-tests/agentcore-test/pom.xml @@ -15,6 +15,8 @@ agentcore-test-plugin agentcore-test-application + dynamic-test-first-plugin + dynamic-test-second-plugin @@ -41,6 +43,8 @@ agentcore-test-plugin agentcore-test-application + dynamic-test-first-plugin + dynamic-test-second-plugin diff --git a/sermant-integration-tests/scripts/AgentLoader.java b/sermant-integration-tests/scripts/AgentLoader.java new file mode 100644 index 0000000000..033fe39ecd --- /dev/null +++ b/sermant-integration-tests/scripts/AgentLoader.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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. + */ + +import com.sun.tools.attach.AgentInitializationException; +import com.sun.tools.attach.AgentLoadException; +import com.sun.tools.attach.AttachNotSupportedException; +import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.VirtualMachineDescriptor; + +import java.io.IOException; +import java.util.List; + +/** + * AgentLoader mainClass + * + * @author tangle + * @since 2023-09-26 + */ +public class AgentLoader { + private AgentLoader() { + } + + /** + * AgentLoader 的main方法 + * + * @param args 入参,args[0]:agentpath, args[1]:agentArgs + */ + public static void main(String[] args) { + try { + List list = VirtualMachine.list(); + for (VirtualMachineDescriptor vmd : list) { + if (vmd.displayName().endsWith("agentcore-test-application-1.0.0-jar-with-dependencies.jar")) { + VirtualMachine virtualMachine = VirtualMachine.attach(vmd.id()); + if (args.length == 0) { + return; + } + if (args.length == 1) { + String agentPath = args[0]; + virtualMachine.loadAgent(agentPath); + return; + } + String agentPath = args[0]; + String agentArgs = args[1]; + virtualMachine.loadAgent(agentPath, agentArgs); + virtualMachine.detach(); + } + } + } catch (AgentInitializationException | IOException | AgentLoadException | AttachNotSupportedException ignore) { + // ignore + } + } +} diff --git a/suppressions.xml b/suppressions.xml index b760c1bdd2..5c729978e3 100644 --- a/suppressions.xml +++ b/suppressions.xml @@ -4,4 +4,7 @@ "https://checkstyle.org/dtds/suppressions_1_2.dtd"> + + + \ No newline at end of file From 30b0e19168d1fba7658567d3aa5c154a9525d14a Mon Sep 17 00:00:00 2001 From: TLPC <470193496@qq.com> Date: Sat, 7 Oct 2023 17:08:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?AgentCore=E5=8A=A8=E6=80=81=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E5=8D=B8=E8=BD=BD=E6=B5=8B=E8=AF=95:=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scenarios/agentcore/dynamic/action.yml | 4 +- .../agentcore/test/request/RequestTest.java | 75 ++++++++++++++++ .../request/{utils => }/RequestUtils.java | 7 +- .../dynamic/DynamicInstallPluginTest.java | 38 -------- .../dynamic/DynamicReInstallAgentTest.java | 38 -------- .../dynamic/DynamicUninstallAgentTest.java | 38 -------- .../dynamic/DynamicUninstallPluginTest.java | 38 -------- .../dynamicconfig/DynamicConfigTest.java | 38 -------- .../controller/TestController.java | 9 ++ .../application/results/DynamicResults.java | 2 +- .../tests/dynamic/DynamicTest.java | 90 +++++++++++++++++++ 11 files changed, 181 insertions(+), 196 deletions(-) create mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java rename sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/{utils => }/RequestUtils.java (96%) delete mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java delete mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java delete mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java delete mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java delete mode 100644 sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/tests/dynamic/DynamicTest.java diff --git a/.github/actions/scenarios/agentcore/dynamic/action.yml b/.github/actions/scenarios/agentcore/dynamic/action.yml index 9d25c7faec..e63c39ca96 100644 --- a/.github/actions/scenarios/agentcore/dynamic/action.yml +++ b/.github/actions/scenarios/agentcore/dynamic/action.yml @@ -1,5 +1,5 @@ -name: "Agent-core Dynamic Test" -description: "Auto test for agent-core dynamic" +name: "Agent-core Dynamic Install/Uninstall Test" +description: "Auto test for agent-core dynamic install/uninstall" runs: using: "composite" steps: diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java new file mode 100644 index 0000000000..5dd1e6b33e --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.request; + +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +/** + * 请求测试类,采用http请求调用方式测试 + * + * @author tangle + * @since 2023-09-07 + */ +public class RequestTest { + /** + * 动态配置测试方法 + */ + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "DYNAMIC_CONFIG") + public void testDynamicConfig() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testDynamicConfig"); + } + + /** + * 动态卸载插件测试方法 + */ + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "UNINSTALL_PLUGIN") + public void testUninstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testUninstallPlugin"); + } + + /** + * 动态卸载Agent测试方法 + */ + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "UNINSTALL_AGENT") + public void testUninstallAgent() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testUninstallAgent"); + } + + /** + * 动态重装Agent测试方法 + */ + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "REINSTALL_AGENT") + public void testReInstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testReInstallAgent"); + } + + /** + * 动态安装插件测试方法 + */ + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "INSTALL_PLUGIN") + public void testInstallPlugin() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testInstallPlugin"); + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/utils/RequestUtils.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestUtils.java similarity index 96% rename from sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/utils/RequestUtils.java rename to sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestUtils.java index f4b1dea46e..4cf8b8e572 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/utils/RequestUtils.java +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestUtils.java @@ -14,9 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.agentcore.test.request.utils; - -import com.huaweicloud.agentcore.test.request.dynamicconfig.DynamicConfigTest; +package com.huaweicloud.agentcore.test.request; import com.alibaba.fastjson.JSONObject; @@ -43,6 +41,9 @@ public class RequestUtils { private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtils.class); + private RequestUtils() { + } + /** * 对测试请求结果进行断言判断 * diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java deleted file mode 100644 index b635ab308b..0000000000 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicInstallPluginTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; - -import com.huaweicloud.agentcore.test.request.utils.RequestUtils; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledIfSystemProperty; - -import java.io.IOException; - -/** - * 动态安装插件测试方法,采用http请求调用方式测试 - * - * @author tangle - * @since 2023-09-26 - */ -@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "INSTALL_PLUGIN") -public class DynamicInstallPluginTest { - @Test - public void testInstallPlugin() throws IOException { - RequestUtils.testRequest("http://127.0.0.1:8915/testInstallPlugin"); - } -} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java deleted file mode 100644 index 8f0ee59932..0000000000 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicReInstallAgentTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; - -import com.huaweicloud.agentcore.test.request.utils.RequestUtils; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledIfSystemProperty; - -import java.io.IOException; - -/** - * 动态重装Agent测试方法,采用http请求调用方式测试 - * - * @author tangle - * @since 2023-09-26 - */ -@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "REINSTALL_AGENT") -public class DynamicReInstallAgentTest { - @Test - public void testInstallPlugin() throws IOException { - RequestUtils.testRequest("http://127.0.0.1:8915/testReInstallAgent"); - } -} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java deleted file mode 100644 index 9c28e492b5..0000000000 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallAgentTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; - -import com.huaweicloud.agentcore.test.request.utils.RequestUtils; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledIfSystemProperty; - -import java.io.IOException; - -/** - * 动态卸载Agent测试方法,采用http请求调用方式测试 - * - * @author tangle - * @since 2023-09-26 - */ -@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "UNINSTALL_AGENT") -public class DynamicUninstallAgentTest { - @Test - public void testInstallPlugin() throws IOException { - RequestUtils.testRequest("http://127.0.0.1:8915/testUninstallAgent"); - } -} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java deleted file mode 100644 index 8066f1b093..0000000000 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamic/DynamicUninstallPluginTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed 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 com.huaweicloud.agentcore.test.request.dynamic; - -import com.huaweicloud.agentcore.test.request.utils.RequestUtils; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledIfSystemProperty; - -import java.io.IOException; - -/** - * 动态卸载插件测试方法,采用http请求调用方式测试 - * - * @author tangle - * @since 2023-09-26 - */ -@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "UNINSTALL_PLUGIN") -public class DynamicUninstallPluginTest { - @Test - public void testInstallPlugin() throws IOException { - RequestUtils.testRequest("http://127.0.0.1:8915/testUninstallPlugin"); - } -} diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java deleted file mode 100644 index 86ae5cf6cf..0000000000 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/dynamicconfig/DynamicConfigTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed 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 com.huaweicloud.agentcore.test.request.dynamicconfig; - -import com.huaweicloud.agentcore.test.request.utils.RequestUtils; - -import org.junit.jupiter.api.condition.EnabledIfSystemProperty; -import org.junit.jupiter.api.Test; - -import java.io.IOException; - -/** - * 动态配置测试方法,采用http请求调用方式测试 - * - * @author tangle - * @since 2023-09-07 - */ -@EnabledIfSystemProperty(named = "agentcore.test.type", matches = "DYNAMIC_CONFIG") -public class DynamicConfigTest { - @Test - public void testDynamicConfig() throws IOException { - RequestUtils.testRequest("http://127.0.0.1:8915/testDynamicConfig"); - } -} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java index 464fef9ee4..bdee14a0da 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/controller/TestController.java @@ -18,6 +18,7 @@ import com.huaweicloud.agentcore.test.application.results.DynamicConfigResults; import com.huaweicloud.agentcore.test.application.results.DynamicResults; +import com.huaweicloud.agentcore.test.application.tests.dynamic.DynamicTest; import com.huaweicloud.agentcore.test.application.tests.dynamicconfig.DynamicConfigTest; import com.alibaba.fastjson.JSONObject; @@ -68,6 +69,8 @@ public String testDynamicConfig() { */ public String testInstallPlugin() { Map resultMap = new HashMap<>(); + DynamicTest dynamicTest = new DynamicTest(); + dynamicTest.testInstallPlugin(); resultMap.put(DynamicResults.DYNAMIC_INSTALL_PLUGIN_REPEAT_ENHANCE.name(), DynamicResults.DYNAMIC_INSTALL_PLUGIN_REPEAT_ENHANCE.getResult()); JSONObject jsonObject = new JSONObject(resultMap); @@ -81,6 +84,8 @@ public String testInstallPlugin() { */ public String testUninstallPlugin() { Map resultMap = new HashMap<>(); + DynamicTest dynamicTest = new DynamicTest(); + dynamicTest.testUninstallPlugin(); resultMap.put(DynamicResults.DYNAMIC_UNINSTALL_PLUGIN_INTERCEPTOR_FAILURE.name(), DynamicResults.DYNAMIC_UNINSTALL_PLUGIN_INTERCEPTOR_FAILURE.getResult()); resultMap.put(DynamicResults.DYNAMIC_UNINSTALL_REPEAT_ENHANCE.name(), @@ -96,6 +101,8 @@ public String testUninstallPlugin() { */ public String testUninstallAgent() { Map resultMap = new HashMap<>(); + DynamicTest dynamicTest = new DynamicTest(); + dynamicTest.testUninstallAgent(); resultMap.put(DynamicResults.DYNAMIC_UNINSTALL_AGENT_PLUGIN_FAILURE.name(), DynamicResults.DYNAMIC_UNINSTALL_AGENT_PLUGIN_FAILURE.getResult()); JSONObject jsonObject = new JSONObject(resultMap); @@ -109,6 +116,8 @@ public String testUninstallAgent() { */ public String testReInstallAgent() { Map resultMap = new HashMap<>(); + DynamicTest dynamicTest = new DynamicTest(); + dynamicTest.testReInstallAgent(); resultMap.put(DynamicResults.DYNAMIC_REINSTALL_AGENT_PLUGIN_SUCCESS.name(), DynamicResults.DYNAMIC_REINSTALL_AGENT_PLUGIN_SUCCESS.getResult()); JSONObject jsonObject = new JSONObject(resultMap); diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java index 7f84262a8b..819fd9bd39 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/results/DynamicResults.java @@ -70,7 +70,7 @@ public enum DynamicResults { */ DynamicResults(String description) { this.description = description; - this.result = true; + this.result = false; } public boolean getResult() { diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/tests/dynamic/DynamicTest.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/tests/dynamic/DynamicTest.java new file mode 100644 index 0000000000..a599cc0a2b --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/tests/dynamic/DynamicTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.agentcore.test.application.tests.dynamic; + +import com.huaweicloud.agentcore.test.application.results.DynamicResults; + +/** + * 动态安装卸载测试类 + * + * @author tangle + * @since 2023-09-08 + */ +public class DynamicTest { + /** + * 用于测试插件反射修改的回执结果:监听成功 + */ + private static boolean serviceCloseSuccess; + + public static void setServiceCloseSuccess(boolean flag) { + serviceCloseSuccess = flag; + } + + /** + * 测试动态安装插件 + */ + public void testInstallPlugin() { + boolean[] result = repeatEnhance(false, false); + if (result[0] && result[1]) { + DynamicResults.DYNAMIC_INSTALL_PLUGIN_REPEAT_ENHANCE.setResult(true); + } + } + + /** + * 测试动态卸载插件 + */ + public void testUninstallPlugin() { + boolean[] result = repeatEnhance(false, false); + if (!result[0]) { + DynamicResults.DYNAMIC_UNINSTALL_PLUGIN_INTERCEPTOR_FAILURE.setResult(true); + } + if (result[1]) { + DynamicResults.DYNAMIC_UNINSTALL_REPEAT_ENHANCE.setResult(true); + } + } + + /** + * 测试动态卸载Agent + */ + public void testUninstallAgent() { + boolean[] result = repeatEnhance(false, false); + if (!result[0] && !result[1]) { + DynamicResults.DYNAMIC_UNINSTALL_AGENT_PLUGIN_FAILURE.setResult(true); + } + } + + /** + * 测试动态重装Agent + */ + public void testReInstallAgent() { + boolean[] result = repeatEnhance(false, false); + if (result[0] && result[1]) { + DynamicResults.DYNAMIC_REINSTALL_AGENT_PLUGIN_SUCCESS.setResult(true); + } + } + + /** + * 测试插件的增强拦截方法 + * + * @param firstEnhanceFlag first-plugin的增强flag + * @param secondEnhanceFlag second-plugin的增强flag + * @return 增强结果数组 + */ + private boolean[] repeatEnhance(boolean firstEnhanceFlag, boolean secondEnhanceFlag) { + return new boolean[]{firstEnhanceFlag, secondEnhanceFlag}; + } +}