From 508cb0898747ef78843e5f4a07eee36e67c20bc5 Mon Sep 17 00:00:00 2001 From: rsandell Date: Wed, 24 Jun 2020 18:42:18 +0200 Subject: [PATCH 1/3] Prepare pom for JCasC --- pom.xml | 75 ++++++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 43 deletions(-) diff --git a/pom.xml b/pom.xml index 33d43691..c5b76a7f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.jenkins-ci.plugins plugin - 3.43 + 4.2 @@ -34,11 +34,10 @@ 1.17 -SNAPSHOT - 2.150.1 + 2.222.4 8 - 2.19 - 2.67 - 3.3 + 1.3.8 + 1.41 @@ -59,12 +58,10 @@ org.jenkins-ci.plugins credentials - 2.1.18 org.jenkins-ci.plugins credentials-binding - 1.18 org.jenkins-ci.plugins @@ -74,7 +71,7 @@ org.jenkinsci.plugins pipeline-model-extensions - 1.3.8 + ${pipeline-model-definition-version} true @@ -84,115 +81,107 @@ multiline-secrets-ui 1.0 - - org.jenkins-ci.plugins - scm-api - 2.4.1 - test + structs + + org.jenkins-ci.plugins - structs - 1.18 + scm-api test org.jenkins-ci.plugins.workflow workflow-api - 2.33 test org.jenkins-ci.plugins.workflow workflow-step-api - ${workflow-step-api.version} test org.jenkins-ci.plugins.workflow workflow-step-api - ${workflow-step-api.version} tests test org.jenkins-ci.plugins.workflow workflow-cps - ${workflow-cps.version} test org.jenkins-ci.plugins.workflow workflow-cps - ${workflow-cps.version} tests test org.jenkins-ci.plugins.workflow workflow-job - 2.32 test org.jenkins-ci.plugins.workflow workflow-durable-task-step - 2.30 test org.jenkins-ci.plugins.workflow workflow-basic-steps - 2.15 test org.jenkins-ci.plugins.workflow workflow-support - ${workflow-support.version} test org.jenkins-ci.plugins.workflow workflow-support - ${workflow-support.version} tests test org.jenkinsci.plugins pipeline-model-definition - 1.3.8 - test - - - - org.apache.commons - commons-lang3 - 3.7 + ${pipeline-model-definition-version} test + - org.jenkins-ci.plugins - mailer - 1.20 + io.jenkins + configuration-as-code + ${jcasc.version} test - org.jenkins-ci.plugins - ssh-credentials - 1.13 + io.jenkins.configuration-as-code + test-harness + ${jcasc.version} test + - org.jenkins-ci.plugins.workflow - workflow-scm-step - 2.6 + org.apache.commons + commons-lang3 + 3.9 test - + + + + io.jenkins.tools.bom + bom-2.222.x + 11 + import + pom + + + From a3d8988c9529d172262e0d10ffa56d13af73c995 Mon Sep 17 00:00:00 2001 From: rsandell Date: Tue, 30 Jun 2020 15:33:26 +0200 Subject: [PATCH 2/3] Configuration as Code support Unit tests and Symbols --- .../credentials/DockerServerCredentials.java | 3 +- .../docker/commons/tools/DockerTool.java | 4 +- .../commons/tools/DockerToolInstaller.java | 3 +- .../plugins/docker/commons/CasCTest.java | 103 ++++++++++++++++++ .../plugins/docker/commons/casc_bare.yaml | 22 ++++ .../plugins/docker/commons/casc_symbols.yaml | 22 ++++ 6 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/jenkinsci/plugins/docker/commons/CasCTest.java create mode 100644 src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml create mode 100644 src/test/resources/org/jenkinsci/plugins/docker/commons/casc_symbols.yaml diff --git a/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerCredentials.java b/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerCredentials.java index 9e34d39c..55501135 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerCredentials.java +++ b/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerCredentials.java @@ -28,6 +28,7 @@ import hudson.Extension; import hudson.Util; import hudson.util.Secret; +import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; import javax.annotation.CheckForNull; @@ -111,7 +112,7 @@ public String getServerCaCertificate() { return serverCaCertificate; } - @Extension + @Extension @Symbol({"x509ClientCert", "dockerServer"}) public static class DescriptorImpl extends BaseStandardCredentialsDescriptor { @Override public String getDisplayName() { diff --git a/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerTool.java b/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerTool.java index ce2dd92e..623b7138 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerTool.java +++ b/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerTool.java @@ -43,6 +43,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import jenkins.model.Jenkins; +import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; /** @@ -114,7 +115,8 @@ public DockerTool forNode(Node node, TaskListener log) throws IOException, Inter } } - @Extension public static class DescriptorImpl extends ToolDescriptor { + @Extension @Symbol("dockerTool") + public static class DescriptorImpl extends ToolDescriptor { @Override public String getDisplayName() { return "Docker"; diff --git a/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerToolInstaller.java b/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerToolInstaller.java index dc8530cf..0570db56 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerToolInstaller.java +++ b/src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerToolInstaller.java @@ -44,6 +44,7 @@ import java.util.regex.Pattern; import javax.annotation.Nonnull; import jenkins.security.MasterToSlaveCallable; +import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; /** @@ -165,7 +166,7 @@ private static VersionNumber parseVersion(String version) { throw new IllegalArgumentException("Failed to parse version " + version); } - @Extension + @Extension @Symbol("fromDocker") public static class DescriptorImpl extends ToolInstallerDescriptor { @Override diff --git a/src/test/java/org/jenkinsci/plugins/docker/commons/CasCTest.java b/src/test/java/org/jenkinsci/plugins/docker/commons/CasCTest.java new file mode 100644 index 00000000..41568a90 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/docker/commons/CasCTest.java @@ -0,0 +1,103 @@ +package org.jenkinsci.plugins.docker.commons; + +import com.cloudbees.plugins.credentials.CredentialsMatchers; +import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.common.IdCredentials; +import hudson.security.ACL; +import hudson.tools.InstallSourceProperty; +import hudson.tools.ToolProperty; +import hudson.tools.ToolPropertyDescriptor; +import hudson.util.DescribableList; +import hudson.util.Secret; +import io.jenkins.plugins.casc.misc.RoundTripAbstractTest; +import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials; +import org.jenkinsci.plugins.docker.commons.credentials.DockerServerDomainRequirement; +import org.jenkinsci.plugins.docker.commons.tools.DockerTool; +import org.jenkinsci.plugins.docker.commons.tools.DockerToolInstaller; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.jvnet.hudson.test.RestartableJenkinsRule; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; +import static org.hamcrest.collection.ArrayMatching.arrayContaining; +import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.hamcrest.core.AllOf.allOf; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(Parameterized.class) +public class CasCTest extends RoundTripAbstractTest { + + final String resource; + + public CasCTest(final String resource) { + this.resource = resource; + } + + @Parameterized.Parameters(name = "{0}") + public static Object[][] params() { + return new Object[][]{{"casc_bare.yaml"}, {"casc_symbols.yaml"}}; + } + + @Override + protected void assertConfiguredAsExpected(final RestartableJenkinsRule j, final String s) { + + //The credentials + final IdCredentials cred = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(IdCredentials.class, j.j.jenkins, + ACL.SYSTEM, new DockerServerDomainRequirement()), CredentialsMatchers.withId("dockerx509")); + assertNotNull(cred); + assertThat(cred, instanceOf(DockerServerCredentials.class)); + DockerServerCredentials dCreds = (DockerServerCredentials) cred; + assertEquals("THE CLIENT", dCreds.getClientCertificate()); + assertEquals("THE SERVER", dCreds.getServerCaCertificate()); + assertEquals("Be wewy wewy cuwiet", Secret.toString(dCreds.getClientKeySecret())); + assertEquals("Docker X.509", dCreds.getDescription()); + + + //The ToolInstaller + final DockerTool[] installations = j.j.jenkins.getDescriptorByType(DockerTool.DescriptorImpl.class).getInstallations(); + assertNotNull(installations); + assertThat(installations, arrayWithSize(2)); + assertThat(installations, arrayContaining( + allOf( + hasProperty("name", equalTo("docker-latest")), + hasProperty("home", nullValue()) + ), + allOf( + hasProperty("name", equalTo("docker-native")), + hasProperty("home", equalTo("/etc/docket/docker")) + ) + )); + final DescribableList, ToolPropertyDescriptor> properties = installations[0].getProperties(); + assertThat(properties, contains(instanceOf(InstallSourceProperty.class))); + final InstallSourceProperty property = (InstallSourceProperty) properties.get(0); + assertThat(property.installers, contains( + allOf( + instanceOf(DockerToolInstaller.class), + hasProperty("version", equalTo("latest")) + ) + )); + + /* + * DockerRegistryEndpoint is not directly used in this plugin in any global config sense, + * So it is better tested in the plugins that uses it for example docker workflow plugin: + * https://github.com/jenkinsci/docker-workflow-plugin/blob/2ba1ac97b75a3f188e243333b31ef06d55b9221a/src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/GlobalConfig.java + */ + + } + + @Override + protected String configResource() { + return resource; + } + + @Override + protected String stringInLogExpected() { + return DockerServerCredentials.class.getName(); + } +} diff --git a/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml b/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml new file mode 100644 index 00000000..18193f11 --- /dev/null +++ b/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml @@ -0,0 +1,22 @@ +credentials: + system: + domainCredentials: + - credentials: + - dockerServer: + clientCertificate: "THE CLIENT" + clientKeySecret: "Be wewy wewy cuwiet" + description: "Docker X.509" + id: "dockerx509" + scope: GLOBAL + serverCaCertificate: "THE SERVER" +tool: + dockerTool: + installations: + - name: "docker-latest" + properties: + - installSource: + installers: + - docker: + version: "latest" + - home: "/etc/docket/docker" + name: "docker-native" \ No newline at end of file diff --git a/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_symbols.yaml b/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_symbols.yaml new file mode 100644 index 00000000..f98ba538 --- /dev/null +++ b/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_symbols.yaml @@ -0,0 +1,22 @@ +credentials: + system: + domainCredentials: + - credentials: + - x509ClientCert: + clientCertificate: "THE CLIENT" + clientKeySecret: "Be wewy wewy cuwiet" + description: "Docker X.509" + id: "dockerx509" + scope: GLOBAL + serverCaCertificate: "THE SERVER" +tool: + dockerTool: + installations: + - name: "docker-latest" + properties: + - installSource: + installers: + - fromDocker: + version: "latest" + - home: "/etc/docket/docker" + name: "docker-native" From 01ed2ad2195c4961dfe3f38ccf38e13f0ea8f939 Mon Sep 17 00:00:00 2001 From: Robert Sandell Date: Tue, 30 Jun 2020 15:56:22 +0200 Subject: [PATCH 3/3] Update src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml new line at end of file Co-authored-by: Adrien Lecharpentier --- .../org/jenkinsci/plugins/docker/commons/casc_bare.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml b/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml index 18193f11..7f9baa35 100644 --- a/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml +++ b/src/test/resources/org/jenkinsci/plugins/docker/commons/casc_bare.yaml @@ -19,4 +19,4 @@ tool: - docker: version: "latest" - home: "/etc/docket/docker" - name: "docker-native" \ No newline at end of file + name: "docker-native"