Add property to control Docker Compose start command execution

If the property 'spring.docker.compose.start.skip' is set to 'never',
the start command is always executed. The default value of 'if-running'
only executes the start command if there are no services running
already, which is the old behavior.

Closes gh-39749
This commit is contained in:
Moritz Halbritter
2024-04-05 08:36:21 +02:00
parent 2ab0e024c8
commit 6d192e62fd
5 changed files with 116 additions and 8 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.
@@ -39,6 +39,7 @@ import org.springframework.boot.docker.compose.core.DockerCompose;
import org.springframework.boot.docker.compose.core.DockerComposeFile;
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.Start.Skip;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ApplicationContext;
@@ -384,6 +385,38 @@ class DockerComposeLifecycleManagerTests {
assertThat(output).doesNotContain("There are already Docker Compose services running, skipping startup");
}
@Test
void shouldStartIfSkipModeIsIfRunningAndNoServicesAreRunning() {
given(this.dockerCompose.hasDefinedServices()).willReturn(true);
this.properties.getStart().setSkip(Skip.IF_RUNNING);
this.lifecycleManager.start();
then(this.dockerCompose).should().up(any());
}
@Test
void shouldNotStartIfSkipModeIsIfRunningAndServicesAreAlreadyRunning() {
setUpRunningServices();
this.properties.getStart().setSkip(Skip.IF_RUNNING);
this.lifecycleManager.start();
then(this.dockerCompose).should(never()).up(any());
}
@Test
void shouldStartIfSkipModeIsNeverAndNoServicesAreRunning() {
given(this.dockerCompose.hasDefinedServices()).willReturn(true);
this.properties.getStart().setSkip(Skip.NEVER);
this.lifecycleManager.start();
then(this.dockerCompose).should().up(any());
}
@Test
void shouldStartIfSkipModeIsNeverAndServicesAreAlreadyRunning() {
setUpRunningServices();
this.properties.getStart().setSkip(Skip.NEVER);
this.lifecycleManager.start();
then(this.dockerCompose).should().up(any());
}
private void setUpRunningServices() {
setUpRunningServices(true);
}

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,16 +18,21 @@ package org.springframework.boot.docker.compose.lifecycle;
import java.io.File;
import java.time.Duration;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
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.Start.Skip;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DockerComposeProperties}.
@@ -84,4 +89,16 @@ class DockerComposePropertiesTests {
assertThat(properties.getReadiness().getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(500));
}
@Test
void skipModeNeverShouldNeverSkip() {
assertThat(Skip.NEVER.shouldSkip(Collections.emptyList())).isFalse();
assertThat(Skip.NEVER.shouldSkip(List.of(mock(RunningService.class)))).isFalse();
}
@Test
void skipModeIfRunningShouldSkipWhenServicesAreRunning() {
assertThat(Skip.IF_RUNNING.shouldSkip(Collections.emptyList())).isFalse();
assertThat(Skip.IF_RUNNING.shouldSkip(List.of(mock(RunningService.class)))).isTrue();
}
}