From 8086666ea5a7e3071e10413e82765d5a9b2a9e7c Mon Sep 17 00:00:00 2001 From: Cyril Dubuisson Date: Tue, 23 Jul 2019 19:40:26 +0200 Subject: [PATCH] :sparkles: : add type for job --- src/main/java/io/codeka/gaia/bo/Job.java | 10 +- src/main/java/io/codeka/gaia/bo/JobType.java | 5 + .../io/codeka/gaia/runner/StackRunner.java | 6 +- src/main/resources/static/css/style.css | 59 ++++++- src/main/resources/templates/stack.html | 11 +- .../codeka/gaia/runner/StackRunnerTest.java | 159 +++++++++--------- 6 files changed, 164 insertions(+), 86 deletions(-) create mode 100644 src/main/java/io/codeka/gaia/bo/JobType.java diff --git a/src/main/java/io/codeka/gaia/bo/Job.java b/src/main/java/io/codeka/gaia/bo/Job.java index 020ebaa76..1dd58dd42 100644 --- a/src/main/java/io/codeka/gaia/bo/Job.java +++ b/src/main/java/io/codeka/gaia/bo/Job.java @@ -34,6 +34,8 @@ public void setDateTime(LocalDateTime dateTime) { private JobStatus jobStatus; + private JobType jobType; + public String getId() { return id; } @@ -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(){ @@ -81,4 +84,9 @@ public String getStackId() { public void setStackId(String stackId) { this.stackId = stackId; } + + public JobType getType() { + return this.jobType; + } + } diff --git a/src/main/java/io/codeka/gaia/bo/JobType.java b/src/main/java/io/codeka/gaia/bo/JobType.java new file mode 100644 index 000000000..82485e753 --- /dev/null +++ b/src/main/java/io/codeka/gaia/bo/JobType.java @@ -0,0 +1,5 @@ +package io.codeka.gaia.bo; + +public enum JobType { + PREVIEW, RUN, STOP +} diff --git a/src/main/java/io/codeka/gaia/runner/StackRunner.java b/src/main/java/io/codeka/gaia/runner/StackRunner.java index a910989df..288152b09 100644 --- a/src/main/java/io/codeka/gaia/runner/StackRunner.java +++ b/src/main/java/io/codeka/gaia/runner/StackRunner.java @@ -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); @@ -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); @@ -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); diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css index 15dd4c6b0..0e8df14a0 100755 --- a/src/main/resources/static/css/style.css +++ b/src/main/resources/static/css/style.css @@ -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; @@ -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; } diff --git a/src/main/resources/templates/stack.html b/src/main/resources/templates/stack.html index f2fc6deeb..b96184b07 100644 --- a/src/main/resources/templates/stack.html +++ b/src/main/resources/templates/stack.html @@ -137,10 +137,13 @@

Module variables values

Job history

diff --git a/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java b/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java index e55ec562a..aeb70fd1b 100644 --- a/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java +++ b/src/test/java/io/codeka/gaia/runner/StackRunnerTest.java @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 @@ -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()); + } + } \ No newline at end of file