Skip to content

Commit

Permalink
🚸 : use TF_IN_AUTOMATION env var
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Aug 2, 2019
1 parent 5078358 commit 00f3ce8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 54 deletions.
7 changes: 6 additions & 1 deletion src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,9 +56,13 @@ public StackRunner(DockerClient dockerClient, ContainerConfig.Builder containerC

private int runContainerForJob(Job job, String script) {
try{
var env = new ArrayList<String>();
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();

Expand Down
99 changes: 46 additions & 53 deletions src/test/java/io/codeka/gaia/runner/StackRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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 {
Expand All @@ -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("");
Expand All @@ -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("");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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("");
Expand All @@ -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("");
Expand All @@ -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("");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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"));
}

}

0 comments on commit 00f3ce8

Please sign in to comment.