From 2cc46e2bcde7dcd4e0e7d08b4a91258f2868e9c7 Mon Sep 17 00:00:00 2001 From: David Byron Date: Wed, 1 May 2024 21:54:42 -0700 Subject: [PATCH 1/4] feat(docker): add HEALTHCHECK to facilitate testing container startup --- Dockerfile.java11.slim | 3 ++- Dockerfile.java11.ubuntu | 3 ++- Dockerfile.slim | 3 ++- Dockerfile.ubuntu | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Dockerfile.java11.slim b/Dockerfile.java11.slim index b86637131..b41d85463 100644 --- a/Dockerfile.java11.slim +++ b/Dockerfile.java11.slim @@ -1,9 +1,10 @@ FROM alpine:3.16 LABEL maintainer="sig-platform@spinnaker.io" -RUN apk --no-cache add --update bash openjdk11-jre +RUN apk --no-cache add --update bash curl openjdk11-jre RUN addgroup -S -g 10111 spinnaker RUN adduser -S -G spinnaker -u 10111 spinnaker COPY kayenta-web/build/install/kayenta /opt/kayenta RUN mkdir -p /opt/kayenta/plugins && chown -R spinnaker:nogroup /opt/kayenta/plugins USER spinnaker +HEALTHCHECK CMD curl --fail http://localhost:8090/health CMD ["/opt/kayenta/bin/kayenta"] diff --git a/Dockerfile.java11.ubuntu b/Dockerfile.java11.ubuntu index e19df9e59..7f7de94b7 100644 --- a/Dockerfile.java11.ubuntu +++ b/Dockerfile.java11.ubuntu @@ -1,8 +1,9 @@ FROM ubuntu:bionic LABEL maintainer="sig-platform@spinnaker.io" -RUN apt-get update && apt-get -y install openjdk-11-jre-headless wget +RUN apt-get update && apt-get -y install curl openjdk-11-jre-headless wget RUN adduser --system --uid 10111 --group spinnaker COPY kayenta-web/build/install/kayenta /opt/kayenta RUN mkdir -p /opt/kayenta/plugins && chown -R spinnaker:nogroup /opt/kayenta/plugins USER spinnaker +HEALTHCHECK CMD curl --fail http://localhost:8090/health CMD ["/opt/kayenta/bin/kayenta"] diff --git a/Dockerfile.slim b/Dockerfile.slim index bea616a47..40c0585ce 100644 --- a/Dockerfile.slim +++ b/Dockerfile.slim @@ -1,9 +1,10 @@ FROM alpine:3.16 LABEL maintainer="sig-platform@spinnaker.io" -RUN apk --no-cache add --update bash openjdk17-jre +RUN apk --no-cache add --update bash curl openjdk17-jre RUN addgroup -S -g 10111 spinnaker RUN adduser -S -G spinnaker -u 10111 spinnaker COPY kayenta-web/build/install/kayenta /opt/kayenta RUN mkdir -p /opt/kayenta/plugins && chown -R spinnaker:nogroup /opt/kayenta/plugins USER spinnaker +HEALTHCHECK CMD curl --fail http://localhost:8090/health CMD ["/opt/kayenta/bin/kayenta"] diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu index 1c0395b4f..50ee57a93 100644 --- a/Dockerfile.ubuntu +++ b/Dockerfile.ubuntu @@ -1,8 +1,9 @@ FROM ubuntu:bionic LABEL maintainer="sig-platform@spinnaker.io" -RUN apt-get update && apt-get -y install openjdk-17-jre-headless wget +RUN apt-get update && apt-get -y install curl openjdk-17-jre-headless wget RUN adduser --system --uid 10111 --group spinnaker COPY kayenta-web/build/install/kayenta /opt/kayenta RUN mkdir -p /opt/kayenta/plugins && chown -R spinnaker:nogroup /opt/kayenta/plugins USER spinnaker +HEALTHCHECK CMD curl --fail http://localhost:8090/health CMD ["/opt/kayenta/bin/kayenta"] From 4282cce853c26542b394ae8cc7e11b4614276d79 Mon Sep 17 00:00:00 2001 From: David Byron Date: Wed, 1 May 2024 21:55:42 -0700 Subject: [PATCH 2/4] feat(build): add kayenta-integration module to exercise the just-built docker image --- .../kayenta-integration.gradle | 24 ++++ .../kayenta/StandaloneContainerTest.java | 132 ++++++++++++++++++ .../src/test/resources/logback.xml | 36 +++++ settings.gradle | 1 + 4 files changed, 193 insertions(+) create mode 100644 kayenta-integration/kayenta-integration.gradle create mode 100644 kayenta-integration/src/test/java/com/netflix/spinnaker/kayenta/StandaloneContainerTest.java create mode 100644 kayenta-integration/src/test/resources/logback.xml diff --git a/kayenta-integration/kayenta-integration.gradle b/kayenta-integration/kayenta-integration.gradle new file mode 100644 index 000000000..d19d36f39 --- /dev/null +++ b/kayenta-integration/kayenta-integration.gradle @@ -0,0 +1,24 @@ +dependencies { + testImplementation "com.fasterxml.jackson.core:jackson-databind" + testImplementation "org.assertj:assertj-core" + testImplementation "org.junit.jupiter:junit-jupiter-api" + testImplementation "org.slf4j:slf4j-api" + testImplementation "org.testcontainers:testcontainers" + testImplementation "org.testcontainers:junit-jupiter" + testRuntimeOnly "ch.qos.logback:logback-classic" +} + +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/kayenta-integration/src/test/java/com/netflix/spinnaker/kayenta/StandaloneContainerTest.java b/kayenta-integration/src/test/java/com/netflix/spinnaker/kayenta/StandaloneContainerTest.java new file mode 100644 index 000000000..3f19539dc --- /dev/null +++ b/kayenta-integration/src/test/java/com/netflix/spinnaker/kayenta/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.kayenta; + +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.time.Duration; +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 final GenericContainer redis = + new GenericContainer(DockerImageName.parse("library/redis:5-alpine")) + .withNetwork(network) + .withNetworkAliases(REDIS_NETWORK_ALIAS) + .withExposedPorts(REDIS_PORT); + + private static GenericContainer kayentaContainer; + + @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); + + kayentaContainer = + new GenericContainer(dockerImageName) + .withNetwork(network) + .withExposedPorts(8090) + .dependsOn(redis) + .waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(90))) + .withEnv("SPRING_APPLICATION_JSON", getSpringApplicationJson()); + + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger); + kayentaContainer.start(); + kayentaContainer.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 (kayentaContainer != null) { + kayentaContainer.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://" + + kayentaContainer.getHost() + + ":" + + kayentaContainer.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/kayenta-integration/src/test/resources/logback.xml b/kayenta-integration/src/test/resources/logback.xml new file mode 100644 index 000000000..6145d3878 --- /dev/null +++ b/kayenta-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 2e307c093..053280fc7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -27,6 +27,7 @@ include 'kayenta-gcs' include 'kayenta-google' include 'kayenta-graphite' include 'kayenta-influxdb' +include 'kayenta-integration' include 'kayenta-judge' include 'kayenta-mannwhitney' include 'kayenta-newrelic-insights' From 435294613441621e3d5a6363ee81c094fb24dc14 Mon Sep 17 00:00:00 2001 From: David Byron Date: Wed, 1 May 2024 21:56:31 -0700 Subject: [PATCH 3/4] feat(gha): run integration test in pr builds multi-arch with --load doesn't work, so add a separate step using the local platform to make an image available for testing. see docker/buildx#59 --- .github/workflows/pr.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index e6ffed7f2..018a9755b 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -129,3 +129,16 @@ jobs: 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" + - 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 ${{ steps.build_variables.outputs.REPO }}-integration:test From f9935b40196e220349bb2e1d6e151c5d80a80e7e Mon Sep 17 00:00:00 2001 From: David Byron Date: Wed, 1 May 2024 21:57:10 -0700 Subject: [PATCH 4/4] feat(gha): run integration test in branch builds --- .github/workflows/build.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ef1b50552..2dbb449b6 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 ${{ steps.build_variables.outputs.repo }}-integration:test - name: Login to GAR # Only run this on repositories in the 'spinnaker' org, not on forks. if: startsWith(github.repository, 'spinnaker/')