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();
}
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();
}
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;
}
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.");
}
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;
}
Aggregations