Working Zipkin Stream tests
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package integration;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
|
||||
import org.springframework.cloud.sleuth.TraceManager;
|
||||
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import tools.AbstractDockerIntegrationTest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class SampleApp {
|
||||
|
||||
@Autowired
|
||||
private TraceManager traceManager;
|
||||
|
||||
@SneakyThrows
|
||||
@RequestMapping("/hi2")
|
||||
public String hi2() {
|
||||
log.info("I'm in the sample app");
|
||||
final Random random = new Random();
|
||||
int millis = random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
this.traceManager.addAnnotation("random-sleep-millis", String.valueOf(millis));
|
||||
log.info("Current span is [{}]", TraceContextHolder.getCurrentSpan());
|
||||
return "hi2";
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@Slf4j
|
||||
public static class Config {
|
||||
|
||||
public static final int RABBITMQ_PORT = 5672;
|
||||
|
||||
@Bean SampleApp sampleApp() {
|
||||
return new SampleApp();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SneakyThrows
|
||||
ConnectionFactory connectionFactory() {
|
||||
RabbitProperties config = rabbitProperties();
|
||||
AbstractDockerIntegrationTest.await().until(() -> {
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(RABBITMQ_PORT, 50,
|
||||
InetAddress.getByName(AbstractDockerIntegrationTest.getDockerURI().getHost()));
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
log.info("RabbitMQ is up and running - proceeding");
|
||||
return true;
|
||||
}
|
||||
log.warn("RabbitMQ has not started yet...");
|
||||
return false;
|
||||
});
|
||||
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
|
||||
if (config.getHost() != null) {
|
||||
factory.setHost(config.getHost());
|
||||
factory.setPort(config.getPort());
|
||||
}
|
||||
if (config.getUsername() != null) {
|
||||
factory.setUsername(config.getUsername());
|
||||
}
|
||||
if (config.getPassword() != null) {
|
||||
factory.setPassword(config.getPassword());
|
||||
}
|
||||
if (config.getVirtualHost() != null) {
|
||||
factory.setVirtualHost(config.getVirtualHost());
|
||||
}
|
||||
if (config.getRequestedHeartbeat() != null) {
|
||||
factory.setRequestedHeartbeat(config.getRequestedHeartbeat());
|
||||
}
|
||||
RabbitProperties.Ssl ssl = config.getSsl();
|
||||
if (ssl.isEnabled()) {
|
||||
factory.setUseSSL(true);
|
||||
factory.setKeyStore(ssl.getKeyStore());
|
||||
factory.setKeyStorePassphrase(ssl.getKeyStorePassword());
|
||||
factory.setTrustStore(ssl.getTrustStore());
|
||||
factory.setTrustStorePassphrase(ssl.getTrustStorePassword());
|
||||
}
|
||||
factory.afterPropertiesSet();
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
|
||||
factory.getObject());
|
||||
connectionFactory.setAddresses(config.getAddresses());
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
RabbitProperties rabbitProperties() {
|
||||
RabbitProperties rabbitProperties = new RabbitProperties();
|
||||
rabbitProperties.setHost(AbstractDockerIntegrationTest.getDockerURI().getHost());
|
||||
return rabbitProperties;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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
|
||||
*
|
||||
* http://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 integration;
|
||||
|
||||
import example.ZipkinStreamServerApplication;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.WebIntegrationTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.JdkIdGenerator;
|
||||
import org.testcontainers.containers.DockerComposeContainer;
|
||||
import tools.AbstractDockerIntegrationTest;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = { SampleApp.Config.class,
|
||||
AbstractDockerIntegrationTest.ZipkinConfig.class, ZipkinStreamServerApplication.class })
|
||||
@WebIntegrationTest()
|
||||
@Slf4j
|
||||
public class ZipkinStreamDockerTests extends AbstractDockerIntegrationTest {
|
||||
|
||||
private static int port = 9411;
|
||||
private static String sampleAppUrl = "http://localhost:" + port;
|
||||
|
||||
@ClassRule
|
||||
public static DockerComposeContainer environment =
|
||||
new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))
|
||||
.withExposedService("rabbitmq_1", 5672)
|
||||
.withExposedService("rabbitmq_1", 15672)
|
||||
.withExposedService("mysql_1", 3306);
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
public void should_propagate_spans_to_zipkin() {
|
||||
await().until(zipkinServerIsUp());
|
||||
String traceId = new JdkIdGenerator().generateId().toString();
|
||||
|
||||
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/hi2", traceId));
|
||||
|
||||
await().until(allSpansWereRegisteredInZipkinWithTraceIdEqualTo(traceId));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getZipkinRootUrl() {
|
||||
return "http://localhost";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user