Skip to content

Commit

Permalink
Merge pull request #87 from rsandell/jcasc
Browse files Browse the repository at this point in the history
Configuration as Code support
  • Loading branch information
rsandell authored Jul 1, 2020
2 parents f5fcfdd + 01ed2ad commit 3186bed
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 46 deletions.
75 changes: 32 additions & 43 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.43</version>
<version>4.2</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -34,11 +34,10 @@
<properties>
<revision>1.17</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.150.1</jenkins.version>
<jenkins.version>2.222.4</jenkins.version>
<java.level>8</java.level>
<workflow-step-api.version>2.19</workflow-step-api.version>
<workflow-cps.version>2.67</workflow-cps.version>
<workflow-support.version>3.3</workflow-support.version>
<pipeline-model-definition-version>1.3.8</pipeline-model-definition-version>
<jcasc.version>1.41</jcasc.version>
</properties>

<repositories>
Expand All @@ -59,12 +58,10 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.18</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -74,7 +71,7 @@
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-extensions</artifactId>
<version>1.3.8</version>
<version>${pipeline-model-definition-version}</version>
<!-- optional because the dependency subtree is quite large -->
<optional>true</optional>
</dependency>
Expand All @@ -84,115 +81,107 @@
<artifactId>multiline-secrets-ui</artifactId>
<version>1.0</version>
</dependency>

<!-- for Pipeline-based unit tests -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.4.1</version>
<scope>test</scope>
<artifactId>structs</artifactId>
</dependency>

<!-- for Pipeline-based unit tests -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.18</version>
<artifactId>scm-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.33</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow-step-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow-step-api.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-cps.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-cps.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.32</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.15</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>${workflow-support.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>${workflow-support.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>1.3.8</version>
<scope>test</scope>
</dependency>
<!-- these dependencies are added to satisfy the enforcer rules -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
<version>${pipeline-model-definition-version}</version>
<scope>test</scope>
</dependency>
<!-- Dependencies for testing JCasC compatibility-->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>1.20</version>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${jcasc.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
<version>1.13</version>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${jcasc.version}</version>
<scope>test</scope>
</dependency>
<!-- these dependencies are added to satisfy the enforcer rules -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.6</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
<scope>test</scope>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.222.x</artifactId>
<version>11</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +112,7 @@ public String getServerCaCertificate() {
return serverCaCertificate;
}

@Extension
@Extension @Symbol({"x509ClientCert", "dockerServer"})
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
@Override
public String getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -114,7 +115,8 @@ public DockerTool forNode(Node node, TaskListener log) throws IOException, Inter
}
}

@Extension public static class DescriptorImpl extends ToolDescriptor<DockerTool> {
@Extension @Symbol("dockerTool")
public static class DescriptorImpl extends ToolDescriptor<DockerTool> {

@Override public String getDisplayName() {
return "Docker";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<DockerToolInstaller> {

@Override
Expand Down
103 changes: 103 additions & 0 deletions src/test/java/org/jenkinsci/plugins/docker/commons/CasCTest.java
Original file line number Diff line number Diff line change
@@ -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<ToolProperty<?>, 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();
}
}
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 3186bed

Please sign in to comment.