Skip to content
Open
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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-slaves</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public String whoAmI() throws IOException, InterruptedException {

}

private static final Pattern hostnameMount = Pattern.compile("/containers/([a-z0-9]{64})/hostname");
private static final Pattern hostnameMount = Pattern.compile("(?<=/containers/)[a-z0-9]{64}(?=/hostname)|(?<=/containers/overlay-containers/)[a-z0-9]{64}(?=/userdata/hostname)");

/**
* Checks if this {@link DockerClient} instance is running inside a container and returns the id of the container
Expand Down Expand Up @@ -368,7 +368,7 @@ public Optional<String> getContainerIdIfContainerized() throws IOException, Inte
while ((line = br.readLine()) != null) {
Matcher m = hostnameMount.matcher(line);
if (m.find()) {
return Optional.of(m.group(1));
return Optional.of(m.group());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.jenkinsci.plugins.docker.workflow.client;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.google.common.base.Optional;

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;

public class ContainerizedCGroupV2Test {
private FilePath mountInfo;
private DockerClient dockerClient;

ContainerizedCGroupV2Test() {

mountInfo = mock(FilePath.class);

Node node = mock(Node.class);
when(node.createPath("/proc/1/mountinfo")).thenReturn(mountInfo);

TaskListener taskListener = StreamTaskListener.fromStderr();
Launcher launcher = new Launcher.LocalLauncher(taskListener);

dockerClient = new DockerClient(launcher, node, null);
}

@ParameterizedTest
@ValueSource(strings = {"org/jenkinsci/plugins/docker/workflow/client/mountinfo_containerized_docker",
"org/jenkinsci/plugins/docker/workflow/client/mountinfo_containerized_podman"})
public void test_is_containerized_cgroupV2(String mountInfoPath) throws IOException, InterruptedException {

when(mountInfo.exists()).thenReturn(true);

when(mountInfo.read()).thenReturn(
getClass().getClassLoader().getResourceAsStream(mountInfoPath));
Optional<String> result = dockerClient.getContainerIdIfContainerized();

assertEquals("32199af8e73b51d2b9f7cc0cf2c2bb4ef1792c54a656f1c4da53858698396fd6", result.get());
}

@Test
public void test_is_not_containerized_cgroupV2() throws IOException, InterruptedException {

when(mountInfo.exists()).thenReturn(true);

when(mountInfo.read()).thenReturn(
getClass().getClassLoader().getResourceAsStream("org/jenkinsci/plugins/docker/workflow/client/mountinfo_not_containerized"));
Optional<String> result = dockerClient.getContainerIdIfContainerized();

assertEquals(Optional.absent(), result);
}

@Test
public void test_is_not_cgroupV2() throws IOException, InterruptedException {

when(mountInfo.exists()).thenReturn(false);

Optional<String> result = dockerClient.getContainerIdIfContainerized();

assertEquals(Optional.absent(), result);
}
}




























Loading
Loading