@Test public void configRoundTrip() throws Exception { WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "p"); job.setDefinition(new CpsFlowDefinition("echo 'whatever'", false)); jenkins.configRoundtrip(job); }
/** * Tests the ability to execute a step with an unnamed monomorphic list argument. */ @Issue("JENKINS-29711") @Test public void monomorphicList() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "monList"); p.setDefinition(new CpsFlowDefinition("monomorphListStep([[firstArg:'one', secondArg:'two'], [firstArg:'three', secondArg:'four']])", true)); WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); r.assertLogContains("First arg: one, second arg: two", b); r.assertLogContains("First arg: three, second arg: four", b); }
@Issue("JENKINS-29922") @Test public void nonexistentFunctions() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("nonexistent()", true)); WorkflowRun b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0)); r.assertLogContains("nonexistent", b); r.assertLogContains("wrapInCurve", b); r.assertLogContains("polygon", b); }
/** * Performs a configuration round-trip testing for a publisher. */ public <P extends Publisher> P configRoundtrip(P before) throws Exception { FreeStyleProject p = createFreeStyleProject(); p.getPublishersList().add(before); configRoundtrip((Item) p); return (P)p.getPublishersList().get(before.getClass()); }
/** * Performs a configuration round-trip testing for a builder. */ public <B extends Builder> B configRoundtrip(B before) throws Exception { FreeStyleProject p = createFreeStyleProject(); p.getBuildersList().add(before); configRoundtrip((Item)p); return (B)p.getBuildersList().get(before.getClass()); }
@Issue({"JENKINS-34599", "JENKINS-45629"}) @Test public void fieldInitializers() throws Exception { WorkflowJob p = jenkins.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("class X {final String val; X(String _val) {val = _val}}; echo(/hello ${new X('world').val}/)", true)); jenkins.assertLogContains("hello world", jenkins.buildAndAssertSuccess(p)); p.setDefinition(new CpsFlowDefinition("class X {String world = 'world'; String message = 'hello ' + world}; echo(new X().message)", true)); jenkins.assertLogContains("hello world", jenkins.buildAndAssertSuccess(p)); }
@Issue("JENKINS-48379") @Test public void submitterParameterInInput() throws Exception { j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "submitterParameterInInput"); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("submitterParameterInInput"), true)); while (b.getAction(InputAction.class)==null) { e.waitForSuspension(); InputAction a = b.getAction(InputAction.class); assertEquals(1, a.getExecutions().size()); assertEquals("alice", is.getInput().getSubmitter()); JenkinsRule.WebClient wc = j.createWebClient(); wc.login("alice"); HtmlPage page = wc.getPage(b, a.getUrlName()); j.submit(page.getFormByName(is.getId()), "proceed"); assertEquals(0, a.getExecutions().size()); q.get(); j.assertBuildStatusSuccess(j.waitForCompletion(b)); j.assertLogContains("Who approved? alice", b);
@Issue("JENKINS-46894") @Test public void BuildStatusWhenWithTimeTriggerSkipped() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "buildStatusWhenTimerTriggerSkipped"); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("buildStatusWhenSCMTrigger"), true)); // get the build going, and wait until workflow pauses WorkflowRun b = p.scheduleBuild2(0, new CauseAction(new TimerTriggerCause())) .getStartCondition().get(); j.waitForCompletion(b); j.assertLogContains("Stage \"Two\" skipped due to when conditional",b); j.assertLogNotContains("World", b); j.assertLogNotContains("Heal it", b); }
@Issue("JENKINS-44149") @Test public void propsRemoved() throws Exception { WorkflowRun b = getAndStartNonRepoBuild("simpleJobProperties"); j.assertBuildStatusSuccess(j.waitForCompletion(b)); WorkflowJob job = b.getParent(); assertNotNull(job.getProperty(BuildDiscarderProperty.class)); job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); j.buildAndAssertSuccess(job); assertNull(job.getProperty(BuildDiscarderProperty.class)); }
@Test public void sandboxInvokerUsed() throws Exception { jenkins.jenkins.setSecurityRealm(jenkins.createDummySecurityRealm()); jenkins.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy(). grant(Jenkins.RUN_SCRIPTS, Jenkins.READ, Item.READ).everywhere().to("runScriptsUser"). grant(Jenkins.READ, Item.READ).everywhere().to("otherUser")); WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "p"); job.setDefinition(new CpsFlowDefinition("[a: 1, b: 2].collectEntries { k, v ->\n" + " Jenkins.getInstance()\n" + " [(v): k]\n" + "}\n", true)); WorkflowRun r = jenkins.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get()); jenkins.assertLogContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance", r); jenkins.assertLogContains("Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance. " + Messages.SandboxContinuable_ScriptApprovalLink(), r); JenkinsRule.WebClient wc = jenkins.createWebClient(); wc.login("runScriptsUser"); // make sure we see the annotation for the RUN_SCRIPTS user. HtmlPage rsp = wc.getPage(r, "console"); assertEquals(1, DomNodeUtil.selectNodes(rsp, "//A[@href='" + jenkins.contextPath + "/scriptApproval']").size()); // make sure raw console output doesn't include the garbage and has the right message. TextPage raw = (TextPage)wc.goTo(r.getUrl()+"consoleText","text/plain"); assertThat(raw.getContent(), containsString(" getInstance. " + Messages.SandboxContinuable_ScriptApprovalLink())); wc.login("otherUser"); // make sure we don't see the link for the other user. HtmlPage rsp2 = wc.getPage(r, "console"); assertEquals(0, DomNodeUtil.selectNodes(rsp2, "//A[@href='" + jenkins.contextPath + "/scriptApproval']").size()); // make sure raw console output doesn't include the garbage and has the right message. TextPage raw2 = (TextPage)wc.goTo(r.getUrl()+"consoleText","text/plain"); assertThat(raw2.getContent(), containsString(" getInstance. " + Messages.SandboxContinuable_ScriptApprovalLink())); }
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b3 = p.getLastBuild(); assertEquals(3, b3.getNumber()); // Resume #3, and verify that the build completes with the expected replacements. SemaphoreStep.success("wait/3", null); story.j.waitForCompletion(b3); story.j.assertLogNotContains("trying edits", b3); story.j.assertLogContains("new first part", b3); story.j.assertLogContains("newer second part", b3); } });
private FreeStyleBuild configureTestBuild(String projectName) throws Exception { FreeStyleProject p = projectName == null ? rule.createFreeStyleProject() : rule.createFreeStyleProject(projectName); p.getBuildersList().add(new TestBuilder() { public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { build.getWorkspace().child("junit.xml").copyFrom( getClass().getResource("junit-report-20090516.xml")); return true; } }); p.getPublishersList().add(new JUnitResultArchiver("*.xml")); return rule.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0).get()); }
@Issue("JENKINS-50880") @Test public void whenBeforeInputFalse() throws Exception { String whenFile = "whenBeforeInputFalse"; WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, whenFile); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources(whenFile), true)); // get the build going, and wait until workflow pauses QueueTaskFuture<WorkflowRun> q = p.scheduleBuild2(0); WorkflowRun b = q.getStartCondition().get(); CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get(); j.assertBuildStatusSuccess(j.waitForCompletion(b)); j.assertLogNotContains("X-NO-SHOW-X", b); }
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("param", ""))); p.setDefinition(new CpsFlowDefinition("echo \"run with ${param}\"", true)); WorkflowRun b1 = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("param", "some value")))); story.j.assertLogContains("run with some value", b1); // When we replay a parameterized build, we expect the original parameter values to be set. WorkflowRun b2 = (WorkflowRun) b1.getAction(ReplayAction.class).run("echo \"run again with ${param}\"", Collections.<String,String>emptyMap()).get(); story.j.assertLogContains("run again with some value", story.j.assertBuildStatusSuccess(b2)); } });
/** * Tests the ability to execute a step with an unnamed monomorphic describable argument. */ @Issue("JENKINS-29711") @Test public void monomorphic() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "mon"); p.setDefinition(new CpsFlowDefinition("monomorphStep([firstArg:'one', secondArg:'two'])", true)); r.assertLogContains("First arg: one, second arg: two", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); WorkflowRun run = p.getLastBuild(); LinearScanner scanner = new LinearScanner(); FlowNode node = scanner.findFirstMatch(run.getExecution().getCurrentHeads(), new NodeStepTypePredicate("monomorphStep")); ArgumentsAction argumentsAction = node.getPersistentAction(ArgumentsAction.class); Assert.assertNotNull(argumentsAction); Assert.assertEquals("one,two", ArgumentsAction.getStepArgumentsAsString(node)); }
@Override public void evaluate() throws Throwable { jenkins().setSecurityRealm(story.j.createDummySecurityRealm()); jenkins().save(); QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(Collections.singletonMap("demo", User.getById("someone", true).impersonate()))); p = jenkins().createProject(WorkflowJob.class, "demo"); p.setDefinition(new CpsFlowDefinition("echo \"ran as ${auth()}\"", true)); b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0)); story.j.assertLogContains("ran as someone", b); } });
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); // As in loadStep, will set up a main and auxiliary script. FilePath f = story.j.jenkins.getWorkspaceFor(p).child("f.groovy"); f.write("'original text'", null); p.setDefinition(new CpsFlowDefinition("node {def t = load 'f.groovy'; echo \"got ${t}\"}", true)); WorkflowRun b1 = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0)); story.j.assertLogContains("got original text", b1); // s/got/received/ on main script assertEquals(0, new CLICommandInvoker(story.j, "replay-pipeline").withStdin(IOUtils.toInputStream("node {def t = load 'f.groovy'; echo \"received ${t}\"}")).invokeWithArgs("p").returnCode()); story.j.waitUntilNoActivity(); WorkflowRun b2 = p.getLastBuild(); assertEquals(2, b2.getNumber()); story.j.assertLogContains("received original text", b2); // s/original/new/ on auxiliary script, and explicitly asking to replay #1 rather than the latest assertEquals(0, new CLICommandInvoker(story.j, "replay-pipeline").withStdin(IOUtils.toInputStream("'new text'")).invokeWithArgs("p", "-n", "1", "-s", "Script1").returnCode()); story.j.waitUntilNoActivity(); WorkflowRun b3 = p.getLastBuild(); assertEquals(3, b3.getNumber()); // Main script picked up from #1, not #2. story.j.assertLogContains("got new text", b3); } });
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getLastBuild(); assertTrue(b.isBuilding()); CpsFlowExecution e = (CpsFlowExecution) b.getExecution(); assertTrue(e.isPaused()); JenkinsRule.WebClient wc = story.j.createWebClient(); WebRequest wrs = new WebRequest(wc.createCrumbedUrl(b.getUrl() + PauseUnpauseAction.URL + "/toggle"), HttpMethod.POST); wc.login("dev").getPage(wrs); assertFalse(e.isPaused()); story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)); assertFalse(e.isPaused()); } });
@Issue("JENKINS-51227") @Test public void quietPeriodRemoved() throws Exception { WorkflowRun b = getAndStartNonRepoBuild("quietPeriod"); j.assertBuildStatusSuccess(j.waitForCompletion(b)); WorkflowJob job = b.getParent(); assertEquals(15, job.getQuietPeriod()); job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); j.buildAndAssertSuccess(job); assertEquals(j.jenkins.getQuietPeriod(), job.getQuietPeriod()); }
@Test public void triggersRoundtrip() throws Exception { SampleComputedFolder s = r.jenkins.createProject(SampleComputedFolder.class, "s"); s.addTrigger(new PeriodicFolderTrigger("30m")); SampleComputedFolder s2 = r.configRoundtrip(s); Trigger<?> trigger = s2.getTriggers().get(r.jenkins.getDescriptorByType(PeriodicFolderTrigger.DescriptorImpl.class)); assertThat(trigger, notNullValue()); assertThat(trigger, instanceOf(PeriodicFolderTrigger.class)); assertThat(((PeriodicFolderTrigger)trigger).getInterval(), is("30m")); }