Search in sources :

Example 1 with ContainerInfo

use of com.microsoft.applicationinsights.smoketest.docker.ContainerInfo in project ApplicationInsights-Java by microsoft.

the class AiSmokeTest method startDependencyContainers.

private static void startDependencyContainers() throws IOException, InterruptedException {
    if (dependencyImages.isEmpty()) {
        System.out.println("No dependency containers to start.");
        return;
    }
    Map<String, String> hostnameEnvVars = new HashMap<>();
    for (DependencyContainer dc : dependencyImages) {
        String imageName = dc.imageName().isEmpty() ? dc.value() : dc.imageName();
        System.out.printf("Starting container: %s%n", imageName);
        String containerName = "dependency" + new Random().nextInt(Integer.MAX_VALUE);
        String[] envVars = substitue(dc.environmentVariables(), hostnameEnvVars, containerName);
        String containerId = AiDockerClient.startDependencyContainer(imageName, envVars, dc.portMapping(), networkId, containerName);
        if (containerId == null || containerId.isEmpty()) {
            throw new AssertionError("'containerId' was null/empty attempting to start container: " + imageName);
        }
        System.out.printf("Dependency container started: %s (%s)%n", imageName, containerId);
        if (!dc.hostnameEnvironmentVariable().isEmpty()) {
            hostnameEnvVars.put(dc.hostnameEnvironmentVariable(), containerName);
        }
        ContainerInfo depConInfo = new ContainerInfo(containerId, containerName);
        depConInfo.setContainerName(containerName);
        depConInfo.setDependencyContainerInfo(dc);
        System.out.printf("Dependency container name for %s: %s%n", imageName, containerName);
        allContainers.push(depConInfo);
        // wait a bit after starting a server.
        TimeUnit.MILLISECONDS.sleep(500);
    }
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) ContainerInfo(com.microsoft.applicationinsights.smoketest.docker.ContainerInfo)

Example 2 with ContainerInfo

use of com.microsoft.applicationinsights.smoketest.docker.ContainerInfo in project ApplicationInsights-Java by microsoft.

the class AiSmokeTest method startTestApplicationContainer.

