Skip to content

Commit

Permalink
Improve test speed
Browse files Browse the repository at this point in the history
  • Loading branch information
emerkle826 committed Jan 20, 2022
1 parent 7acc28c commit ffcd0ef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public static void setup() throws InterruptedException
{
temporaryFolder.create();
docker = new DockerHelper(getTempDir());
docker.removeExistingCntainers();
}
catch (IOException e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright DataStax, Inc.
*
* Please see the included license file for details.
*/
package com.datastax.mgmtapi;

import java.io.IOException;
import org.junit.After;

/**
* This class adds and After method to stop the Management API container after each
* individual test. Extend this if tests can't be run against the same running container
* without predictable results.
*/
public abstract class BaseDockerIsolatedIntegrationTest extends BaseDockerIntegrationTest
{

public BaseDockerIsolatedIntegrationTest(String version) throws IOException {
super(version);
}

@After
public void after()
{
docker.stopManagementAPI();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.junit.Assume.assumeTrue;

@RunWith(Parameterized.class)
public class DestructiveOpsIT extends BaseDockerIntegrationTest
public class DestructiveOpsIT extends BaseDockerIsolatedIntegrationTest
{
private static final Logger logger = LoggerFactory.getLogger(NonDestructiveOpsIT.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import static org.junit.Assume.assumeTrue;

@RunWith(Parameterized.class)
public class LifecycleIT extends BaseDockerIntegrationTest
public class LifecycleIT extends BaseDockerIsolatedIntegrationTest
{
public LifecycleIT(String version) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class DockerHelper
{
private static Logger logger = LoggerFactory.getLogger(DockerHelper.class);

public static final String CONTAINER_NAME = "mgmtapi";
// Keep track of Docker images built during test runs
private static final Set<String> IMAGE_NAMES = new HashSet<>();

Expand Down Expand Up @@ -112,16 +113,15 @@ public void startManagementAPI(String version, List<String> envVars)
if (!config.dockerFile.exists())
throw new RuntimeException("Missing " + config.dockerFile.getAbsolutePath());

String name = "mgmtapi";
List<Integer> ports = Arrays.asList(9042, 8080);
List<String> volumeDescList = Arrays.asList(dataDir.getAbsolutePath() + ":/var/log/cassandra");
List<String> envList = Lists.newArrayList("MAX_HEAP_SIZE=500M", "HEAP_NEWSIZE=100M");
List<String> cmdList = Lists.newArrayList("mgmtapi");
List<String> cmdList = Lists.newArrayList(CONTAINER_NAME);

if (envVars != null)
envList.addAll(envVars);

this.container = startDocker(config, name, ports, volumeDescList, envList, cmdList);
this.container = startDocker(config, ports, volumeDescList, envList, cmdList);

waitForPort("localhost",8080, Duration.ofMillis(50000), logger, false);
}
Expand Down Expand Up @@ -234,10 +234,10 @@ private void buildImageWithBuildx(DockerBuildConfig config, String name) throws
}
}

private String startDocker(DockerBuildConfig config, String name, List<Integer> ports, List<String> volumeDescList, List<String> envList, List<String> cmdList)
public void removeExistingCntainers()
{
ListContainersCmd listContainersCmd = DOCKER_CLIENT.listContainersCmd();
listContainersCmd.getFilters().put("name", Arrays.asList(name));
listContainersCmd.getFilters().put("name", Arrays.asList(CONTAINER_NAME));
try
{
List<Container> allContainersWithName = listContainersCmd.exec();
Expand All @@ -257,15 +257,18 @@ private String startDocker(DockerBuildConfig config, String name, List<Integer>
logger.error("$ sudo gpasswd -a ${USER} docker && newgrp docker");
System.exit(1);
}
}

Container containerId = searchContainer(name);
private String startDocker(DockerBuildConfig config, List<Integer> ports, List<String> volumeDescList, List<String> envList, List<String> cmdList)
{
Container containerId = searchContainer(CONTAINER_NAME);
if (containerId != null)
{
return containerId.getId();
}

// see if we have the image already built
final String imageName = String.format("%s-%s-test", name, config.dockerFile.getName()).toLowerCase();
final String imageName = String.format("%s-%s-test", CONTAINER_NAME, config.dockerFile.getName()).toLowerCase();
Image image = searchImages(imageName);
if (image == null)
{
Expand All @@ -281,7 +284,7 @@ public void onNext(BuildResponseItem item)
}
};

logger.info(String.format("Building container: name=%s, Dockerfile=%s, image name=%s", name, config.dockerFile.getPath(), imageName));
logger.info(String.format("Building container: name=%s, Dockerfile=%s, image name=%s", CONTAINER_NAME, config.dockerFile.getPath(), imageName));
if (config.useBuildx)
{
try
Expand Down Expand Up @@ -344,7 +347,7 @@ public void onNext(BuildResponseItem item)
// don't bind /var/log/cassandra, it causes permissions issues with startup
//.withBinds(volumeBindList)
)
.withName(name)
.withName(CONTAINER_NAME)
.exec();


Expand All @@ -358,7 +361,7 @@ public void onNext(Frame item)

@Override
public void onStart(Closeable stream) {
System.out.println("Starting container " + name);
System.out.println("Starting container " + CONTAINER_NAME);
}
});

Expand Down

0 comments on commit ffcd0ef

Please sign in to comment.