Skip to content

Commit

Permalink
Log a warning if a DockerCloud name is blank
Browse files Browse the repository at this point in the history
Do not break existing usage of blank names, but warn in the log file
that blank names are no longer supported after Jenkins 2.402.

The Jenkins 2.421 cloud creation UI will not allow a blank name, but
earlier versions may have allowed it.

Assert the log message for blank or name cloud name
  • Loading branch information
MarkEWaite committed Sep 3, 2023
1 parent 1fc91ca commit 1416f89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public DockerCloud(String name, DockerAPI dockerApi, List<DockerTemplate> templa
super(name);
this.dockerApi = dockerApi;
this.templates = templates;
if (name == null || name.isBlank()) {
LOGGER.warn("Docker cloud requires a non-blank name after Jenkins 2.402");
}
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +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 @@ -33,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 @@ -48,6 +56,28 @@ 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
public void globalConfigRoundtrip() throws Exception {

Expand Down

0 comments on commit 1416f89

Please sign in to comment.