Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #56 from liebowitz/master
[JENKINS-39181] Invalid fully qualified image name when registry URL specified
  • Loading branch information
oleg-nenashev committed Jan 11, 2017
2 parents 9d298f8 + acd1a92 commit 8edbc8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Expand Up @@ -228,14 +228,22 @@ public KeyMaterialFactory newKeyMaterialFactory(@CheckForNull Item context, @Non
* @return the full registry:port/namespace/name string
* @throws IOException
*/
public String imageName(String userAndRepo) throws IOException {
if (url==null) return userAndRepo;
public String imageName(@Nonnull String userAndRepo) throws IOException {
if (userAndRepo == null) {
throw new IllegalArgumentException("Image name cannot be null.");
}
if (url == null) {
return userAndRepo;
}
URL effectiveUrl = getEffectiveUrl();

StringBuilder s = new StringBuilder(effectiveUrl.getHost());
if (effectiveUrl.getPort() > 0 && effectiveUrl.getDefaultPort() != effectiveUrl.getPort()) {
s.append(':').append(effectiveUrl.getPort());
}
if (userAndRepo.startsWith(String.valueOf(s))) {
return userAndRepo;
}
return s.append('/').append(userAndRepo).toString();
}

Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;

import org.junit.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author Carlos Sanchez <carlos@apache.org>
Expand All @@ -52,6 +53,25 @@ public void testParseWithTags() throws Exception {
assertRegistry("https://docker.acme.com", "docker.acme.com/busybox:tag");
}

@Issue("JENKINS-39181")
@Test
public void testParseFullyQualifiedImageName() throws Exception {
assertEquals("private-repo:5000/test-image", new DockerRegistryEndpoint("http://private-repo:5000/", null).imageName("private-repo:5000/test-image"));
assertEquals("private-repo:5000/test-image", new DockerRegistryEndpoint("http://private-repo:5000/", null).imageName("test-image"));
}

@Issue("JENKINS-39181")
@Test(expected = IllegalArgumentException.class)
public void testParseNullImageName() throws Exception {
new DockerRegistryEndpoint("http://private-repo:5000/", null).imageName(null);
}

@Issue("JENKINS-39181")
@Test(expected = IllegalArgumentException.class)
public void testParseNullUrlAndImageName() throws Exception {
new DockerRegistryEndpoint(null, null).imageName(null);
}

private void assertRegistry(String url, String repo) throws IOException {
assertEquals(url, DockerRegistryEndpoint.fromImageName(repo, null).getEffectiveUrl().toString());
}
Expand Down

0 comments on commit 8edbc8e

Please sign in to comment.