Search in sources :

Example 11 with JobDslBuildStep

use of org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep in project acceptance-test-harness by jenkinsci.

the class JobDslPluginTest method should_use_grooy_sandbox_whitelisted_content.

/**
 * Verifies that if script security for Job DSL scripts is enabled,
 * scripts saved by non administrators can run in a Groovy sandbox
 * without approval. All Job DSL methods are whitelisted by default.
 */
@Test
@WithPlugins({ "matrix-auth@2.3", "mock-security-realm", "authorize-project" })
public void should_use_grooy_sandbox_whitelisted_content() {
    GlobalSecurityConfig sc = setUpSecurity();
    runBuildAsUserWhoTriggered(sc);
    jenkins.login().doLogin(USER);
    FreeStyleJob seedJob = createSeedJob();
    JobDslBuildStep jobDsl = seedJob.addBuildStep(JobDslBuildStep.class);
    jobDsl.setScript("job('New_Job')");
    jobDsl.setUseSandbox(false);
    seedJob.save();
    // Build should fail because script is saved from non administrator an not yet approved
    Build build = seedJob.scheduleBuild().shouldFail();
    assertThat(build.getConsole(), containsString("script not yet approved for use"));
    seedJob.configure(() -> jobDsl.setUseSandbox(true));
    // Build should succeed because the script runs in Groovy sandbox
    // and only Job DSL methods are used.
    seedJob.scheduleBuild().shouldSucceed();
}
Also used : JobDslBuildStep(org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep) Build(org.jenkinsci.test.acceptance.po.Build) FreeStyleJob(org.jenkinsci.test.acceptance.po.FreeStyleJob) GlobalSecurityConfig(org.jenkinsci.test.acceptance.po.GlobalSecurityConfig) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test) WithPlugins(org.jenkinsci.test.acceptance.junit.WithPlugins)

Example 12 with JobDslBuildStep

use of org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep in project acceptance-test-harness by jenkinsci.

the class JobDslPluginTest method should_use_grooy_sandbox_no_whitelisted_content.

/**
 * Verifies that if script security for Job DSL scripts is enabled,
 * scripts with not whitelisted content saved by non administrators
 * wont be executed even it should run in a Groovy sandbox.
 * Administrators can approve this content in the 'Script Approval' of the
 * 'Manage Jenkins' area. Approved scripts can be executed.
 */
@Test
@WithPlugins({ "matrix-auth@2.3", "mock-security-realm", "authorize-project" })
public void should_use_grooy_sandbox_no_whitelisted_content() {
    GlobalSecurityConfig sc = setUpSecurity();
    runBuildAsUserWhoTriggered(sc);
    jenkins.login().doLogin(USER);
    FreeStyleJob seedJob = createSeedJob();
    JobDslBuildStep jobDsl = seedJob.addBuildStep(JobDslBuildStep.class);
    jobDsl.setScript("def jobNames = [\"First_Job\", \"Second_Job\"].toArray()\n" + "\n" + "for(name in jobNames) {\n" + "  job(name)\n" + "}");
    jobDsl.setUseSandbox(true);
    seedJob.save();
    // Build should fail because script contains not whitelisted content.
    // It don't matter that the script runs in sandbox.
    Build build = seedJob.scheduleBuild().shouldFail();
    assertThat(build.getConsole(), containsString("Scripts not permitted to use method java.util.Collection toArray"));
    jenkins.logout();
    jenkins.login().doLogin(ADMIN);
    ScriptApproval sa = new ScriptApproval(jenkins);
    sa.open();
    sa.findSignature("toArray").approve();
    jenkins.logout();
    jenkins.login().doLogin(USER);
    // Build should succeed because the not whitelisted content was approved.
    seedJob.scheduleBuild().shouldSucceed();
}
Also used : JobDslBuildStep(org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep) Build(org.jenkinsci.test.acceptance.po.Build) FreeStyleJob(org.jenkinsci.test.acceptance.po.FreeStyleJob) GlobalSecurityConfig(org.jenkinsci.test.acceptance.po.GlobalSecurityConfig) ScriptApproval(org.jenkinsci.test.acceptance.plugins.script_security.ScriptApproval) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test) WithPlugins(org.jenkinsci.test.acceptance.junit.WithPlugins)

Example 13 with JobDslBuildStep

use of org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep in project acceptance-test-harness by jenkinsci.

the class JobDslPluginTest method runSeedJobInFolder.

/**
 * Creates and executes a seed job in a folder. The seed job generates a new job
 * with the specified lookup strategy.
 * @param jobName The name the job to generate.
 * @param strategy The context to use for relative job names.
 * @return The folder in which the seed job is located.
 */
