Merge branch '3.1.x'

Closes gh-38661
This commit is contained in:
Moritz Halbritter 2023-12-05 14:02:52 +01:00
commit de70b4fb4c
2 changed files with 31 additions and 7 deletions

View File

@ -118,14 +118,19 @@ class DockerComposeLifecycleManager {
Stop stop = this.properties.getStop(); Stop stop = this.properties.getStop();
Wait wait = this.properties.getReadiness().getWait(); Wait wait = this.properties.getReadiness().getWait();
List<RunningService> runningServices = dockerCompose.getRunningServices(); List<RunningService> runningServices = dockerCompose.getRunningServices();
if (lifecycleManagement.shouldStart() && runningServices.isEmpty()) { if (lifecycleManagement.shouldStart()) {
start.getCommand().applyTo(dockerCompose, start.getLogLevel()); if (runningServices.isEmpty()) {
runningServices = dockerCompose.getRunningServices(); start.getCommand().applyTo(dockerCompose, start.getLogLevel());
if (wait == Wait.ONLY_IF_STARTED) { runningServices = dockerCompose.getRunningServices();
wait = Wait.ALWAYS; if (wait == Wait.ONLY_IF_STARTED) {
wait = Wait.ALWAYS;
}
if (lifecycleManagement.shouldStop()) {
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
}
} }
if (lifecycleManagement.shouldStop()) { else {
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout())); logger.info("There are already Docker Compose services running, skipping startup");
} }
} }
List<RunningService> relevantServices = new ArrayList<>(runningServices); List<RunningService> relevantServices = new ArrayList<>(runningServices);

View File

@ -29,6 +29,7 @@ import java.util.Set;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.aot.AotDetector; import org.springframework.aot.AotDetector;
@ -38,6 +39,8 @@ import org.springframework.boot.docker.compose.core.DockerCompose;
import org.springframework.boot.docker.compose.core.DockerComposeFile; import org.springframework.boot.docker.compose.core.DockerComposeFile;
import org.springframework.boot.docker.compose.core.RunningService; import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Readiness.Wait; import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Readiness.Wait;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
@ -59,6 +62,7 @@ import static org.mockito.Mockito.never;
* @author Phillip Webb * @author Phillip Webb
* @author Scott Frederick * @author Scott Frederick
*/ */
@ExtendWith(OutputCaptureExtension.class)
class DockerComposeLifecycleManagerTests { class DockerComposeLifecycleManagerTests {
@TempDir @TempDir
@ -365,6 +369,21 @@ class DockerComposeLifecycleManagerTests {
assertThat(event.getRunningServices()).isEqualTo(this.runningServices); assertThat(event.getRunningServices()).isEqualTo(this.runningServices);
} }
@Test
void shouldLogIfServicesAreAlreadyRunning(CapturedOutput output) {
setUpRunningServices();
this.lifecycleManager.start();
assertThat(output).contains("There are already Docker Compose services running, skipping startup");
}
@Test
void shouldNotLogIfThereAreNoServicesRunning(CapturedOutput output) {
given(this.dockerCompose.hasDefinedServices()).willReturn(true);
given(this.dockerCompose.getRunningServices()).willReturn(Collections.emptyList());
this.lifecycleManager.start();
assertThat(output).doesNotContain("There are already Docker Compose services running, skipping startup");
}
private void setUpRunningServices() { private void setUpRunningServices() {
setUpRunningServices(true); setUpRunningServices(true);
} }