Search in sources :

Example 6 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method testBuildWithDynamicPVCWorkspaceVolume.

@Test
public void testBuildWithDynamicPVCWorkspaceVolume() {
    PodTemplate template = new PodTemplate();
    template.setWorkspaceVolume(new DynamicPVCWorkspaceVolume());
    ContainerTemplate containerTemplate = new ContainerTemplate("name", "image");
    containerTemplate.setWorkingDir("");
    template.getContainers().add(containerTemplate);
    setupStubs();
    Pod pod = new PodTemplateBuilder(template, slave).build();
    List<Container> containers = pod.getSpec().getContainers();
    assertEquals(2, containers.size());
    Container container0 = containers.get(0);
    Container container1 = containers.get(1);
    List<VolumeMount> volumeMounts = Collections.singletonList(new VolumeMountBuilder().withMountPath("/home/jenkins/agent").withName("workspace-volume").withReadOnly(false).build());
    assertEquals(volumeMounts, container0.getVolumeMounts());
    assertEquals(volumeMounts, container1.getVolumeMounts());
    assertNotNull(pod.getSpec().getVolumes().get(0).getPersistentVolumeClaim());
}
Also used : DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) Test(org.junit.Test)

Example 7 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method yamlOverrideContainer.

@Test
public void yamlOverrideContainer() throws Exception {
    PodTemplate parent = new PodTemplate();
    parent.setYaml("apiVersion: v1\n" + "kind: Pod\n" + "metadata:\n" + "  labels:\n" + "    some-label: some-label-value\n" + "spec:\n" + "  containers:\n" + "  - name: container\n" + "    image: busybox\n" + "    command:\n" + "    - cat\n" + "    tty: true\n");
    PodTemplate child = new PodTemplate();
    child.setYaml("spec:\n" + "  containers:\n" + "  - name: container\n" + "    image: busybox2\n" + "    command:\n" + "    - cat\n" + "    tty: true\n");
    child.setInheritFrom("parent");
    child.setYamlMergeStrategy(merge());
    setupStubs();
    PodTemplate result = combine(parent, child);
    Pod pod = new PodTemplateBuilder(result, slave).build();
    // inherit from parent
    assertEquals("some-label-value", pod.getMetadata().getLabels().get("some-label"));
    assertThat(pod.getSpec().getContainers(), hasSize(2));
    Optional<Container> container = pod.getSpec().getContainers().stream().filter(c -> "container".equals(c.getName())).findFirst();
    assertTrue(container.isPresent());
    assertEquals("busybox2", container.get().getImage());
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) Arrays(java.util.Arrays) Issue(org.jvnet.hudson.test.Issue) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) YamlMergeStrategy(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.YamlMergeStrategy) KeyValueEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar) FlagRule(org.jvnet.hudson.test.FlagRule) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) Map(java.util.Map) Spy(org.mockito.Spy) JUnitParamsRunner(junitparams.JUnitParamsRunner) PodTemplateUtils(org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils) Jenkins(jenkins.model.Jenkins) WithoutJenkins(org.jvnet.hudson.test.WithoutJenkins) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Optional(java.util.Optional) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) Parameters(junitparams.Parameters) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Merge(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Merge) LoggerRule(org.jvnet.hudson.test.LoggerRule) EmptyDirWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume) MockitoJUnit(org.mockito.junit.MockitoJUnit) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestCaseName(junitparams.naming.TestCaseName) Before(org.junit.Before) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOUtils(org.apache.commons.compress.utils.IOUtils) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Test(org.junit.Test) Mockito(org.mockito.Mockito) TemplateEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar) Collectors.toList(java.util.stream.Collectors.toList) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Matcher(org.hamcrest.Matcher) MockitoRule(org.mockito.junit.MockitoRule) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test)

Example 8 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method testInheritsFromWithYaml.

/**
 * This is counter intuitive, the yaml contents are ignored because the parent fields are merged first with the
 * child ones. Then the fields override what is defined in the yaml, so in effect the parent resource limits and
 * requests are used.
 */
