|
| 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 | +} |
0 commit comments