Skip to content

Commit

Permalink
[JENKINS-51137] Merge labels from yaml
Browse files Browse the repository at this point in the history
Add tests for labels
  • Loading branch information
carlossg committed May 23, 2018
1 parent f538240 commit fca584c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public static Pod combine(Pod parent, Pod template) {

Map<String, String> podAnnotations = mergeMaps(parent.getMetadata().getAnnotations(),
template.getMetadata().getAnnotations());
Map<String, String> podLabels = mergeMaps(parent.getMetadata().getLabels(), template.getMetadata().getLabels());

Set<LocalObjectReference> imagePullSecrets = new LinkedHashSet<>();
imagePullSecrets.addAll(parent.getSpec().getImagePullSecrets());
Expand Down Expand Up @@ -217,7 +218,7 @@ public static Pod combine(Pod parent, Pod template) {
// toolLocationNodeProperties.addAll(template.getNodeProperties());

MetadataNested<PodBuilder> metadataBuilder = new PodBuilder().withNewMetadataLike(parent.getMetadata()) //
.withAnnotations(podAnnotations);
.withAnnotations(podAnnotations).withLabels(podLabels);
if (!Strings.isNullOrEmpty(template.getMetadata().getName())) {
metadataBuilder.withName(template.getMetadata().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ private void setupStubs() {
}

private void validatePod(Pod pod) {
assertEquals(ImmutableMap.of("some-label", "some-label-value"), pod.getMetadata().getLabels());
assertThat(pod.getMetadata().getLabels(), hasEntry("some-label", "some-label-value"));
assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave"));

// check containers
Map<String, Container> containers = pod.getSpec().getContainers().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ public void shouldCombineAllAnnotations() {
assertEquals("value3", result.getAnnotations().get(0).getValue().toString());
}

@Test
public void shouldCombineAllLabels() {
Pod pod1 = new PodBuilder().withNewMetadata().withLabels( //
ImmutableMap.of("label1", "pod1", "label2", "pod1") //
).endMetadata().withNewSpec().endSpec().build();
Pod pod2 = new PodBuilder().withNewMetadata().withLabels( //
ImmutableMap.of("label1", "pod2", "label3", "pod2") //
).endMetadata().withNewSpec().endSpec().build();

Map<String, String> labels = combine(pod1, pod2).getMetadata().getLabels();
assertThat(labels, hasEntry("label1", "pod2"));
assertThat(labels, hasEntry("label2", "pod1"));
assertThat(labels, hasEntry("label3", "pod2"));
}

@Test
public void shouldUnwrapParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand All @@ -42,6 +44,8 @@

import hudson.model.Result;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClient;

/**
Expand All @@ -63,25 +67,39 @@ public void runInPod() throws Exception {
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
List<PodTemplate> templates = cloud.getAllTemplates();
while (hasPodTemplateWithLabel("mypod",templates)) {
LOGGER.log(Level.INFO, "Waiting for template to be created");

PodTemplate template = null;
while ((template = podTemplateWithLabel("mypod", templates)) == null) {
LOGGER.log(Level.INFO, "Waiting for mypod template to be created");
templates = cloud.getAllTemplates();
Thread.sleep(1000);
}
assertFalse(templates.isEmpty());
PodTemplate template = templates.get(0);

PodList pods = cloud.connect().pods().list();
while (pods.getItems().isEmpty()) {
LOGGER.log(Level.INFO, "Waiting for pod to be created");
pods = cloud.connect().pods().list();
Thread.sleep(1000);
}

assertEquals(Integer.MAX_VALUE, template.getInstanceCap());
assertThat(template.getLabelsMap(), hasEntry("jenkins", "slave"));
assertThat(template.getLabelsMap(), hasEntry("jenkins/mypod", "true"));

assertEquals(1, pods.getItems().size());
Pod pod = pods.getItems().get(0);
assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave"));
assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins/mypod", "true"));

r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("script file contents: ", b);
assertFalse("There are pods leftover after test execution, see previous logs",
deletePods(cloud.connect(), getLabels(this), true));
}

private boolean hasPodTemplateWithLabel(String label, List<PodTemplate> templates) {
return templates != null
&& templates.stream()
.map(PodTemplate::getLabel)
.anyMatch(label::equals);
private PodTemplate podTemplateWithLabel(String label, List<PodTemplate> templates) {
return templates != null ? templates.stream().filter(t -> label.equals(t.getLabel())).findFirst().orElse(null)
: null;
}

@Test
Expand Down

0 comments on commit fca584c

Please sign in to comment.