Skip to content

Commit 98addf9

Browse files
basilMarkRx
andauthored
Revive test suite on Windows (#331)
Co-authored-by: Mark <[email protected]>
1 parent 1001d94 commit 98addf9

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

Jenkinsfile

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ buildPlugin(
66
useContainerAgent: false, // Set to `false` if you need to use Docker for containerized tests
77
configurations: [
88
[platform: 'linux', jdk: 21],
9-
[platform: 'linux', jdk: 17],
10-
// TODO Windows tests seem to be failing on temporary Windows CI infrastructure from https://github.com/jenkins-infra/helpdesk/issues/4490
11-
//[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.
9+
[platform: 'windows', jdk: 17],
1210
])

src/test/java/org/jenkinsci/plugins/docker/workflow/DockerDSLTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ private static void grep(File dir, String text, String prefix, Set<String> match
270270
@Test public void buildWithMultiStage() {
271271
story.addStep(new Statement() {
272272
@Override public void evaluate() throws Throwable {
273-
assumeDocker(new VersionNumber("17.05"));
273+
assumeDocker(DockerTestUtil.DockerOsMode.LINUX, new VersionNumber("17.05"));
274274
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
275275
p.setDefinition(new CpsFlowDefinition(
276276
"node {\n" +

src/test/java/org/jenkinsci/plugins/docker/workflow/DockerTestUtil.java

+19-25
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.Assume;
3232

3333
import java.io.ByteArrayOutputStream;
34+
import java.io.File;
3435
import java.io.IOException;
3536
import java.util.Arrays;
3637
import java.util.List;
@@ -39,7 +40,6 @@
3940
import java.util.regex.Matcher;
4041
import java.util.regex.Pattern;
4142

42-
import org.hamcrest.Matchers;
4343
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
4444

4545
/**
@@ -58,33 +58,20 @@ public class DockerTestUtil {
5858
"10.0.26100.2605" // 2025
5959
);
6060

61+
public enum DockerOsMode {
62+
LINUX,
63+
WINDOWS
64+
}
6165

6266
public static void assumeDocker() throws Exception {
63-
assumeDocker(new VersionNumber(DEFAULT_MINIMUM_VERSION));
67+
assumeDocker(DockerOsMode.LINUX, new VersionNumber(DEFAULT_MINIMUM_VERSION));
6468
}
65-
66-
public static void assumeDocker(VersionNumber minimumVersion) throws Exception {
67-
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
68-
try {
69-
int status = localLauncher
70-
.launch()
71-
.cmds(DockerTool.getExecutable(null, null, null, null), "ps")
72-
.start()
73-
.joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, localLauncher.getListener());
74-
Assume.assumeTrue("Docker working", status == 0);
75-
} catch (IOException x) {
76-
Assume.assumeNoException("have Docker installed", x);
77-
}
78-
DockerClient dockerClient = new DockerClient(localLauncher, null, null);
79-
Assume.assumeFalse("Docker version not < " + minimumVersion.toString(), dockerClient.version().isOlderThan(minimumVersion));
69+
70+
public static void assumeDocker(DockerOsMode osMode) throws Exception {
71+
assumeDocker(osMode, new VersionNumber(DEFAULT_MINIMUM_VERSION));
8072
}
8173

82-
/**
83-
* Used to assume docker Windows is running in a particular os mode
84-
* @param os The os [windows, linux]
85-
* @throws Exception
86-
*/
87-
public static void assumeDockerServerOSMode(String os) throws Exception {
74+
public static void assumeDocker(DockerOsMode osMode, VersionNumber minimumVersion) throws Exception {
8875
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
8976
try {
9077
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -94,11 +81,14 @@ public static void assumeDockerServerOSMode(String os) throws Exception {
9481
.stdout(out)
9582
.start()
9683
.joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, localLauncher.getListener());
84+
DockerOsMode cmdOsMode = DockerOsMode.valueOf(out.toString().trim().toUpperCase());
9785
Assume.assumeTrue("Docker working", status == 0);
98-
Assume.assumeThat("Docker running in " + os + " mode", out.toString().trim(), Matchers.equalToIgnoringCase(os));
86+
Assume.assumeTrue("Docker os mode " + osMode, osMode == cmdOsMode);
9987
} catch (IOException x) {
100-
Assume.assumeNoException("Docker retrieve OS", x);
88+
Assume.assumeNoException("have Docker installed", x);
10189
}
90+
DockerClient dockerClient = new DockerClient(localLauncher, null, null);
91+
Assume.assumeFalse("Docker version not < " + minimumVersion.toString(), dockerClient.version().isOlderThan(minimumVersion));
10292
}
10393

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

102+
public static void assumeDrive(char drive) throws Exception {
103+
Assume.assumeTrue(new File(drive + ":/").exists());
104+
}
105+
112106
public static String getWindowsKernelVersion() throws Exception {
113107
Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
114108
ByteArrayOutputStream out = new ByteArrayOutputStream();

src/test/java/org/jenkinsci/plugins/docker/workflow/WithContainerStepTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public class WithContainerStepTest {
299299
@Test public void cd() throws Exception {
300300
story.addStep(new Statement() {
301301
@Override public void evaluate() throws Throwable {
302-
DockerTestUtil.assumeDocker(new VersionNumber("17.12"));
302+
DockerTestUtil.assumeDocker(DockerTestUtil.DockerOsMode.LINUX, new VersionNumber("17.12"));
303303
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
304304
p.setDefinition(new CpsFlowDefinition(
305305
"node {\n" +
@@ -498,8 +498,7 @@ private static final class Execution extends SynchronousNonBlockingStepExecution
498498
story.addStep(new Statement() {
499499
@Override public void evaluate() throws Throwable {
500500
DockerTestUtil.assumeWindows();
501-
DockerTestUtil.assumeDocker();
502-
DockerTestUtil.assumeDockerServerOSMode("windows");
501+
DockerTestUtil.assumeDocker(DockerTestUtil.DockerOsMode.WINDOWS);
503502

504503
// Kernel must match when running Windows containers on docker on Windows
505504
String releaseTag = DockerTestUtil.getWindowsImageTag();

src/test/java/org/jenkinsci/plugins/docker/workflow/client/WindowsDockerClientTest.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,23 @@
1010
import org.junit.Before;
1111
import org.junit.Test;
1212

13-
import java.io.IOException;
1413
import java.util.Collections;
1514

1615
public class WindowsDockerClientTest {
1716

1817
private DockerClient dockerClient;
1918

2019
@Before
21-
public void setup() throws Exception {
22-
DockerTestUtil.assumeDocker();
23-
20+
public void setup() {
2421
TaskListener taskListener = StreamTaskListener.fromStderr();
2522
Launcher.LocalLauncher launcher = new Launcher.LocalLauncher(taskListener);
2623

2724
dockerClient = new WindowsDockerClient(launcher, null, null);
2825
}
2926

3027
@Test
31-
public void test_run() throws IOException, InterruptedException {
28+
public void test_run() throws Exception {
29+
DockerTestUtil.assumeDocker();
3230
EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv();
3331
String containerId = dockerClient.run(
3432
launchEnv,

0 commit comments

Comments
 (0)