|
| 1 | +package io.javaoperatorsdk.operator.api.config; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.concurrent.ExecutorService; |
| 5 | +import java.util.concurrent.Executors; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 10 | +import io.fabric8.kubernetes.client.Config; |
| 11 | +import io.fabric8.kubernetes.client.ConfigBuilder; |
| 12 | +import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec; |
| 13 | +import io.javaoperatorsdk.operator.api.monitoring.Metrics; |
| 14 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; |
| 15 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | + |
| 21 | +class ConfigurationServiceOverriderTest { |
| 22 | + |
| 23 | + private static final Metrics METRICS = new Metrics() {}; |
| 24 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 25 | + private static final LeaderElectionConfiguration LEADER_ELECTION_CONFIGURATION = |
| 26 | + new LeaderElectionConfiguration("foo", "fooNS"); |
| 27 | + private static final DependentResourceFactory FACTORY = new DependentResourceFactory() { |
| 28 | + @Override |
| 29 | + public <T extends DependentResource<?, ?>> T createFrom(DependentResourceSpec<T, ?> spec) { |
| 30 | + return DependentResourceFactory.super.createFrom(spec); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + private static final Cloner CLONER = new Cloner() { |
| 35 | + @Override |
| 36 | + public <R extends HasMetadata> R clone(R object) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + @Test |
| 42 | + void overrideShouldWork() { |
| 43 | + final var config = new BaseConfigurationService(null) { |
| 44 | + @Override |
| 45 | + public boolean checkCRDAndValidateLocalModel() { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Config getClientConfiguration() { |
| 51 | + return new ConfigBuilder().withNamespace("namespace").build(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int concurrentReconciliationThreads() { |
| 56 | + return -1; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int getTerminationTimeoutSeconds() { |
| 61 | + return -1; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Metrics getMetrics() { |
| 66 | + return METRICS; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public ExecutorService getExecutorService() { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean closeClientOnStop() { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public ObjectMapper getObjectMapper() { |
| 81 | + return OBJECT_MAPPER; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Cloner getResourceCloner() { |
| 86 | + return CLONER; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public DependentResourceFactory dependentResourceFactory() { |
| 91 | + return FACTORY; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Optional<LeaderElectionConfiguration> getLeaderElectionConfiguration() { |
| 96 | + return Optional.of(LEADER_ELECTION_CONFIGURATION); |
| 97 | + } |
| 98 | + }; |
| 99 | + final var overridden = new ConfigurationServiceOverrider(config) |
| 100 | + .withClientConfiguration(new ConfigBuilder().withNamespace("newNS").build()) |
| 101 | + .checkingCRDAndValidateLocalModel(true) |
| 102 | + .withExecutorService(Executors.newSingleThreadExecutor()) |
| 103 | + .withCloseClientOnStop(false) |
| 104 | + .withObjectMapper(new ObjectMapper()) |
| 105 | + .withResourceCloner(new Cloner() { |
| 106 | + @Override |
| 107 | + public <R extends HasMetadata> R clone(R object) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + }) |
| 111 | + .withConcurrentReconciliationThreads(25) |
| 112 | + .withTerminationTimeoutSeconds(100) |
| 113 | + .withMetrics(new Metrics() {}) |
| 114 | + .withLeaderElectionConfiguration(new LeaderElectionConfiguration("newLease", "newLeaseNS")) |
| 115 | + .build(); |
| 116 | + |
| 117 | + assertNotEquals(config.closeClientOnStop(), overridden.closeClientOnStop()); |
| 118 | + assertNotEquals(config.checkCRDAndValidateLocalModel(), |
| 119 | + overridden.checkCRDAndValidateLocalModel()); |
| 120 | + assertNotEquals(config.concurrentReconciliationThreads(), |
| 121 | + overridden.concurrentReconciliationThreads()); |
| 122 | + assertNotEquals(config.getTerminationTimeoutSeconds(), |
| 123 | + overridden.getTerminationTimeoutSeconds()); |
| 124 | + assertNotEquals(config.dependentResourceFactory(), overridden.dependentResourceFactory()); |
| 125 | + assertNotEquals(config.getClientConfiguration(), overridden.getClientConfiguration()); |
| 126 | + assertNotEquals(config.getExecutorService(), overridden.getExecutorService()); |
| 127 | + assertNotEquals(config.getMetrics(), overridden.getMetrics()); |
| 128 | + assertNotEquals(config.getObjectMapper(), overridden.getObjectMapper()); |
| 129 | + assertNotEquals(config.getLeaderElectionConfiguration(), |
| 130 | + overridden.getLeaderElectionConfiguration()); |
| 131 | + } |
| 132 | +} |
0 commit comments