Skip to content

Commit

Permalink
Merge pull request #108 from yrodiere/fix-infinite-wait-on-invalid-label
Browse files Browse the repository at this point in the history
Fix infinite wait on invalid label
  • Loading branch information
TobiX committed Oct 1, 2019
2 parents 23bb495 + 3b90b05 commit 1eefe86
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ public void validate() throws Exception {

/**
* Label and resource are mutual exclusive.
* The label, if provided, must be configured (at least one resource must have this label).
*/
public static void validate(String resource, String label, int quantity) throws Exception {
if (label != null && !label.isEmpty() && resource != null && !resource.isEmpty()) {
throw new IllegalArgumentException("Label and resource name cannot be specified simultaneously.");
}
if (label != null && !LockableResourcesManager.get().isValidLabel( label ) ) {
throw new IllegalArgumentException("The label does not exist: " + label);
}
}

private static final long serialVersionUID = 1L;
Expand All @@ -108,6 +112,9 @@ public static FormValidation doCheckLabel(@QueryParameter String value, @QueryPa
if ((resourceLabel == null) && (resourceName == null)) {
return FormValidation.error("Either label or resource name must be specified.");
}
if (resourceLabel != null && !LockableResourcesManager.get().isValidLabel(resourceLabel)) {
return FormValidation.error("The label does not exist: " + resourceLabel);
}
return FormValidation.ok();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,21 @@ public void lockWithLabelAndLabeledResourceQuantity() throws Exception {
assertNotNull(LockableResourcesManager.get().fromName("resource3"));
assertNotNull(LockableResourcesManager.get().fromName("resource4"));
}

@Test
public void lockWithInvalidLabel() throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource1", "label1");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(
new CpsFlowDefinition(
"lock(label: 'invalidLabel', variable: 'var', quantity: 1) {\n" +
" echo \"Resource locked: ${env.var}\"\n" +
"}\n" +
"echo 'Finish'"));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.FAILURE, b1);
j.assertLogContains("The label does not exist: invalidLabel", b1);
isPaused(b1, 0, 0);
}
}

0 comments on commit 1eefe86

Please sign in to comment.