Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TR Integration tests updated to use a mock server for TM and TO #6140

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- [#5576](https://github.com/apache/trafficcontrol/issues/5576) - Inconsistent Profile Name restrictions
- Fixed Federations IMS so TR federations watcher will get updates.
- [#5129](https://github.com/apache/trafficcontrol/issues/5129) - Updated TM so that it returns a 404 if the endpoint is not supported.
- [#5992](https://github.com/apache/trafficcontrol/issues/5992) - Updated Traffic Router Integration tests to use a mock Traffic Monitor and Traffic Ops server

### Changed
- Migrated completely off of bower in favor of npm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.apache.traffic_control.traffic_router.core;

import org.apache.traffic_control.traffic_router.core.external.HttpDataServer;
import org.apache.traffic_control.traffic_router.shared.DeliveryServiceCertificates;
import org.apache.traffic_control.traffic_router.shared.DeliveryServiceCertificatesMBean;
import org.apache.log4j.ConsoleAppender;
Expand All @@ -27,28 +28,89 @@

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.util.SocketUtils.findAvailableTcpPort;
import static org.springframework.util.SocketUtils.findAvailableUdpPort;

public class TestBase {
private static final Logger LOGGER = Logger.getLogger(TestBase.class);
public static final String monitorPropertiesPath = "src/test/conf/traffic_monitor.properties";

private static int testHttpServerPort = findAvailableTcpPort();
private static HttpDataServer httpDataServer = new HttpDataServer(testHttpServerPort);
private static File tmpDeployDir;
private static ApplicationContext context;

public static ApplicationContext getContext() {
public static void setupFakeServers() throws Exception {
// Set up a local server that can hand out
// cr-config and cr-states (i.e. fake traffic monitor endpoints)
// czmap
// federations
// steering
// fake setting a cookie

System.setProperty("deploy.dir", "src/test");
System.setProperty("dns.zones.dir", "src/test/var/auto-zones");
if (tmpDeployDir == null) {
tmpDeployDir = Files.createTempDirectory("ext-test-").toFile();
}

final String TRAFFIC_MONITOR_BOOTSTRAP_LOCAL = "TRAFFIC_MONITOR_BOOTSTRAP_LOCAL";
final String TRAFFIC_MONITOR_HOSTS = "TRAFFIC_MONITOR_HOSTS";
String FAKE_SERVER;

FAKE_SERVER = "localhost:" + testHttpServerPort + ";";

Map<String, String> additionalEnvironment = new HashMap<>();

System.setProperty("dns.tcp.port", String.valueOf(findAvailableTcpPort()));
System.setProperty("dns.udp.port", String.valueOf(findAvailableUdpPort()));
additionalEnvironment.put(TRAFFIC_MONITOR_BOOTSTRAP_LOCAL, "true");
additionalEnvironment.put(TRAFFIC_MONITOR_HOSTS, FAKE_SERVER);

if (context != null) {
return context;
if (System.getenv(TRAFFIC_MONITOR_HOSTS) != null) {
System.out.println("External Test Suite overriding env var [" + TRAFFIC_MONITOR_HOSTS + "] to " + FAKE_SERVER);
}

if (System.getenv(TRAFFIC_MONITOR_BOOTSTRAP_LOCAL) != null) {
System.out.println("External Test Suite overriding env var [" + TRAFFIC_MONITOR_BOOTSTRAP_LOCAL + "] to true");
}

addToEnv(additionalEnvironment);

assertThat(System.getenv(TRAFFIC_MONITOR_BOOTSTRAP_LOCAL), equalTo("true"));
assertThat(System.getenv(TRAFFIC_MONITOR_HOSTS), equalTo(FAKE_SERVER));

httpDataServer.start(testHttpServerPort);

System.setProperty("testHttpServerPort", "" + testHttpServerPort);
System.setProperty("routerHttpPort", "" + findAvailableTcpPort());
System.setProperty("routerSecurePort", "" + findAvailableTcpPort());

new File(tmpDeployDir,"conf").mkdirs();
System.out.println();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println(">>>>>>>> Going to use tmp directory '" + tmpDeployDir + "' as traffic router deploy directory");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println();
System.setProperty("deploy.dir", tmpDeployDir.getAbsolutePath());
System.setProperty("dns.zones.dir", "src/test/var/auto-zones");

System.setProperty("cache.health.json.refresh.period", "10000");
System.setProperty("cache.config.json.refresh.period", "10000");
System.setProperty("dns.tcp.port", "" + findAvailableTcpPort());
System.setProperty("dns.udp.port", "" + findAvailableUdpPort());
System.setProperty("traffic_monitor.properties", "not_needed");

File dbDirectory = new File(tmpDeployDir, "db");
dbDirectory.mkdir();

LogManager.getLogger("org.eclipse.jetty").setLevel(Level.WARN);
LogManager.getLogger("org.springframework").setLevel(Level.WARN);

final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
try {
final ObjectName objectName = new ObjectName(DeliveryServiceCertificatesMBean.OBJECT_NAME);
Expand All @@ -59,12 +121,30 @@ public static ApplicationContext getContext() {

ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout("%d{ISO8601} [%-5p] %c{4}: %m%n"));
LogManager.getRootLogger().addAppender(consoleAppender);
LogManager.getRootLogger().setLevel(Level.WARN);
LogManager.getRootLogger().setLevel(Level.INFO);
}

LOGGER.warn("Initializing context before running integration tests");
context = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
LOGGER.warn("Context initialized integration tests will now start running");
return context;
public static void addToEnv(Map<String, String> envVars) throws Exception {
Map<String, String> envMap = System.getenv();
Class<?> clazz = envMap.getClass();
Field m = clazz.getDeclaredField("m");
m.setAccessible(true);

Map<String, String> mutableEnvMap = (Map<String, String>) m.get(envMap);
mutableEnvMap.putAll(envVars);
}

public static void tearDownFakeServers() throws Exception {
httpDataServer.stop();
tmpDeployDir.deleteOnExit();
}

public static ApplicationContext getContext() {
if (context == null) {
LOGGER.warn("Initializing context before running integration tests");
context = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
LOGGER.warn("Context initialized integration tests will now start running");
}
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.apache.traffic_control.traffic_router.core.dns;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -25,8 +24,6 @@
import java.io.File;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -37,6 +34,7 @@
import org.apache.traffic_control.traffic_router.core.util.IntegrationTest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -66,7 +64,7 @@ public class ZoneManagerTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
assertThat("Copy core/src/main/conf/traffic_monitor.properties to core/src/test/conf and set 'traffic_monitor.bootstrap.hosts' to a real traffic monitor", Files.exists(Paths.get(TestBase.monitorPropertiesPath)), equalTo(true));
TestBase.setupFakeServers();
context = TestBase.getContext();
}

Expand Down Expand Up @@ -154,6 +152,11 @@ public void testDynamicZoneCache() throws TextParseException {
}
}

@AfterClass
public static void tearDown() throws Exception {
TestBase.tearDownFakeServers();
}

private BigInteger fact(final int n) {
BigInteger p = new BigInteger("1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class HttpDataServer implements HttpHandler {
private HttpServer httpServer;
private int testHttpServerPort;
private static String apiVersion = "2.0";

public HttpDataServer(int testHttpServerPort) {
this.testHttpServerPort = testHttpServerPort;
Expand Down Expand Up @@ -119,7 +120,7 @@ public void run() {
path += ".json";
}

if ("api/2.0/user/login".equals(path)) {
if (("api/" + apiVersion + "/user/login").equals(path)) {
try {
Headers headers = httpExchange.getResponseHeaders();
headers.set("Set-Cookie", new HttpCookie("mojolicious","fake-cookie").toString());
Expand All @@ -130,12 +131,12 @@ public void run() {
}

// Pretend that someone externally changed steering.json data
if (receivedSteeringPost && "api/2.0/steering".equals(path)) {
path = "api/2.0/steering2";
if (receivedSteeringPost && ("api/" + apiVersion + "/steering").equals(path)) {
path = "api/" + apiVersion + "/steering2";
}

// pretend certificates have not been updated
if (!receivedCertificatesPost && "api/2.0/cdns/name/thecdn/sslkeys".equals(path)) {
if (!receivedCertificatesPost && ("api/" + apiVersion + "/cdns/name/thecdn/sslkeys").equals(path)) {
path = path.replace("/sslkeys", "/sslkeys-missing-1");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,17 @@

import org.apache.traffic_control.traffic_router.core.util.IntegrationTest;
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;
import org.junit.experimental.categories.Category;
import org.springframework.context.ApplicationContext;

import org.junit.Assert;

import org.apache.traffic_control.traffic_router.core.TestBase;
import org.apache.traffic_control.traffic_router.geolocation.Geolocation;

import java.nio.file.Files;
import java.nio.file.Paths;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@Category(IntegrationTest.class)
public class GeoTest {
private static final Logger LOGGER = Logger.getLogger(GeoTest.class);
Expand All @@ -44,7 +38,7 @@ public class GeoTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
assertThat("Copy core/src/main/conf/traffic_monitor.properties to core/src/test/conf and set 'traffic_monitor.bootstrap.hosts' to a real traffic monitor", Files.exists(Paths.get(TestBase.monitorPropertiesPath)), equalTo(true));
TestBase.setupFakeServers();
context = TestBase.getContext();
}

Expand Down Expand Up @@ -78,4 +72,9 @@ public void testIps() {
e.printStackTrace();
}
}

@AfterClass
public static void tearDown() throws Exception {
TestBase.tearDownFakeServers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.traffic_control.traffic_router.core.util.IntegrationTest;
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -29,12 +30,6 @@
import org.apache.traffic_control.traffic_router.core.request.HTTPRequest;
import org.apache.traffic_control.traffic_router.core.router.StatTracker.Track;

import java.nio.file.Files;
import java.nio.file.Paths;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@Category(IntegrationTest.class)
public class StatelessTrafficRouterTest {
private static final Logger LOGGER = Logger.getLogger(StatelessTrafficRouterTest.class);
Expand All @@ -45,7 +40,7 @@ public class StatelessTrafficRouterTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
assertThat("Copy core/src/main/conf/traffic_monitor.properties to core/src/test/conf and set 'traffic_monitor.bootstrap.hosts' to a real traffic monitor", Files.exists(Paths.get(TestBase.monitorPropertiesPath)), equalTo(true));
TestBase.setupFakeServers();
context = TestBase.getContext();
}

Expand Down Expand Up @@ -78,4 +73,9 @@ public void testRouteHTTPRequestTrack() throws Exception {
trafficRouterManager.getTrafficRouter().route(req, track);
}

@AfterClass
public static void tearDown() throws Exception {
TestBase.tearDownFakeServers();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.ApplicationContext;

import java.nio.file.Files;
import java.nio.file.Paths;

import static org.apache.traffic_control.traffic_router.core.ds.SteeringWatcher.DEFAULT_STEERING_DATA_URL;
import static org.apache.traffic_control.traffic_router.core.loc.FederationsWatcher.DEFAULT_FEDERATION_DATA_URL;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNull;


Expand All @@ -56,8 +53,8 @@ public class AbstractResourceWatcherTest {
private String oldFedUrl;

@BeforeClass
public static void setUpBeforeClass() {
assertThat("Copy core/src/main/conf/traffic_monitor.properties to core/src/test/conf and set 'traffic_monitor.bootstrap.hosts' to a real traffic monitor", Files.exists(Paths.get(TestBase.monitorPropertiesPath)), equalTo(true));
public static void setUpBeforeClass() throws Exception {
TestBase.setupFakeServers();
context = TestBase.getContext();
}

Expand Down Expand Up @@ -107,6 +104,11 @@ public void tearDown() {
assertThat(federationsWatcher.getDataBaseURL(), endsWith(DEFAULT_FEDERATION_DATA_URL.split("api")[1]));
}

@AfterClass
public static void tearDownServers() throws Exception {
TestBase.tearDownFakeServers();
}

@Test
public void testWatchers() {
TrafficRouter trafficRouter = trafficRouterManager.getTrafficRouter();
Expand Down