diff --git a/pom.xml b/pom.xml index 7bec667..4114bdf 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,11 @@ 1.19.7 1.5.5-11 + + + 1756.v2b_7eea_874392 + 1.87 + Jenkins Job Cacher plugin @@ -166,6 +171,24 @@ tests test + + io.jenkins + configuration-as-code + ${configuration-as-code.version} + test + + + io.jenkins.configuration-as-code + test-harness + ${configuration-as-code.version} + test + + + org.jenkins-ci.plugins + job-dsl + ${jobdsl.version} + test + diff --git a/src/main/java/jenkins/plugins/jobcacher/CacheWrapper.java b/src/main/java/jenkins/plugins/jobcacher/CacheWrapper.java index ea33f7f..ff2e8a2 100644 --- a/src/main/java/jenkins/plugins/jobcacher/CacheWrapper.java +++ b/src/main/java/jenkins/plugins/jobcacher/CacheWrapper.java @@ -38,6 +38,8 @@ import jenkins.plugins.itemstorage.GlobalItemStorage; import jenkins.plugins.itemstorage.ItemStorage; import jenkins.tasks.SimpleBuildWrapper; + +import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; @@ -93,6 +95,11 @@ public void setSkipSave(boolean skipSave) { } @DataBoundSetter + @SuppressWarnings("unused") + public boolean getSkipRestore() { + return skipRestore; + } + @SuppressWarnings("unused") public void setSkipRestore(boolean skipRestore) { this.skipRestore = skipRestore; @@ -133,6 +140,7 @@ private static List wrapList(List list, Function, List> lis } @Extension + @Symbol("jobcacher") public static final class DescriptorImpl extends BuildWrapperDescriptor { @NonNull diff --git a/src/test/java/jenkins/plugins/itemstorage/ConfigurationAsCodeCompatibilityTest.java b/src/test/java/jenkins/plugins/itemstorage/ConfigurationAsCodeCompatibilityTest.java new file mode 100644 index 0000000..5cb9097 --- /dev/null +++ b/src/test/java/jenkins/plugins/itemstorage/ConfigurationAsCodeCompatibilityTest.java @@ -0,0 +1,57 @@ +package jenkins.plugins.itemstorage; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import org.junit.Rule; +import org.junit.Test; + +import io.jenkins.plugins.casc.misc.ConfiguredWithCode; +import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; +import jenkins.plugins.itemstorage.local.LocalItemStorage; +import jenkins.plugins.itemstorage.s3.NonAWSS3ItemStorage; +import jenkins.plugins.itemstorage.s3.S3ItemStorage; + +public class ConfigurationAsCodeCompatibilityTest { + + @Rule + public JenkinsConfiguredWithCodeRule jenkins = new JenkinsConfiguredWithCodeRule(); + + @Test + @ConfiguredWithCode("local.yml") + public void shouldSupportConfigurationAsCodeForLocalStorage() { + ItemStorage storage = GlobalItemStorage.get().getStorage(); + assertThat(storage, is(notNullValue())); + LocalItemStorage localItemStorage = (LocalItemStorage) storage; + assertThat(localItemStorage.getRoot(), is("jobcaches")); + } + + @Test + @ConfiguredWithCode("nonaws.yml") + public void shouldSupportConfigurationAsCodeForNonAws() { + ItemStorage storage = GlobalItemStorage.get().getStorage(); + assertThat(storage, is(notNullValue())); + NonAWSS3ItemStorage s3ItemStorage = (NonAWSS3ItemStorage) storage; + assertThat(s3ItemStorage.getBucketName(), is("caches")); + assertThat(s3ItemStorage.getRegion(), is("eu-central-2")); + assertThat(s3ItemStorage.getCredentialsId(), is("s3")); + assertThat(s3ItemStorage.getPrefix(), is("the-prefix/")); + assertThat(s3ItemStorage.getEndpoint(), is("http://localhost:9000")); + assertThat(s3ItemStorage.getParallelDownloads(), is(false)); + assertThat(s3ItemStorage.getPathStyleAccess(), is(true)); + } + + @Test + @ConfiguredWithCode("aws.yml") + public void shouldSupportConfigurationAsCodeForAws() { + ItemStorage storage = GlobalItemStorage.get().getStorage(); + assertThat(storage, is(notNullValue())); + S3ItemStorage s3ItemStorage = (S3ItemStorage) storage; + assertThat(s3ItemStorage.getBucketName(), is("caches")); + assertThat(s3ItemStorage.getRegion(), is("eu-central-2")); + assertThat(s3ItemStorage.getCredentialsId(), is("s3")); + assertThat(s3ItemStorage.getPrefix(), is("the-prefix/")); + } + +} diff --git a/src/test/java/jenkins/plugins/itemstorage/DataMigrationTest.java b/src/test/java/jenkins/plugins/itemstorage/DataMigrationTest.java new file mode 100644 index 0000000..9e1a8ef --- /dev/null +++ b/src/test/java/jenkins/plugins/itemstorage/DataMigrationTest.java @@ -0,0 +1,57 @@ +package jenkins.plugins.itemstorage; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.recipes.LocalData; + +import jenkins.plugins.itemstorage.local.LocalItemStorage; +import jenkins.plugins.itemstorage.s3.NonAWSS3ItemStorage; +import jenkins.plugins.itemstorage.s3.S3ItemStorage; + +public class DataMigrationTest { + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @Test + @LocalData + public void shouldMigrateLocalData() { + ItemStorage storage = GlobalItemStorage.get().getStorage(); + assertThat(storage, is(notNullValue())); + LocalItemStorage localItemStorage = (LocalItemStorage) storage; + assertThat(localItemStorage.getRoot(), is("jobcaches")); + } + + @Test + @LocalData + public void shouldMigrateAwsData() { + ItemStorage storage = GlobalItemStorage.get().getStorage(); + assertThat(storage, is(notNullValue())); + S3ItemStorage s3ItemStorage = (S3ItemStorage) storage; + assertThat(s3ItemStorage.getBucketName(), is("bucket1")); + assertThat(s3ItemStorage.getRegion(), is("us-gov-west-1")); + assertThat(s3ItemStorage.getCredentialsId(), is("s3")); + assertThat(s3ItemStorage.getPrefix(), is("the-prefix/")); + } + + @Test + @LocalData + public void shouldMigrateNonAwsData() { + ItemStorage storage = GlobalItemStorage.get().getStorage(); + assertThat(storage, is(notNullValue())); + NonAWSS3ItemStorage s3ItemStorage = (NonAWSS3ItemStorage) storage; + assertThat(s3ItemStorage.getBucketName(), is("bucket1")); + assertThat(s3ItemStorage.getRegion(), is("eu-central-2")); + assertThat(s3ItemStorage.getCredentialsId(), is("s3")); + assertThat(s3ItemStorage.getPrefix(), is("the-prefix/")); + assertThat(s3ItemStorage.getEndpoint(), is("http://localhost:9000")); + assertThat(s3ItemStorage.getParallelDownloads(), is(true)); + assertThat(s3ItemStorage.getPathStyleAccess(), is(true)); + } + +} diff --git a/src/test/java/jenkins/plugins/jobdsl/JobDslTest.java b/src/test/java/jenkins/plugins/jobdsl/JobDslTest.java new file mode 100644 index 0000000..753bb16 --- /dev/null +++ b/src/test/java/jenkins/plugins/jobdsl/JobDslTest.java @@ -0,0 +1,57 @@ +package jenkins.plugins.jobdsl; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.JenkinsRule; + +import hudson.model.FreeStyleProject; +import javaposse.jobdsl.plugin.ExecuteDslScripts; +import javaposse.jobdsl.plugin.LookupStrategy; +import javaposse.jobdsl.plugin.RemovedJobAction; +import javaposse.jobdsl.plugin.RemovedViewAction; +import jenkins.plugins.jobcacher.CacheWrapper; + +public class JobDslTest { + + public static final String ANSIBLE_DSL_GROOVY_PLAYBOOK = "jobdsl/playbook.groovy"; + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @Test + @Issue("https://github.com/jenkinsci/jobcacher-plugin/issues/271") + @Ignore + public void shouldCreateFreestyleJob() throws Exception { + runJobDsl("/jobdsl/freestyle.groovy", jenkins); + CacheWrapper step = jenkins.jenkins.getItemByFullName("freestyle", FreeStyleProject.class).getBuildWrappersList().get(CacheWrapper.class); + assertNotNull(step); + assertThat(step.getCaches(), hasSize(2)); + assertThat(step.getSkipSave(), is(true)); + assertThat(step.getSkipRestore(), is(false)); + assertThat(step.getMaxCacheSize(), is(1024L)); + assertThat(step.getDefaultBranch(), is("main")); + } + + private void runJobDsl(String script, JenkinsRule rule) throws Exception { + FreeStyleProject job = rule.createFreeStyleProject(); + String scriptText = IOUtils.toString(JobDslTest.class.getResourceAsStream(script), StandardCharsets.UTF_8); + ExecuteDslScripts builder = new ExecuteDslScripts(); + builder.setScriptText(scriptText); + builder.setRemovedJobAction(RemovedJobAction.DELETE); + builder.setRemovedViewAction(RemovedViewAction.DELETE); + builder.setLookupStrategy(LookupStrategy.JENKINS_ROOT); + job.getBuildersList().add(builder); + rule.buildAndAssertSuccess(job); + } + +} diff --git a/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateAwsData/jenkins.plugins.itemstorage.GlobalItemStorage.xml b/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateAwsData/jenkins.plugins.itemstorage.GlobalItemStorage.xml new file mode 100644 index 0000000..5881f62 --- /dev/null +++ b/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateAwsData/jenkins.plugins.itemstorage.GlobalItemStorage.xml @@ -0,0 +1,9 @@ + + + + s3 + bucket1 + the-prefix/ + us-gov-west-1 + + \ No newline at end of file diff --git a/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateLocalData/jenkins.plugins.itemstorage.GlobalItemStorage.xml b/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateLocalData/jenkins.plugins.itemstorage.GlobalItemStorage.xml new file mode 100644 index 0000000..e74f17b --- /dev/null +++ b/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateLocalData/jenkins.plugins.itemstorage.GlobalItemStorage.xml @@ -0,0 +1,6 @@ + + + + jobcaches + + diff --git a/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateNonAwsData/jenkins.plugins.itemstorage.GlobalItemStorage.xml b/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateNonAwsData/jenkins.plugins.itemstorage.GlobalItemStorage.xml new file mode 100644 index 0000000..d1c624c --- /dev/null +++ b/src/test/resources/jenkins/plugins/itemstorage/DataMigrationTest/shouldMigrateNonAwsData/jenkins.plugins.itemstorage.GlobalItemStorage.xml @@ -0,0 +1,12 @@ + + + + bucket1 + the-prefix/ + http://localhost:9000 + eu-central-2 + true + true + s3 + + \ No newline at end of file diff --git a/src/test/resources/jenkins/plugins/itemstorage/aws.yml b/src/test/resources/jenkins/plugins/itemstorage/aws.yml new file mode 100644 index 0000000..04f5955 --- /dev/null +++ b/src/test/resources/jenkins/plugins/itemstorage/aws.yml @@ -0,0 +1,8 @@ +unclassified: + globalItemStorage: + storage: + s3: + bucketName: "caches" + prefix: "the-prefix/" + credentialsId: "s3" + region: "eu-central-2" diff --git a/src/test/resources/jenkins/plugins/itemstorage/local.yml b/src/test/resources/jenkins/plugins/itemstorage/local.yml new file mode 100644 index 0000000..58913c1 --- /dev/null +++ b/src/test/resources/jenkins/plugins/itemstorage/local.yml @@ -0,0 +1,5 @@ +unclassified: + globalItemStorage: + storage: + local: + root: "jobcaches" diff --git a/src/test/resources/jenkins/plugins/itemstorage/nonaws.yml b/src/test/resources/jenkins/plugins/itemstorage/nonaws.yml new file mode 100644 index 0000000..2877f12 --- /dev/null +++ b/src/test/resources/jenkins/plugins/itemstorage/nonaws.yml @@ -0,0 +1,11 @@ +unclassified: + globalItemStorage: + storage: + nonAWSS3: + bucketName: "caches" + prefix: "the-prefix/" + credentialsId: "s3" + endpoint: "http://localhost:9000" + parallelDownloads: false + pathStyleAccess: true + region: "eu-central-2" diff --git a/src/test/resources/jobdsl/freestyle.groovy b/src/test/resources/jobdsl/freestyle.groovy new file mode 100644 index 0000000..f111549 --- /dev/null +++ b/src/test/resources/jobdsl/freestyle.groovy @@ -0,0 +1,25 @@ +freeStyleJob('freestyle') { + wrappers { + jobcacher { + caches { + arbitraryFileCache { + path('node_modules') + includes('**/*') + excludes(null) + } + arbitraryFileCache { + path('.m2/repository') + includes('**/*') + excludes(null) + } + } + skipRestore(false) + skipSave(true) + defaultBranch('main') + maxCacheSize(1024L) + } + } + steps { + shell('ls -lah') + } +}