|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 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.io.FileReader; |
| 21 | +import java.io.FileWriter; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.io.TempDir; |
| 30 | + |
| 31 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeConfig; |
| 32 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeDown; |
| 33 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposePs; |
| 34 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeStart; |
| 35 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeStop; |
| 36 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeUp; |
| 37 | +import org.springframework.boot.docker.compose.core.DockerCliCommand.Inspect; |
| 38 | +import org.springframework.boot.logging.LogLevel; |
| 39 | +import org.springframework.boot.testsupport.process.DisabledIfProcessUnavailable; |
| 40 | +import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable; |
| 41 | +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; |
| 42 | +import org.springframework.core.io.ClassPathResource; |
| 43 | +import org.springframework.util.FileCopyUtils; |
| 44 | + |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests for {@link DockerCli}. |
| 49 | + * |
| 50 | + * @author Moritz Halbritter |
| 51 | + * @author Andy Wilkinson |
| 52 | + * @author Phillip Webb |
| 53 | + */ |
| 54 | +@DisabledIfDockerUnavailable |
| 55 | +@DisabledIfProcessUnavailable({ "docker", "compose" }) |
| 56 | +class DockerCliIntegrationTests { |
| 57 | + |
| 58 | + @TempDir |
| 59 | + private static Path tempDir; |
| 60 | + |
| 61 | + @Test |
| 62 | + void runBasicCommand() { |
| 63 | + DockerCli cli = new DockerCli(null, null, Collections.emptySet()); |
| 64 | + List<DockerCliContextResponse> context = cli.run(new DockerCliCommand.Context()); |
| 65 | + assertThat(context).isNotEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void runLifecycle() throws IOException { |
| 70 | + File composeFile = createComposeFile(); |
| 71 | + DockerCli cli = new DockerCli(null, DockerComposeFile.of(composeFile), Collections.emptySet()); |
| 72 | + try { |
| 73 | + // Verify that no services are running (this is a fresh compose project) |
| 74 | + List<DockerCliComposePsResponse> ps = cli.run(new ComposePs()); |
| 75 | + assertThat(ps).isEmpty(); |
| 76 | + // List the config and verify that redis is there |
| 77 | + DockerCliComposeConfigResponse config = cli.run(new ComposeConfig()); |
| 78 | + assertThat(config.services()).containsOnlyKeys("redis"); |
| 79 | + // Run up |
| 80 | + cli.run(new ComposeUp(LogLevel.INFO)); |
| 81 | + // Run ps and use id to run inspect on the id |
| 82 | + ps = cli.run(new ComposePs()); |
| 83 | + assertThat(ps).hasSize(1); |
| 84 | + String id = ps.get(0).id(); |
| 85 | + List<DockerCliInspectResponse> inspect = cli.run(new Inspect(List.of(id))); |
| 86 | + assertThat(inspect).isNotEmpty(); |
| 87 | + assertThat(inspect.get(0).id()).isEqualTo(id); |
| 88 | + // Run stop, then run ps and verify the services are stopped |
| 89 | + cli.run(new ComposeStop(Duration.ofSeconds(10))); |
| 90 | + ps = cli.run(new ComposePs()); |
| 91 | + assertThat(ps).isEmpty(); |
| 92 | + // Run start, verify that service is there, then run down and verify they are |
| 93 | + // gone |
| 94 | + cli.run(new ComposeStart(LogLevel.INFO)); |
| 95 | + ps = cli.run(new ComposePs()); |
| 96 | + assertThat(ps).hasSize(1); |
| 97 | + cli.run(new ComposeDown(Duration.ofSeconds(10))); |
| 98 | + ps = cli.run(new ComposePs()); |
| 99 | + assertThat(ps).isEmpty(); |
| 100 | + } |
| 101 | + finally { |
| 102 | + // Clean up in any case |
| 103 | + quietComposeDown(cli); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static void quietComposeDown(DockerCli cli) { |
| 108 | + try { |
| 109 | + cli.run(new ComposeDown(Duration.ZERO)); |
| 110 | + } |
| 111 | + catch (RuntimeException ex) { |
| 112 | + // Ignore |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static File createComposeFile() throws IOException { |
| 117 | + File composeFile = new ClassPathResource("redis-compose.yaml", DockerCliIntegrationTests.class).getFile(); |
| 118 | + File tempComposeFile = Path.of(tempDir.toString(), composeFile.getName()).toFile(); |
| 119 | + String composeFileContent; |
| 120 | + try (FileReader reader = new FileReader(composeFile)) { |
| 121 | + composeFileContent = FileCopyUtils.copyToString(reader); |
| 122 | + } |
| 123 | + composeFileContent = composeFileContent.replace("{imageName}", |
| 124 | + DockerImageNames.redis().asCanonicalNameString()); |
| 125 | + try (FileWriter writer = new FileWriter(tempComposeFile)) { |
| 126 | + FileCopyUtils.copy(composeFileContent, writer); |
| 127 | + } |
| 128 | + return tempComposeFile; |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments