Skip to content

Commit

Permalink
✨ : add type for job
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Jul 23, 2019
1 parent 0319b72 commit 8086666
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 86 deletions.
10 changes: 9 additions & 1 deletion src/main/java/io/codeka/gaia/bo/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public void setDateTime(LocalDateTime dateTime) {

private JobStatus jobStatus;

private JobType jobType;

public String getId() {
return id;
}
Expand All @@ -58,8 +60,9 @@ public JobStatus getStatus(){
return this.jobStatus;
}

public void start() {
public void start(JobType jobType) {
this.jobStatus = JobStatus.RUNNING;
this.jobType = jobType;
}

public void end(){
Expand All @@ -81,4 +84,9 @@ public String getStackId() {
public void setStackId(String stackId) {
this.stackId = stackId;
}

public JobType getType() {
return this.jobType;
}

}
5 changes: 5 additions & 0 deletions src/main/java/io/codeka/gaia/bo/JobType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.codeka.gaia.bo;

public enum JobType {
PREVIEW, RUN, STOP
}
6 changes: 3 additions & 3 deletions src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private int runContainerForJob(Job job, String script) {
@Async
public void apply(Job job, TerraformModule module, Stack stack) {
this.jobs.put(job.getId(), job);
job.start();
job.start(JobType.RUN);

var applyScript = stackCommandBuilder.buildApplyScript(stack, module);

Expand Down Expand Up @@ -134,7 +134,7 @@ public void apply(Job job, TerraformModule module, Stack stack) {
@Async
public void plan(Job job, TerraformModule module, Stack stack) {
this.jobs.put(job.getId(), job);
job.start();
job.start(JobType.PREVIEW);

var planScript = stackCommandBuilder.buildPlanScript(stack, module);

Expand Down Expand Up @@ -181,7 +181,7 @@ public Job getJob(String jobId) {
@Async
public void stop(Job job, TerraformModule module, Stack stack) {
this.jobs.put(job.getId(), job);
job.start();
job.start(JobType.STOP);

var destroyScript = stackCommandBuilder.buildDestroyScript(stack, module);

Expand Down
59 changes: 58 additions & 1 deletion src/main/resources/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ a[data-toggle="collapse"] {
}

.job_list li {
padding: 20px 40px;
padding: 10px;
border-bottom: solid #eee 1px;
line-height: normal;
font-size: 16px;
Expand Down Expand Up @@ -1803,6 +1803,63 @@ button.main_bt {
border-left-color: #1ed085;
}

.job_list .job {
display: flex;
}

.job_list .job .job_id {
display: flex;
justify-content: center;
align-items: center;
min-width: 100px;
}

.job_list .job .job_id a {
font-size: larger;
}

.job_list .job .job_detail {
display: flex;
flex-direction: column;
justify-content: center;
border-left: 2px dashed cadetblue;
margin-left: 10px;
padding-left: 10px;
}

.job_list .job .job_attr_id {
width: 50px;
}

.job_list .job .job_attr_value {
font-weight: bold;
margin-left: 5px;
}

.job_list .job .job_attr_value.FINISHED {
color: #1ed085;
}

.job_list .job .job_attr_value.RUNNING {
color: #2196f3;
}

.job_list .job .job_attr_value.FAILED {
color: #e91e63;
}

.job_list .job .job_attr_value.PREVIEW {
color: #17a2b8;
}

.job_list .job .job_attr_value.RUN {
color: #007bff;
}

.job_list .job .job_attr_value.STOP {
color: #dc3545;
}

.msg_list li:nth-child(1) {
border-left-color: #ff9800;
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/resources/templates/stack.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ <h2>Module variables values</h2>
<h2><span><i class="fas fa-history"></i> Job history</span></h2>
</div>
<ul class="job_list">
<li v-for="(job, index) in jobs" :class="job.status">
<a :href="'/stacks/' + job.stackId + '/jobs/' + job.id">Job #{{index+1}} - {{job.status}}</a>
<br>
<strong>{{job.dateTime | dateTime}}</strong>
<li v-for="(job, index) in jobs" :class="job.status" class="job">
<div class="job_id"><a :href="'/stacks/' + job.stackId + '/jobs/' + job.id">Job #{{index+1}}</a></div>
<div class="job_detail">
<div><span class="job_attr_id">Type</span><span class="job_attr_value" :class="job.type">{{job.type}}</span></div>
<div><span class="job_attr_id">Status</span><span class="job_attr_value" :class="job.status">{{job.status}}</span></div>
<div><span class="job_attr_id">Date</span><span class="job_attr_value">{{job.dateTime | dateTime}}</span></div>
</div>
</li>
</ul>
</div>
Expand Down
159 changes: 82 additions & 77 deletions src/test/java/io/codeka/gaia/runner/StackRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.codeka.gaia.bo.*;
import io.codeka.gaia.repository.JobRepository;
import io.codeka.gaia.repository.StackRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
Expand Down Expand Up @@ -46,29 +47,37 @@ class StackRunnerTest {
@Mock
private JobRepository jobRepository;

@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);

@BeforeEach
void containerCreateMock() throws Exception {
// simulating a container with id 12
var containerCreation = mock(ContainerCreation.class);
when(containerCreation.id()).thenReturn("12");
when(dockerClient.createContainer(any())).thenReturn(containerCreation);
}

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

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

void containerExitMock(Long statusCode) throws Exception {
// given
var containerExit = mock(ContainerExit.class);
when(containerExit.statusCode()).thenReturn(0L);
when(containerExit.statusCode()).thenReturn(statusCode);
when(dockerClient.waitContainer("12")).thenReturn(containerExit);
}

@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);

httpHijackWorkaroundMock();
containerExitMock(0L);
when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn("");

// when
stackRunner.apply(job, module, stack);
Expand All @@ -82,25 +91,12 @@ 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);

// 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);

httpHijackWorkaroundMock();
containerExitMock(0L);
when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn("");

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

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

Expand All @@ -115,25 +111,12 @@ void plan_shouldUpdateTheStackState_whenThereIsADiffForRunningStacks() throws Ex
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);

httpHijackWorkaroundMock();
containerExitMock(2L);
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);

Expand All @@ -150,25 +133,12 @@ void plan_shouldNotUpdateTheStackState_whenThereIsADiffForNewStacks() throws Exc
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);

httpHijackWorkaroundMock();
containerExitMock(2L);
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);

Expand All @@ -185,25 +155,12 @@ void stop_shouldUpdateTheStackState_whenSuccessful() throws Exception {
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);

httpHijackWorkaroundMock();
containerExitMock(0L);
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);

Expand All @@ -223,13 +180,7 @@ void jobShouldFail_whenFailingToStartContainer() throws Exception {

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
Expand All @@ -240,4 +191,58 @@ void jobShouldFail_whenFailingToStartContainer() throws Exception {
verify(jobRepository).save(job);
}

@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);

httpHijackWorkaroundMock();
containerExitMock(0L);
when(stackCommandBuilder.buildPlanScript(stack, module)).thenReturn("");

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

// then
assertEquals(JobType.PREVIEW, job.getType());
}

@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);

httpHijackWorkaroundMock();
containerExitMock(0L);
when(stackCommandBuilder.buildApplyScript(stack, module)).thenReturn("");

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

// then
assertEquals(JobType.RUN, job.getType());
}

@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);

httpHijackWorkaroundMock();
containerExitMock(0L);
when(stackCommandBuilder.buildDestroyScript(stack, module)).thenReturn("");

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

// then
assertEquals(JobType.STOP, job.getType());
}

}

0 comments on commit 8086666

Please sign in to comment.