EnvironmentRepositoryConfigurationTests.java
/*
* Copyright 2018-present the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.config.server.config;
import java.util.List;
import com.google.cloud.storage.Storage;
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.config.server.environment.AwsParameterStoreEnvironmentRepositoryFactory;
import org.springframework.cloud.config.server.environment.CompositeEnvironmentRepository;
import org.springframework.cloud.config.server.environment.ConfigTokenProvider;
import org.springframework.cloud.config.server.environment.EnvironmentConfigTokenProvider;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.environment.GcsEnvironmentRepositoryFactory;
import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository;
import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepositoryFactory;
import org.springframework.cloud.config.server.environment.NativeEnvironmentProperties;
import org.springframework.cloud.config.server.environment.NativeEnvironmentRepository;
import org.springframework.cloud.config.server.environment.NativeEnvironmentRepositoryFactory;
import org.springframework.cloud.config.server.environment.ObservationEnvironmentRepositoryContext;
import org.springframework.cloud.config.server.environment.ObservationEnvironmentRepositoryWrapper;
import org.springframework.cloud.config.server.support.AzureDevOpsWorkloadIdentitySupport;
import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
public class EnvironmentRepositoryConfigurationTests {
@Test
public void ignoreUnresolvableNestedPlaceholdersDefaultsToFalse() {
new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(EnableConfigurationPropertiesBeans.class, ConfigServerProperties.class))
.run((context) -> assertThat(
context.getBean(ConfigServerProperties.class).isIgnoreUnresolvableNestedPlaceholders())
.isFalse());
}
@Test
public void ignoreUnresolvableNestedPlaceholdersCanBeConfigured() {
new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(EnableConfigurationPropertiesBeans.class, ConfigServerProperties.class))
.withPropertyValues("spring.cloud.config.server.ignore-unresolvable-nested-placeholders=true")
.run((context) -> assertThat(
context.getBean(ConfigServerProperties.class).isIgnoreUnresolvableNestedPlaceholders())
.isTrue());
}
@Test
public void configTokenProviderCanBeOverridden() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class))
.withPropertyValues("spring.profiles.active=composite",
"spring.cloud.config.server.vault.authentication=TOKEN",
"spring.cloud.config.server.vault.token=testTokenValue",
"spring.cloud.config.server.composite[0].type=vault",
"spring.cloud.config.server.composite[1].type=git",
"spring.cloud.config.server.composite[1].uri=https://test.com/Some-Test-Repo.git")
.run((context) -> {
assertThat(context.getBean(ConfigTokenProvider.class)).isNotNull();
assertThat(context.getBean(ConfigTokenProvider.class))
.isInstanceOf(EnvironmentConfigTokenProvider.class);
EnvironmentConfigTokenProvider tokenProvider = context.getBean(EnvironmentConfigTokenProvider.class);
assertThat(tokenProvider.getToken()).isEqualTo("testTokenValue");
});
}
@Test
public void awsParamStoreFactoryBeanExistsWithComposite() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class))
.withPropertyValues("spring.profiles.active=composite",
"spring.cloud.config.server.composite[0].type=awsparamstore",
"spring.cloud.config.server.composite[0].region=us-east-1",
"spring.cloud.config.server.composite[1].type=git",
"spring.cloud.config.server.composite[1].uri=https://test.com/Some-Test-Repo.git")
.run((context) -> {
assertThat(context.getBean(AwsParameterStoreEnvironmentRepositoryFactory.class)).isNotNull();
});
}
@Test
public void customGitCredentialsProvider() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GitTestBeans.class, TestBeans.class,
EnvironmentRepositoryConfiguration.class))
.withPropertyValues("spring.profiles.active=git",
"spring.cloud.config.server.git.uri=http://github.com/user/test")
.run((context) -> {
assertThat(context.getBean(GitCredentialsProviderFactory.class))
.isInstanceOf(GitTestBeans.CustomGitCredentialsProviderFactory.class);
});
}
@Test
public void customStorageBeanIsUsedByGcsEnvironmentRepositoryFactory() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class,
CustomStorageBeans.class))
.withPropertyValues("spring.profiles.active=gcs", "spring.cloud.config.server.gcs.bucket=test-bucket")
.run((context) -> {
assertThat(context).hasNotFailed();
assertThat(context).hasSingleBean(GcsEnvironmentRepositoryFactory.class);
Storage customStorage = context.getBean(Storage.class);
GcsEnvironmentRepositoryFactory factory = context.getBean(GcsEnvironmentRepositoryFactory.class);
assertThat(ReflectionTestUtils.getField(factory, "storage")).isSameAs(customStorage);
});
}
@Test
public void configServerActuatorConfigurationWithDefaultSettings() {
new ApplicationContextRunner().withConfiguration(
AutoConfigurations.of(EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
ConfigServerProperties.class))
.run((context) -> {
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
assertThat(ReflectionTestUtils.getField(healthIndicator, "downHealthStatus")).isEqualTo("DOWN");
assertThat(ReflectionTestUtils.getField(healthIndicator, "acceptEmpty")).isEqualTo(true);
});
}
@Test
public void configServerActuatorConfigurationWithCustomHealthStatus() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
ConfigServerProperties.class))
.withPropertyValues("spring.cloud.config.server.health.down-health-status=CUSTOMIZED")
.run((context) -> {
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
assertThat(ReflectionTestUtils.getField(healthIndicator, "downHealthStatus")).isEqualTo("CUSTOMIZED");
});
}
@Test
public void configServerActuatorConfigurationWithServerAcceptEmptyFalse() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
ConfigServerProperties.class))
.withPropertyValues("spring.cloud.config.server.accept-empty=false")
.run((context) -> {
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
assertThat(ReflectionTestUtils.getField(healthIndicator, "acceptEmpty")).isEqualTo(false);
});
}
@Test
public void configServerActuatorConfigurationWithOverriddenAcceptEmptyTrue() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
ConfigServerProperties.class))
.withPropertyValues("spring.cloud.config.server.accept-empty=false",
"spring.cloud.config.server.health.accept-empty=true")
.run((context) -> {
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
assertThat(ReflectionTestUtils.getField(healthIndicator, "acceptEmpty")).isEqualTo(true);
});
}
@Test
public void azureDevOpsWorkloadIdentitySupportBeanExists() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class))
.withPropertyValues("spring.profiles.active=git",
"spring.cloud.config.server.git.uri=https://dev.azure.com/tenant/repo")
.run((context) -> {
assertThat(context).hasSingleBean(AzureDevOpsWorkloadIdentitySupport.class);
});
}
@Test
public void customObservationConventionIsUsedByNativeEnvironmentRepositoryFactory() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class,
CustomObservationConventionBeans.class))
.withPropertyValues("spring.profiles.active=native")
.run((context) -> {
assertThat(context).hasNotFailed();
assertThat(context).hasSingleBean(NativeEnvironmentRepositoryFactory.class);
NativeEnvironmentRepositoryFactory factory = context.getBean(NativeEnvironmentRepositoryFactory.class);
assertThat(ReflectionTestUtils.getField(factory, "convention"))
.isSameAs(context.getBean(ObservationConvention.class));
});
}
@Test
public void defaultEnvironmentRepositoryCanBeDisabled() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class))
.withBean(EnvironmentRepository.class, () -> mock(EnvironmentRepository.class))
.withPropertyValues("spring.cloud.config.server.default-repository.enabled=false")
.run((context) -> {
assertThat(context).hasNotFailed();
assertThat(context).doesNotHaveBean(MultipleJGitEnvironmentRepository.class);
});
}
@Test
public void customObservationConventionIsUsedByGitEnvironmentRepositoryFactory() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class,
CustomObservationConventionBeans.class))
.withPropertyValues("spring.profiles.active=git",
"spring.cloud.config.server.git.uri=https://github.com/test/test-repo.git")
.run((context) -> {
assertThat(context).hasNotFailed();
assertThat(context).hasSingleBean(MultipleJGitEnvironmentRepositoryFactory.class);
MultipleJGitEnvironmentRepositoryFactory factory = context
.getBean(MultipleJGitEnvironmentRepositoryFactory.class);
assertThat(ReflectionTestUtils.getField(factory, "convention"))
.isSameAs(context.getBean(ObservationConvention.class));
});
}
@Test
public void customObservationConventionIsUsedByCompositeEnvironmentRepository() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class,
CustomObservationConventionBeans.class))
.withBean(ObservationRegistry.class, () -> {
ObservationRegistry registry = ObservationRegistry.create();
registry.observationConfig()
.observationHandler(new DefaultMeterObservationHandler(new SimpleMeterRegistry()));
return registry;
})
.withPropertyValues("spring.profiles.active=composite",
"spring.cloud.config.server.vault.authentication=TOKEN",
"spring.cloud.config.server.vault.token=testTokenValue",
"spring.cloud.config.server.composite[0].type=vault",
"spring.cloud.config.server.composite[1].type=git",
"spring.cloud.config.server.composite[1].uri=https://test.com/Some-Test-Repo.git")
.run((context) -> {
assertThat(context).hasNotFailed();
ObservationRegistry registry = context.getBean(ObservationRegistry.class);
assertThat(registry.isNoop()).isFalse();
CompositeEnvironmentRepository repository = context.getBean(CompositeEnvironmentRepository.class);
List<?> repositories = (List<?>) ReflectionTestUtils.getField(repository, "environmentRepositories");
assertThat(repositories).isNotEmpty();
assertThat(repositories).allSatisfy(delegate -> {
assertThat(delegate).isInstanceOf(ObservationEnvironmentRepositoryWrapper.class);
assertThat(ReflectionTestUtils.getField(delegate, "convention"))
.isSameAs(context.getBean("customObservationConvention"));
});
});
}
@Test
public void customObservationConventionIsUsedWhenFindingEnvironment() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class,
CustomObservationConventionBeans.class))
.withBean(TestObservationRegistry.class, TestObservationRegistry::create)
.withPropertyValues("spring.profiles.active=native")
.run((context) -> {
assertThat(context).hasNotFailed();
NativeEnvironmentRepositoryFactory factory = context.getBean(NativeEnvironmentRepositoryFactory.class);
NativeEnvironmentRepository repository = factory.build(new NativeEnvironmentProperties());
repository.findOne("application", "default", null);
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
io.micrometer.observation.tck.TestObservationRegistryAssert.assertThat(registry)
.hasSingleObservationThat()
.hasNameEqualTo("custom.environment.repository.find")
.hasBeenStarted()
.hasBeenStopped()
.hasLowCardinalityKeyValue("custom.tag", "custom-value");
});
}
@TestConfiguration
public static class TestBeans {
@Bean
public ConfigServerProperties configServerProperties() {
return new ConfigServerProperties();
}
}
@TestConfiguration
public static class CustomObservationConventionBeans {
@Bean
public ObservationConvention<ObservationEnvironmentRepositoryContext> customObservationConvention() {
return new ObservationConvention<>() {
@Override
public KeyValues getLowCardinalityKeyValues(ObservationEnvironmentRepositoryContext context) {
return KeyValues.of(KeyValue.of("custom.tag", "custom-value"));
}
@Override
public boolean supportsContext(Observation.Context context) {
return context instanceof ObservationEnvironmentRepositoryContext;
}
@Override
public String getName() {
return "custom.environment.repository.find";
}
@Override
public String getContextualName(ObservationEnvironmentRepositoryContext context) {
return "custom environment repository";
}
};
}
}
@TestConfiguration
public static class CustomStorageBeans {
@Bean
@Primary
public Storage customStorage() {
return mock(Storage.class);
}
}
@TestConfiguration
@AutoConfigureBefore(EnvironmentRepositoryConfiguration.class)
public static class GitTestBeans {
@Bean
public GitCredentialsProviderFactory customGitCredentialsProviderFactory() {
return new CustomGitCredentialsProviderFactory();
}
public static class CustomGitCredentialsProviderFactory extends GitCredentialsProviderFactory {
}
}
@TestConfiguration
@EnableConfigurationProperties
public static class EnableConfigurationPropertiesBeans {
@Bean
public EnvironmentRepository environmentRepository() {
return mock(EnvironmentRepository.class);
}
}
}