Java Code Examples for org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep#success()
The following examples show how to use
org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep#success() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultsBinderTest.java From pipeline-multibranch-defaults-plugin with MIT License | 6 votes |
@Test public void testDefaultJenkinsFileLoadFromWorkspace() throws Exception { GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(GlobalConfigFiles.class).get(GlobalConfigFiles.class); ConfigFileStore store = globalConfigFiles.get(); Config config = new GroovyScript("Jenkinsfile", "Jenkinsfile", "", "semaphore 'wait'; node {checkout scm; load 'Jenkinsfile'}"); store.save(config); sampleGitRepo.init(); sampleGitRepo.write("Jenkinsfile", "echo readFile('file')"); sampleGitRepo.git("add", "Jenkinsfile"); sampleGitRepo.write("file", "initial content"); sampleGitRepo.git("commit", "--all", "--message=flow"); WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p"); mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0]))); WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "master"); SemaphoreStep.waitForStart("wait/1", null); WorkflowRun b1 = p.getLastBuild(); assertNotNull(b1); assertEquals(1, b1.getNumber()); SemaphoreStep.success("wait/1", null); r.assertLogContains("initial content", r.waitForCompletion(b1)); }
Example 2
Source File: DefaultsBinderTest.java From pipeline-multibranch-defaults-plugin with MIT License | 6 votes |
@Test public void testDefaultJenkinsFile() throws Exception { GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(GlobalConfigFiles.class).get(GlobalConfigFiles.class); ConfigFileStore store = globalConfigFiles.get(); Config config = new GroovyScript("Jenkinsfile", "Jenkinsfile", "", "semaphore 'wait'; node {checkout scm; echo readFile('file')}"); store.save(config); sampleGitRepo.init(); sampleGitRepo.write("file", "initial content"); sampleGitRepo.git("commit", "--all", "--message=flow"); WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p"); mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0]))); WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "master"); SemaphoreStep.waitForStart("wait/1", null); WorkflowRun b1 = p.getLastBuild(); assertNotNull(b1); assertEquals(1, b1.getNumber()); SemaphoreStep.success("wait/1", null); r.assertLogContains("initial content", r.waitForCompletion(b1)); }
Example 3
Source File: KubernetesPipelineTest.java From kubernetes-plugin with Apache License 2.0 | 6 votes |
@Test public void computerCantBeConfigured() throws Exception { r.jenkins.setSecurityRealm(r.createDummySecurityRealm()); r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy(). grant(Jenkins.ADMINISTER).everywhere().to("admin")); SemaphoreStep.waitForStart("pod/1", b); Optional<KubernetesSlave> optionalNode = r.jenkins.getNodes().stream().filter(KubernetesSlave.class::isInstance).map(KubernetesSlave.class::cast).findAny(); assertTrue(optionalNode.isPresent()); KubernetesSlave node = optionalNode.get(); JenkinsRule.WebClient wc = r.createWebClient().login("admin"); wc.getOptions().setPrintContentOnFailingStatusCode(false); HtmlPage nodeIndex = wc.getPage(node); assertNotXPath(nodeIndex, "//*[text() = 'configure']"); wc.assertFails(node.toComputer().getUrl()+"configure", 403); SemaphoreStep.success("pod/1", null); }
Example 4
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 6 votes |
private void waitAndClear(int semaphoreIndex, List<WorkflowRun> nextRuns) throws Exception { WorkflowRun toClear = nextRuns.get(0); System.err.println("Waiting for semaphore to start for " + toClear.getNumber()); SemaphoreStep.waitForStart("wait-inside-2/" + semaphoreIndex, toClear); List<WorkflowRun> remainingRuns = new ArrayList<>(); if (nextRuns.size() > 1) { remainingRuns.addAll(nextRuns.subList(1, nextRuns.size())); for (WorkflowRun r : remainingRuns) { System.err.println("Verifying no semaphore yet for " + r.getNumber()); j.assertLogNotContains("Entering semaphore now", r); } } SemaphoreStep.success("wait-inside-2/" + semaphoreIndex, null); System.err.println("Waiting for " + toClear.getNumber() + " to complete"); j.assertBuildStatusSuccess(j.waitForCompletion(toClear)); if (!remainingRuns.isEmpty()) { waitAndClear(semaphoreIndex + 1, remainingRuns); } }
Example 5
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 5 votes |
@Issue("JENKINS-34433") @Test public void manualUnreserveUnblocksJob() throws Exception { LockableResourcesManager.get().createResource("resource1"); JenkinsRule.WebClient wc = j.createWebClient(); wc.goTo("lockable-resources/reserve?resource=resource1"); LockableResource resource1 = LockableResourcesManager.get().fromName("resource1"); assertNotNull(resource1); resource1.setReservedBy("someone"); assertTrue(resource1.isReserved()); JSONObject apiRes = TestHelpers.getResourceFromApi(j, "resource1", false); assertThat(apiRes, hasEntry("reserved", true)); assertThat(apiRes, hasEntry("reservedBy", "someone")); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "retry(99) {\n" + " lock('resource1') {\n" + " semaphore('wait-inside')\n" + " }\n" + "}", true)); WorkflowRun r = p.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is locked, waiting...", r); wc.goTo("lockable-resources/unreserve?resource=resource1"); SemaphoreStep.waitForStart("wait-inside/1", r); SemaphoreStep.success("wait-inside/1", null); j.assertBuildStatusSuccess(j.waitForCompletion(r)); }
Example 6
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 5 votes |
@Issue("JENKINS-40879") @Test public void parallelLockRelease() throws Exception { LockableResourcesManager.get().createResource("resource1"); LockableResourcesManager.get().createResource("resource2"); WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "j"); job.setDefinition( new CpsFlowDefinition( "lock(resource: 'resource1') {\n" + " semaphore 'wait-inside-1'\n" + "}\n" + "lock(resource: 'resource2') { \n" + " echo 'Entering semaphore now'\n" + " semaphore 'wait-inside-2'\n" + "}\n", true)); List<WorkflowRun> nextRuns = new ArrayList<>(); WorkflowRun toUnlock = null; for (int i = 0; i < 5; i++) { WorkflowRun rNext = job.scheduleBuild2(0).waitForStart(); if (toUnlock != null) { j.waitForMessage( "[resource1] is locked by " + toUnlock.getFullDisplayName() + ", waiting...", rNext); isPaused(rNext, 1, 1); SemaphoreStep.success("wait-inside-1/" + i, null); } SemaphoreStep.waitForStart("wait-inside-1/" + (i + 1), rNext); isPaused(rNext, 1, 0); nextRuns.add(rNext); toUnlock = rNext; } SemaphoreStep.success("wait-inside-1/" + nextRuns.size(), null); waitAndClear(1, nextRuns); }
Example 7
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 5 votes |
@Test public void parallelLock() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "parallel a: {\n" + " semaphore 'before-a'\n" + " lock('resource1') {\n" + " semaphore 'inside-a'\n" + " }\n" + "}, b: {\n" + " lock('resource1') {\n" + " semaphore 'wait-b'\n" + " }\n" + "}\n", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-b/1", b1); SemaphoreStep.waitForStart("before-a/1", b1); // both messages are in the log because branch b acquired the lock and branch a is waiting to // lock j.waitForMessage("Lock acquired on [resource1]", b1); SemaphoreStep.success("before-a/1", null); j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b1); isPaused(b1, 2, 1); SemaphoreStep.success("wait-b/1", null); j.waitForMessage("Lock acquired on [resource1]", b1); SemaphoreStep.waitForStart("inside-a/1", b1); isPaused(b1, 2, 0); SemaphoreStep.success("inside-a/1", null); j.waitForCompletion(b1); assertNull(LockableResourcesManager.get().fromName("resource1")); }
Example 8
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 5 votes |
@Test public void lockOrder() throws Exception { LockableResourcesManager.get().createResource("resource1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock('resource1') {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowRun b2 = p.scheduleBuild2(0).waitForStart(); // Ensure that b2 reaches the lock before b3 j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2); isPaused(b2, 1, 1); WorkflowRun b3 = p.scheduleBuild2(0).waitForStart(); // Both 2 and 3 are waiting for locking resource1 j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3); isPaused(b3, 1, 1); // Unlock resource1 SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [resource1]", b1); isPaused(b1, 1, 0); // #2 gets the lock before #3 (in the order as they requested the lock) j.waitForMessage("Lock acquired on [resource1]", b2); SemaphoreStep.success("wait-inside/2", null); isPaused(b2, 1, 0); j.waitForMessage("Lock acquired on [resource1]", b3); SemaphoreStep.success("wait-inside/3", null); j.waitForMessage("Finish", b3); isPaused(b3, 1, 0); }
Example 9
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 5 votes |
@Test @Issue("JENKINS-50176") public void lockWithLabelFillsVariable() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', variable: 'someVar') {\n" + " semaphore 'wait-inside'\n" + " echo \"VAR IS $env.someVar\"\n" + "}", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', variable: 'someVar2') {\n" + " echo \"VAR2 IS $env.someVar2\"\n" + "}", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); j.waitForMessage("is locked, waiting...", b2); isPaused(b2, 1, 1); // Unlock resources SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource", b1); isPaused(b1, 1, 0); // Now job 2 should get and release the lock... j.waitForCompletion(b1); j.waitForCompletion(b2); isPaused(b2, 1, 0); // Variable should have been filled in both cases j.assertLogContains("VAR IS resource1", b1); j.assertLogContains("VAR2 IS resource1", b2); }
Example 10
Source File: KubernetesPipelineTest.java From kubernetes-plugin with Apache License 2.0 | 5 votes |
@Test @Issue("JENKINS-58602") public void jenkinsSecretHidden() throws Exception { SemaphoreStep.waitForStart("pod/1", b); Optional<SlaveComputer> scOptional = Arrays.stream(r.jenkins.getComputers()) .filter(SlaveComputer.class::isInstance) .map(SlaveComputer.class::cast) .findAny(); assertTrue(scOptional.isPresent()); String jnlpMac = scOptional.get().getJnlpMac(); SemaphoreStep.success("pod/1", b); r.assertBuildStatusSuccess(r.waitForCompletion(b)); r.assertLogNotContains(jnlpMac, b); }
Example 11
Source File: LockStepHardKillTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Issue("JENKINS-36479") @Test public void hardKillNewBuildClearsLock() throws Exception { LockableResourcesManager.get().createResource("resource1"); WorkflowJob p1 = j.jenkins.createProject(WorkflowJob.class, "p"); p1.setDefinition( new CpsFlowDefinition( "lock('resource1') { echo 'locked!'; semaphore 'wait-inside' }", true)); WorkflowRun b1 = p1.scheduleBuild2(0).waitForStart(); j.waitForMessage("locked!", b1); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition("lock('resource1') {\n" + " semaphore 'wait-inside'\n" + "}", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); // Make sure that b2 is blocked on b1's lock. j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2); isPaused(b2, 1, 1); // Now b2 is still sitting waiting for a lock. Create b3 and launch it to clear the // lock. WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition("lock('resource1') {\n" + " semaphore 'wait-inside'\n" + "}", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3); isPaused(b3, 1, 1); // Kill b1 hard. b1.doKill(); j.waitForMessage("Hard kill!", b1); j.waitForCompletion(b1); j.assertBuildStatus(Result.ABORTED, b1); // Verify that b2 gets the lock. j.waitForMessage("Lock acquired on [resource1]", b2); SemaphoreStep.success("wait-inside/2", b2); // Verify that b2 releases the lock and finishes successfully. j.waitForMessage("Lock released on resource [resource1]", b2); j.assertBuildStatusSuccess(j.waitForCompletion(b2)); isPaused(b2, 1, 0); // Now b3 should get the lock and do its thing. j.waitForMessage("Lock acquired on [resource1]", b3); SemaphoreStep.success("wait-inside/3", b3); j.assertBuildStatusSuccess(j.waitForCompletion(b3)); isPaused(b3, 1, 0); }
Example 12
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockOrderLabel() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource2", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource3", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', quantity: 2) {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowRun b2 = p.scheduleBuild2(0).waitForStart(); // Ensure that b2 reaches the lock before b3 j.waitForMessage("[Label: label1, Quantity: 2] is locked, waiting...", b2); j.waitForMessage("Found 1 available resource(s). Waiting for correct amount: 2.", b2); isPaused(b2, 1, 1); WorkflowRun b3 = p.scheduleBuild2(0).waitForStart(); // Both 2 and 3 are waiting for locking Label: label1, Quantity: 2 j.waitForMessage("[Label: label1, Quantity: 2] is locked, waiting...", b3); j.waitForMessage("Found 1 available resource(s). Waiting for correct amount: 2.", b3); isPaused(b3, 1, 1); // Unlock Label: label1, Quantity: 2 SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [Label: label1, Quantity: 2]", b1); isPaused(b1, 1, 0); // #2 gets the lock before #3 (in the order as they requested the lock) j.waitForMessage("Lock acquired on [Label: label1, Quantity: 2]", b2); SemaphoreStep.success("wait-inside/2", null); j.waitForMessage("Finish", b2); isPaused(b2, 1, 0); j.waitForMessage("Lock acquired on [Label: label1, Quantity: 2]", b3); SemaphoreStep.success("wait-inside/3", null); j.waitForMessage("Finish", b3); isPaused(b3, 1, 0); assertNotNull(LockableResourcesManager.get().fromName("resource1")); assertNotNull(LockableResourcesManager.get().fromName("resource2")); assertNotNull(LockableResourcesManager.get().fromName("resource3")); }
Example 13
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockOrderLabelQuantity() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource2", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource3", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', quantity: 2) {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowRun b2 = p.scheduleBuild2(0).waitForStart(); // Ensure that b2 reaches the lock before b3 j.waitForMessage("[Label: label1, Quantity: 2] is locked, waiting...", b2); j.waitForMessage("Found 1 available resource(s). Waiting for correct amount: 2.", b2); isPaused(b2, 1, 1); WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', quantity: 1) {\n" + " semaphore 'wait-inside-quantity1'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); // While 2 continues waiting, 3 can continue directly SemaphoreStep.waitForStart("wait-inside-quantity1/1", b3); // Let 3 finish SemaphoreStep.success("wait-inside-quantity1/1", null); j.waitForMessage("Finish", b3); isPaused(b3, 1, 0); // Unlock Label: label1, Quantity: 2 SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [Label: label1, Quantity: 2]", b1); isPaused(b1, 1, 0); // #2 gets the lock before #3 (in the order as they requested the lock) j.waitForMessage("Lock acquired on [Label: label1, Quantity: 2]", b2); SemaphoreStep.success("wait-inside/2", null); j.waitForMessage("Finish", b2); isPaused(b2, 1, 0); assertNotNull(LockableResourcesManager.get().fromName("resource1")); assertNotNull(LockableResourcesManager.get().fromName("resource2")); assertNotNull(LockableResourcesManager.get().fromName("resource3")); }
Example 14
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockOrderLabelQuantityFreedResources() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource2", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource3", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(label: 'label1') {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); JSONObject apiRes = TestHelpers.getResourceFromApi(j, "resource1", true); assertThat(apiRes, hasEntry("labels", "label1")); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', quantity: 2) {\n" + " semaphore 'wait-inside-quantity2'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); // Ensure that b2 reaches the lock before b3 j.waitForMessage("[Label: label1, Quantity: 2] is locked, waiting...", b2); j.waitForMessage("Found 0 available resource(s). Waiting for correct amount: 2.", b2); isPaused(b2, 1, 1); WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', quantity: 1) {\n" + " semaphore 'wait-inside-quantity1'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); j.waitForMessage("[Label: label1, Quantity: 1] is locked, waiting...", b3); j.waitForMessage("Found 0 available resource(s). Waiting for correct amount: 1.", b3); isPaused(b3, 1, 1); // Unlock Label: label1 SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [Label: label1]", b1); isPaused(b1, 1, 0); // Both get their lock j.waitForMessage("Lock acquired on [Label: label1, Quantity: 2]", b2); j.waitForMessage("Lock acquired on [Label: label1, Quantity: 1]", b3); SemaphoreStep.success("wait-inside-quantity2/1", null); SemaphoreStep.success("wait-inside-quantity1/1", null); j.waitForMessage("Finish", b2); j.waitForMessage("Finish", b3); isPaused(b2, 1, 0); isPaused(b3, 1, 0); assertNotNull(LockableResourcesManager.get().fromName("resource1")); assertNotNull(LockableResourcesManager.get().fromName("resource2")); assertNotNull(LockableResourcesManager.get().fromName("resource3")); }
Example 15
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test @Issue("JENKINS-50176") public void parallelLockWithLabelFillsVariable() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "parallel p1: {\n" + " lock(label: 'label1', variable: 'someVar') {\n" + " semaphore 'wait-inside'\n" + " echo \"VAR IS $env.someVar\"\n" + " }\n" + "},\n" + "p2: {\n" + " semaphore 'wait-outside'\n" + " lock(label: 'label1', variable: 'someVar2') {\n" + " echo \"VAR2 IS $env.someVar2\"\n" + " }\n" + "}", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-outside/1", b1); SemaphoreStep.waitForStart("wait-inside/1", b1); SemaphoreStep.success("wait-outside/1", null); j.waitForMessage("is locked, waiting...", b1); isPaused(b1, 2, 1); // Unlock resources SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource", b1); isPaused(b1, 2, 0); // Now the second parallel branch should get and release the lock... j.waitForCompletion(b1); isPaused(b1, 2, 0); // Variable should have been filled in both cases j.assertLogContains("VAR IS resource1", b1); j.assertLogContains("VAR2 IS resource1", b1); }
Example 16
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockWithLabelAndLabeledResourceQuantity() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource2", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource3", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource4", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(variable: 'var', extra: [[resource: 'resource4'], [resource: 'resource2'], [label: 'label1', quantity: 2]]) {\n" + " def lockedResources = env.var.split(',').sort()\n" + " echo \"Resources locked: ${lockedResources}\"\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); // #1 should lock as few resources as possible WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', variable: 'var', quantity: 3) {\n" + " def lockedResources = env.var.split(',').sort()\n" + " echo \"Resources locked: ${lockedResources}\"\n" + " semaphore 'wait-inside-quantity3'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); j.waitForMessage("[Label: label1, Quantity: 3] is locked, waiting...", b2); j.waitForMessage("Found 2 available resource(s). Waiting for correct amount: 3.", b2); isPaused(b2, 1, 1); WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', variable: 'var', quantity: 2) {\n" + " def lockedResources = env.var.split(',').sort()\n" + " echo \"Resources locked: ${lockedResources}\"\n" + " semaphore 'wait-inside-quantity2'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); // While 2 continues waiting, 3 can continue directly SemaphoreStep.waitForStart("wait-inside-quantity2/1", b3); // Let 3 finish SemaphoreStep.success("wait-inside-quantity2/1", null); j.waitForMessage("Finish", b3); j.assertLogContains("Resources locked: [resource1, resource3]", b3); isPaused(b3, 1, 0); // Unlock resources SemaphoreStep.success("wait-inside/1", null); j.waitForMessage( "Lock released on resource [{resource4},{resource2},{Label: label1, Quantity: 2}]", b1); j.assertLogContains("Resources locked: [resource2, resource4]", b1); isPaused(b1, 1, 0); // #2 gets the lock j.waitForMessage("Lock acquired on [Label: label1, Quantity: 3]", b2); SemaphoreStep.success("wait-inside-quantity3/1", null); j.waitForMessage("Finish", b2); // Could be any 3 resources, so just check the beginning of the message j.assertLogContains("Resources locked: [resource", b2); isPaused(b2, 1, 0); assertNotNull(LockableResourcesManager.get().fromName("resource1")); assertNotNull(LockableResourcesManager.get().fromName("resource2")); assertNotNull(LockableResourcesManager.get().fromName("resource3")); assertNotNull(LockableResourcesManager.get().fromName("resource4")); }
Example 17
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Issue("JENKINS-36479") @Test public void deleteRunningBuildNewBuildClearsLock() throws Exception { assumeFalse(Functions.isWindows()); LockableResourcesManager.get().createResource("resource1"); WorkflowJob p1 = j.jenkins.createProject(WorkflowJob.class, "p"); p1.setDefinition( new CpsFlowDefinition( "lock('resource1') { echo 'locked!'; semaphore 'wait-inside' }", true)); WorkflowRun b1 = p1.scheduleBuild2(0).waitForStart(); j.waitForMessage("locked!", b1); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition("lock('resource1') {\n" + " semaphore 'wait-inside'\n" + "}", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); // Make sure that b2 is blocked on b1's lock. j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2); isPaused(b2, 1, 1); // Now b2 is still sitting waiting for a lock. Create b3 and launch it to verify order of // unlock. WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition("lock('resource1') {\n" + " semaphore 'wait-inside'\n" + "}", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3); isPaused(b3, 1, 1); b1.delete(); // Verify that b2 gets the lock. j.waitForMessage("Lock acquired on [resource1]", b2); SemaphoreStep.success("wait-inside/2", b2); // Verify that b2 releases the lock and finishes successfully. j.waitForMessage("Lock released on resource [resource1]", b2); j.assertBuildStatusSuccess(j.waitForCompletion(b2)); isPaused(b2, 1, 0); // Now b3 should get the lock and do its thing. j.waitForMessage("Lock acquired on [resource1]", b3); SemaphoreStep.success("wait-inside/3", b3); j.assertBuildStatusSuccess(j.waitForCompletion(b3)); isPaused(b3, 1, 0); }
Example 18
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockMultipleResources() throws Exception { LockableResourcesManager.get().createResource("resource1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(resource: 'resource1', extra: [[resource: 'resource2']]) {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition( "lock('resource1') {\n" + " semaphore 'wait-inside-p2'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2); isPaused(b2, 1, 1); WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition( "lock('resource2') {\n" + " semaphore 'wait-inside-p3'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource2] is locked by " + b1.getFullDisplayName() + ", waiting...", b3); isPaused(b3, 1, 1); // Unlock resources SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [{resource1},{resource2}]", b1); isPaused(b1, 1, 0); // Both get their lock j.waitForMessage("Lock acquired on [resource1]", b2); j.waitForMessage("Lock acquired on [resource2]", b3); SemaphoreStep.success("wait-inside-p2/1", null); SemaphoreStep.success("wait-inside-p3/1", null); j.waitForMessage("Finish", b2); j.waitForMessage("Finish", b3); isPaused(b2, 1, 0); isPaused(b3, 1, 0); }
Example 19
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockWithLabelAndResource() throws Exception { LockableResourcesManager.get().createResource("resource1"); LockableResourcesManager.get().createResourceWithLabel("resource2", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource3", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', extra: [[resource: 'resource1']]) {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition( "lock('resource1') {\n" + " semaphore 'wait-inside-p2'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2); isPaused(b2, 1, 1); WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition( "lock(label: 'label1') {\n" + " semaphore 'wait-inside-p3'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); j.waitForMessage("[Label: label1] is locked, waiting...", b3); isPaused(b3, 1, 1); // Unlock resources SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [{Label: label1},{resource1}]", b1); isPaused(b2, 1, 0); // Both get their lock j.waitForMessage("Lock acquired on [resource1]", b2); j.waitForMessage("Lock acquired on [Label: label1]", b3); SemaphoreStep.success("wait-inside-p2/1", null); SemaphoreStep.success("wait-inside-p3/1", null); j.waitForMessage("Finish", b2); j.waitForMessage("Finish", b3); isPaused(b2, 1, 0); isPaused(b3, 1, 0); }
Example 20
Source File: LockStepTest.java From lockable-resources-plugin with MIT License | 4 votes |
@Test public void lockWithLabelAndLabeledResource() throws Exception { LockableResourcesManager.get().createResourceWithLabel("resource1", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource2", "label1"); LockableResourcesManager.get().createResourceWithLabel("resource3", "label1"); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition( new CpsFlowDefinition( "lock(label: 'label1', extra: [[resource: 'resource1']]) {\n" + " semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait-inside/1", b1); WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2"); p2.setDefinition( new CpsFlowDefinition( "lock('resource1') {\n" + " semaphore 'wait-inside-p2'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2); isPaused(b2, 1, 1); WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3"); p3.setDefinition( new CpsFlowDefinition( "lock(label: 'label1') {\n" + " semaphore 'wait-inside-p3'\n" + "}\n" + "echo 'Finish'", true)); WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart(); j.waitForMessage("[Label: label1] is locked, waiting...", b3); isPaused(b3, 1, 1); // Unlock resources SemaphoreStep.success("wait-inside/1", null); j.waitForMessage("Lock released on resource [{Label: label1},{resource1}]", b1); isPaused(b1, 1, 0); // #2 gets the lock before #3 (in the order as they requested the lock) j.waitForMessage("Lock acquired on [resource1]", b2); SemaphoreStep.success("wait-inside-p2/1", null); j.waitForMessage("Finish", b2); isPaused(b2, 1, 0); j.waitForMessage("Lock acquired on [Label: label1]", b3); SemaphoreStep.success("wait-inside-p3/1", null); j.waitForMessage("Finish", b3); isPaused(b3, 1, 0); assertNotNull(LockableResourcesManager.get().fromName("resource1")); assertNotNull(LockableResourcesManager.get().fromName("resource2")); assertNotNull(LockableResourcesManager.get().fromName("resource3")); }