Skip to content

Commit

Permalink
Convert empty or blank name to non-blank in readResolve
Browse files Browse the repository at this point in the history
Jenkins 2.403 improves the cloud management user interface.  Each cloud is
now managed from a separate page.  That separate page requires a non-blank
name for the cloud.  Jenkins 2.402 and later allowed a blank name.

When a blank name is detected, convert it to a non-blank name based on
the hashCode of the DockerAPI and templates.
  • Loading branch information
MarkEWaite committed Sep 4, 2023
1 parent d2092ac commit 4579c0c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public DockerCloud(String name, DockerAPI dockerApi, List<DockerTemplate> templa
}
}

/* Constructor to create a new DockerCloud based on an existing DockerCloud */
public DockerCloud(@NonNull String name, @NonNull DockerCloud source) {
super(name);
this.dockerApi = source.dockerApi;
this.templates = source.templates;
}

@Deprecated
public DockerCloud(
String name,
Expand Down Expand Up @@ -731,7 +738,9 @@ protected Object readResolve() {
}

if (name == null || name.isBlank()) {

Check warning on line 740 in src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 740 is only partially covered, 4 branches are missing
LOGGER.warn("Docker cloud requires a non-blank name after Jenkins 2.402");
String newName = "docker-cloud-" + Integer.toHexString(hashCode());

Check warning on line 741 in src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 741 is not covered by tests
LOGGER.warn("Docker cloud requires a non-blank name after Jenkins 2.402, using '" + newName + "'");

Check warning on line 742 in src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 742 is not covered by tests
return new DockerCloud(newName, this);

Check warning on line 743 in src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 743 is not covered by tests
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public void testConstructorWithNullName() {
MatcherAssert.assertThat(lr.getMessages(), IsIterableContaining.hasItem(LOG_MESSAGE));
}

@Issue("JENKINS-70729") // Handle null or empty cloud name
@Test
public void testCopyConstructor() {
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));
String newName = "docker-cloud-" + Integer.toHexString(cloud.hashCode());
DockerCloud copy = new DockerCloud(newName, cloud);
Assert.assertEquals(cloud.getDockerApi(), copy.getDockerApi());
Assert.assertEquals(cloud.getTemplates().hashCode(), copy.getTemplates().hashCode());
Assert.assertEquals(newName, copy.getDisplayName());
}

@Test
public void globalConfigRoundtrip() throws Exception {

Expand Down

0 comments on commit 4579c0c

Please sign in to comment.