diff --git a/src/main/java/io/codeka/gaia/runner/StackRunner.java b/src/main/java/io/codeka/gaia/runner/StackRunner.java index e3bc11e8b..95d9c1857 100644 --- a/src/main/java/io/codeka/gaia/runner/StackRunner.java +++ b/src/main/java/io/codeka/gaia/runner/StackRunner.java @@ -16,6 +16,7 @@ import java.nio.channels.Channels; import java.nio.channels.WritableByteChannel; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -55,9 +56,13 @@ public StackRunner(DockerClient dockerClient, ContainerConfig.Builder containerC private int runContainerForJob(Job job, String script) { try{ + var env = new ArrayList(); + env.add("TF_IN_AUTOMATION=true"); + env.addAll(settings.env()); + // FIXME This is certainly no thread safe ! var containerConfig = containerConfigBuilder - .env(settings.env()) + .env(env) .image("hashicorp/terraform:" + job.getCliVersion()) .build(); diff --git a/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java b/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java index 54f45b6e4..585738fe1 100644 --- a/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java +++ b/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java @@ -8,14 +8,18 @@ import io.codeka.gaia.bo.*; import io.codeka.gaia.repository.JobRepository; import io.codeka.gaia.repository.StackRepository; +import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Answers; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import java.io.OutputStream; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; @@ -44,12 +48,25 @@ class StackRunnerTest { @Mock private JobRepository jobRepository; + private Job job; + + private TerraformModule module; + + private Stack stack; + + @InjectMocks + private StackRunner stackRunner; + @BeforeEach - void containerCreateMock() throws Exception { + void setUp() throws Exception { // simulating a container with id 12 var containerCreation = mock(ContainerCreation.class); when(containerCreation.id()).thenReturn("12"); when(dockerClient.createContainer(any())).thenReturn(containerCreation); + + stack = new Stack(); + job = new Job(); + module = new TerraformModule(); } void httpHijackWorkaroundMock() throws Exception { @@ -67,11 +84,7 @@ void containerExitMock(Long statusCode) throws Exception { @Test void job_shouldBeSavedToDatabaseAfterRun() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); - + // given httpHijackWorkaroundMock(); containerExitMock(0L); when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn(""); @@ -85,11 +98,7 @@ void job_shouldBeSavedToDatabaseAfterRun() throws Exception { @Test void successfullJob_shouldSetTheStackStateToRunning() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); - + // given httpHijackWorkaroundMock(); containerExitMock(0L); when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn(""); @@ -104,11 +113,8 @@ void successfullJob_shouldSetTheStackStateToRunning() throws Exception { @Test void plan_shouldUpdateTheStackState_whenThereIsADiffForRunningStacks() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); + // given stack.setState(StackState.RUNNING); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); httpHijackWorkaroundMock(); containerExitMock(2L); @@ -126,11 +132,8 @@ void plan_shouldUpdateTheStackState_whenThereIsADiffForRunningStacks() throws Ex @Test void plan_shouldNotUpdateTheStackState_whenThereIsADiffForNewStacks() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); + // given stack.setState(StackState.NEW); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); httpHijackWorkaroundMock(); containerExitMock(2L); @@ -148,11 +151,8 @@ void plan_shouldNotUpdateTheStackState_whenThereIsADiffForNewStacks() throws Exc @Test void stop_shouldUpdateTheStackState_whenSuccessful() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); + // given stack.setState(StackState.RUNNING); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); httpHijackWorkaroundMock(); containerExitMock(0L); @@ -170,9 +170,7 @@ void stop_shouldUpdateTheStackState_whenSuccessful() throws Exception { @Test void jobShouldFail_whenFailingToStartContainer() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); + // given stack.setState(StackState.RUNNING); var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); @@ -190,11 +188,7 @@ void jobShouldFail_whenFailingToStartContainer() throws Exception { @Test void plan_shouldStartPreviewJob() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); - + // given httpHijackWorkaroundMock(); containerExitMock(0L); when(stackCommandBuilder.buildPlanScript(stack, module)).thenReturn(""); @@ -208,11 +202,7 @@ void plan_shouldStartPreviewJob() throws Exception { @Test void apply_shouldStartRunob() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); - + // given httpHijackWorkaroundMock(); containerExitMock(0L); when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn(""); @@ -226,11 +216,7 @@ void apply_shouldStartRunob() throws Exception { @Test void stop_shouldStartStopJob() throws Exception { - var job = new Job(); - var module = new TerraformModule(); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); - + // given httpHijackWorkaroundMock(); containerExitMock(0L); when(stackCommandBuilder.buildDestroyScript(stack, module)).thenReturn(""); @@ -244,11 +230,8 @@ void stop_shouldStartStopJob() throws Exception { @Test void plan_shouldConsiderModuleCLIVersion() throws Exception { - var job = new Job(); - var module = new TerraformModule(); + // given module.setCliVersion("0.12.0"); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); httpHijackWorkaroundMock(); containerExitMock(0L); @@ -265,11 +248,8 @@ void plan_shouldConsiderModuleCLIVersion() throws Exception { @Test void apply_shouldConsiderModuleCLIVersion() throws Exception { - var job = new Job(); - var module = new TerraformModule(); + // given module.setCliVersion("0.12.0"); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); httpHijackWorkaroundMock(); containerExitMock(0L); @@ -286,11 +266,8 @@ void apply_shouldConsiderModuleCLIVersion() throws Exception { @Test void stop_shouldConsiderModuleCLIVersion() throws Exception { - var job = new Job(); - var module = new TerraformModule(); + // given module.setCliVersion("0.12.0"); - var stack = new Stack(); - var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository); httpHijackWorkaroundMock(); containerExitMock(0L); @@ -305,4 +282,20 @@ void stop_shouldConsiderModuleCLIVersion() throws Exception { verify(dockerClient).pull("hashicorp/terraform:0.12.0"); } + @Test + void jobShouldBeRunned_with_TF_IN_AUTOMATION_envVar() throws Exception { + // given + httpHijackWorkaroundMock(); + containerExitMock(0L); + when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn(""); + + when(settings.env()).thenReturn(List.of("SOME_VAR=SOME_VALUE")); + + // when + stackRunner.apply(job, module, stack); + + // then + verify(builder).env(List.of("TF_IN_AUTOMATION=true", "SOME_VAR=SOME_VALUE")); + } + } \ No newline at end of file