Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -32,19 +32,16 @@

import io.fabric8.kubernetes.client.KubernetesClient;
import java.net.URL;
import java.util.logging.Logger;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.mockito.MockedStatic;

public class ClientAuthenticationTest {

private static final Logger LOGGER = Logger.getLogger(ClientAuthenticationTest.class.getName());
class ClientAuthenticationTest {

@Issue("JENKINS-76047")
@Test
public void testConnectCallsCreateClient() throws Exception {
void testConnectCallsCreateClient() throws Exception {
KubernetesClient mockClient = mock(KubernetesClient.class);
when(mockClient.getMasterUrl()).thenReturn(new URL("http://localhost:9999/"));

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);
}
}
Loading