|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Properties; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import org.junit.platform.launcher.LauncherSession; |
| 13 | +import org.junit.platform.launcher.LauncherSessionListener; |
| 14 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 15 | + |
| 16 | +public class GitLabContainersSetupTeardownListener implements LauncherSessionListener { |
| 17 | + |
| 18 | + private static final List<String> GITLAB_TESTED_VERSIONS = Arrays.asList("12.9.2-ce.0", "13.0.14-ce.0", |
| 19 | + "13.12.15-ce.0"); |
| 20 | + private static final Map<String, GitLabContainer> gitLabContainers = new HashMap<>(); |
| 21 | + |
| 22 | + @Override |
| 23 | + public void launcherSessionOpened(LauncherSession session) { |
| 24 | + if (gitLabContainers.isEmpty()) { |
| 25 | + |
| 26 | + List<String> gitLabVersions = new ArrayList<>(); |
| 27 | + |
| 28 | + String gitLabVersionsFromProperties = System.getenv("GITLAB4JAPI_GITLAB_VERSIONS"); |
| 29 | + |
| 30 | + if (gitLabVersionsFromProperties != null && !gitLabVersionsFromProperties.isEmpty()) { |
| 31 | + gitLabVersions.addAll(Arrays.stream(gitLabVersionsFromProperties.split(",")) |
| 32 | + .filter(s -> s != null && !s.isEmpty()) |
| 33 | + .collect(Collectors.toList())); |
| 34 | + } else { |
| 35 | + gitLabVersions.addAll(GITLAB_TESTED_VERSIONS); |
| 36 | + } |
| 37 | + |
| 38 | + gitLabVersions.forEach(version -> { |
| 39 | + Version parsedVersion = new Version(version); |
| 40 | + int port = parsedVersion.getPort(); |
| 41 | + |
| 42 | + GitLabContainer container = new GitLabContainer(version) |
| 43 | + .withGitLabPort(port) |
| 44 | + .waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(300))); |
| 45 | + |
| 46 | + System.out.println("GitLab container version " + container.getVersion() |
| 47 | + + " started on http://localhost:" + container.getPort() + "/"); |
| 48 | + |
| 49 | + gitLabContainers.put(version, container); |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void launcherSessionClosed(LauncherSession session) { |
| 56 | + gitLabContainers.values().forEach(container -> { |
| 57 | + container.stop(); |
| 58 | + System.out.println("GitLab container version " + container.getVersion() + " stopped"); |
| 59 | + }); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + class Version { |
| 64 | + private Integer major; |
| 65 | + private Integer minor; |
| 66 | + private String fix; |
| 67 | + private Integer build; |
| 68 | + |
| 69 | + public Version(String version) { |
| 70 | + String[] splittedVersion = version.split("\\."); |
| 71 | + this.major = Integer.valueOf(splittedVersion[0]); |
| 72 | + this.minor = Integer.valueOf(splittedVersion[1]); |
| 73 | + this.fix = splittedVersion[2]; |
| 74 | + this.build = Integer.valueOf(splittedVersion[3]); |
| 75 | + } |
| 76 | + |
| 77 | + public int getMajor() { |
| 78 | + return major; |
| 79 | + } |
| 80 | + |
| 81 | + public int getMinor() { |
| 82 | + return minor; |
| 83 | + } |
| 84 | + |
| 85 | + public String getFix() { |
| 86 | + return fix; |
| 87 | + } |
| 88 | + |
| 89 | + public int getBuild() { |
| 90 | + return build; |
| 91 | + } |
| 92 | + |
| 93 | + int getPort() { |
| 94 | + String port = "8" + major.toString() + String.format("%02d", minor); |
| 95 | + return Integer.valueOf(port); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments