Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<spotbugs.effort>Max</spotbugs.effort>
<spotbugs.threshold>Low</spotbugs.threshold>
<spotless.check.skip>false</spotless.check.skip>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -261,13 +262,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
package org.csanchez.jenkins.plugins.kubernetes;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import edu.umd.cs.findbugs.annotations.NonNull;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.utils.Serialization;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.csanchez.jenkins.plugins.kubernetes.pod.decorator.PodDecorator;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;

abstract class AbstractGoldenFileTest {

protected KubernetesCloud cloud;
protected PodDecorator decorator;

@Before
public void setUpCloud() {
@BeforeEach
void beforeEach() {
decorator = newDecorator();
cloud = new KubernetesCloud("test");
}

protected abstract PodDecorator newDecorator();

protected void test(String name) throws IOException {
protected void test(String name) throws Exception {
var beforeYAML = loadFileAsStream(name + "-before.yaml");
var before = Serialization.unmarshal(beforeYAML, Pod.class);
assertEquals(name + "-before.yaml is not normalized", Serialization.asYaml(before), beforeYAML);
assertEquals(Serialization.asYaml(before), beforeYAML, name + "-before.yaml is not normalized");
var afterYAML = loadFileAsStream(name + "-after.yaml");
var after = decorator.decorate(cloud, before);
assertEquals(name + "-after.yaml processed", Serialization.asYaml(after), afterYAML);
assertEquals(Serialization.asYaml(after), afterYAML, name + "-after.yaml processed");
}

@NonNull
private String loadFileAsStream(String name) throws IOException {
private String loadFileAsStream(String name) throws Exception {
var is = getClass().getResourceAsStream(getClass().getSimpleName() + "/" + name);
if (is == null) {
throw new IllegalStateException("Test file \"src/test/resources/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@

package org.csanchez.jenkins.plugins.kubernetes;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class ConfigMapVolumeTest {
class ConfigMapVolumeTest {

@Test
public void testNullSubPathValue() {
void testNullSubPathValue() {
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
assertNull(configMapVolume.getSubPath());
}

@Test
public void testValidSubPathValue() {
void testValidSubPathValue() {
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
configMapVolume.setSubPath("miSubpath");
assertEquals(configMapVolume.getSubPath(), "miSubpath");
assertEquals("miSubpath", configMapVolume.getSubPath());
Copy link
Member

Choose a reason for hiding this comment

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

👍

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@

package org.csanchez.jenkins.plugins.kubernetes;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import hudson.util.FormValidation;
import java.util.Collections;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class ContainerTemplateTest {
class ContainerTemplateTest {

@Test
public void testCopyConstructorCreatesEqualInstance() {
void testCopyConstructorCreatesEqualInstance() {
ContainerTemplate originalTemplate = new ContainerTemplate("myname", "myimage");
originalTemplate.setPrivileged(true);
originalTemplate.setAlwaysPullImage(true);
Expand All @@ -43,31 +44,31 @@ public void testCopyConstructorCreatesEqualInstance() {

ContainerTemplate clonedTemplate = new ContainerTemplate(originalTemplate);

assertEquals("Cloned ContainerTemplate is not equal to the original one!", originalTemplate, clonedTemplate);
assertEquals(originalTemplate, clonedTemplate, "Cloned ContainerTemplate is not equal to the original one!");
assertEquals(
"String representation (toString()) of the cloned and original ContainerTemplate is not equal!",
originalTemplate.toString(),
clonedTemplate.toString());
clonedTemplate.toString(),
"String representation (toString()) of the cloned and original ContainerTemplate is not equal!");
}

@SuppressWarnings("ResultOfObjectAllocationIgnored")
@Test
public void badImage() throws Exception {
void badImage() {
new ContainerTemplate("n", "something");
assertEquals(FormValidation.Kind.OK, new ContainerTemplate.DescriptorImpl().doCheckImage("something").kind);
for (String empty : new String[] {null, ""}) {
assertThrows("rejected " + empty, IllegalArgumentException.class, () -> new ContainerTemplate("n", empty));
assertThrows(IllegalArgumentException.class, () -> new ContainerTemplate("n", empty), "rejected " + empty);
assertEquals(
"tolerating " + empty + " during form validation",
FormValidation.Kind.OK,
new ContainerTemplate.DescriptorImpl().doCheckImage(empty).kind);
new ContainerTemplate.DescriptorImpl().doCheckImage(empty).kind,
"tolerating " + empty + " during form validation");
}
for (String bad : new String[] {" ", " something"}) {
assertThrows("rejected " + bad, IllegalArgumentException.class, () -> new ContainerTemplate("n", bad));
assertThrows(IllegalArgumentException.class, () -> new ContainerTemplate("n", bad), "rejected " + bad);
assertEquals(
"rejected " + bad,
FormValidation.Kind.ERROR,
new ContainerTemplate.DescriptorImpl().doCheckImage(bad).kind);
new ContainerTemplate.DescriptorImpl().doCheckImage(bad).kind,
"rejected " + bad);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.deletePods;
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.getLabels;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.Result;
import org.csanchez.jenkins.plugins.kubernetes.pipeline.AbstractKubernetesPipelineTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class KubectlBuildWrapperTest extends AbstractKubernetesPipelineTest {
@Before
public void setUp() throws Exception {
class KubectlBuildWrapperTest extends AbstractKubernetesPipelineTest {

@BeforeEach
void beforeEach() throws Exception {
deletePods(cloud.connect(), getLabels(cloud, this, name), false);
assertNotNull(createJobThenScheduleRun());
UsernamePasswordCredentialsImpl creds = new UsernamePasswordCredentialsImpl(
Expand All @@ -23,13 +24,13 @@ public void setUp() throws Exception {
}

@Test
public void kubectlBuildWrapper_missingCredentials() throws Exception {
void kubectlBuildWrapper_missingCredentials() throws Exception {
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
r.assertLogContains("No credentials found for id \"abcd\"", b);
}

@Test
public void kubectlBuildWrapper_invalidCredentials() throws Exception {
void kubectlBuildWrapper_invalidCredentials() throws Exception {
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
r.assertLogContains("Unable to connect to the server", b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
*/
package org.csanchez.jenkins.plugins.kubernetes;

import static org.junit.Assert.assertEquals;

import java.util.function.Consumer;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Always;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class KubernetesClientProviderTest {
class KubernetesClientProviderTest {

@Test
public void testGetValidity() {
void testGetValidity() {
KubernetesCloud cloud = new KubernetesCloud("foo");
// changes to these properties should trigger different validity value
checkValidityChanges(
Expand All @@ -60,15 +58,16 @@ public void testGetValidity() {
c -> c.setDirectConnection(true));

// verify stability
assertEquals(KubernetesClientProvider.getValidity(cloud), KubernetesClientProvider.getValidity(cloud));
Assertions.assertEquals(
KubernetesClientProvider.getValidity(cloud), KubernetesClientProvider.getValidity(cloud));
}

private void checkValidityChanges(KubernetesCloud cloud, Consumer<KubernetesCloud>... mutations) {
checkValidity(cloud, Assert::assertNotEquals, mutations);
checkValidity(cloud, Assertions::assertNotEquals, mutations);
}

private void checkValidityDoesNotChange(KubernetesCloud cloud, Consumer<KubernetesCloud>... mutations) {
checkValidity(cloud, Assert::assertEquals, mutations);
checkValidity(cloud, Assertions::assertEquals, mutations);
}

private void checkValidity(
Expand All @@ -78,12 +77,12 @@ private void checkValidity(
for (Consumer<KubernetesCloud> mut : mutations) {
mut.accept(cloud);
int after = KubernetesClientProvider.getValidity(cloud);
validityAssertion.doAssert("change #" + count++ + " of " + mutations.length, v, after);
validityAssertion.doAssert(v, after, "change #" + count++ + " of " + mutations.length);
v = after;
}
}

interface ValidityAssertion {
void doAssert(String message, int before, int after);
void doAssert(int before, int after, String message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,80 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

import hudson.ExtensionList;
import io.jenkins.cli.shaded.org.apache.commons.io.FileUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import jenkins.security.FIPS140;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.FlagRule;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.hudson.test.recipes.LocalData;

public class KubernetesCloudFIPSTest {
@WithJenkins
class KubernetesCloudFIPSTest {

@ClassRule
public static FlagRule<String> fipsFlag = FlagRule.systemProperty(FIPS140.class.getName() + ".COMPLIANCE", "true");
private static String fipsFlag;

@Rule
public JenkinsRule r = new JenkinsRule();
private JenkinsRule r;

@BeforeAll
static void beforeAll() {
fipsFlag = System.setProperty(FIPS140.class.getName() + ".COMPLIANCE", "true");
}

@BeforeEach
void beforeEach(JenkinsRule rule) {
r = rule;
}

@AfterAll
static void afterAll() {
if (fipsFlag != null) {
System.setProperty(FIPS140.class.getName() + ".COMPLIANCE", fipsFlag);
} else {
System.clearProperty(FIPS140.class.getName() + ".COMPLIANCE");
}
}

@Test
@Issue("JENKINS-73460")
public void onlyFipsCompliantValuesAreAcceptedTest() throws IOException {
void onlyFipsCompliantValuesAreAcceptedTest() throws Exception {
KubernetesCloud cloud = new KubernetesCloud("test-cloud");
assertThrows(IllegalArgumentException.class, () -> cloud.setSkipTlsVerify(true));
cloud.setSkipTlsVerify(false);
assertThrows(IllegalArgumentException.class, () -> cloud.setServerUrl("http://example.org"));
cloud.setServerUrl("https://example.org");
assertThrows(
"Invalid certificates throw exception",
IllegalArgumentException.class,
() -> cloud.setServerCertificate(getCert("not-a-cert")));
() -> cloud.setServerCertificate(getCert("not-a-cert")),
"Invalid certificates throw exception");
Throwable exception = assertThrows(
"Invalid length", IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("rsa1024")));
IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("rsa1024")), "Invalid length");
assertThat(exception.getLocalizedMessage(), containsString("2048"));
cloud.setServerCertificate(getCert("rsa2048"));
exception = assertThrows(
"invalid length", IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("dsa1024")));
IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("dsa1024")), "invalid length");
assertThat(exception.getLocalizedMessage(), containsString("2048"));
cloud.setServerCertificate(getCert("dsa2048"));
exception = assertThrows(
"Invalid field size",
IllegalArgumentException.class,
() -> cloud.setServerCertificate(getCert("ecdsa192")));
() -> cloud.setServerCertificate(getCert("ecdsa192")),
"Invalid field size");
assertThat(exception.getLocalizedMessage(), containsString("224"));
cloud.setServerCertificate(getCert("ecdsa224"));
}

@Test
@Issue("JENKINS-73460")
@LocalData
public void nonCompliantCloudsAreCleanedTest() {
void nonCompliantCloudsAreCleanedTest() {
assertThat("compliant-cloud is loaded", r.jenkins.getCloud("compliant-cloud"), notNullValue());
assertThat(
"no certificate is a valid cloud",
Expand All @@ -72,7 +90,7 @@ public void nonCompliantCloudsAreCleanedTest() {

@Test
@Issue("JENKINS-73460")
public void formValidationTest() throws IOException {
void formValidationTest() throws Exception {
ExtensionList<KubernetesCloud.DescriptorImpl> descriptors =
ExtensionList.lookup(KubernetesCloud.DescriptorImpl.class);
KubernetesCloud.DescriptorImpl descriptor = descriptors.stream()
Expand Down Expand Up @@ -109,7 +127,7 @@ public void formValidationTest() throws IOException {
notNullValue());
}

private String getCert(String alg) throws IOException {
private String getCert(String alg) throws Exception {
return FileUtils.readFileToString(
Paths.get("src/test/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudFIPSTest/certs")
.resolve(alg)
Expand Down
Loading