Skip to content

Commit

Permalink
Label dynamic PVC with same labels as pod; Filter down lists to curre…
Browse files Browse the repository at this point in the history
…nt test to ease parallel testing. (#1550)
  • Loading branch information
Vlatombe committed May 14, 2024
1 parent 70f656a commit 9e58a7c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ default PersistentVolumeClaim createPVC(KubernetesClient client, ObjectMeta podM
PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder()
.withNewMetadata()
.withName(pvcName)
.withLabels(podMetaData.getLabels())
.withOwnerReferences(ownerReference)
.endMetadata()
.withNewSpec()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.deletePods;
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.getLabels;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.oneOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -45,6 +47,7 @@
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeNotNull;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Computer;
import hudson.model.Label;
import hudson.model.Result;
Expand All @@ -62,6 +65,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -71,12 +75,12 @@
import org.csanchez.jenkins.plugins.kubernetes.GarbageCollection;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesComputer;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil;
import org.csanchez.jenkins.plugins.kubernetes.MetricNames;
import org.csanchez.jenkins.plugins.kubernetes.PodAnnotation;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.htmlunit.html.DomNodeUtil;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlPage;
Expand Down Expand Up @@ -786,15 +790,38 @@ public void dynamicPVCVolume() throws Exception {
private void dynamicPVC() throws Exception {
assumePvcAccess();
var client = cloud.connect();
var pods = client.pods().list().getItems();
var pvcs = client.persistentVolumeClaims().list().getItems();
SemaphoreStep.waitForStart("before/1", b);
var pods = getPodNames(client);
assertThat(pods, empty());
var pvcs = getPvcNames(client);
SemaphoreStep.success("before/1", null);
SemaphoreStep.waitForStart("pod/1", b);
assertThat(getPodNames(client), hasSize(1));
assertThat(getPvcNames(client), hasSize(1));
SemaphoreStep.success("pod/1", null);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
await("The pods should be the same as before building")
.timeout(Duration.ofMinutes(1))
.until(() -> client.pods().list().getItems(), equalTo(pods));
.until(() -> getPodNames(client), equalTo(pods));
await("The PVCs should be the same as before building")
.timeout(Duration.ofMinutes(1))
.until(() -> client.persistentVolumeClaims().list().getItems(), equalTo(pvcs));
.until(() -> getPvcNames(client), equalTo(pvcs));
}

private @NonNull Set<String> getPvcNames(KubernetesClient client) {
return client.persistentVolumeClaims().withLabels(getTestLabels()).list().getItems().stream()
.map(pvc -> pvc.getMetadata().getName())
.collect(Collectors.toSet());
}

private @NonNull Set<String> getPodNames(KubernetesClient client) {
return client.pods().withLabels(getTestLabels()).list().getItems().stream()
.map(pod -> pod.getMetadata().getName())
.collect(Collectors.toSet());
}

private @NonNull Map<String, String> getTestLabels() {
return KubernetesTestUtil.getLabels(cloud, this, name);
}

private void assumePvcAccess() throws KubernetesAuthException, IOException {
Expand Down Expand Up @@ -846,7 +873,7 @@ private <R extends Run> R assertBuildStatus(R run, Result... status) throws Exce
}
}
String msg = "unexpected build status; build log was:\n------\n" + r.getLog(run) + "\n------\n";
MatcherAssert.assertThat(msg, run.getResult(), Matchers.is(oneOf(status)));
MatcherAssert.assertThat(msg, run.getResult(), is(oneOf(status)));
return run;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
semaphore 'before'
podTemplate(volumes: [dynamicPVC(requestsSize: '10Gi', mountPath: '/tmp/mountPath')], yaml:'''
spec:
securityContext:
fsGroup: 1000
''') {

node(POD_LABEL) {
semaphore 'pod'
container(name: 'jnlp') {
sh 'cat /var/run/secrets/kubernetes.io/serviceaccount/namespace'
git 'https://github.com/jenkinsci/kubernetes-plugin.git'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
semaphore 'before'
podTemplate(workspaceVolume: dynamicPVC(requestsSize: "10Gi"), yaml:'''
spec:
securityContext:
fsGroup: 1000
''') {

node(POD_LABEL) {
semaphore 'pod'
container(name: 'jnlp') {
sh 'cat /var/run/secrets/kubernetes.io/serviceaccount/namespace'
git 'https://github.com/jenkinsci/kubernetes-plugin.git'
Expand Down

0 comments on commit 9e58a7c

Please sign in to comment.