Merge branch '2.2.x'

This commit is contained in:
spencergibb
2020-09-24 10:58:05 -04:00
24 changed files with 355 additions and 150 deletions

View File

@@ -69,6 +69,13 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>

View File

@@ -29,6 +29,7 @@ import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.ConfigurableApplicationContext;
/**
@@ -52,9 +53,9 @@ public class ZookeeperConfigAutoConfigurationTests {
}
@Test(expected = NoSuchBeanDefinitionException.class)
public void testConfigEnabledFalseDoesNotLoadZookeeperConfigAutoConfiguration()
throws Exception {
public void testConfigEnabledFalseDoesNotLoadZookeeperConfigAutoConfiguration() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.listeners(new ZookeeperTestingServer())
.sources(Config.class).web(WebApplicationType.NONE)
.run("--spring.application.name=testZookeeperConfigEnabledSetToFalse",
"--spring.config.use-legacy-processing=true",
@@ -76,6 +77,7 @@ public class ZookeeperConfigAutoConfigurationTests {
expectedException.expectCause(Matchers.isA(KeeperException.class));
new SpringApplicationBuilder().sources(Config.class).web(WebApplicationType.NONE)
.listeners(new ZookeeperTestingServer())
.run("--spring.application.name=testZookeeperConfigEnabledSetToTrue",
"--spring.config.use-legacy-processing=true",
"--spring.jmx.default-domain=testZookeeperConfigEnabledSetToTrue",

View File

@@ -26,7 +26,6 @@ import org.apache.commons.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.TestingServer;
import org.apache.zookeeper.KeeperException;
import org.junit.After;
import org.junit.Before;
@@ -39,12 +38,12 @@ import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.refresh.LegacyContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.SocketUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -88,7 +87,7 @@ public class ZookeeperPropertySourceLocatorTests {
private ConfigurableApplicationContext context;
private TestingServer testingServer;
private ZookeeperTestingServer testingServer;
private CuratorFramework curator;
@@ -96,9 +95,9 @@ public class ZookeeperPropertySourceLocatorTests {
@Before
public void setup() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
this.testingServer = new TestingServer(port);
String connectString = "localhost:" + port;
this.testingServer = new ZookeeperTestingServer();
testingServer.start();
String connectString = "localhost:" + testingServer.getPort();
this.curator = CuratorFrameworkFactory.builder()
.retryPolicy(new RetryOneTime(500)).connectString(connectString).build();
this.curator.start();

View File

@@ -22,6 +22,18 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@@ -30,6 +30,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.test.annotation.DirtiesContext;
@@ -47,7 +48,7 @@ import static org.mockito.Mockito.mock;
public class ZookeeperAutoConfigurationTests {
@RunWith(SpringRunner.class)
@ContextConfiguration(
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class,
classes = { BaseTestConfig.class, ZookeeperAutoConfiguration.class })
public static class BaseTests {
@@ -78,7 +79,7 @@ public class ZookeeperAutoConfigurationTests {
}
@RunWith(SpringRunner.class)
@ContextConfiguration(
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class,
classes = { EnsembleTestConfig.class, ZookeeperAutoConfiguration.class })
public static class EnsembleTests {
@@ -122,7 +123,8 @@ public class ZookeeperAutoConfigurationTests {
}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { BaseTestConfig.class,
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class,
classes = { BaseTestConfig.class,
ZookeeperAutoConfiguration.class, CuratorFrameworkCustomizerConfig.class })
@DirtiesContext
public static class CuratorFrameworkCustomizerTest {
@@ -162,7 +164,8 @@ public class ZookeeperAutoConfigurationTests {
}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { BaseTestConfig.class, TracerDriverTestConfig.class,
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class,
classes = { BaseTestConfig.class, TracerDriverTestConfig.class,
ZookeeperAutoConfiguration.class })
public static class TracerDriverTests {

View File

@@ -22,6 +22,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
/**
@@ -33,6 +34,7 @@ import org.springframework.context.annotation.Bean;
public class ZookeeperHealthAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withInitializer(new ZookeeperTestingServer.Initializer())
.withConfiguration(AutoConfigurations.of(ZookeeperAutoConfiguration.class,
ZookeeperHealthAutoConfiguration.class))
.withUserConfiguration(ZookeeperAutoConfigurationTests.BaseTestConfig.class);

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2015-2020 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.zookeeper.test;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.test.TestingServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.cloud.zookeeper.ZookeeperProperties;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
public class ZookeeperTestingServer implements SmartApplicationListener, Ordered {
private final Log log = LogFactory.getLog(getClass());
private AtomicBoolean started = new AtomicBoolean();
private TestingServer testingServer;
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
public int getPort() {
Assert.notNull(testingServer, "testingServer not started");
return testingServer.getPort();
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType)
|| ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
envPrepared(((ApplicationEnvironmentPreparedEvent) event).getEnvironment());
}
else if (event instanceof ApplicationPreparedEvent) {
appPrepared(((ApplicationPreparedEvent) event).getApplicationContext());
}
}
public void envPrepared(ConfigurableEnvironment environment) {
MutablePropertySources sources = environment.getPropertySources();
start();
if (!sources.contains("zookeeperTestingServer")) {
HashMap<String, Object> map = new HashMap<>();
map.put(ZookeeperProperties.PREFIX + ".connect-string", "localhost:" + testingServer.getPort());
sources.addFirst(new MapPropertySource("zookeeperTestingServer", map));
}
}
public void start() {
if (started.compareAndSet(false, true)) {
try {
testingServer = new TestingServer();
}
catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
log.debug("Starting TestingServer on port " + testingServer.getPort());
}
}
public void appPrepared(ConfigurableApplicationContext context) {
context.addApplicationListener(new CloseListener(this));
}
public void close() {
try {
if (started.compareAndSet(true, false)) {
log.debug("Closing TestingServer on port " + testingServer.getPort());
testingServer.close();
}
}
catch (IOException ex) {
ReflectionUtils.rethrowRuntimeException(ex);
}
}
static class CloseListener implements ApplicationListener<ContextClosedEvent>, Ordered {
private final ZookeeperTestingServer testingServer;
CloseListener(ZookeeperTestingServer testingServer) {
this.testingServer = testingServer;
}
@Override
public void onApplicationEvent(ContextClosedEvent event) {
testingServer.close();
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
public static class Loader extends SpringBootContextLoader {
@Override
protected SpringApplication getSpringApplication() {
SpringApplication application = super.getSpringApplication();
ZookeeperTestingServer testingServer = new ZookeeperTestingServer();
application.addListeners(testingServer);
application.addListeners(new CloseListener(testingServer));
return application;
}
}
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext context) {
ZookeeperTestingServer testingServer = new ZookeeperTestingServer();
testingServer.envPrepared(context.getEnvironment());
testingServer.appPrepared(context);
}
}
}

View File

@@ -118,6 +118,13 @@
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>

View File

@@ -30,9 +30,11 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@@ -46,6 +48,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@SpringBootTest(classes = ZookeeperDiscoveryAutoRegistrationFalseTests.Config.class, properties = {
"spring.application.name=testzkautoregfalse",
"debug=true" }, webEnvironment = RANDOM_PORT)
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
@DirtiesContext
public class ZookeeperDiscoveryAutoRegistrationFalseTests {

View File

@@ -17,7 +17,6 @@
package org.springframework.cloud.zookeeper.discovery;
import org.apache.curator.framework.CuratorFramework;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -25,9 +24,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
@@ -37,12 +38,11 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = ZookeeperDiscoveryDisabledTests.SomeApp.class,
webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"spring.cloud.zookeeper.discovery.enabled=false", "debug=true" })
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoveryDisabledTests {
@Test
@Ignore // FIXME 2.0.0 error creating zookeeperHealthIndicator, CuratorFramework not
// found, but report says it is
public void should_start_the_context_with_discovery_disabled() throws Exception {
public void should_start_the_context_with_discovery_disabled() {
}
@Configuration

View File

@@ -24,7 +24,9 @@ import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@@ -34,7 +36,9 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Spencer Gibb
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "management.health.zookeeper.enabled=false")
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = { "management.health.zookeeper.enabled=false",
"spring.cloud.service-registry.auto-registration.enabled=false" })
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoveryHealthIndicatorDisabledTests {
@Autowired(required = false)

View File

@@ -37,11 +37,13 @@ import org.springframework.cloud.zookeeper.discovery.test.TestLoadBalancedClient
import org.springframework.cloud.zookeeper.serviceregistry.ServiceInstanceRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistry;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
@@ -56,10 +58,10 @@ import static org.springframework.cloud.zookeeper.discovery.test.TestLoadBalance
@SpringBootTest(classes = ZookeeperDiscoveryHealthIndicatorWithNestedStructureTests.Config.class,
properties = "management.endpoints.web.exposure.include=*", webEnvironment = RANDOM_PORT)
@ActiveProfiles("nestedstructure")
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoveryHealthIndicatorWithNestedStructureTests {
private static final Log log = LogFactory
.getLog(MethodHandles.lookup().lookupClass());
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
@Autowired
TestLoadBalancedClient testLoadBalancedClient;
@@ -76,8 +78,7 @@ public class ZookeeperDiscoveryHealthIndicatorWithNestedStructureTests {
// then:
log.info("Received response [" + response + "]");
then(this.curatorFramework.getChildren().forPath("/services/me")).isNotEmpty();
then(this.curatorFramework.getChildren()
.forPath("/services/a/b/c/d/anotherservice")).isNotEmpty();
then(this.curatorFramework.getChildren().forPath("/services/a/b/c/d/anotherservice")).isNotEmpty();
}
@Configuration
@@ -94,9 +95,8 @@ public class ZookeeperDiscoveryHealthIndicatorWithNestedStructureTests {
@PostConstruct
void registerNestedDependency() {
try {
this.registration = ServiceInstanceRegistration.builder().defaultUriSpec()
.address("anyUrl").port(10).name("/a/b/c/d/anotherservice")
.build();
this.registration = ServiceInstanceRegistration.builder().defaultUriSpec().address("anyUrl").port(10)
.name("/a/b/c/d/anotherservice").build();
this.serviceRegistry.register(registration);
}
catch (Exception e) {

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2015-2019 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.zookeeper.discovery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author wmz7year
*/
@SpringBootTest(properties = { "spring.application.name=testZookeeperDiscovery",
"spring.cloud.service-registry.auto-registration.enabled=false",
"spring.cloud.zookeeper.discovery.instance-id=zkpropstestid-123",
"spring.cloud.zookeeper.discovery.preferIpAddress=true",
"spring.cloud.zookeeper.discovery.instanceIpAddress=1.1.1.1" },
classes = ZookeeperDiscoveryPropertiesIntegrationTests.Config.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoveryPropertiesIntegrationTests {
@Autowired
private ZookeeperDiscoveryProperties discoveryProperties;
@Test
public void testPreferIpAddress() {
assertThat(this.discoveryProperties.getInstanceId())
.isEqualTo("zkpropstestid-123");
assertThat(this.discoveryProperties.getInstanceHost()).isEqualTo("1.1.1.1");
}
@Configuration
@EnableAutoConfiguration
@Import(CommonTestConfig.class)
static class Config {
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2015-2019 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.zookeeper.discovery;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(Parameterized.class)
public class ZookeeperDiscoveryPropertiesTest {
private String root;
public ZookeeperDiscoveryPropertiesTest(String root) {
this.root = root;
}
@Parameterized.Parameters(name = "With root {0}")
public static Iterable<String> rootVariations() {
return Arrays.asList("es", "es/", "/es");
}
@Test
public void should_escape_root() {
// given:
ZookeeperDiscoveryProperties zookeeperDiscoveryProperties = new ZookeeperDiscoveryProperties(
new InetUtils(new InetUtilsProperties()));
// when:
zookeeperDiscoveryProperties.setRoot(root);
// then:
then(zookeeperDiscoveryProperties.getRoot()).isEqualTo("/es");
}
}

View File

@@ -16,47 +16,40 @@
package org.springframework.cloud.zookeeper.discovery;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
/**
* @author wmz7year
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(properties = { "pring.application.name=testZookeeperDiscovery",
"spring.cloud.zookeeper.discovery.instance-id=zkpropstestid-123",
"spring.cloud.zookeeper.discovery.preferIpAddress=true",
"spring.cloud.zookeeper.discovery.instanceIpAddress=1.1.1.1" },
classes = ZookeeperDiscoveryPropertiesTests.Config.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(Parameterized.class)
public class ZookeeperDiscoveryPropertiesTests {
@Autowired
private ZookeeperDiscoveryProperties discoveryProperties;
private String root;
@Test
public void testPreferIpAddress() {
assertThat(this.discoveryProperties.getInstanceId())
.isEqualTo("zkpropstestid-123");
assertThat(this.discoveryProperties.getInstanceHost()).isEqualTo("1.1.1.1");
public ZookeeperDiscoveryPropertiesTests(String root) {
this.root = root;
}
@Configuration
@EnableAutoConfiguration
@Import(CommonTestConfig.class)
static class Config {
@Parameterized.Parameters(name = "With root {0}")
public static Iterable<String> rootVariations() {
return Arrays.asList("es", "es/", "/es");
}
@Test
public void should_escape_root() {
// given:
ZookeeperDiscoveryProperties zookeeperDiscoveryProperties = new ZookeeperDiscoveryProperties(
new InetUtils(new InetUtilsProperties()));
// when:
zookeeperDiscoveryProperties.setRoot(root);
// then:
then(zookeeperDiscoveryProperties.getRoot()).isEqualTo("/es");
}
}

View File

@@ -27,11 +27,13 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@@ -47,6 +49,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.zookeeper.discovery.instance-ssl-port=8443" }, webEnvironment = RANDOM_PORT)
@ActiveProfiles("loadbalancer")
@DirtiesContext
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoverySecurePortTests {
@Autowired

View File

@@ -36,6 +36,7 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.discovery.test.TestLoadBalancedClient;
import org.springframework.cloud.zookeeper.serviceregistry.ServiceInstanceRegistration;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@@ -43,6 +44,7 @@ import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -63,6 +65,7 @@ import static org.springframework.cloud.zookeeper.discovery.test.TestLoadBalance
"management.endpoints.web.exposure.include=*" }, webEnvironment = RANDOM_PORT)
@ActiveProfiles("loadbalancer")
@DirtiesContext
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoveryTests {
@Autowired

View File

@@ -26,8 +26,10 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,6 +43,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.application.name=myTestNotRegisteredService",
"spring.cloud.zookeeper.discovery.register=false",
"spring.cloud.zookeeper.dependency.enabled=false" }, webEnvironment = RANDOM_PORT)
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperLifecycleRegistrationDisabledTests {
@Autowired

View File

@@ -32,9 +32,11 @@ import org.springframework.cloud.config.client.DiscoveryClientConfigServiceBoots
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.cloud.test.ModifiedClassPathRunner;
import org.springframework.cloud.zookeeper.ZookeeperAutoConfiguration;
import org.springframework.cloud.zookeeper.ZookeeperProperties;
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClient;
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClientConfiguration;
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -70,32 +72,29 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
"spring.cloud.zookeeper.discovery.instance-port:7001",
"spring.cloud.zookeeper.discovery.instance-host:foo",
"spring.cloud.config.discovery.service-id:configserver");
assertThat(this.context
.getBeanNamesForType(ZookeeperConfigServerAutoConfiguration.class).length)
.isEqualTo(1);
ZookeeperDiscoveryClient client = this.context.getParent()
.getBean(ZookeeperDiscoveryClient.class);
assertThat(this.context.getBeanNamesForType(ZookeeperConfigServerAutoConfiguration.class).length).isEqualTo(1);
ZookeeperDiscoveryClient client = this.context.getParent().getBean(ZookeeperDiscoveryClient.class);
verify(client, atLeast(2)).getInstances("configserver");
ConfigClientProperties locator = this.context
.getBean(ConfigClientProperties.class);
ConfigClientProperties locator = this.context.getBean(ConfigClientProperties.class);
assertThat(locator.getUri()[0]).isEqualTo("http://foo:7001/");
}
private void setup(String... env) {
ZookeeperTestingServer testingServer = new ZookeeperTestingServer();
testingServer.start();
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
TestPropertyValues.of(env).applyTo(parent);
parent.register(UtilAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, EnvironmentKnobbler.class,
ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class,
ConfigClientProperties.class);
TestPropertyValues.of(ZookeeperProperties.PREFIX + ".connect-string=localhost:" + testingServer.getPort())
.applyTo(parent);
parent.register(UtilAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
EnvironmentKnobbler.class, ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class);
testingServer.appPrepared(parent);
parent.refresh();
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ZookeeperConfigServerAutoConfiguration.class,
ZookeeperAutoConfiguration.class,
ZookeeperDiscoveryClientConfiguration.class);
this.context.register(PropertyPlaceholderAutoConfiguration.class, ZookeeperConfigServerAutoConfiguration.class,
ZookeeperAutoConfiguration.class, ZookeeperDiscoveryClientConfiguration.class);
this.context.refresh();
}
@@ -103,14 +102,11 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
protected static class EnvironmentKnobbler {
@Bean
public ZookeeperDiscoveryClient zookeeperDiscoveryClient(
ZookeeperDiscoveryProperties properties) {
public ZookeeperDiscoveryClient zookeeperDiscoveryClient(ZookeeperDiscoveryProperties properties) {
ZookeeperDiscoveryClient client = mock(ZookeeperDiscoveryClient.class);
ServiceInstance instance = new DefaultServiceInstance("configserver1",
"configserver", properties.getInstanceHost(),
properties.getInstancePort(), false);
given(client.getInstances("configserver"))
.willReturn(Arrays.asList(instance));
ServiceInstance instance = new DefaultServiceInstance("configserver1", "configserver",
properties.getInstanceHost(), properties.getInstancePort(), false);
given(client.getInstances("configserver")).willReturn(Arrays.asList(instance));
return client;
}

View File

@@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -68,6 +69,7 @@ public class ZookeeperConfigServerAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class,
ZookeeperConfigServerAutoConfiguration.class,
ConfigServerProperties.class, ZookeeperDiscoveryProperties.class)
.listeners(new ZookeeperTestingServer())
.web(WebApplicationType.NONE).properties(env).run();
}

View File

@@ -28,10 +28,12 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.zookeeper.discovery.test.TestLoadBalancedClient;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static com.jayway.awaitility.Awaitility.await;
@@ -47,6 +49,7 @@ import static org.springframework.cloud.zookeeper.discovery.test.TestLoadBalance
"debug=true",
"management.endpoints.web.exposure.include=*"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("dependencies")
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperDiscoveryWithDependenciesIntegrationTests {
@Autowired

View File

@@ -29,6 +29,7 @@ import org.springframework.cloud.client.discovery.health.reactive.ReactiveDiscov
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryAutoConfiguration;
import org.springframework.cloud.zookeeper.support.CuratorServiceDiscoveryAutoConfiguration;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
import static org.assertj.core.api.Assertions.assertThat;
@@ -40,6 +41,7 @@ import static org.mockito.Mockito.mock;
class ZookeeperReactiveDiscoveryClientConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withInitializer(new ZookeeperTestingServer.Initializer())
.withConfiguration(
AutoConfigurations.of(UtilAutoConfiguration.class,
ReactiveCommonsClientAutoConfiguration.class,

View File

@@ -19,10 +19,6 @@ package org.springframework.cloud.zookeeper.discovery.watcher;
import java.util.concurrent.Callable;
import com.jayway.awaitility.Awaitility;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;
import org.apache.curator.x.discovery.ServiceCache;
import org.junit.Ignore;
import org.junit.Test;
@@ -36,13 +32,14 @@ import org.springframework.cloud.zookeeper.discovery.watcher.presence.Dependency
import org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistry;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.BDDAssertions.then;
@@ -53,6 +50,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DefaultDependencyWatcherSpringTests.Config.class, webEnvironment = RANDOM_PORT)
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
@ActiveProfiles("watcher")
public class DefaultDependencyWatcherSpringTests {
@@ -107,19 +105,6 @@ public class DefaultDependencyWatcherSpringTests {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(destroyMethod = "close")
TestingServer testingServer() throws Exception {
return new TestingServer(SocketUtils.findAvailableTcpPort());
}
@Bean(initMethod = "start", destroyMethod = "close")
CuratorFramework curatorFramework() throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(
testingServer().getConnectString(),
new ExponentialBackoffRetry(20, 20, 500));
return curatorFramework;
}
@Bean
DependencyWatcherListener dependencyWatcherListener() {
return new AssertableDependencyWatcherListener();

View File

@@ -31,7 +31,9 @@ import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryPropertie
import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.support.StatusConstants;
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -45,6 +47,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.application.name=myTestService1-F",
"spring.cloud.zookeeper.discovery.initial-status=OUT_OF_SERVICE"
}, webEnvironment = RANDOM_PORT)
@ContextConfiguration(loader = ZookeeperTestingServer.Loader.class)
public class ZookeeperAutoServiceRegistrationTests {
@Autowired