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

[JENKINS-47178] Replace Windows path separator with Unix path separator #308

Merged
merged 1 commit into from Jun 26, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Pod build() {
for (final PodVolume volume : template.getVolumes()) {
final String volumeName = "volume-" + i;
//We need to normalize the path or we can end up in really hard to debug issues.
final String mountPath = substituteEnv(Paths.get(volume.getMountPath()).normalize().toString());
final String mountPath = substituteEnv(Paths.get(volume.getMountPath()).normalize().toString().replace("\\", "/"));
if (!volumeMounts.containsKey(mountPath)) {
volumeMounts.put(mountPath, new VolumeMount(mountPath, volumeName, false, null));
volumes.put(volumeName, volume.buildVolume(volumeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -14,7 +15,11 @@

import org.apache.commons.compress.utils.IOUtils;
import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume;
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -25,6 +30,7 @@
import org.mockito.junit.MockitoRule;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import io.fabric8.kubernetes.api.model.Container;
Expand Down Expand Up @@ -117,6 +123,30 @@ public void testBuildWithCustomWorkspaceVolume() throws Exception {
assertEquals(volumeMounts, container1.getVolumeMounts());
}

public void testBuildFromTemplate() throws Exception {
PodTemplate template = new PodTemplate();

List<PodVolume> volumes = new ArrayList<PodVolume>();
volumes.add(new HostPathVolume("/host/data", "/container/data"));
volumes.add(new EmptyDirVolume("/empty/dir", false));
template.setVolumes(volumes);

List<ContainerTemplate> containers = new ArrayList<ContainerTemplate>();
ContainerTemplate busyboxContainer = new ContainerTemplate("busybox", "busybox");
busyboxContainer.setCommand("cat");
busyboxContainer.setTtyEnabled(true);
List<TemplateEnvVar> envVars = new ArrayList<TemplateEnvVar>();
envVars.add(new KeyValueEnvVar("CONTAINER_ENV_VAR", "container-env-var-value"));
busyboxContainer.setEnvVars(envVars);
containers.add(busyboxContainer);
template.setContainers(containers);

setupStubs();
Pod pod = new PodTemplateBuilder(template).withSlave(slave).build();
pod.getMetadata().setLabels(ImmutableMap.of("some-label","some-label-value"));
validatePod(pod, false);
}

private void setupStubs() {
doReturn(JENKINS_URL).when(cloud).getJenkinsUrlOrDie();
when(computer.getName()).thenReturn(AGENT_NAME);
Expand All @@ -126,6 +156,10 @@ private void setupStubs() {
}

private void validatePod(Pod pod) {
validatePod(pod, true);
}

private void validatePod(Pod pod, boolean fromYaml) {
assertThat(pod.getMetadata().getLabels(), hasEntry("some-label", "some-label-value"));

// check containers
Expand All @@ -141,13 +175,25 @@ private void validatePod(Pod pod) {
.collect(Collectors.toMap(Volume::getName, Function.identity()));
assertEquals(3, volumes.size());
assertNotNull(volumes.get("workspace-volume"));
assertNotNull(volumes.get("empty-volume"));
assertNotNull(volumes.get("host-volume"));
if (fromYaml) {
assertNotNull(volumes.get("empty-volume"));
assertNotNull(volumes.get("host-volume"));
} else {
assertNotNull(volumes.get("volume-0"));
assertNotNull(volumes.get("volume-1"));
}

List<VolumeMount> mounts = containers.get("busybox").getVolumeMounts();
assertEquals(2, mounts.size());
assertEquals(new VolumeMount("/container/data", "host-volume", null, null), mounts.get(0));
assertEquals(new VolumeMount("/home/jenkins", "workspace-volume", false, null), mounts.get(1));
if (fromYaml) {
assertEquals(2, mounts.size());
assertEquals(new VolumeMount("/container/data", "host-volume", null, null), mounts.get(0));
assertEquals(new VolumeMount("/home/jenkins", "workspace-volume", false, null), mounts.get(1));
} else {
assertEquals(3, mounts.size());
assertEquals(new VolumeMount("/container/data", "volume-0", null, null), mounts.get(0));
assertEquals(new VolumeMount("/empty/dir", "volume-1", null, null), mounts.get(1));
assertEquals(new VolumeMount("/home/jenkins", "workspace-volume", false, null), mounts.get(2));
}

validateJnlpContainer(containers.get("jnlp"), slave);
}
Expand Down