@Test
@TestCaseName("{method}(directConnection={0})")
@Parameters({ "true", "false" })
public void testInheritsFromWithYaml(boolean directConnection) throws Exception {
    cloud.setDirectConnection(directConnection);
    PodTemplate parent = new PodTemplate();
    ContainerTemplate container1 = new ContainerTemplate("jnlp", "image1");
    container1.setResourceLimitCpu("1");
    container1.setResourceLimitMemory("1Gi");
    container1.setResourceRequestCpu("100m");
    container1.setResourceRequestMemory("156Mi");
    container1.setRunAsUser("1000");
    container1.setRunAsGroup("2000");
    parent.setContainers(Arrays.asList(container1));
    PodTemplate template = new PodTemplate();
    template.setYaml(loadYamlFile("pod-overrides.yaml"));
    template.setInheritFrom("parent");
    setupStubs();
    PodTemplate result = combine(parent, template);
    Pod pod = new PodTemplateBuilder(result, slave).build();
    Map<String, Container> containers = toContainerMap(pod);
    assertEquals(1, containers.size());
    Container jnlp = containers.get("jnlp");
    PodTemplateUtilsTest.assertQuantity("1", jnlp.getResources().getLimits().get("cpu"));
    PodTemplateUtilsTest.assertQuantity("1Gi", jnlp.getResources().getLimits().get("memory"));
    PodTemplateUtilsTest.assertQuantity("100m", jnlp.getResources().getRequests().get("cpu"));
    PodTemplateUtilsTest.assertQuantity("156Mi", jnlp.getResources().getRequests().get("memory"));
    assertEquals(Long.valueOf(1000L), jnlp.getSecurityContext().getRunAsUser());
    assertEquals(Long.valueOf(2000L), jnlp.getSecurityContext().getRunAsGroup());
    validateContainers(pod, slave, directConnection);
}
Also used : PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) Parameters(junitparams.Parameters) Test(org.junit.Test) TestCaseName(junitparams.naming.TestCaseName)

Example 9 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method yamlOverrideHostNetwork.

@Test
public void yamlOverrideHostNetwork() {
    PodTemplate parent = new PodTemplate();
    parent.setYaml("apiVersion: v1\n" + "kind: Pod\n" + "metadata:\n" + "  labels:\n" + "    some-label: some-label-value\n" + "spec:\n" + "  hostNetwork: false\n" + "  containers:\n" + "  - name: container\n" + "    securityContext:\n" + "      runAsUser: 1000\n" + "      runAsGroup: 1000\n" + "    image: busybox\n" + "    command:\n" + "    - cat\n" + "    tty: true\n");
    PodTemplate child = new PodTemplate();
    child.setYaml("spec:\n" + "  hostNetwork: true\n" + "  containers:\n" + "  - name: container\n" + "    image: busybox2\n" + "    securityContext:\n" + "      runAsUser: 2000\n" + "      runAsGroup: 2000\n" + "    command:\n" + "    - cat\n" + "    tty: true\n");
    child.setInheritFrom("parent");
    child.setYamlMergeStrategy(merge());
    PodTemplate result = combine(parent, child);
    Pod pod = new PodTemplateBuilder(result, slave).build();
    assertTrue(pod.getSpec().getHostNetwork());
}
Also used : PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test)

Example 10 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method testBuildJnlpFromYamlWithNullEnv.

@Test
public void testBuildJnlpFromYamlWithNullEnv() throws Exception {
    PodTemplate template = new PodTemplate();
    template.setYaml(loadYamlFile("pod-jnlp-nullenv.yaml"));
    Pod pod = new PodTemplateBuilder(template, slave).build();
    Optional<Container> jnlp = pod.getSpec().getContainers().stream().filter(c -> KubernetesCloud.JNLP_NAME.equals(c.getName())).findFirst();
    assertThat("jnlp container is present", jnlp.isPresent(), is(true));
    assertThat(jnlp.get().getEnv(), hasSize(greaterThan(0)));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) Arrays(java.util.Arrays) Issue(org.jvnet.hudson.test.Issue) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) YamlMergeStrategy(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.YamlMergeStrategy) KeyValueEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar) FlagRule(org.jvnet.hudson.test.FlagRule) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) Map(java.util.Map) Spy(org.mockito.Spy) JUnitParamsRunner(junitparams.JUnitParamsRunner) PodTemplateUtils(org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils) Jenkins(jenkins.model.Jenkins) WithoutJenkins(org.jvnet.hudson.test.WithoutJenkins) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Optional(java.util.Optional) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) Parameters(junitparams.Parameters) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Merge(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Merge) LoggerRule(org.jvnet.hudson.test.LoggerRule) EmptyDirWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume) MockitoJUnit(org.mockito.junit.MockitoJUnit) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestCaseName(junitparams.naming.TestCaseName) Before(org.junit.Before) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOUtils(org.apache.commons.compress.utils.IOUtils) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Test(org.junit.Test) Mockito(org.mockito.Mockito) TemplateEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar) Collectors.toList(java.util.stream.Collectors.toList) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Matcher(org.hamcrest.Matcher) MockitoRule(org.mockito.junit.MockitoRule) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)21 PodTemplateBuilder (org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder)21 Test (org.junit.Test)21 Container (io.fabric8.kubernetes.api.model.Container)16 Parameters (junitparams.Parameters)13 TestCaseName (junitparams.naming.TestCaseName)13 Quantity (io.fabric8.kubernetes.api.model.Quantity)8 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)8 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)8 KeyValueEnvVar (org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar)8 TemplateEnvVar (org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar)8 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)7 PodSecurityContext (io.fabric8.kubernetes.api.model.PodSecurityContext)7 ResourceRequirements (io.fabric8.kubernetes.api.model.ResourceRequirements)7 ArrayList (java.util.ArrayList)7 EmptyDirVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume)7 HostPathVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume)7 PodVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume)7 DynamicPVCWorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume)7 EmptyDirWorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume)7