From f52169fa9502fb28aa236f9874ef333c3348dacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez=20Gonzales?= Date: Mon, 21 Oct 2024 14:12:19 -0500 Subject: [PATCH] Parse docker compose file correctly when version is not declared (#9420) `version` is optional in compose file. Fixes #8109 --- .../containers/ParsedDockerComposeFile.java | 18 +++++++++--------- .../ParsedDockerComposeFileValidationTest.java | 13 +++++++++++++ ...compose-imagename-parsing-v2-no-version.yml | 9 +++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 core/src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml diff --git a/core/src/main/java/org/testcontainers/containers/ParsedDockerComposeFile.java b/core/src/main/java/org/testcontainers/containers/ParsedDockerComposeFile.java index b5b11686491..d797a86bfbf 100644 --- a/core/src/main/java/org/testcontainers/containers/ParsedDockerComposeFile.java +++ b/core/src/main/java/org/testcontainers/containers/ParsedDockerComposeFile.java @@ -78,16 +78,16 @@ protected Object constructObject(Node node) { private void parseAndValidate() { final Map servicesMap; - if (composeFileContent.containsKey("version")) { - if ("2.0".equals(composeFileContent.get("version"))) { - log.warn( - "Testcontainers may not be able to clean up networks spawned using Docker Compose v2.0 files. " + - "Please see https://github.com/testcontainers/moby-ryuk/issues/2, and specify 'version: \"2.1\"' or " + - "higher in {}", - composeFileName - ); - } + if (composeFileContent.containsKey("version") && "2.0".equals(composeFileContent.get("version"))) { + log.warn( + "Testcontainers may not be able to clean up networks spawned using Docker Compose v2.0 files. " + + "Please see https://github.com/testcontainers/moby-ryuk/issues/2, and specify 'version: \"2.1\"' or " + + "higher in {}", + composeFileName + ); + } + if (composeFileContent.containsKey("services")) { final Object servicesElement = composeFileContent.get("services"); if (servicesElement == null) { log.debug( diff --git a/core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java b/core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java index 08b2746b386..9067d77d825 100644 --- a/core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java +++ b/core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java @@ -111,6 +111,19 @@ public void shouldObtainImageNamesV2() { ); } + @Test + public void shouldObtainImageNamesV2WithNoVersionTag() { + File file = new File("src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml"); + ParsedDockerComposeFile parsedFile = new ParsedDockerComposeFile(file); + assertThat(parsedFile.getServiceNameToImageNames()) + .as("all defined images are found") + .contains( + entry("mysql", Sets.newHashSet("mysql")), + entry("redis", Sets.newHashSet("redis")), + entry("custom", Sets.newHashSet("postgres")) + ); + } + @Test public void shouldObtainImageFromDockerfileBuild() { File file = new File("src/test/resources/docker-compose-imagename-parsing-dockerfile.yml"); diff --git a/core/src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml b/core/src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml new file mode 100644 index 00000000000..0b48ad3eb05 --- /dev/null +++ b/core/src/test/resources/docker-compose-imagename-parsing-v2-no-version.yml @@ -0,0 +1,9 @@ +services: + redis: + image: redis + mysql: + image: mysql + custom: + build: . +networks: + custom_network: {}