add conditional instantiation (#189)

Fixes gh-183
This commit is contained in:
Ryan Dawson
2018-10-30 16:36:00 +00:00
committed by Spencer Gibb
parent bc6fccd867
commit b3aaf4e57a
4 changed files with 149 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
package org.springframework.cloud.kubernetes.config;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* @author Ryan Dawson
*/
public class KubernetesConfigConfigurationTest {
private ConfigurableApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void kubernetesWhenKubernetesDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=false");
assertFalse(context.containsBean("configMapPropertySourceLocator"));
assertFalse(context.containsBean("secretsPropertySourceLocator"));
}
@Test
public void kubernetesWhenKubernetesConfigDisabled() throws Exception {
setup("spring.cloud.kubernetes.config.enabled=false");
assertFalse(context.containsBean("configMapPropertySourceLocator"));
assertFalse(context.containsBean("secretsPropertySourceLocator"));
}
@Test
public void kubernetesDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true");
assertTrue(context.containsBean("configMapPropertySourceLocator"));
assertTrue(context.containsBean("secretsPropertySourceLocator"));
}
private void setup(String... env) {
this.context = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class,
BootstrapConfiguration.class).web(org.springframework.boot.WebApplicationType.NONE)
.properties(env).run();
}
@Configuration
static class KubernetesClientTestConfiguration {
@Bean
KubernetesClient kubernetesClient() {
return mock(KubernetesClient.class);
}
}
}

View File

@@ -28,9 +28,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
@ConditionalOnProperty(name="spring.cloud.kubernetes.enabled", matchIfMissing = true)
public class KubernetesDiscoveryClientAutoConfiguration {
@Bean
@ConditionalOnProperty(name = "spring.cloud.kubernetes.discovery.enabled",matchIfMissing = true)
public DiscoveryClient discoveryClient(KubernetesClient client,
KubernetesDiscoveryProperties properties) {
return new KubernetesDiscoveryClient(client, properties);

View File

@@ -50,6 +50,13 @@ public class KubernetesCatalogServicesWatchConfigurationTest {
assertFalse(context.containsBean("kubernetesCatalogWatch"));
}
@Test
public void kubernetesCatalogWatchWhenKubernetesDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=false");
assertFalse(context.containsBean("kubernetesCatalogWatch"));
}
@Test
public void kubernetesCatalogWatchDefaultEnabled() throws Exception {
setup();

View File

@@ -0,0 +1,68 @@
package org.springframework.cloud.kubernetes.discovery;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* @author Ryan Dawson
*/
public class KubernetesDiscoveryConfigurationTest {
private ConfigurableApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void kubernetesDiscoveryDisabled() throws Exception {
setup("spring.cloud.kubernetes.discovery.enabled=false","spring.cloud.kubernetes.discovery.catalog-services-watch.enabled=false");
assertFalse(context.containsBean("discoveryClient"));
}
@Test
public void kubernetesDiscoveryWhenKubernetesDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=false");
assertFalse(context.containsBean("discoveryClient"));
}
@Test
public void kubernetesDiscoveryDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true");
assertTrue(context.containsBean("discoveryClient"));
}
private void setup(String... env) {
this.context = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class,
KubernetesDiscoveryClientAutoConfiguration.class).web(org.springframework.boot.WebApplicationType.NONE)
.properties(env).run();
}
@Configuration
static class KubernetesClientTestConfiguration {
@Bean
KubernetesClient kubernetesClient() {
return mock(KubernetesClient.class);
}
}
}