Skip to content

Commit

Permalink
♻ : use stepId instead of step object
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Nov 12, 2020
1 parent 9ed36bb commit 27f35db
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 64 deletions.
17 changes: 7 additions & 10 deletions src/main/java/io/gaia_app/runner/RunnerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Stack;

/**
* Controller for the operations that are called by the runner only
Expand Down Expand Up @@ -48,14 +47,12 @@ public String tfvars(@PathVariable String id){
* Gets the first job step that can be run by the runner (one in "pending" state)
*/
@GetMapping("/steps/request")
public Map<String, Object> findFirstRunnableJob() {
var job = this.jobRepository.findFirstByStatusEqualsOrStatusEquals(JobStatus.PLAN_PENDING, JobStatus.APPLY_PENDING)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
var stack = this.stackRepository.findById(job.getStackId()).orElseThrow();
public Map<String, Object> findFirstRunnableStep() {
var step = this.stepRepository.findFirstByStatus(StepStatus.PENDING)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NO_CONTENT));

// get the workflow
var workflow = new JobWorkflow(job);
var step = workflow.getCurrentStep();
var job = this.jobRepository.findById(step.getJobId()).orElseThrow();
var stack = this.stackRepository.findById(job.getStackId()).orElseThrow();

var script = "";
// generate the script
Expand Down Expand Up @@ -85,7 +82,7 @@ public Map<String, Object> findFirstRunnableJob() {
}

