Skip to content

Commit

Permalink
[JENKINS-26670] Added a test for WorkflowJob as a test for Job. It's …
Browse files Browse the repository at this point in the history
…commented out as workflow requires Jenkins 1.580.1. Comment in those codes in pom.xml and ProjectQueueItemAuthenticatorTest to run that test locally.
  • Loading branch information
ikedam committed May 7, 2015
1 parent 63c5d2a commit 135849e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
26 changes: 26 additions & 0 deletions pom.xml
Expand Up @@ -4,6 +4,7 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.532</version><!-- QueueItemAuthenticator is since 1.520 -->
<!--<version>1.580.1</version>--><!-- When you test the integration with workflow -->
</parent>

<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -39,8 +40,33 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<workflow.version>1.0</workflow.version>

This comment has been minimized.

Copy link
@jglick

jglick May 8, 2015

Member

Might as well use 1.6 BTW. (And 1.596.1.)

This comment has been minimized.

Copy link
@ikedam

ikedam May 10, 2015

Author Member

I tried tests with workflow 1.6 and tests passed.
Do you mean there're changes about authorization / authentication in workflow 1.6?

</properties>

<dependencies>
<!-- When you test the integration with workflow -->
<!--
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
-->
</dependencies>

<!-- get every artifact through repo.jenkins-ci.org, which proxies all the artifacts that we need -->
<repositories>
<repository>
Expand Down
Expand Up @@ -53,6 +53,24 @@
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

/*
// classes for workflowTest
import java.io.IOException;
import jenkins.tasks.SimpleBuildStep;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.InvisibleAction;
import hudson.model.TaskListener;
import hudson.model.Run;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategy;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.kohsuke.stapler.DataBoundConstructor;
*/

/**
*
*/
Expand Down Expand Up @@ -364,6 +382,7 @@ public Authentication authenticate(AbstractProject<?, ?> project, Queue.Item ite

@Test
public void testOldSignature() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new AuthorizeProjectProperty(new AuthorizeProjectStrategyWithOldSignature("test1")));
AuthorizationCheckBuilder checker = new AuthorizationCheckBuilder();
Expand All @@ -372,4 +391,75 @@ public void testOldSignature() throws Exception {
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertEquals("test1", checker.authentication.getName());
}

/*
// A test for workflow plugin (which extends Job, not AbstractProject).
// This is disabled as workflow requires Jenkins >= 1.580.1
// but authorize-project targets Jenkins >= 1.532.
public static class AuthorizationRecordAction extends InvisibleAction {
public final Authentication authentication;
public AuthorizationRecordAction(Authentication authentication) {
this.authentication = authentication;
}
}
public static class AuthorizationCheckSimpleBuilder extends Builder implements SimpleBuildStep {
@DataBoundConstructor
public AuthorizationCheckSimpleBuilder() {
}
@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {
run.addAction(new AuthorizationRecordAction(Jenkins.getAuthentication()));
}
@TestExtension("testWorkflow")
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
@Override
public String getDisplayName() {
return "AuthorizationCheckSimpleBuilder";
}
}
}
@Test
public void testWorkflow() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
{
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "test"+j.jenkins.getItems().size());
p.setDefinition(new CpsFlowDefinition("node{ step([$class: 'AuthorizationCheckSimpleBuilder']); }", true));
WorkflowRun b = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(b);
assertEquals(ACL.SYSTEM, b.getAction(AuthorizationRecordAction.class).authentication);
}
{
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "test"+j.jenkins.getItems().size());
p.addProperty(new AuthorizeProjectProperty(new AuthorizeProjectStrategyWithOldSignature("test1")));
p.setDefinition(new CpsFlowDefinition("node{ step([$class: 'AuthorizationCheckSimpleBuilder']); }", true));
WorkflowRun b = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(b);
// Strategies with old signatures don't work for Jobs.
assertEquals(ACL.SYSTEM, b.getAction(AuthorizationRecordAction.class).authentication);
}
{
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "test"+j.jenkins.getItems().size());
p.addProperty(new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("test1", true)));
User.get("test1"); // create
p.setDefinition(new CpsFlowDefinition("node{ step([$class: 'AuthorizationCheckSimpleBuilder']); }", true));
WorkflowRun b = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(b);
assertEquals(User.get("test1").impersonate(), b.getAction(AuthorizationRecordAction.class).authentication);
}
}
*/
}
Expand Up @@ -51,7 +51,7 @@ public void throwFailingHttpStatusCodeExceptionIfNecessary(WebResponse webRespon
};
}

protected void before() throws Throwable {
public void before() throws Throwable {
super.before();
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator());
}
Expand Down

0 comments on commit 135849e

Please sign in to comment.