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

enable test containers for new tests #935

Closed
Closed
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
23 changes: 20 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
<servlet.version>4.0.4</servlet.version>
<activation.version>1.2.2</activation.version>

<junit.version>5.8.2</junit.version>
<testcontainers.version>1.15.3</testcontainers.version>
<junit.version>5.9.2</junit.version>
<testcontainers.version>1.17.6</testcontainers.version>
<mockito.version>4.4.0</mockito.version>
<hamcrest.version>1.3</hamcrest.version>
<systemRules.version>1.19.0</systemRules.version>
Expand Down Expand Up @@ -423,13 +423,30 @@
<artifactId>jakarta.servlet-api</artifactId>
<version>${servlet.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/gitlab4j/api/UserApiIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.gitlab4j.api;

import org.gitlab4j.test.GitLabContainer;
import org.gitlab4j.test.GitLabInvocationContextProvider;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(GitLabInvocationContextProvider.class)
class UserApiIntegrationTest {


@TestTemplate
void testName(GitLabContainer container) throws Exception {

System.out.println(container.version);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gitlab4j.test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnableForGitLabVersionAndSuperiorCondition.class)
public @interface EnableForGitLabVersionAndSuperior {

/**
* The minimal version on which this test will be executed.
*/
String version() default "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.gitlab4j.test;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import org.junit.platform.commons.util.AnnotationUtils;
import org.opentest4j.TestAbortedException;

public class EnableForGitLabVersionAndSuperiorCondition implements InvocationInterceptor {

private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult
.enabled("@EnableForGitLabVersionAndSuperiorCondition is not present");

@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
Method testMethod = extensionContext.getRequiredTestMethod();
List<Object> arguments = invocationContext.getArguments();

List<Object> containers = arguments.stream().filter(o -> {
if (o instanceof GitLabContainer) {
return true;
}
return false;
})
.collect(Collectors.toList());

if (containers.size() != 1) {
throw new ExtensionConfigurationException(
String.format(
"Method %s should declare 1 GitLabContainer. It's actually declare %d.",
testMethod.getName(),
containers.size()
)
);
}

GitLabContainer gitlabContainer = (GitLabContainer) containers.get(0);


AnnotatedElement element = extensionContext.getElement().orElse(null);
ConditionEvaluationResult result = AnnotationUtils.findAnnotation(element, EnableForGitLabVersionAndSuperior.class) //
.map(annotation -> toResult(annotation, gitlabContainer)) //
.orElse(ENABLED);

if (result.isDisabled()) {
throw new TestAbortedException(result.getReason().get());
}

invocation.proceed();

}

private ConditionEvaluationResult toResult(EnableForGitLabVersionAndSuperior annotation, GitLabContainer gitlabContainer) {
Version minimalVersion = new Version(annotation.version());
Version currentVersion = gitlabContainer.parsedVersion;

if (currentVersion.isBefore(minimalVersion)) {
return ConditionEvaluationResult.disabled(String.format("The minimal version of GitLab for this test is %s. Current execution is against GitLab %s", minimalVersion.version(), currentVersion.version()));
}
return ConditionEvaluationResult.enabled(minimalVersion.toString());
}
}


42 changes: 42 additions & 0 deletions src/test/java/org/gitlab4j/test/GitLabContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.gitlab4j.test;

import java.time.Duration;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.utility.DockerImageName;

public class GitLabContainer extends GenericContainer<GitLabContainer> {


public final String version;
public final Version parsedVersion;

public GitLabContainer(String version) {
super(DockerImageName.parse("gitlab/gitlab-ce:" + version));
this.version = version;
this.parsedVersion = new Version(version);

this.withExposedPorts(80)
.withEnv("GITLAB_OMNIBUS_CONFIG", "gitlab_rails['initial_root_password']=\"password\";gitlab_rails['lfs_enabled']=false;")
.waitingFor(
Wait
.forHttp("/")
.forStatusCode(302)
.forStatusCode(200)
.withStartupTimeout(Duration.ofSeconds(300))
);
}

public String url() {
return "http://localhost:" + getMappedPort(80) + "";
}

@Override
public String toString() {
return "GitLabContainer [version=" + version + ", parsedVersion=" + parsedVersion + ", url()= " + url() + " ]";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.gitlab4j.test;

import static java.util.Arrays.asList;

import java.util.List;
import java.util.stream.Stream;

import org.gitlab4j.test.SetupTeardownLauncherSessionListener.GitLabContainers;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;

public class GitLabInvocationContextProvider implements TestTemplateInvocationContextProvider {

@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
return GitLabContainers.containers().stream().map(this::invocationContext);
}

private TestTemplateInvocationContext invocationContext(GitLabContainer gitLabcontainer) {
return new TestTemplateInvocationContext() {

@Override
public String getDisplayName(int invocationIndex) {
return gitLabcontainer.version;
}

@Override
public List<Extension> getAdditionalExtensions() {
return asList(
new ParameterResolver() {
@Override
public boolean supportsParameter(ParameterContext parameterCtx, ExtensionContext extensionCtx) {
return parameterCtx.getParameter().getType().equals(GitLabContainer.class);
}

@Override
public Object resolveParameter(ParameterContext parameterCtx, ExtensionContext extensionCtx) {
return gitLabcontainer;
}
});
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.gitlab4j.test;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.User;
import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.LauncherSessionListener;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;

public class SetupTeardownLauncherSessionListener implements LauncherSessionListener {

private GitLabContainers gitLabContainers;

@Override
public void launcherSessionOpened(LauncherSession session) {
session.getLauncher().registerTestExecutionListeners(new TestExecutionListener() {
@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
if (gitLabContainers == null) {
gitLabContainers = new GitLabContainers();
gitLabContainers.setUp();
}
}
});
}

@Override
public void launcherSessionClosed(LauncherSession session) {
if (gitLabContainers != null) {
gitLabContainers.tearDown();
gitLabContainers = null;
}
}

static class GitLabContainers {

private static final String[] GITLAB_VERSIONS = {
"15.4.2-ce.0",
"14.10.5-ce.0",
};

private static final Map<String, GitLabContainer> containers = new HashMap<>();

public GitLabContainers() {
String propertyVersion = System.getProperty("gitlab4j.gitlab.version");

if (propertyVersion == null) {
for (String version : GITLAB_VERSIONS) {
containers.put(version, new GitLabContainer(version));
}
} else {
containers.put(propertyVersion, new GitLabContainer(propertyVersion));
}
}

public static Collection<GitLabContainer> containers() {
return Collections.unmodifiableCollection(containers.values());
}

void setUp() {
// containers.forEach((version, container) -> container.start());
}

void tearDown() {
containers.forEach((version, container) -> {
if (container.isRunning()) {
container.stop();
}
});
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/gitlab4j/test/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.gitlab4j.test;

public class Version {
final String version;
final int major;
final int minor;
final int fix;

Version(String version) {
this.version = version;
String[] split = version.substring(0, version.indexOf('-')).split("\\.");
major = Integer.valueOf(split[0]).intValue();
minor = Integer.valueOf(split[1]).intValue();
fix = Integer.valueOf(split[2]).intValue();
}


public String version() {
return version;
}

public boolean isBefore(Version minimalVersion) {
return major < minimalVersion.major || minor < minimalVersion.minor || minor < minimalVersion.fix;
}

@Override
public String toString() {
return "Version [major=" + major + ", minor=" + minor + ", fix=" + fix + "]";
}
}