private static void startTestApplicationContainer() throws Exception {
    System.out.printf("Starting container: %s%n", currentImageName);
    Map<String, String> envVars = generateAppContainerEnvVarMap();
    String containerId = AiDockerClient.startContainer(currentImageName, appServerPort + ":8080", networkId, null, envVars, false);
    if (containerId == null || containerId.isEmpty()) {
        throw new AssertionError("'containerId' was null/empty attempting to start container: " + currentImageName);
    }
    System.out.printf("Container started: %s (%s)%n", currentImageName, containerId);
    ContainerInfo containerInfo = new ContainerInfo(containerId, currentImageName);
    currentContainerInfo.set(containerInfo);
    if (currentImageName.startsWith("javase_")) {
        // can proceed straight to deploying the app
        // (there's nothing running at this point, unlike images based on servlet containers)
        allContainers.push(containerInfo);
    } else {
        try {
            String url = String.format("http://localhost:%s/", String.valueOf(appServerPort));
            System.out.printf("Verifying appserver has started (%s)...%n", url);
            allContainers.push(containerInfo);
            waitForUrlWithRetries(url, APPSERVER_HEALTH_CHECK_TIMEOUT, TimeUnit.SECONDS, String.format("app server on image '%s'", currentImageName), HEALTH_CHECK_RETRIES);
            System.out.println("App server is ready.");
        } catch (RuntimeException e) {
            System.err.println("Error starting app server");
            if (docker.isContainerRunning(containerInfo.getContainerId())) {
                System.out.println("Container is not running.");
                allContainers.remove(containerInfo);
            } else {
                System.out.println("Yet, the container is running.");
            }
            System.out.println("Printing container logs: ");
            System.out.println("# LOGS START =========================");
            AiDockerClient.printContainerLogs(containerInfo.getContainerId());
            System.out.println("# LOGS END ===========================");
            throw e;
        }
    }
    try {
        System.out.printf("Deploying test application: %s...%n", warFileName);
        docker.copyAndDeployToContainer(containerId, new File(Resources.getResource(warFileName).toURI()));
        System.out.println("Test application deployed.");
    } catch (Exception e) {
        System.err.println("Error deploying test application.");
        throw e;
    }
}
Also used : ContainerInfo(com.microsoft.applicationinsights.smoketest.docker.ContainerInfo) File(java.io.File) TimeoutException(com.microsoft.applicationinsights.smoketest.exceptions.TimeoutException) SmokeTestException(com.microsoft.applicationinsights.smoketest.exceptions.SmokeTestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ContainerInfo

use of com.microsoft.applicationinsights.smoketest.docker.ContainerInfo in project ApplicationInsights-Java by microsoft.

the class AiSmokeTest method configureEnvironment.

@BeforeWithParams
public static void configureEnvironment(String appServer, String os, String jreVersion) throws Exception {
    System.out.println("Preparing environment...");
    try {
        ContainerInfo containerInfo = currentContainerInfo.get();
        if (containerInfo != null) {
            // test cleanup didn't take...try to clean up
            if (docker.isContainerRunning(containerInfo.getContainerId())) {
                System.err.println("From last test run, container is still running: " + containerInfo);
                try {
                    docker.stopContainer(containerInfo.getContainerId());
                } catch (Exception e) {
                    System.err.println("Couldn't clean up environment. Must be done manually.");
                    throw e;
                }
            } else {
                // container must have stopped after timeout reached.
                currentContainerInfo.set(null);
            }
        }
        checkParams(appServer, os, jreVersion);
        setupProperties(appServer, os, jreVersion);
        startMockedIngestion();
        createDockerNetwork();
        startAllContainers();
        waitForApplicationToStart();
        System.out.println("Environment preparation complete.");
    } catch (Exception e) {
        String additionalMessage;
        if (e instanceof TimeoutException) {
            additionalMessage = e.getLocalizedMessage();
        } else {
            additionalMessage = ExceptionUtils.getStackTrace(e);
        }
        System.err.printf("Could not configure environment: %s%n", additionalMessage);
        throw e;
    }
}
Also used : ContainerInfo(com.microsoft.applicationinsights.smoketest.docker.ContainerInfo) TimeoutException(com.microsoft.applicationinsights.smoketest.exceptions.TimeoutException) SmokeTestException(com.microsoft.applicationinsights.smoketest.exceptions.SmokeTestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(com.microsoft.applicationinsights.smoketest.exceptions.TimeoutException) BeforeWithParams(com.microsoft.applicationinsights.smoketest.fixtures.BeforeWithParams)

Example 4 with ContainerInfo

use of com.microsoft.applicationinsights.smoketest.docker.ContainerInfo in project ApplicationInsights-Java by microsoft.

the class AiSmokeTest method generateAppContainerEnvVarMap.

private static Map<String, String> generateAppContainerEnvVarMap() {
    Map<String, String> map = new HashMap<>();
    if (agentMode != null) {
        map.put("AI_AGENT_MODE", agentMode);
    }
    for (ContainerInfo info : allContainers) {
        if (!info.isDependency()) {
            continue;
        }
        DependencyContainer dc = info.getDependencyContainerInfo();
        String varname = dc.hostnameEnvironmentVariable();
        if (varname.isEmpty()) {
            varname = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, dc.value());
        }
        String containerName = info.getContainerName();
        if (containerName == null || containerName.isEmpty()) {
            throw new SmokeTestException("Null/empty container name for dependency container");
        }
        map.put(varname, containerName);
        System.out.printf("Adding env var to test app container: %s=%s%n", varname, containerName);
    }
    return map;
}
Also used : SmokeTestException(com.microsoft.applicationinsights.smoketest.exceptions.SmokeTestException) HashMap(java.util.HashMap) ContainerInfo(com.microsoft.applicationinsights.smoketest.docker.ContainerInfo)

Example 5 with ContainerInfo

use of com.microsoft.applicationinsights.smoketest.docker.ContainerInfo in project ApplicationInsights-Java by microsoft.

the class AiSmokeTest method stopAllContainers.

public static void stopAllContainers() throws Exception {
    if (allContainers.isEmpty()) {
        System.out.println("No containers to stop");
        return;
    }
    System.out.printf("Stopping %d containers...", allContainers.size());
    List<ContainerInfo> failedToStop = new ArrayList<>();
    while (!allContainers.isEmpty()) {
        ContainerInfo c = allContainers.pop();
        if (c.equals(currentContainerInfo.get())) {
            System.out.println("Cleaning up app container");
            currentContainerInfo.set(null);
        }
        stopContainer(c);
        if (docker.isContainerRunning(c.getContainerId())) {
            System.err.printf("ERROR: Container failed to stop: %s%n", c.toString());
            failedToStop.add(c);
        }
    }
    ContainerInfo containerInfo = currentContainerInfo.get();
    if (containerInfo != null) {
        System.err.println("Could not find app container in stack. Stopping...");
        stopContainer(containerInfo);
        if (!docker.isContainerRunning(containerInfo.getContainerId())) {
            currentContainerInfo.set(null);
        }
    }
    if (!failedToStop.isEmpty()) {
        System.err.println("Some containers failed to stop. Subsequent tests may fail.");
        for (ContainerInfo c : failedToStop) {
            if (docker.isContainerRunning(c.getContainerId())) {
                System.err.println("Failed to stop: " + c.toString());
            }
        }
    }
}
Also used : ContainerInfo(com.microsoft.applicationinsights.smoketest.docker.ContainerInfo) ArrayList(java.util.ArrayList)

Aggregations

ContainerInfo (com.microsoft.applicationinsights.smoketest.docker.ContainerInfo)6 SmokeTestException (com.microsoft.applicationinsights.smoketest.exceptions.SmokeTestException)4 TimeoutException (com.microsoft.applicationinsights.smoketest.exceptions.TimeoutException)3 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 HashMap (java.util.HashMap)2 BeforeWithParams (com.microsoft.applicationinsights.smoketest.fixtures.BeforeWithParams)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1