|
| 1 | +/* |
| 2 | + * Copyright 2024 Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.netflix.spinnaker.clouddriver; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Map; |
| 23 | +import org.junit.jupiter.api.AfterAll; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.TestInstance; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.testcontainers.containers.GenericContainer; |
| 30 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 31 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 32 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 33 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 34 | +import org.testcontainers.utility.DockerImageName; |
| 35 | + |
| 36 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 37 | +@Testcontainers |
| 38 | +public class PostgresMigrationContainerTest extends BaseContainerTest { |
| 39 | + |
| 40 | + private static final Logger logger = |
| 41 | + LoggerFactory.getLogger(PostgresMigrationContainerTest.class); |
| 42 | + |
| 43 | + private static final String POSTGRES_NETWORK_ALIAS = "postgresHost"; |
| 44 | + |
| 45 | + private static final int POSTGRES_PORT = 5432; |
| 46 | + |
| 47 | + private PostgreSQLContainer<?> postgres; |
| 48 | + |
| 49 | + private GenericContainer<?> clouddriverInitialContainer; |
| 50 | + |
| 51 | + // this is the latest image that is still running on liquibase 3.10.3 which create the conditions |
| 52 | + // similar to real scenario so that test identifies when validChecksums are not added in the later |
| 53 | + // version of clouddriver where higher liquibase versions are used |
| 54 | + private static final DockerImageName previousDockerImageName = |
| 55 | + DockerImageName.parse( |
| 56 | + "us-docker.pkg.dev/spinnaker-community/docker/clouddriver:5.82.2-dev-release-1.32.x-7a8e6e8b3-202406051721-unvalidated"); |
| 57 | + |
| 58 | + private String jdbcUrl = ""; |
| 59 | + |
| 60 | + @BeforeEach |
| 61 | + void setup() throws Exception { |
| 62 | + postgres = |
| 63 | + new PostgreSQLContainer<>("postgres:15") |
| 64 | + .withDatabaseName("clouddriver") |
| 65 | + .withUsername("postgres") |
| 66 | + .withPassword("postgres") |
| 67 | + .withNetwork(network) |
| 68 | + .withNetworkAliases(POSTGRES_NETWORK_ALIAS) |
| 69 | + .withInitScript("postgres_init.sql") |
| 70 | + .withReuse(true); |
| 71 | + postgres.start(); |
| 72 | + jdbcUrl = |
| 73 | + String.format("jdbc:postgresql://%s:%d/clouddriver", POSTGRES_NETWORK_ALIAS, POSTGRES_PORT); |
| 74 | + |
| 75 | + // Start the first clouddriver(from previous release) container so that all the db changelog |
| 76 | + // sets are executed |
| 77 | + clouddriverInitialContainer = |
| 78 | + new GenericContainer(previousDockerImageName) |
| 79 | + .withNetwork(network) |
| 80 | + .withExposedPorts(CLOUDDRIVER_PORT) |
| 81 | + .waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(120))) |
| 82 | + .dependsOn(postgres) |
| 83 | + .withEnv("SPRING_APPLICATION_JSON", getSpringApplicationJson()); |
| 84 | + clouddriverInitialContainer.start(); |
| 85 | + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger); |
| 86 | + clouddriverInitialContainer.followOutput(logConsumer); |
| 87 | + clouddriverInitialContainer.stop(); |
| 88 | + |
| 89 | + // Start the second clouddriver(latest) container to validate migration |
| 90 | + clouddriverContainer |
| 91 | + .dependsOn(postgres) |
| 92 | + .withEnv("SPRING_APPLICATION_JSON", getSpringApplicationJson()) |
| 93 | + .start(); |
| 94 | + |
| 95 | + clouddriverContainer.followOutput(logConsumer); |
| 96 | + } |
| 97 | + |
| 98 | + private String getSpringApplicationJson() throws JsonProcessingException { |
| 99 | + logger.info("----------- jdbcUrl: '{}'", jdbcUrl); |
| 100 | + Map<String, String> connectionPool = |
| 101 | + Map.of("jdbcUrl", jdbcUrl, "user", "clouddriver_service", "password", "c10uddriver"); |
| 102 | + Map<String, String> migration = |
| 103 | + Map.of("jdbcUrl", jdbcUrl, "user", "clouddriver_migrate", "password", "c10uddriver"); |
| 104 | + |
| 105 | + Map<String, Object> properties = |
| 106 | + Map.of( |
| 107 | + "sql.enabled", |
| 108 | + "true", |
| 109 | + "services.fiat.baseUrl", |
| 110 | + "http://nowhere", |
| 111 | + "sql.connectionPool", |
| 112 | + connectionPool, |
| 113 | + "redis.enabled", |
| 114 | + "false", |
| 115 | + "sql.migration", |
| 116 | + migration); |
| 117 | + ObjectMapper mapper = new ObjectMapper(); |
| 118 | + return mapper.writeValueAsString(properties); |
| 119 | + } |
| 120 | + |
| 121 | + @AfterAll |
| 122 | + void cleanupOnce() { |
| 123 | + if (clouddriverContainer != null) { |
| 124 | + clouddriverContainer.stop(); |
| 125 | + } |
| 126 | + |
| 127 | + if (postgres != null) { |
| 128 | + postgres.stop(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testHealthCheckWithPostgres() throws Exception { |
| 134 | + super.testHealthCheck(); |
| 135 | + } |
| 136 | +} |
0 commit comments