private Folder runSeedJobInFolder(String jobName, JobDslLookupStrategy strategy) {
    final Folder folder = jenkins.jobs.create(Folder.class, "Folder");
    FreeStyleJob seedJob = folder.getJobs().create(FreeStyleJob.class, "Seed");
    JobDslBuildStep jobDsl = seedJob.addBuildStep(JobDslBuildStep.class);
    jobDsl.setScript("job('" + jobName + "')");
    jobDsl.setLookupStrategy(strategy);
    seedJob.save();
    seedJob.scheduleBuild().shouldSucceed();
    return folder;
}
Also used : JobDslBuildStep(org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep) FreeStyleJob(org.jenkinsci.test.acceptance.po.FreeStyleJob) Folder(org.jenkinsci.test.acceptance.po.Folder)

Example 14 with JobDslBuildStep

use of org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep in project acceptance-test-harness by jenkinsci.

the class JobDslPluginTest method should_use_additional_classpath.

/**
 * Verifies that a newline separated list of additional classpath entries will be considered.
 * If Groovy classes are located in subfolders in the workspace or in JAR files,
 * the additional classpath option is needed to import these classes.
 */
@Test
public void should_use_additional_classpath() {
    FreeStyleJob seedJob = createSeedJob();
    seedJob.copyResource(resource("/job_dsl_plugin/MyUtilities.groovy"), "src/utilities/MyUtilities.groovy");
    seedJob.copyResource(resource("/job_dsl_plugin/Utility.jar"));
    JobDslBuildStep jobDsl = seedJob.addBuildStep(JobDslBuildStep.class);
    jobDsl.setScript("import utilities.MyUtilities\n" + "import utilities.MyUtilitiesFromJar\n" + "\n" + "def job_folder = job('job_src_folder')\n" + "MyUtilities.addDescription(job_folder)\n" + "\n" + "def job_jar = job('job_jar_file')\n" + "MyUtilitiesFromJar.addDescription(job_jar)");
    seedJob.save();
    Build build = seedJob.scheduleBuild().shouldFail();
    assertThat(build.getConsole(), containsString("unable to resolve class utilities.MyUtilities"));
    assertThat(build.getConsole(), containsString("unable to resolve class utilities.MyUtilitiesFromJar"));
    assertThat(getJob("job_src_folder"), pageObjectDoesNotExist());
    assertThat(getJob("job_jar_file"), pageObjectDoesNotExist());
    seedJob.configure(() -> jobDsl.setAdditionalClasspath("src", "Utility.jar"));
    Build build2 = seedJob.scheduleBuild().shouldSucceed();
    assertThat(build2.getConsole(), containsString("GeneratedJob{name='job_src_folder'}"));
    assertThat(build2.getConsole(), containsString("GeneratedJob{name='job_jar_file'}"));
    checkDescription(getJob("job_src_folder"), "Description from class in src folder.");
    checkDescription(getJob("job_jar_file"), "Description from class in JAR file.");
}
Also used : JobDslBuildStep(org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep) Build(org.jenkinsci.test.acceptance.po.Build) FreeStyleJob(org.jenkinsci.test.acceptance.po.FreeStyleJob) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test)

Example 15 with JobDslBuildStep

use of org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep in project acceptance-test-harness by jenkinsci.

the class JobDslPluginTest method executeRemovedViewAction.

/**
 * Let a seed job run a Job DSL script that do not reference a previous generated view anymore.
 * @param action The action what to do when a previously generated view is not referenced anymore.
 * @return The executed seed job.
 */
private FreeStyleJob executeRemovedViewAction(JobDslRemovedViewAction action) {
    FreeStyleJob seedJob = createSeedJob();
    JobDslBuildStep jobDsl = seedJob.addBuildStep(JobDslBuildStep.class);
    jobDsl.setScript("listView('Old_View')");
    seedJob.save();
    seedJob.scheduleBuild().shouldSucceed();
    seedJob.configure(() -> {
        jobDsl.setScript("listView('New_View')");
        jobDsl.setRemovedViewAction(action);
    });
    return seedJob;
}
Also used : JobDslBuildStep(org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep) FreeStyleJob(org.jenkinsci.test.acceptance.po.FreeStyleJob)

Aggregations

JobDslBuildStep (org.jenkinsci.test.acceptance.plugins.job_dsl.JobDslBuildStep)23 FreeStyleJob (org.jenkinsci.test.acceptance.po.FreeStyleJob)23 AbstractJUnitTest (org.jenkinsci.test.acceptance.junit.AbstractJUnitTest)17 Test (org.junit.Test)17 Build (org.jenkinsci.test.acceptance.po.Build)13 WithPlugins (org.jenkinsci.test.acceptance.junit.WithPlugins)10 GlobalSecurityConfig (org.jenkinsci.test.acceptance.po.GlobalSecurityConfig)5 Pattern (java.util.regex.Pattern)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ScriptApproval (org.jenkinsci.test.acceptance.plugins.script_security.ScriptApproval)2 Folder (org.jenkinsci.test.acceptance.po.Folder)1 Job (org.jenkinsci.test.acceptance.po.Job)1 ListView (org.jenkinsci.test.acceptance.po.ListView)1 PluginManager (org.jenkinsci.test.acceptance.po.PluginManager)1 PluginSpec (org.jenkinsci.test.acceptance.update_center.PluginSpec)1