Skip to content

Commit 9910e0f

Browse files
committed
test: add DockerCli tests for command generation and constructor
Signed-off-by: 이규민 <[email protected]> Signed-off-by: 이규민 <[email protected]>
1 parent 75bc4b2 commit 9910e0f

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DockerCliIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class DockerCliIntegrationTests {
6363

6464
@Test
6565
void runBasicCommand() {
66-
DockerCli cli = new DockerCli(null, null);
66+
DockerCli cli = new DockerCli((File) null, null);
6767
List<DockerCliContextResponse> context = cli.run(new DockerCliCommand.Context());
6868
assertThat(context).isNotEmpty();
6969
}
@@ -72,7 +72,7 @@ void runBasicCommand() {
7272
void runLifecycle() throws IOException {
7373
File composeFile = createComposeFile("redis-compose.yaml");
7474
String projectName = UUID.randomUUID().toString();
75-
DockerCli cli = new DockerCli(null, new DockerComposeOptions(DockerComposeFile.of(composeFile),
75+
DockerCli cli = new DockerCli((File) null, new DockerComposeOptions(DockerComposeFile.of(composeFile),
7676
Collections.emptySet(), List.of("--project-name=" + projectName)));
7777
try {
7878
// Verify that no services are running (this is a fresh compose project)
@@ -112,7 +112,7 @@ void runLifecycle() throws IOException {
112112
@Test
113113
void shouldWorkWithMultipleComposeFiles() throws IOException {
114114
List<File> composeFiles = createComposeFiles();
115-
DockerCli cli = new DockerCli(null,
115+
DockerCli cli = new DockerCli((File) null,
116116
new DockerComposeOptions(DockerComposeFile.of(composeFiles), Set.of("dev"), Collections.emptyList()));
117117
try {
118118
// List the config and verify that both redis are there

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCli.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ class DockerCli {
6868
this.composeVersion = ComposeVersion.of(this.dockerCommands.get(Type.DOCKER_COMPOSE).version());
6969
}
7070

71+
// 테스트나 DI에서 사용하는 생성자
72+
DockerCli(ProcessRunner processRunner, DockerComposeOptions dockerComposeOptions) {
73+
this.processRunner = processRunner;
74+
this.dockerCommands = dockerCommandsCache.computeIfAbsent(
75+
new File("."), (key) -> new DockerCommands(this.processRunner));
76+
this.dockerComposeOptions = (dockerComposeOptions != null) ? dockerComposeOptions : DockerComposeOptions.none();
77+
this.composeVersion = ComposeVersion.of(this.dockerCommands.get(Type.DOCKER_COMPOSE).version());
78+
}
79+
7180
/**
7281
* Run the given {@link DockerCli} command and return the response.
7382
* @param <R> the response type
@@ -89,7 +98,9 @@ private Consumer<String> createOutputConsumer(LogLevel logLevel) {
8998
return (line) -> logLevel.log(logger, line);
9099
}
91100

92-
private List<String> createCommand(Type type) {
101+
102+
103+
List<String> createCommand(Type type) {
93104
return switch (type) {
94105
case DOCKER -> new ArrayList<>(this.dockerCommands.get(type).command());
95106
case DOCKER_COMPOSE -> {

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCompose.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.docker.compose.core;
1818

19+
import java.io.File;
1920
import java.time.Duration;
2021
import java.util.Collections;
2122
import java.util.List;
@@ -142,7 +143,7 @@ static DockerCompose get(DockerComposeFile file, String hostname, Set<String> ac
142143
*/
143144
static DockerCompose get(DockerComposeFile file, String hostname, Set<String> activeProfiles,
144145
List<String> arguments) {
145-
DockerCli cli = new DockerCli(null, new DockerComposeOptions(file, activeProfiles, arguments));
146+
DockerCli cli = new DockerCli((File) null, new DockerComposeOptions(file, activeProfiles, arguments));
146147
return new DefaultDockerCompose(cli, hostname);
147148
}
148149

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docker.compose.core;
18+
19+
import java.io.File;
20+
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
import org.junit.jupiter.api.Assumptions;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.boot.docker.compose.core.DockerCli.DockerComposeOptions;
28+
import org.springframework.boot.docker.compose.core.DockerCliCommand.Type;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.ArgumentMatchers.any;
32+
import static org.mockito.Mockito.doReturn;
33+
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.spy;
35+
import static org.mockito.Mockito.when;
36+
37+
class DockerCliTests {
38+
@Test
39+
void createCommandWithDockerComposeShouldIncludeCorrectOptions() {
40+
ProcessRunner mockProcessRunner = mock(ProcessRunner.class);
41+
when(mockProcessRunner.run(any(String[].class))).thenReturn("{\"version\":\"1.0.0\"}");
42+
43+
DockerComposeFile mockFile = mock(DockerComposeFile.class);
44+
when(mockFile.getFiles()).thenReturn(List.of(new File("docker-compose.yml")));
45+
46+
DockerCli.DockerComposeOptions options = new DockerCli.DockerComposeOptions(mockFile, Set.of("dev"), List.of("--verbose"));
47+
DockerCli cli = new DockerCli(mockProcessRunner, options);
48+
49+
List<String> command = cli.createCommand(DockerCliCommand.Type.DOCKER_COMPOSE);
50+
assertThat(command).contains("--file", "docker-compose.yml", "--profile", "dev", "--verbose");
51+
}
52+
53+
@Test
54+
void fileConstructorShouldInitializeCorrectly_whenDockerIsRunning() {
55+
// assume docker is running (아니면 생략 가능)
56+
Assumptions.assumeTrue(new File("/var/run/docker.sock").exists() || System.getProperty("os.name").startsWith("Windows"));
57+
58+
DockerComposeFile mockFile = mock(DockerComposeFile.class);
59+
when(mockFile.getFiles()).thenReturn(List.of(new File("docker-compose.yml")));
60+
61+
DockerCli.DockerComposeOptions options = new DockerCli.DockerComposeOptions(mockFile, Set.of("dev"), List.of("--verbose"));
62+
63+
DockerCli cli = new DockerCli(new File("."), options); // ✅ 여기! 기존 생성자 직접 호출
64+
65+
List<String> command = cli.createCommand(DockerCliCommand.Type.DOCKER_COMPOSE);
66+
67+
assertThat(command).contains("--file", "docker-compose.yml", "--profile", "dev", "--verbose");
68+
}
69+
70+
}

0 commit comments

Comments
 (0)