Skip to content

Commit

Permalink
✅ : add plan and stop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jul 19, 2019
1 parent c9c9f96 commit b997ab5
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void plan(Job job, TerraformModule module, Stack stack) {
}
else if(result == 2){
// there is a diff, set the status of the stack to : "TO_UPDATE"
if(StackState.RUNNING != stack.getState() && StackState.NEW != stack.getState()){
if(StackState.NEW != stack.getState()){
stack.setState(StackState.TO_UPDATE);
stackRepository.save(stack);
}
Expand Down
134 changes: 134 additions & 0 deletions src/test/java/io/codeka/gaia/runner/StackRunnerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.codeka.gaia.runner;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.ContainerExit;
Expand All @@ -13,6 +15,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.WritableByteChannel;

Expand Down Expand Up @@ -106,4 +109,135 @@ void successfullJob_shouldSetTheStackStateToRunning() throws Exception {
verify(stackRepository).save(stack);
}

@Test
void plan_shouldUpdateTheStackState_whenThereIsADiffForRunningStacks() throws Exception {
var job = new Job();
var module = new TerraformModule();
var stack = new Stack();
stack.setState(StackState.RUNNING);

var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository);

// simulating a container with id 12
var containerCreation = mock(ContainerCreation.class);
when(containerCreation.id()).thenReturn("12");
when(dockerClient.createContainer(any())).thenReturn(containerCreation);

// setting mocks to let test pass till the end
var writableByteChannel = mock(OutputStream.class);
when(httpHijackWorkaround.getOutputStream(any(), any())).thenReturn(writableByteChannel);

when(stackCommandBuilder.buildPlanScript(stack, module)).thenReturn("");

// given
var containerExit = mock(ContainerExit.class);
when(containerExit.statusCode()).thenReturn(2L);
when(dockerClient.waitContainer("12")).thenReturn(containerExit);

// when
stackRunner.plan(job, module, stack);

// then
verify(jobRepository).save(job);

assertEquals(StackState.TO_UPDATE, stack.getState());
verify(stackRepository).save(stack);
}

@Test
void plan_shouldNotUpdateTheStackState_whenThereIsADiffForNewStacks() throws Exception {
var job = new Job();
var module = new TerraformModule();
var stack = new Stack();
stack.setState(StackState.NEW);

var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository);

// simulating a container with id 12
var containerCreation = mock(ContainerCreation.class);
when(containerCreation.id()).thenReturn("12");
when(dockerClient.createContainer(any())).thenReturn(containerCreation);

// setting mocks to let test pass till the end
var writableByteChannel = mock(OutputStream.class);
when(httpHijackWorkaround.getOutputStream(any(), any())).thenReturn(writableByteChannel);

when(stackCommandBuilder.buildPlanScript(stack, module)).thenReturn("");

// given
var containerExit = mock(ContainerExit.class);
when(containerExit.statusCode()).thenReturn(2L);
when(dockerClient.waitContainer("12")).thenReturn(containerExit);

// when
stackRunner.plan(job, module, stack);

// then
verify(jobRepository).save(job);

assertEquals(StackState.NEW, stack.getState());
verifyZeroInteractions(stackRepository);
}

@Test
void stop_shouldUpdateTheStackState_whenSuccessful() throws Exception {
var job = new Job();
var module = new TerraformModule();
var stack = new Stack();
stack.setState(StackState.RUNNING);

var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository);

// simulating a container with id 12
var containerCreation = mock(ContainerCreation.class);
when(containerCreation.id()).thenReturn("12");
when(dockerClient.createContainer(any())).thenReturn(containerCreation);

// setting mocks to let test pass till the end
var writableByteChannel = mock(OutputStream.class);
when(httpHijackWorkaround.getOutputStream(any(), any())).thenReturn(writableByteChannel);

when(stackCommandBuilder.buildDestroyScript(stack, module)).thenReturn("");

// given
var containerExit = mock(ContainerExit.class);
when(containerExit.statusCode()).thenReturn(0L);
when(dockerClient.waitContainer("12")).thenReturn(containerExit);

// when
stackRunner.stop(job, module, stack);

// then
verify(jobRepository).save(job);

assertEquals(StackState.STOPPED, stack.getState());
verify(stackRepository).save(stack);
}

@Test
void jobShouldFail_whenFailingToStartContainer() throws Exception {
var job = new Job();
var module = new TerraformModule();
var stack = new Stack();
stack.setState(StackState.RUNNING);

var stackRunner = new StackRunner(dockerClient, builder, settings, stackCommandBuilder, stackRepository, httpHijackWorkaround, jobRepository);

// simulating a container with id 12
var containerCreation = mock(ContainerCreation.class);
when(containerCreation.id()).thenReturn("12");
when(dockerClient.createContainer(any())).thenReturn(containerCreation);

doThrow(new DockerException("test")).when(dockerClient).startContainer("12");

when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn("");

// when
stackRunner.apply(job, module, stack);

// then
assertEquals(JobStatus.FAILED, job.getStatus());
verify(jobRepository).save(job);
}

}

0 comments on commit b997ab5

Please # to comment.