Skip to content

Commit

Permalink
Merge pull request #264 from jonesbusy/feature/jcasc-tests
Browse files Browse the repository at this point in the history
Add JCasC, local data and JobDSL tests
  • Loading branch information
repolevedavaj committed Mar 6, 2024
2 parents dc59d38 + 1ce0451 commit 64afb0d
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

<testcontainers.version>1.19.7</testcontainers.version>
<zstd-jni.version>1.5.5-11</zstd-jni.version>

<!-- Test dependencies -->
<configuration-as-code.version>1756.v2b_7eea_874392</configuration-as-code.version>
<jobdsl.version>1.87</jobdsl.version>

</properties>

<name>Jenkins Job Cacher plugin</name>
Expand Down Expand Up @@ -166,6 +171,24 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
<version>${jobdsl.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/jenkins/plugins/jobcacher/CacheWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -133,6 +140,7 @@ private static <T> List<T> wrapList(List<T> list, Function<List<T>, List<T>> lis
}

@Extension
@Symbol("jobcacher")
public static final class DescriptorImpl extends BuildWrapperDescriptor {

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -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/"));
}

}
57 changes: 57 additions & 0 deletions src/test/java/jenkins/plugins/itemstorage/DataMigrationTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
57 changes: 57 additions & 0 deletions src/test/java/jenkins/plugins/jobdsl/JobDslTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.1' encoding='UTF-8'?>
<jenkins.plugins.itemstorage.GlobalItemStorage plugin="jobcacher@999999-SNAPSHOT">
<storage class="jenkins.plugins.itemstorage.s3.S3ItemStorage">
<credentialsId>s3</credentialsId>
<bucketName>bucket1</bucketName>
<prefix>the-prefix/</prefix>
<region>us-gov-west-1</region>
</storage>
</jenkins.plugins.itemstorage.GlobalItemStorage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.1' encoding='UTF-8'?>
<jenkins.plugins.itemstorage.GlobalItemStorage plugin="jobcacher@999999-SNAPSHOT">
<storage class="jenkins.plugins.itemstorage.local.LocalItemStorage">
<root>jobcaches</root>
</storage>
</jenkins.plugins.itemstorage.GlobalItemStorage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.1' encoding='UTF-8'?>
<jenkins.plugins.itemstorage.GlobalItemStorage plugin="jobcacher@999999-SNAPSHOT">
<storage class="jenkins.plugins.itemstorage.s3.NonAWSS3ItemStorage">
<bucketName>bucket1</bucketName>
<prefix>the-prefix/</prefix>
<endpoint>http://localhost:9000</endpoint>
<region>eu-central-2</region>
<pathStyleAccess>true</pathStyleAccess>
<parallelDownloads>true</parallelDownloads>
<credentialsId>s3</credentialsId>
</storage>
</jenkins.plugins.itemstorage.GlobalItemStorage>
8 changes: 8 additions & 0 deletions src/test/resources/jenkins/plugins/itemstorage/aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
unclassified:
globalItemStorage:
storage:
s3:
bucketName: "caches"
prefix: "the-prefix/"
credentialsId: "s3"
region: "eu-central-2"
5 changes: 5 additions & 0 deletions src/test/resources/jenkins/plugins/itemstorage/local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
unclassified:
globalItemStorage:
storage:
local:
root: "jobcaches"
11 changes: 11 additions & 0 deletions src/test/resources/jenkins/plugins/itemstorage/nonaws.yml
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions src/test/resources/jobdsl/freestyle.groovy
Original file line number Diff line number Diff line change
@@ -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')
}
}

0 comments on commit 64afb0d

Please sign in to comment.