Skip to content

Commit a3d8988

Browse files
committed
Configuration as Code support
Unit tests and Symbols
1 parent 508cb08 commit a3d8988

File tree

6 files changed

+154
-3
lines changed

6 files changed

+154
-3
lines changed

src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerCredentials.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import hudson.Extension;
2929
import hudson.Util;
3030
import hudson.util.Secret;
31+
import org.jenkinsci.Symbol;
3132
import org.kohsuke.stapler.DataBoundConstructor;
3233

3334
import javax.annotation.CheckForNull;
@@ -111,7 +112,7 @@ public String getServerCaCertificate() {
111112
return serverCaCertificate;
112113
}
113114

114-
@Extension
115+
@Extension @Symbol({"x509ClientCert", "dockerServer"})
115116
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
116117
@Override
117118
public String getDisplayName() {

src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerTool.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import javax.annotation.Nonnull;
4444
import javax.annotation.Nullable;
4545
import jenkins.model.Jenkins;
46+
import org.jenkinsci.Symbol;
4647
import org.kohsuke.stapler.DataBoundConstructor;
4748

4849
/**
@@ -114,7 +115,8 @@ public DockerTool forNode(Node node, TaskListener log) throws IOException, Inter
114115
}
115116
}
116117

117-
@Extension public static class DescriptorImpl extends ToolDescriptor<DockerTool> {
118+
@Extension @Symbol("dockerTool")
119+
public static class DescriptorImpl extends ToolDescriptor<DockerTool> {
118120

119121
@Override public String getDisplayName() {
120122
return "Docker";

src/main/java/org/jenkinsci/plugins/docker/commons/tools/DockerToolInstaller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.regex.Pattern;
4545
import javax.annotation.Nonnull;
4646
import jenkins.security.MasterToSlaveCallable;
47+
import org.jenkinsci.Symbol;
4748
import org.kohsuke.stapler.DataBoundConstructor;
4849

4950
/**
@@ -165,7 +166,7 @@ private static VersionNumber parseVersion(String version) {
165166
throw new IllegalArgumentException("Failed to parse version " + version);
166167
}
167168

168-
@Extension
169+
@Extension @Symbol("fromDocker")
169170
public static class DescriptorImpl extends ToolInstallerDescriptor<DockerToolInstaller> {
170171

171172
@Override
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.jenkinsci.plugins.docker.commons;
2+
3+
import com.cloudbees.plugins.credentials.CredentialsMatchers;
4+
import com.cloudbees.plugins.credentials.CredentialsProvider;
5+
import com.cloudbees.plugins.credentials.common.IdCredentials;
6+
import hudson.security.ACL;
7+
import hudson.tools.InstallSourceProperty;
8+
import hudson.tools.ToolProperty;
9+
import hudson.tools.ToolPropertyDescriptor;
10+
import hudson.util.DescribableList;
11+
import hudson.util.Secret;
12+
import io.jenkins.plugins.casc.misc.RoundTripAbstractTest;
13+
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
14+
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerDomainRequirement;
15+
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
16+
import org.jenkinsci.plugins.docker.commons.tools.DockerToolInstaller;
17+
import org.junit.runner.RunWith;
18+
import org.junit.runners.Parameterized;
19+
import org.jvnet.hudson.test.RestartableJenkinsRule;
20+
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
23+
import static org.hamcrest.collection.ArrayMatching.arrayContaining;
24+
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
25+
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
26+
import static org.hamcrest.core.AllOf.allOf;
27+
import static org.hamcrest.core.IsEqual.equalTo;
28+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
29+
import static org.hamcrest.core.IsNull.nullValue;
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
32+
33+
@RunWith(Parameterized.class)
34+
public class CasCTest extends RoundTripAbstractTest {
35+
36+
final String resource;
37+
38+
public CasCTest(final String resource) {
39+
this.resource = resource;
40+
}
41+
42+
@Parameterized.Parameters(name = "{0}")
43+
public static Object[][] params() {
44+
return new Object[][]{{"casc_bare.yaml"}, {"casc_symbols.yaml"}};
45+
}
46+
47+
@Override
48+
protected void assertConfiguredAsExpected(final RestartableJenkinsRule j, final String s) {
49+
50+
//The credentials
51+
final IdCredentials cred = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(IdCredentials.class, j.j.jenkins,
52+
ACL.SYSTEM, new DockerServerDomainRequirement()), CredentialsMatchers.withId("dockerx509"));
53+
assertNotNull(cred);
54+
assertThat(cred, instanceOf(DockerServerCredentials.class));
55+
DockerServerCredentials dCreds = (DockerServerCredentials) cred;
56+
assertEquals("THE CLIENT", dCreds.getClientCertificate());
57+
assertEquals("THE SERVER", dCreds.getServerCaCertificate());
58+
assertEquals("Be wewy wewy cuwiet", Secret.toString(dCreds.getClientKeySecret()));
59+
assertEquals("Docker X.509", dCreds.getDescription());
60+
61+
62+
//The ToolInstaller
63+
final DockerTool[] installations = j.j.jenkins.getDescriptorByType(DockerTool.DescriptorImpl.class).getInstallations();
64+
assertNotNull(installations);
65+
assertThat(installations, arrayWithSize(2));
66+
assertThat(installations, arrayContaining(
67+
allOf(
68+
hasProperty("name", equalTo("docker-latest")),
69+
hasProperty("home", nullValue())
70+
),
71+
allOf(
72+
hasProperty("name", equalTo("docker-native")),
73+
hasProperty("home", equalTo("/etc/docket/docker"))
74+
)
75+
));
76+
final DescribableList<ToolProperty<?>, ToolPropertyDescriptor> properties = installations[0].getProperties();
77+
assertThat(properties, contains(instanceOf(InstallSourceProperty.class)));
78+
final InstallSourceProperty property = (InstallSourceProperty) properties.get(0);
79+
assertThat(property.installers, contains(
80+
allOf(
81+
instanceOf(DockerToolInstaller.class),
82+
hasProperty("version", equalTo("latest"))
83+
)
84+
));
85+
86+
/*
87+
* DockerRegistryEndpoint is not directly used in this plugin in any global config sense,
88+
* So it is better tested in the plugins that uses it for example docker workflow plugin:
89+
* https://github.com/jenkinsci/docker-workflow-plugin/blob/2ba1ac97b75a3f188e243333b31ef06d55b9221a/src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/GlobalConfig.java
90+
*/
91+
92+
}
93+
94+
@Override
95+
protected String configResource() {
96+
return resource;
97+
}
98+
99+
@Override
100+
protected String stringInLogExpected() {
101+
return DockerServerCredentials.class.getName();
102+
}
103+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
credentials:
2+
system:
3+
domainCredentials:
4+
- credentials:
5+
- dockerServer:
6+
clientCertificate: "THE CLIENT"
7+
clientKeySecret: "Be wewy wewy cuwiet"
8+
description: "Docker X.509"
9+
id: "dockerx509"
10+
scope: GLOBAL
11+
serverCaCertificate: "THE SERVER"
12+
tool:
13+
dockerTool:
14+
installations:
15+
- name: "docker-latest"
16+
properties:
17+
- installSource:
18+
installers:
19+
- docker:
20+
version: "latest"
21+
- home: "/etc/docket/docker"
22+
name: "docker-native"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
credentials:
2+
system:
3+
domainCredentials:
4+
- credentials:
5+
- x509ClientCert:
6+
clientCertificate: "THE CLIENT"
7+
clientKeySecret: "Be wewy wewy cuwiet"
8+
description: "Docker X.509"
9+
id: "dockerx509"
10+
scope: GLOBAL
11+
serverCaCertificate: "THE SERVER"
12+
tool:
13+
dockerTool:
14+
installations:
15+
- name: "docker-latest"
16+
properties:
17+
- installSource:
18+
installers:
19+
- fromDocker:
20+
version: "latest"
21+
- home: "/etc/docket/docker"
22+
name: "docker-native"

0 commit comments

Comments
 (0)