diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ef1b505524..08d8905c3c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -39,6 +39,19 @@ jobs: env: ORG_GRADLE_PROJECT_version: ${{ steps.build_variables.outputs.VERSION }} run: ./gradlew build --stacktrace ${{ steps.build_variables.outputs.REPO }}-web:installDist + - name: Build local slim container image for testing + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile.slim + load: true + platforms: local + tags: | + "${{ steps.build_variables.outputs.REPO }}:${{ steps.build_variables.outputs.VERSION }}-unvalidated" + - name: Test local slim container image + env: + FULL_DOCKER_IMAGE_NAME: "${{ steps.build_variables.outputs.REPO }}:${{ steps.build_variables.outputs.VERSION }}-unvalidated" + run: ./gradlew rosco-integration:test - name: Login to GAR # Only run this on repositories in the 'spinnaker' org, not on forks. if: startsWith(github.repository, 'spinnaker/') diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 30003c87cb..9bc44b30bc 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -72,4 +72,17 @@ jobs: platforms: linux/amd64,linux/arm64 tags: | "${{ env.CONTAINER_REGISTRY }}/${{ steps.build_variables.outputs.REPO }}:latest-java11-ubuntu" - "${{ env.CONTAINER_REGISTRY }}/${{ steps.build_variables.outputs.REPO }}:${{ steps.build_variables.outputs.VERSION }}-java11-ubuntu" \ No newline at end of file + "${{ env.CONTAINER_REGISTRY }}/${{ steps.build_variables.outputs.REPO }}:${{ steps.build_variables.outputs.VERSION }}-java11-ubuntu" + - name: Build local slim container image for testing + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile.slim + load: true + platforms: local + tags: | + "${{ steps.build_variables.outputs.REPO }}:${{ steps.build_variables.outputs.VERSION }}" + - name: Test local slim container image + env: + FULL_DOCKER_IMAGE_NAME: "${{ steps.build_variables.outputs.REPO }}:${{ steps.build_variables.outputs.VERSION }}" + run: ./gradlew rosco-integration:test diff --git a/Dockerfile.java11.slim b/Dockerfile.java11.slim index 49648b7927..72772b66b0 100644 --- a/Dockerfile.java11.slim +++ b/Dockerfile.java11.slim @@ -67,5 +67,6 @@ RUN for plugin in $PACKER_PLUGINS ; do \ fi; \ done -CMD ["/opt/rosco/bin/rosco"] +HEALTHCHECK CMD curl http://localhost:8087/health | grep UP || exit 1 +CMD ["/opt/rosco/bin/rosco"] diff --git a/Dockerfile.java11.ubuntu b/Dockerfile.java11.ubuntu index 5ccf003116..5dff3b5219 100644 --- a/Dockerfile.java11.ubuntu +++ b/Dockerfile.java11.ubuntu @@ -79,4 +79,6 @@ RUN for plugin in $PACKER_PLUGINS ; do \ fi; \ done +HEALTHCHECK CMD curl http://localhost:8087/health | grep UP || exit 1 + CMD ["/opt/rosco/bin/rosco"] diff --git a/Dockerfile.slim b/Dockerfile.slim index 2460ee5908..2513e88059 100644 --- a/Dockerfile.slim +++ b/Dockerfile.slim @@ -67,5 +67,6 @@ RUN for plugin in $PACKER_PLUGINS ; do \ fi; \ done -CMD ["/opt/rosco/bin/rosco"] +HEALTHCHECK CMD curl http://localhost:8087/health | grep UP || exit 1 +CMD ["/opt/rosco/bin/rosco"] diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu index 69c7f35d5f..e47a6a45f5 100644 --- a/Dockerfile.ubuntu +++ b/Dockerfile.ubuntu @@ -79,4 +79,6 @@ RUN for plugin in $PACKER_PLUGINS ; do \ fi; \ done +HEALTHCHECK CMD curl http://localhost:8087/health | grep UP || exit 1 + CMD ["/opt/rosco/bin/rosco"] diff --git a/rosco-integration/rosco-integration.gradle b/rosco-integration/rosco-integration.gradle new file mode 100644 index 0000000000..8c19871c83 --- /dev/null +++ b/rosco-integration/rosco-integration.gradle @@ -0,0 +1,25 @@ +dependencies { + implementation "com.fasterxml.jackson.core:jackson-databind" + implementation "org.assertj:assertj-core" + implementation "org.junit.jupiter:junit-jupiter-api" + implementation "org.slf4j:slf4j-api" + implementation "org.testcontainers:testcontainers" + implementation "org.testcontainers:junit-jupiter" + runtimeOnly "ch.qos.logback:logback-classic" + runtimeOnly "org.junit.jupiter:junit-jupiter-engine" +} + +test.configure { + def fullDockerImageName = System.getenv('FULL_DOCKER_IMAGE_NAME') + onlyIf("there is a docker image to test") { + fullDockerImageName != null && fullDockerImageName.trim() != '' + } +} + +test { + // So stdout and stderr from the just-built container are available in CI + testLogging.showStandardStreams = true + + // Run the tests when the docker image changes + inputs.property 'fullDockerImageName', System.getenv('FULL_DOCKER_IMAGE_NAME') +} diff --git a/rosco-integration/src/test/java/com/netflix/spinnaker/rosco/StandaloneContainerTest.java b/rosco-integration/src/test/java/com/netflix/spinnaker/rosco/StandaloneContainerTest.java new file mode 100644 index 0000000000..d2f83192ba --- /dev/null +++ b/rosco-integration/src/test/java/com/netflix/spinnaker/rosco/StandaloneContainerTest.java @@ -0,0 +1,132 @@ +/* + * Copyright 2024 Salesforce, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.netflix.spinnaker.rosco; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Map; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +@Testcontainers +class StandaloneContainerTest { + + private static final String REDIS_NETWORK_ALIAS = "redisHost"; + + private static final int REDIS_PORT = 6379; + + private static final Logger logger = LoggerFactory.getLogger(StandaloneContainerTest.class); + + private static final Network network = Network.newNetwork(); + + private static GenericContainer redis = + new GenericContainer(DockerImageName.parse("library/redis:5-alpine")) + .withNetwork(network) + .withNetworkAliases(REDIS_NETWORK_ALIAS) + .withExposedPorts(REDIS_PORT); + + private static GenericContainer roscoContainer; + + @BeforeAll + static void setupOnce() throws Exception { + String fullDockerImageName = System.getenv("FULL_DOCKER_IMAGE_NAME"); + + // Skip the tests if there's no docker image. This allows gradlew build to work. + assumeTrue(fullDockerImageName != null); + + redis.start(); + + DockerImageName dockerImageName = DockerImageName.parse(fullDockerImageName); + + roscoContainer = + new GenericContainer(dockerImageName) + .withNetwork(network) + .withExposedPorts(8087) + .dependsOn(redis) + .waitingFor(Wait.forHealthcheck()) + .withEnv("SPRING_APPLICATION_JSON", getSpringApplicationJson()); + + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger); + roscoContainer.start(); + roscoContainer.followOutput(logConsumer); + } + + private static String getSpringApplicationJson() throws JsonProcessingException { + String redisUrl = "redis://" + REDIS_NETWORK_ALIAS + ":" + REDIS_PORT; + logger.info("redisUrl: '{}'", redisUrl); + Map properties = Map.of("redis.connection", redisUrl); + + ObjectMapper mapper = new ObjectMapper(); + return mapper.writeValueAsString(properties); + } + + @AfterAll + static void cleanupOnce() { + if (roscoContainer != null) { + roscoContainer.stop(); + } + + if (redis != null) { + redis.stop(); + } + } + + @BeforeEach + void init(TestInfo testInfo) { + System.out.println("--------------- Test " + testInfo.getDisplayName()); + } + + @Test + void testHealthCheck() throws Exception { + // hit an arbitrary endpoint + HttpRequest request = + HttpRequest.newBuilder() + .uri( + new URI( + "http://" + + roscoContainer.getHost() + + ":" + + roscoContainer.getFirstMappedPort() + + "/health")) + .GET() + .build(); + + HttpClient client = HttpClient.newHttpClient(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assertThat(response).isNotNull(); + logger.info("response: {}, {}", response.statusCode(), response.body()); + assertThat(response.statusCode()).isEqualTo(200); + } +} diff --git a/rosco-integration/src/test/resources/logback.xml b/rosco-integration/src/test/resources/logback.xml new file mode 100644 index 0000000000..d0573f0bfc --- /dev/null +++ b/rosco-integration/src/test/resources/logback.xml @@ -0,0 +1,36 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + diff --git a/settings.gradle b/settings.gradle index 53e004d6a1..6be99e6d84 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,7 +8,11 @@ rootProject.name = 'rosco' -include 'rosco-bom', 'rosco-core', 'rosco-web', 'rosco-manifests' +include 'rosco-bom', + 'rosco-core', + 'rosco-integration', + 'rosco-manifests', + 'rosco-web' def setBuildFile(project) { project.buildFileName = "${project.name}.gradle"