return Map.of(
"step", step,
"id", step.getId(),
"script", script,
"env", env,
"image", job.getTerraformImage().image());
Expand All @@ -104,7 +101,7 @@ public void updateLogs(@PathVariable String stepId, @RequestBody String logs) {
/**
* Updates the step status
*/
@PutMapping("/steps/{stepId}/status")
@PutMapping("/steps/{stepId}/end")
public void updateStepStatus(@PathVariable String stepId, @RequestBody int status) {
// getting jobId
var jobId = this.stepRepository.findById(stepId).orElseThrow().getJobId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.gaia_app.credentials.CredentialsRepository;
import io.gaia_app.stacks.bo.Job;
import io.gaia_app.stacks.bo.JobStatus;
import io.gaia_app.stacks.bo.JobType;
import io.gaia_app.stacks.bo.Stack;
import io.gaia_app.stacks.repository.JobRepository;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/gaia_app/stacks/repository/JobRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ interface JobRepository : MongoRepository<Job, String> {

fun findAllByStackIdOrderByStartDateTimeDesc(stackId: String): List<Job>

fun findFirstByStatusEqualsOrStatusEquals(planPending: JobStatus, applyPending: JobStatus): @NotNull Optional<Job>

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.gaia_app.stacks.repository;

import io.gaia_app.stacks.bo.Step;
import io.gaia_app.stacks.bo.StepStatus;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
* Repository for steps
*/
Expand All @@ -12,4 +15,5 @@ public interface StepRepository extends MongoRepository<Step, String> {

void deleteByJobId(String jobId);

Optional<Step> findFirstByStatus(StepStatus status);
}
4 changes: 3 additions & 1 deletion src/main/java/io/gaia_app/stacks/workflow/JobWorkflow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.gaia_app.stacks.workflow;

import io.gaia_app.stacks.bo.*;
import io.gaia_app.stacks.bo.Job;
import io.gaia_app.stacks.bo.JobStatus;
import io.gaia_app.stacks.bo.Step;
import io.gaia_app.stacks.workflow.state.*;

import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.gaia_app.stacks.workflow.state;

import io.gaia_app.stacks.bo.JobStatus;
import io.gaia_app.stacks.workflow.JobWorkflow;

/**
Expand Down
17 changes: 6 additions & 11 deletions src/test/java/io/gaia_app/runner/RunnerControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ void setUp() {
@Test
@WithMockUser("gaia-runner")
void findFirstRunnableJob_shouldReturnNothing_whenNoJobIsPending() throws Exception {
var stackId = "5a215b6b-fe53-4afa-85f0-a10175a7f264";

mockMvc.perform(get("/api/runner/steps/request"))
.andExpect(status().isNotFound());
.andExpect(status().isNoContent());
}

@Test
Expand All @@ -71,7 +69,7 @@ void findFirstRunnableJob_shouldReturnNothing_whenJobIsCreatedButNotPending() th
.andExpect(jsonPath("$.jobId", notNullValue()));

mockMvc.perform(get("/api/runner/steps/request"))
.andExpect(status().isNotFound());
.andExpect(status().isNoContent());
}

@Test
Expand All @@ -94,7 +92,7 @@ void findFirstRunnableJob_shouldReturnNothing_whenJobIsPending() throws Exceptio
mockMvc.perform(get("/api/runner/steps/request"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.env", notNullValue()))
.andExpect(jsonPath("$.step", notNullValue()))
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.script", notNullValue()))
.andExpect(jsonPath("$.image", notNullValue()));
}
Expand Down Expand Up @@ -167,7 +165,7 @@ void updateStepStatus_shouldUpdateStepState_inCaseOfSuccess() throws Exception {
this.stepRepository.save(step);

mockMvc.perform(
put("/api/runner/steps/{stepId}/status", step.getId())
put("/api/runner/steps/{stepId}/end", step.getId())
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("0"))
Expand All @@ -179,9 +177,6 @@ void updateStepStatus_shouldUpdateStepState_inCaseOfSuccess() throws Exception {

var updatedJob = this.jobRepository.findById("fakeJobId").orElseThrow();
assertThat(updatedJob.getStatus()).isEqualTo(JobStatus.PLAN_FINISHED);

// var updatedStack = this.stackRepository.findById("fakeStackId").orElseThrow();
// assertThat(updatedStack.getState()).isEqualTo(StackState.RUNNING);
}

@Test
Expand Down Expand Up @@ -209,7 +204,7 @@ void updateStepStatus_shouldUpdateStackStateToRunning_inCaseOfSuccessfulApply()
this.stepRepository.saveAll(job.getSteps());

mockMvc.perform(
put("/api/runner/steps/{stepId}/status", planStep.getId())
put("/api/runner/steps/{stepId}/end", planStep.getId())
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("0"))
Expand Down Expand Up @@ -246,7 +241,7 @@ void updateStepStatus_shouldUpdateStackStateToStopped_inCaseOfSuccessfulApply()
this.stepRepository.saveAll(job.getSteps());

mockMvc.perform(
put("/api/runner/steps/{stepId}/status", planStep.getId())
put("/api/runner/steps/{stepId}/end", planStep.getId())
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("0"))
Expand Down
70 changes: 32 additions & 38 deletions src/test/java/io/gaia_app/runner/RunnerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.gaia_app.stacks.bo.*;
import io.gaia_app.stacks.repository.JobRepository;
import io.gaia_app.stacks.repository.StackRepository;
import io.gaia_app.stacks.repository.StepRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -17,8 +18,6 @@
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand All @@ -30,6 +29,9 @@ class RunnerControllerTest {
@Mock
private JobRepository jobRepository;

@Mock
private StepRepository stepRepository;

@Mock
private StackRepository stackRepository;

Expand All @@ -39,6 +41,8 @@ class RunnerControllerTest {
@Mock
private CredentialsService credentialsService;

private Step step;

private Job job;

private Stack stack;
Expand All @@ -51,8 +55,15 @@ void setUp() {
this.job.setSteps(List.of(new Step(), new Step()));
this.job.setTerraformImage(new TerraformImage("hashicorp/terraform", "0.13.0"));

when(jobRepository.findFirstByStatusEqualsOrStatusEquals(JobStatus.PLAN_PENDING, JobStatus.APPLY_PENDING))
.thenReturn(Optional.of(job));
this.step = new Step();
this.step.setId("fakeStepId");
this.step.setStatus(StepStatus.PENDING);
this.step.setType(StepType.PLAN);
this.step.setJobId("fakeJobId");

when(stepRepository.findFirstByStatus(StepStatus.PENDING)).thenReturn(Optional.of(step));

when(jobRepository.findById("fakeJobId")).thenReturn(Optional.of(job));

this.stack = new Stack();
when(stackRepository.findById("fakeStackId")).thenReturn(Optional.of(stack));
Expand All @@ -64,14 +75,14 @@ void setUp() {
}

@Test
void findFirstRunnableJob_shouldUsePlanScript() {
void findFirstRunnableStep_shouldUsePlanScript() {
// given
this.job.setType(JobType.RUN);
this.job.setStatus(JobStatus.PLAN_PENDING);
this.job.getSteps().get(0).setType(StepType.PLAN);

// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
verify(runnerCommandBuilder).buildPlanScript(job, stack, null);
Expand All @@ -81,14 +92,14 @@ void findFirstRunnableJob_shouldUsePlanScript() {
}

@Test
void findFirstRunnableJob_shouldUseApplyScript() {
void findFirstRunnableStep_shouldUseApplyScript() {
// given
this.job.setType(JobType.RUN);
this.job.setStatus(JobStatus.APPLY_PENDING);
this.job.getSteps().get(0).setType(StepType.APPLY);
this.step.setType(StepType.APPLY);

// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
verify(runnerCommandBuilder).buildApplyScript(job, stack, null);
Expand All @@ -98,14 +109,13 @@ void findFirstRunnableJob_shouldUseApplyScript() {
}

@Test
void findFirstRunnableJob_shouldUsePlanDestroyScript() {
void findFirstRunnableStep_shouldUsePlanDestroyScript() {
// given
this.job.setType(JobType.DESTROY);
this.job.setStatus(JobStatus.PLAN_PENDING);
this.job.getSteps().get(0).setType(StepType.PLAN);

// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
verify(runnerCommandBuilder).buildPlanDestroyScript(job, stack, null);
Expand All @@ -115,14 +125,14 @@ void findFirstRunnableJob_shouldUsePlanDestroyScript() {
}

@Test
void findFirstRunnableJob_shouldUseApplyDestroyScript() {
void findFirstRunnableStep_shouldUseApplyDestroyScript() {
// given
this.job.setType(JobType.DESTROY);
this.job.setStatus(JobStatus.APPLY_PENDING);
this.job.getSteps().get(0).setType(StepType.APPLY);
this.step.setType(StepType.APPLY);

// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
verify(runnerCommandBuilder).buildDestroyScript(job, stack, null);
Expand All @@ -132,43 +142,27 @@ void findFirstRunnableJob_shouldUseApplyDestroyScript() {
}

@Test
void findFirstRunnableJob_shouldUsePlanStep() {
//given
this.job.setStatus(JobStatus.PLAN_PENDING);

// when
var result = runnerController.findFirstRunnableJob();

// then
assertThat(result)
.containsEntry("step", job.getSteps().get(0));
}

@Test
void findFirstRunnableJob_shouldUseApplyStep() {
//given
this.job.setStatus(JobStatus.APPLY_PENDING);

void findFirstRunnableStep_shouldUsePlanStep() {
// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
assertThat(result)
.containsEntry("step", job.getSteps().get(1));
.containsEntry("id", "fakeStepId");
}

@Test
void findFirstRunnableJob_shouldUseStackImage() {
void findFirstRunnableStep_shouldUseStackImage() {
// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
assertThat(result)
.containsEntry("image", "hashicorp/terraform:0.13.0");
}

@Test
void findFirstRunnableJob_shouldStackCredentials() {
void findFirstRunnableStep_shouldStackCredentials() {
//given
stack.setCredentialsId("fakeCredentials");

Expand All @@ -177,7 +171,7 @@ void findFirstRunnableJob_shouldStackCredentials() {
when(credentials.toEnv()).thenReturn(List.of("access_key=value","secret_key=secretValue"));

// when
var result = runnerController.findFirstRunnableJob();
var result = runnerController.findFirstRunnableStep();

// then
assertThat(result)
Expand Down

0 comments on commit 27f35db

Please sign in to comment.