Skip to content

Migrate tests to JUnit5 #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3944.v1a_e4f8b_452db_</version>
<version>4136.vca_c3202a_7fd1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
105 changes: 0 additions & 105 deletions src/test/java/org/jenkinsci/plugins/docker/commons/CasCTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,53 @@

package org.jenkinsci.plugins.docker.commons;

import hudson.util.Secret;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.docker.commons.util.SampleDockerBuilder;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.tools.ToolProperty;
import hudson.util.Secret;
import java.util.Collections;
import org.junit.Test;
import org.junit.Rule;
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.docker.commons.util.SampleDockerBuilder;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@WithJenkins
class ConfigTest {

public class ConfigTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test public void configRoundTrip() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
IdCredentials serverCredentials = new DockerServerCredentials(CredentialsScope.GLOBAL, "serverCreds", null, Secret.fromString("clientKey"), "clientCertificate", "serverCaCertificate");
@Test
void configRoundTrip(JenkinsRule r) throws Exception {
CredentialsStore store =
CredentialsProvider.lookupStores(r.jenkins).iterator().next();
IdCredentials serverCredentials = new DockerServerCredentials(
CredentialsScope.GLOBAL,
"serverCreds",
null,
Secret.fromString("clientKey"),
"clientCertificate",
"serverCaCertificate");
store.addCredentials(Domain.global(), serverCredentials);
IdCredentials registryCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "registryCreds", null, "me", "pass");
IdCredentials registryCredentials =
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "registryCreds", null, "me", "pass");
store.addCredentials(Domain.global(), registryCredentials);
SampleDockerBuilder b1 = new SampleDockerBuilder(new DockerServerEndpoint("", ""), new DockerRegistryEndpoint("http://dhe.mycorp.com/", registryCredentials.getId()));
SampleDockerBuilder b1 = new SampleDockerBuilder(
new DockerServerEndpoint("", ""),
new DockerRegistryEndpoint("http://dhe.mycorp.com/", registryCredentials.getId()));
r.assertEqualDataBoundBeans(b1, r.configRoundtrip(b1));
b1 = new SampleDockerBuilder(new DockerServerEndpoint("tcp://192.168.1.104:8333", serverCredentials.getId()), new DockerRegistryEndpoint("", ""));
b1 = new SampleDockerBuilder(
new DockerServerEndpoint("tcp://192.168.1.104:8333", serverCredentials.getId()),
new DockerRegistryEndpoint("", ""));
r.assertEqualDataBoundBeans(b1, r.configRoundtrip(b1));
r.jenkins.getDescriptorByType(DockerTool.DescriptorImpl.class).setInstallations(new DockerTool("Docker 1.5", "/usr/local/docker15", Collections.<ToolProperty<?>>emptyList()));
r.jenkins
.getDescriptorByType(DockerTool.DescriptorImpl.class)
.setInstallations(new DockerTool("Docker 1.5", "/usr/local/docker15", Collections.emptyList()));
b1.setToolName("Docker 1.5");
r.assertEqualDataBoundBeans(b1, r.configRoundtrip(b1));
}

}
119 changes: 119 additions & 0 deletions src/test/java/org/jenkinsci/plugins/docker/commons/JCasCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.jenkinsci.plugins.docker.commons;

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.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

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.junit.jupiter.AbstractRoundTripTest;
import java.util.Collections;
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.jupiter.api.Nested;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

class JCasCTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the rename?

Copy link
Contributor Author

@strangelookingnerd strangelookingnerd May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh, I think it was mainly to satisfy my inner Monk. It's the "official" naming, isn't it? Most similar implementations in other plugins are named the same way.


@Nested
@WithJenkins
class Bare extends AbstractRoundTripTest {

@Override
protected void assertConfiguredAsExpected(JenkinsRule j, String configContent) {
JCasCTest.assertConfiguredAsExpected(j);
}

@Override
protected String configResource() {
return "casc_bare.yaml";
}

@Override
protected String stringInLogExpected() {
return DockerServerCredentials.class.getName();
}
}

@Nested
@WithJenkins
class Symbols extends AbstractRoundTripTest {

@Override
protected void assertConfiguredAsExpected(JenkinsRule j, String configContent) {
JCasCTest.assertConfiguredAsExpected(j);
}

@Override
protected String configResource() {
return "casc_symbols.yaml";
}

@Override
protected String stringInLogExpected() {
return DockerServerCredentials.class.getName();
}
}

private static void assertConfiguredAsExpected(JenkinsRule j) {
// The credentials
final IdCredentials cred = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentialsInItemGroup(
IdCredentials.class,
j.jenkins,
ACL.SYSTEM2,
Collections.singletonList(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.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
*/

}
}
Loading