|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 OnGres, Inc. |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package io.stackgres.operator.validation.shardedcluster; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | +import java.util.Random; |
| 13 | +import java.util.function.Predicate; |
| 14 | + |
| 15 | +import io.stackgres.common.StackGresComponent; |
| 16 | +import io.stackgres.common.crd.sgcluster.StackGresPostgresFlavor; |
| 17 | +import io.stackgres.common.crd.sgshardedcluster.StackGresShardedClusterSpec; |
| 18 | +import io.stackgres.operator.common.StackGresShardedClusterReview; |
| 19 | +import io.stackgres.operator.common.fixture.AdmissionReviewFixtures; |
| 20 | +import io.stackgres.operator.conciliation.shardedcluster.context.ShardedClusterPostgresVersionContextAppender; |
| 21 | +import io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | + |
| 27 | +@ExtendWith(MockitoExtension.class) |
| 28 | +class PostgresFlavorValidatorTest { |
| 29 | + |
| 30 | + private static final List<String> SUPPORTED_POSTGRES_VERSIONS = |
| 31 | + StackGresComponent.POSTGRESQL.getLatest().streamOrderedVersions() |
| 32 | + .toList(); |
| 33 | + private static final String FIRST_BF_MINOR_VERSION = |
| 34 | + StackGresComponent.BABELFISH.getLatest().streamOrderedVersions() |
| 35 | + .get(0).get(); |
| 36 | + |
| 37 | + private static String getRandomPostgresVersion() { |
| 38 | + Random random = new Random(); |
| 39 | + List<String> validPostgresVersions = SUPPORTED_POSTGRES_VERSIONS.stream() |
| 40 | + .filter(Predicate.not(ShardedClusterPostgresVersionContextAppender.BUGGY_PG_VERSIONS.keySet()::contains)) |
| 41 | + .toList(); |
| 42 | + |
| 43 | + int versionIndex = random.nextInt(validPostgresVersions.size()); |
| 44 | + return validPostgresVersions.get(versionIndex); |
| 45 | + } |
| 46 | + |
| 47 | + private PostgresFlavorValidator validator; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void setUp() { |
| 51 | + validator = new PostgresFlavorValidator(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void givenValidPostgresFlavor_shouldNotFail() throws ValidationFailed { |
| 56 | + final StackGresShardedClusterReview review = AdmissionReviewFixtures.shardedCluster().loadCreate().get(); |
| 57 | + |
| 58 | + StackGresShardedClusterSpec spec = review.getRequest().getObject().getSpec(); |
| 59 | + |
| 60 | + final String randomVersion = getRandomPostgresVersion(); |
| 61 | + spec.getPostgres().setVersion(randomVersion); |
| 62 | + |
| 63 | + validator.validate(review); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void givenChangedPostgresFlavorUpdate_shouldFail() throws ValidationFailed { |
| 68 | + final StackGresShardedClusterReview review = AdmissionReviewFixtures.shardedCluster().loadUpdate().get(); |
| 69 | + |
| 70 | + StackGresShardedClusterSpec spec = review.getRequest().getObject().getSpec(); |
| 71 | + spec.getPostgres().setVersion(FIRST_BF_MINOR_VERSION); |
| 72 | + spec.getPostgres().setFlavor(StackGresPostgresFlavor.BABELFISH.toString()); |
| 73 | + |
| 74 | + ValidationFailed exception = assertThrows(ValidationFailed.class, () -> { |
| 75 | + validator.validate(review); |
| 76 | + }); |
| 77 | + |
| 78 | + String resultMessage = exception.getResult().getMessage(); |
| 79 | + |
| 80 | + assertEquals("postgres flavor can not be changed", |
| 81 | + resultMessage); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments