Support multiple Docker Compose files

Closes gh-41691
This commit is contained in:
Moritz Halbritter
2024-08-05 12:08:03 +02:00
parent c7e29b7b1b
commit 4eebb8e629
11 changed files with 161 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.boot.docker.compose.core;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -59,12 +60,20 @@ class DockerComposeFileTests {
assertThat(composeFile.toString()).endsWith(File.separator + "compose.yml");
}
@Test
void toStringReturnsFileNameList() throws Exception {
File file1 = createTempFile("1.yml");
File file2 = createTempFile("2.yml");
DockerComposeFile composeFile = DockerComposeFile.of(List.of(file1, file2));
assertThat(composeFile).hasToString(file1 + ", " + file2);
}
@Test
void findFindsSingleFile() throws Exception {
File file = new File(this.temp, "docker-compose.yml");
FileCopyUtils.copy(new byte[0], file);
DockerComposeFile composeFile = DockerComposeFile.find(file.getParentFile());
assertThat(composeFile.toString()).endsWith(File.separator + "docker-compose.yml");
assertThat(composeFile.getFiles()).containsExactly(file);
}
@Test
@@ -74,7 +83,7 @@ class DockerComposeFileTests {
File f2 = new File(this.temp, "compose.yml");
FileCopyUtils.copy(new byte[0], f2);
DockerComposeFile composeFile = DockerComposeFile.find(f1.getParentFile());
assertThat(composeFile.toString()).endsWith(File.separator + "compose.yml");
assertThat(composeFile.getFiles()).containsExactly(f2);
}
@Test
@@ -94,24 +103,31 @@ class DockerComposeFileTests {
@Test
void findWhenWorkingDirectoryIsNotDirectoryThrowsException() throws Exception {
File file = new File(this.temp, "iamafile");
FileCopyUtils.copy(new byte[0], file);
File file = createTempFile("iamafile");
assertThatIllegalArgumentException().isThrownBy(() -> DockerComposeFile.find(file))
.withMessageEndingWith("is not a directory");
}
@Test
void ofReturnsDockerComposeFile() throws Exception {
File file = new File(this.temp, "anyfile.yml");
FileCopyUtils.copy(new byte[0], file);
File file = createTempFile("compose.yml");
DockerComposeFile composeFile = DockerComposeFile.of(file);
assertThat(composeFile).isNotNull();
assertThat(composeFile).hasToString(file.getCanonicalPath());
assertThat(composeFile.getFiles()).containsExactly(file);
}
@Test
void ofWithMultipleFilesReturnsDockerComposeFile() throws Exception {
File file1 = createTempFile("1.yml");
File file2 = createTempFile("2.yml");
DockerComposeFile composeFile = DockerComposeFile.of(List.of(file1, file2));
assertThat(composeFile).isNotNull();
assertThat(composeFile.getFiles()).containsExactly(file1, file2);
}
@Test
void ofWhenFileIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> DockerComposeFile.of(null))
assertThatIllegalArgumentException().isThrownBy(() -> DockerComposeFile.of((File) null))
.withMessage("File must not be null");
}
@@ -129,9 +145,13 @@ class DockerComposeFileTests {
}
private DockerComposeFile createComposeFile(String name) throws IOException {
return DockerComposeFile.of(createTempFile(name));
}
private File createTempFile(String name) throws IOException {
File file = new File(this.temp, name);
FileCopyUtils.copy(new byte[0], file);
return DockerComposeFile.of(file);
return file.getCanonicalFile();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.boot.docker.compose.core;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -42,8 +43,17 @@ class DockerComposeOriginTests {
void hasToString() throws Exception {
DockerComposeFile composeFile = createTempComposeFile();
DockerComposeOrigin origin = new DockerComposeOrigin(composeFile, "service-1");
assertThat(origin.toString()).startsWith("Docker compose service 'service-1' defined in '")
.endsWith("compose.yaml'");
assertThat(origin.toString()).startsWith("Docker compose service 'service-1' defined in ")
.endsWith("compose.yaml");
}
@Test
void hasToStringWithMultipleFiles() throws IOException {
File file1 = createTempFile("1.yaml");
File file2 = createTempFile("2.yaml");
DockerComposeOrigin origin = new DockerComposeOrigin(DockerComposeFile.of(List.of(file1, file2)), "service-1");
assertThat(origin.toString())
.startsWith("Docker compose service 'service-1' defined in %s, %s".formatted(file1, file2));
}
@Test
@@ -63,9 +73,13 @@ class DockerComposeOriginTests {
}
private DockerComposeFile createTempComposeFile() throws IOException {
File file = new File(this.temp, "compose.yaml");
return DockerComposeFile.of(createTempFile("compose.yaml"));
}
private File createTempFile(String filename) throws IOException {
File file = new File(this.temp, filename);
FileCopyUtils.copy(new byte[0], file);
return DockerComposeFile.of(file);
return file.getCanonicalFile();
}
}

View File

@@ -47,7 +47,7 @@ class DockerComposePropertiesTests {
void getWhenNoPropertiesReturnsNew() {
Binder binder = new Binder(new MapConfigurationPropertySource());
DockerComposeProperties properties = DockerComposeProperties.get(binder);
assertThat(properties.getFile()).isNull();
assertThat(properties.getFile()).isEmpty();
assertThat(properties.getLifecycleManagement()).isEqualTo(LifecycleManagement.START_AND_STOP);
assertThat(properties.getHost()).isNull();
assertThat(properties.getStart().getCommand()).isEqualTo(StartCommand.UP);
@@ -76,7 +76,7 @@ class DockerComposePropertiesTests {
source.put("spring.docker.compose.readiness.tcp.read-timeout", "500ms");
Binder binder = new Binder(new MapConfigurationPropertySource(source));
DockerComposeProperties properties = DockerComposeProperties.get(binder);
assertThat(properties.getFile()).isEqualTo(new File("my-compose.yml"));
assertThat(properties.getFile()).containsExactly(new File("my-compose.yml"));
assertThat(properties.getLifecycleManagement()).isEqualTo(LifecycleManagement.START_ONLY);
assertThat(properties.getHost()).isEqualTo("myhost");
assertThat(properties.getStart().getCommand()).isEqualTo(StartCommand.START);