Skip to content

Commit

Permalink
Assert the log message for blank or name cloud name
Browse files Browse the repository at this point in the history
Example from git plugin GitHooksTest was a good starting point.
  • Loading branch information
MarkEWaite committed Sep 3, 2023
1 parent 768da66 commit 36aba28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public DockerCloud(String name, DockerAPI dockerApi, List<DockerTemplate> templa
this.dockerApi = dockerApi;
this.templates = templates;
if (name == null || name.isBlank()) {
LOGGER.warn(
"Docker cloud created with an empty name. Docker cloud requires a non-blank name after Jenkins 2.402");
LOGGER.warn("Docker cloud requires a non-blank name after Jenkins 2.402");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsIterableContaining;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;

/**
* @author Kanstantsin Shautsou
Expand All @@ -34,6 +38,9 @@ public class DockerCloudTest {
@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Rule
public LoggerRule lr = new LoggerRule();

@SuppressWarnings("unused")
@Test
public void testConstructor_0_10_2() {
Expand All @@ -49,20 +56,26 @@ public void testConstructor_0_10_2() {
null); // dockerHostname
}

private static final String LOG_MESSAGE = "Docker cloud requires a non-blank name after Jenkins 2.402";

@Issue("JENKINS-70729") // Warn if cloud name is empty
@Test
public void testConstructorWithEmptyName() {
lr.record(DockerCloud.class.getName(), Level.ALL).capture(16);
DockerCloud cloud =
new DockerCloud("", new DockerAPI(new DockerServerEndpoint("uri", "credentialsId")), List.of());
Assert.assertEquals(cloud.getDisplayName(), "");
MatcherAssert.assertThat(lr.getMessages(), IsIterableContaining.hasItem(LOG_MESSAGE));
}

@Issue("JENKINS-70729") // Warn if cloud name is null
@Test
public void testConstructorWithNullName() {
lr.record(DockerCloud.class.getName(), Level.ALL).capture(16);
DockerCloud cloud =
new DockerCloud(null, new DockerAPI(new DockerServerEndpoint("uri", "credentialsId")), List.of());
Assert.assertEquals(cloud.getDisplayName(), null);
MatcherAssert.assertThat(lr.getMessages(), IsIterableContaining.hasItem(LOG_MESSAGE));
}

@Test
Expand Down

0 comments on commit 36aba28

Please sign in to comment.