Skip to content

Commit

Permalink
Revive test suite on Windows (#331)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark <MarkRx@users.noreply.github.com>
  • Loading branch information
basil and MarkRx authored Jan 20, 2025
1 parent 1001d94 commit 98addf9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 37 deletions.
4 changes: 1 addition & 3 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ buildPlugin(
useContainerAgent: false, // Set to `false` if you need to use Docker for containerized tests
configurations: [
[platform: 'linux', jdk: 21],
[platform: 'linux', jdk: 17],
// TODO Windows tests seem to be failing on temporary Windows CI infrastructure from https://github.com/jenkins-infra/helpdesk/issues/4490
//[platform: 'maven-17-windows', jdk: 17], // TODO Docker-based tests fail when using Docker on Windows. The maven-windows agents do not have Docker installed so tests that require Docker are skipped.
[platform: 'windows', jdk: 17],
])
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private static void grep(File dir, String text, String prefix, Set<String> match
@Test public void buildWithMultiStage() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
assumeDocker(new VersionNumber("17.05"));
assumeDocker(DockerTestUtil.DockerOsMode.LINUX, new VersionNumber("17.05"));
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.Assume;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
Expand All @@ -39,7 +40,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hamcrest.Matchers;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;

/**
Expand All @@ -58,33 +58,20 @@ public class DockerTestUtil {
"10.0.26100.2605" // 2025
);

public enum DockerOsMode {
LINUX,
WINDOWS
}

public static void assumeDocker() throws Exception {
assumeDocker(new VersionNumber(DEFAULT_MINIMUM_VERSION));
assumeDocker(DockerOsMode.LINUX, new VersionNumber(DEFAULT_MINIMUM_VERSION));
}

public static void assumeDocker(VersionNumber minimumVersion) throws Exception {
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
try {
int status = localLauncher
.launch()
.cmds(DockerTool.getExecutable(null, null, null, null), "ps")
.start()
.joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, localLauncher.getListener());
Assume.assumeTrue("Docker working", status == 0);
} catch (IOException x) {
Assume.assumeNoException("have Docker installed", x);
}
DockerClient dockerClient = new DockerClient(localLauncher, null, null);
Assume.assumeFalse("Docker version not < " + minimumVersion.toString(), dockerClient.version().isOlderThan(minimumVersion));

public static void assumeDocker(DockerOsMode osMode) throws Exception {
assumeDocker(osMode, new VersionNumber(DEFAULT_MINIMUM_VERSION));
}

/**
* Used to assume docker Windows is running in a particular os mode
* @param os The os [windows, linux]
* @throws Exception
*/
public static void assumeDockerServerOSMode(String os) throws Exception {
public static void assumeDocker(DockerOsMode osMode, VersionNumber minimumVersion) throws Exception {
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand All @@ -94,11 +81,14 @@ public static void assumeDockerServerOSMode(String os) throws Exception {
.stdout(out)
.start()
.joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, localLauncher.getListener());
DockerOsMode cmdOsMode = DockerOsMode.valueOf(out.toString().trim().toUpperCase());
Assume.assumeTrue("Docker working", status == 0);
Assume.assumeThat("Docker running in " + os + " mode", out.toString().trim(), Matchers.equalToIgnoringCase(os));
Assume.assumeTrue("Docker os mode " + osMode, osMode == cmdOsMode);
} catch (IOException x) {
Assume.assumeNoException("Docker retrieve OS", x);
Assume.assumeNoException("have Docker installed", x);
}
DockerClient dockerClient = new DockerClient(localLauncher, null, null);
Assume.assumeFalse("Docker version not < " + minimumVersion.toString(), dockerClient.version().isOlderThan(minimumVersion));
}

public static void assumeWindows() throws Exception {
Expand All @@ -109,6 +99,10 @@ public static void assumeNotWindows() throws Exception {
Assume.assumeFalse(System.getProperty("os.name").toLowerCase().contains("windows"));
}

public static void assumeDrive(char drive) throws Exception {
Assume.assumeTrue(new File(drive + ":/").exists());
}

public static String getWindowsKernelVersion() throws Exception {
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public class WithContainerStepTest {
@Test public void cd() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
DockerTestUtil.assumeDocker(new VersionNumber("17.12"));
DockerTestUtil.assumeDocker(DockerTestUtil.DockerOsMode.LINUX, new VersionNumber("17.12"));
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
Expand Down Expand Up @@ -498,8 +498,7 @@ private static final class Execution extends SynchronousNonBlockingStepExecution
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
DockerTestUtil.assumeWindows();
DockerTestUtil.assumeDocker();
DockerTestUtil.assumeDockerServerOSMode("windows");
DockerTestUtil.assumeDocker(DockerTestUtil.DockerOsMode.WINDOWS);

// Kernel must match when running Windows containers on docker on Windows
String releaseTag = DockerTestUtil.getWindowsImageTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;

public class WindowsDockerClientTest {

private DockerClient dockerClient;

@Before
public void setup() throws Exception {
DockerTestUtil.assumeDocker();

public void setup() {
TaskListener taskListener = StreamTaskListener.fromStderr();
Launcher.LocalLauncher launcher = new Launcher.LocalLauncher(taskListener);

dockerClient = new WindowsDockerClient(launcher, null, null);
}

@Test
public void test_run() throws IOException, InterruptedException {
public void test_run() throws Exception {
DockerTestUtil.assumeDocker();
EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv();
String containerId = dockerClient.run(
launchEnv,
Expand Down

0 comments on commit 98addf9

Please sign in to comment.