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

Cache Trivy DB for integration tests #4181

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@

import alpine.model.IConfigProperty;
import alpine.security.crypto.DataEncryption;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateVolumeResponse;
import com.github.dockerjava.api.model.Bind;
import org.dependencytrack.PersistenceCapableTest;
import org.dependencytrack.event.TrivyAnalysisEvent;
import org.dependencytrack.model.Classifier;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Vulnerability;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.images.PullPolicy;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -57,13 +63,24 @@ public static Collection<?> testParameters() {
});
}

private static String trivyCacheVolumeName;
private final String trivyVersion;
private GenericContainer<?> trivyContainer;

public TrivyAnalysisTaskIntegrationTest(String trivyVersion) {
this.trivyVersion = trivyVersion;
}

@BeforeClass
@SuppressWarnings("resource")
public static void beforeClass() {
final DockerClient dockerClient = DockerClientFactory.lazyClient();
final CreateVolumeResponse response = dockerClient.createVolumeCmd()
.withName("dtrack-test-trivy-cache")
.exec();
trivyCacheVolumeName = response.getName();
}

@Before
@Override
@SuppressWarnings("resource")
Expand All @@ -72,8 +89,10 @@ public void before() throws Exception {

trivyContainer = new GenericContainer<>(DockerImageName.parse("aquasec/trivy:" + trivyVersion))
.withImagePullPolicy(PullPolicy.alwaysPull())
.withCommand("server --listen :8080 --token TrivyToken")
.withCommand("server --cache-dir /tmp/cache --listen :8080 --token TrivyToken")
.withExposedPorts(8080)
.withCreateContainerCmdModifier(cmd -> cmd.getHostConfig()
.withBinds(Bind.parse("%s:/tmp/cache".formatted(trivyCacheVolumeName))))
.waitingFor(forLogMessage(".*Listening :8080.*", 1));
trivyContainer.start();

Expand Down Expand Up @@ -110,6 +129,15 @@ public void after() {
super.after();
}

@AfterClass
@SuppressWarnings("resource")
public static void afterClass() {
if (trivyCacheVolumeName != null) {
final DockerClient dockerClient = DockerClientFactory.lazyClient();
dockerClient.removeVolumeCmd(trivyCacheVolumeName).exec();
}
}

@Test
public void test() {
final var project = new Project();
Expand Down
Loading