From ca6a5618c57cd06fdcd6bfd15852d96f608c4095 Mon Sep 17 00:00:00 2001 From: Daming Date: Mon, 17 Feb 2020 17:10:37 +0800 Subject: [PATCH] [test/plugin] remove the image on docker exit (#4374) * remove image on docker exit * polish --- .../test/helper/DockerComposeRunningGenerator.java | 14 +++++++++++++- .../plugin/test/helper/vo/DependencyComponent.java | 9 +++++++++ .../plugin/test/helper/vo/DockerService.java | 9 +++++++++ .../main/resources/compose-start-script.template | 4 +++- .../src/main/resources/docker-compose.template | 3 +++ .../elasticsearch-5.x-scenario/configuration.yml | 1 + .../elasticsearch-6.x-scenario/configuration.yml | 1 + .../elasticsearch-7.x-scenario/configuration.yml | 1 + 8 files changed, 40 insertions(+), 2 deletions(-) diff --git a/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/DockerComposeRunningGenerator.java b/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/DockerComposeRunningGenerator.java index 229def13f..b3d261d5c 100644 --- a/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/DockerComposeRunningGenerator.java +++ b/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/DockerComposeRunningGenerator.java @@ -92,7 +92,7 @@ public class DockerComposeRunningGenerator extends AbstractRunningGenerator { } protected List convertDockerServices(final String version, - Map componentMap) { + Map componentMap) { ArrayList services = Lists.newArrayList(); componentMap.forEach((name, dependency) -> { DockerService service = new DockerService(); @@ -108,6 +108,7 @@ public class DockerComposeRunningGenerator extends AbstractRunningGenerator { service.setEntrypoint(dependency.getEntrypoint()); service.setHealthcheck(dependency.getHealthcheck()); service.setEnvironment(dependency.getEnvironment()); + service.setRemoveOnExit(dependency.getRemoveOnExit()); services.add(service); }); return services; @@ -124,6 +125,17 @@ public class DockerComposeRunningGenerator extends AbstractRunningGenerator { root.put("docker_compose_file", docker_compose_file); root.put("build_id", configuration.dockerImageVersion()); root.put("docker_container_name", configuration.dockerContainerName()); + + StringBuilder removeImagesScript = new StringBuilder(); + configuration.caseConfiguration().getDependencies().forEach((name, service) -> { + if (service.getRemoveOnExit()) { + removeImagesScript.append("docker rmi ") + .append(service.getImage().replace("${CASE_SERVER_IMAGE_VERSION}", configuration.scenarioVersion())) + .append(System.lineSeparator()); + } + }); + root.put("removeImagesScript", removeImagesScript.toString()); + StringWriter out = null; try { diff --git a/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DependencyComponent.java b/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DependencyComponent.java index af704de6d..7ad97b566 100644 --- a/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DependencyComponent.java +++ b/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DependencyComponent.java @@ -23,6 +23,7 @@ public class DependencyComponent { private String image; private String hostname; private String version; + private boolean removeOnExit = false; private List startScript; private List links; private List expose; @@ -110,4 +111,12 @@ public class DependencyComponent { public void setStartScript(List startScript) { this.startScript = startScript; } + + public boolean getRemoveOnExit() { + return removeOnExit; + } + + public void setRemoveOnExit(final boolean removeOnExit) { + this.removeOnExit = removeOnExit; + } } diff --git a/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DockerService.java b/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DockerService.java index 7e69033d8..63bae60a5 100644 --- a/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DockerService.java +++ b/test/plugin/runner-helper/src/main/java/org/apache/skywalking/plugin/test/helper/vo/DockerService.java @@ -23,6 +23,7 @@ public class DockerService { private String name; private String imageName; private String hostname; + private boolean removeOnExit; private List startScript; private List links; private List expose; @@ -110,4 +111,12 @@ public class DockerService { public void setStartScript(List startScript) { this.startScript = startScript; } + + public boolean getRemoveOnExit() { + return removeOnExit; + } + + public void setRemoveOnExit(final boolean removeOnExit) { + this.removeOnExit = removeOnExit; + } } diff --git a/test/plugin/runner-helper/src/main/resources/compose-start-script.template b/test/plugin/runner-helper/src/main/resources/compose-start-script.template index 313222b10..1e6a52832 100644 --- a/test/plugin/runner-helper/src/main/resources/compose-start-script.template +++ b/test/plugin/runner-helper/src/main/resources/compose-start-script.template @@ -36,4 +36,6 @@ else docker-compose -p ${project_name} -f ${compose_file} kill docker-compose -p ${project_name} -f ${compose_file} rm -f fi - \ No newline at end of file + + +${removeImagesScript} \ No newline at end of file diff --git a/test/plugin/runner-helper/src/main/resources/docker-compose.template b/test/plugin/runner-helper/src/main/resources/docker-compose.template index 7d68a44aa..3310dd14a 100644 --- a/test/plugin/runner-helper/src/main/resources/docker-compose.template +++ b/test/plugin/runner-helper/src/main/resources/docker-compose.template @@ -58,6 +58,9 @@ services: ${service.name}: image: ${service.imageName} hostname: ${service.hostname} + <#if service.remoteOnExit??> + remoteOnExit: ${service.remoteOnExit} + <#if service.volumes??> volumes: <#list service.volumes as volume> diff --git a/test/plugin/scenarios/elasticsearch-5.x-scenario/configuration.yml b/test/plugin/scenarios/elasticsearch-5.x-scenario/configuration.yml index df2394332..80c52b4ec 100644 --- a/test/plugin/scenarios/elasticsearch-5.x-scenario/configuration.yml +++ b/test/plugin/scenarios/elasticsearch-5.x-scenario/configuration.yml @@ -23,6 +23,7 @@ dependencies: elasticsearch-server-5.x: image: docker.elastic.co/elasticsearch/elasticsearch:5.4.0 hostname: elasticsearch-server-5.x + removeOnExit: true expose: - 9300 - 9200 diff --git a/test/plugin/scenarios/elasticsearch-6.x-scenario/configuration.yml b/test/plugin/scenarios/elasticsearch-6.x-scenario/configuration.yml index edb99a886..387160b69 100644 --- a/test/plugin/scenarios/elasticsearch-6.x-scenario/configuration.yml +++ b/test/plugin/scenarios/elasticsearch-6.x-scenario/configuration.yml @@ -23,6 +23,7 @@ dependencies: elasticsearch-server-6.x: image: elasticsearch:${CASE_SERVER_IMAGE_VERSION} hostname: elasticsearch-server-6.x + removeOnExit: true expose: - 9200 environment: diff --git a/test/plugin/scenarios/elasticsearch-7.x-scenario/configuration.yml b/test/plugin/scenarios/elasticsearch-7.x-scenario/configuration.yml index 1abc433d1..58deba4d9 100644 --- a/test/plugin/scenarios/elasticsearch-7.x-scenario/configuration.yml +++ b/test/plugin/scenarios/elasticsearch-7.x-scenario/configuration.yml @@ -25,6 +25,7 @@ dependencies: elasticsearch-server-7.x: image: elasticsearch:${CASE_SERVER_IMAGE_VERSION} hostname: elasticsearch-server-7.x + removeOnExit: true expose: - 9200 environment: