-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.gaia_app.runner; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.web.client.ExpectedCount; | ||
import org.springframework.test.web.client.MockRestServiceServer; | ||
import org.springframework.test.web.client.match.MockRestRequestMatchers; | ||
import org.springframework.test.web.client.response.MockRestResponseCreators; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; | ||
|
||
@SpringBootTest | ||
@AutoConfigureWebClient | ||
@AutoConfigureMockMvc | ||
@TestPropertySource(properties = { | ||
"gaia.url=https://gaia-app.io", | ||
"gaia.runner.username=gaia-runner", | ||
"gaia.runner.password=gaia-runner-password", | ||
}) | ||
class RunnerIT { | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
@Test | ||
void runnerShouldRunJobs(){ | ||
// given | ||
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build(); | ||
|
||
server.expect(ExpectedCount.manyTimes(), requestTo("https://gaia-app.io/api/runner/steps/request")) | ||
.andExpect(MockRestRequestMatchers.header("Authorization", "Basic Z2FpYS1ydW5uZXI6cGFzc3dvcmQ=")) | ||
.andRespond(MockRestResponseCreators.withSuccess(new ClassPathResource("/rest/step-request.json"), MediaType.APPLICATION_JSON)); | ||
|
||
server.expect(requestTo("https://gaia-app.io/api/runner/steps/12/start")) | ||
.andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) | ||
.andExpect(MockRestRequestMatchers.header("Authorization", "Basic Z2FpYS1ydW5uZXI6cGFzc3dvcmQ=")) | ||
.andRespond(MockRestResponseCreators.withSuccess()); | ||
|
||
server.expect(requestTo("https://gaia-app.io/api/runner/steps/12/logs")) | ||
.andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) | ||
.andExpect(MockRestRequestMatchers.header("Authorization", "Basic Z2FpYS1ydW5uZXI6cGFzc3dvcmQ=")) | ||
.andExpect(MockRestRequestMatchers.content().json("[gaia] using image hashicorp/terraform:latest\n")) | ||
.andRespond(MockRestResponseCreators.withSuccess()); | ||
|
||
server.expect(requestTo("https://gaia-app.io/api/runner/steps/12/status")) | ||
.andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) | ||
.andExpect(MockRestRequestMatchers.header("Authorization", "Basic Z2FpYS1ydW5uZXI6cGFzc3dvcmQ=")) | ||
.andExpect(MockRestRequestMatchers.content().json("0")) | ||
.andRespond(MockRestResponseCreators.withSuccess()); | ||
|
||
await() | ||
.atMost(Duration.ofSeconds(30)) | ||
.untilAsserted(server::verify); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.gaia_app.runner; | ||
|
||
import io.gaia_app.stacks.bo.Step; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class StepPollerTest { | ||
|
||
@InjectMocks | ||
private StepPoller stepPoller; | ||
|
||
@Mock | ||
private StepRunner stepRunner; | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@Test | ||
void getJobAndRun_shouldGetAJobAndRunIt(){ | ||
// given | ||
ReflectionTestUtils.setField(stepPoller, "gaiaUrl", "http://localhost:8080"); | ||
|
||
var step = new RunnerStep(new Step(), "hashicorp/terraform:latest", "echo 'Hello'", List.of()); | ||
when(restTemplate.getForObject("http://localhost:8080/api/runner/steps/request", RunnerStep.class)).thenReturn(step); | ||
|
||
// when | ||
stepPoller.getJobAndRun(); | ||
|
||
// then | ||
verify(stepRunner).runStep(step); | ||
} | ||
|
||
@Test | ||
void getJobAndRun_shouldDoNothingWhenNoJobIsAvailable(){ | ||
// given | ||
ReflectionTestUtils.setField(stepPoller, "gaiaUrl", "http://localhost:8080"); | ||
|
||
var notFound = HttpClientErrorException.create(HttpStatus.NOT_FOUND, null, null, null, null); | ||
when(restTemplate.getForObject("http://localhost:8080/api/runner/steps/request", RunnerStep.class)).thenThrow(notFound); | ||
|
||
// when | ||
stepPoller.getJobAndRun(); | ||
|
||
// then | ||
verifyNoInteractions(stepRunner); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.gaia_app.runner; | ||
|
||
import io.gaia_app.runner.docker.DockerRunner; | ||
import io.gaia_app.stacks.bo.Step; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class StepRunnerTest { | ||
|
||
@InjectMocks | ||
private StepRunner stepRunner; | ||
|
||
@Mock | ||
private DockerRunner dockerRunner; | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@Test | ||
void runStep_shouldRunAStep() { | ||
// given | ||
var image = "hashicorp/terraform:0.13.0"; | ||
var script = "echo 'Hello'"; | ||
var runnerStep = new RunnerStep(new Step(), image, script, List.of()); | ||
|
||
// when | ||
stepRunner.runStep(runnerStep); | ||
|
||
// then | ||
verify(dockerRunner).runJobStepInContainer(eq(image), any(StepLogger.class), eq(script), eq(List.of())); | ||
} | ||
|
||
@Test | ||
void runStep_shouldRunNotifyThatTheStepHasStartedAndEnded() { | ||
// given | ||
var image = "hashicorp/terraform:0.13.0"; | ||
var script = "echo 'Hello'"; | ||
var step = new Step(); | ||
step.setId("12"); | ||
var runnerStep = new RunnerStep(step, image, script, List.of()); | ||
|
||
ReflectionTestUtils.setField(stepRunner, "gaiaUrl", "http://localhost:8080"); | ||
|
||
when(dockerRunner.runJobStepInContainer(eq(image), any(StepLogger.class), eq(script), eq(List.of()))).thenReturn(2); | ||
|
||
// when | ||
stepRunner.runStep(runnerStep); | ||
|
||
// then | ||
verify(restTemplate).put("http://localhost:8080/api/runner/steps/12/start", null); | ||
verify(restTemplate).put("http://localhost:8080/api/runner/steps/12/status", 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.gaia_app.runner.docker | ||
|
||
import io.gaia_app.runner.StepLogger | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Assert | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.TestPropertySource | ||
|
||
@SpringBootTest(classes = [DockerRunner::class, DockerJavaClientConfig::class, DockerConfigurationProperties::class]) | ||
@EnableConfigurationProperties | ||
@TestPropertySource(properties = ["gaia.dockerDaemonUrl=unix:///var/run/docker.sock"]) | ||
class DockerRunnerIT { | ||
|
||
@Autowired | ||
private lateinit var dockerRunner: DockerRunner | ||
|
||
private val image = "hashicorp/terraform:0.13.0" | ||
|
||
private val printlnLogger = StepLogger { println(it) } | ||
|
||
@Test | ||
fun `runContainerForJob() should work with a simple script`() { | ||
val script = "echo 'Hello World'; exit 0;" | ||
|
||
assertEquals(0, dockerRunner.runJobStepInContainer(image, printlnLogger, script, listOf()).toLong()) | ||
} | ||
|
||
@Test | ||
fun `runContainerForJob() should stop work with a simple script`() { | ||
val script = "set -e; echo 'Hello World'; false; exit 0;" | ||
|
||
assertEquals(1, dockerRunner.runJobStepInContainer(image, printlnLogger, script, listOf()).toLong()) | ||
} | ||
|
||
@Test | ||
fun `runContainerForJob() should return the script exit code`() { | ||
val script = "exit 5" | ||
|
||
Assert.assertEquals(5, dockerRunner.runJobStepInContainer(image, printlnLogger, script, listOf()).toLong()) | ||
} | ||
|
||
@Test | ||
fun `runContainerForJob() should feed step with container logs`() { | ||
val script = "echo 'hello world'; exit 0;" | ||
|
||
val logs = mutableListOf<String>() | ||
val listLogger = StepLogger { logs.add(it) } | ||
|
||
dockerRunner.runJobStepInContainer(image, listLogger, script, listOf()) | ||
assertThat(logs).isEqualTo(listOf("hello world\n")) | ||
} | ||
|
||
@Test | ||
fun `runContainerForJob() use env of the job`() { | ||
val script = "echo \$AWS_ACCESS_KEY_ID; exit 0;" | ||
|
||
val logs = mutableListOf<String>() | ||
val listLogger = StepLogger { logs.add(it) } | ||
|
||
dockerRunner.runJobStepInContainer(image, listLogger, script, listOf("AWS_ACCESS_KEY_ID=SOME_ACCESS_KEY")) | ||
|
||
assertThat(logs).isEqualTo(listOf("SOME_ACCESS_KEY\n")) | ||
} | ||
|
||
@Test | ||
fun `runContainerForJob() use TF_IN_AUTOMATION env var`() { | ||
val script = "echo \$TF_IN_AUTOMATION; exit 0;" | ||
|
||
val logs = mutableListOf<String>() | ||
val listLogger = StepLogger { logs.add(it) } | ||
|
||
dockerRunner.runJobStepInContainer(image, listLogger, script, listOf()) | ||
|
||
assertThat(logs).isEqualTo(listOf("true\n")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"script": "set -e\necho '[gaia] using image hashicorp/terraform:latest'\nexit 0\n", | ||
"step": { | ||
"id": "12" | ||
}, | ||
"env": [ | ||
"FAKE_ACCESS_KEY=fake-access-key" | ||
], | ||
"image": "hashicorp/terraform:latest" | ||
} |