Skip to content

Commit

Permalink
Add more logging to debug issue in runInPod test
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossg committed Sep 4, 2018
1 parent a53a07c commit dd90b8c
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
Expand All @@ -46,6 +48,7 @@
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.api.model.PodListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;

/**
Expand All @@ -66,24 +69,28 @@ public void runInPod() throws Exception {
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("runInPod.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
List<PodTemplate> templates = cloud.getAllTemplates();

PodTemplate template = null;
while ((template = podTemplateWithLabel("runInPod", templates)) == null) {
List<PodTemplate> templates = null;
while ((templates = podTemplatesWithLabel("runInPod", cloud.getAllTemplates())).isEmpty()) {
LOGGER.log(Level.INFO, "Waiting for runInPod template to be created");
templates = cloud.getAllTemplates();
Thread.sleep(1000);
}
LOGGER.log(Level.INFO, "Found templates with label runInPod: {0}", templates);
for (PodTemplate template : cloud.getAllTemplates()) {
LOGGER.log(Level.INFO, "Cloud template \"{0}\" labels: {1}",
new Object[] { template.getName(), template.getLabelsMap() });
}

Map<String, String> labels = getLabels(cloud, this);
LOGGER.log(Level.INFO, "Waiting for pods to be created with labels: {0}", labels);
PodList pods = cloud.connect().pods().withLabels(labels).list();
PodList pods = new PodListBuilder().withItems(Collections.emptyList()).build();
while (pods.getItems().isEmpty()) {
LOGGER.log(Level.INFO, "Waiting for pods to be created with labels: {0}", labels);
pods = cloud.connect().pods().withLabels(labels).list();
Thread.sleep(1000);
}

assertThat(templates, hasSize(1));
PodTemplate template = templates.get(0);
assertEquals(Integer.MAX_VALUE, template.getInstanceCap());
assertThat(template.getLabelsMap(), hasEntry("jenkins/runInPod", "true"));

Expand All @@ -99,9 +106,8 @@ public void runInPod() throws Exception {
deletePods(cloud.connect(), getLabels(cloud, this), true));
}

private PodTemplate podTemplateWithLabel(String label, List<PodTemplate> templates) {
return templates != null ? templates.stream().filter(t -> label.equals(t.getLabel())).findFirst().orElse(null)
: null;
private List<PodTemplate> podTemplatesWithLabel(String label, List<PodTemplate> templates) {
return templates.stream().filter(t -> label.equals(t.getLabel())).collect(Collectors.toList());
}

@Test
Expand Down

0 comments on commit dd90b8c

Please sign in to comment.