Skip to content

Commit

Permalink
#459 : Adapted regex to the 'official' regexps used by docker.
Browse files Browse the repository at this point in the history
tbh, https://github.com/docker/docker/blob/master/vendor/src/github.com/docker/distribution/reference/regexp.go goes a bit over the top with abstrcaction. would be so cool if they used plain regexps, not that supposed to be super-intelligent abstraction.
  • Loading branch information
rhuss committed May 26, 2016
1 parent 9808a5f commit 18f27b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/main/java/io/fabric8/maven/docker/util/ImageName.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ public static void validate(String image) {
// Validations patterns, taken directly from the docker source
// https://github.com/docker/docker/blob/master/vendor/src/github.com/docker/distribution/reference/regexp.go
// https://github.com/docker/docker/blob/master/vendor/src/github.com/docker/distribution/reference/reference.go
private final Pattern NAME_COMP_REGEXP = Pattern.compile("[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*");
private final Pattern COMP_PATH_REGEXP = Pattern.compile("([/]?[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)*");
private final String hPartPattern = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
private final Pattern REGISTRY_REGEXP = Pattern.compile("^" + hPartPattern + "(?:\\." + hPartPattern + ")*(?::[0-9]+)?$");
private final String nameComponentRegexp = "[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*";
private final String hostnameComponentRegexp = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
private final String hostnameRegexp = hostnameComponentRegexp + "(?:(?:\\." + hostnameComponentRegexp + ")+)?(?::[0-9]+)?";
private final Pattern NAME_COMP_REGEXP = Pattern.compile(nameComponentRegexp);
private final Pattern IMAGE_NAME_REGEXP = Pattern.compile("(?:" + hostnameRegexp + "/)?" + nameComponentRegexp + "(?:/" + nameComponentRegexp + ")?");
private final Pattern REGISTRY_REGEXP = Pattern.compile("^" + hostnameComponentRegexp + "(?:\\." + hostnameComponentRegexp + ")*(?::[0-9]+)?$");
private final Pattern TAG_REGEXP = Pattern.compile("^[\\w][\\w.-]{0,127}$");

// Validate parts and throw an IllegalArgumentException if a part is not valid
Expand All @@ -216,8 +218,8 @@ private void doValidate() {
// Stripp of user from repository name
String image = user != null ? repository.substring(user.length() + 1) : repository;
Object[] checks = new Object[] {
"registry",REGISTRY_REGEXP, registry,
"image", COMP_PATH_REGEXP, image,
"registry", REGISTRY_REGEXP, registry,
"image", IMAGE_NAME_REGEXP, image,
"user", NAME_COMP_REGEXP, user,
"tag", TAG_REGEXP, tag
};
Expand Down
16 changes: 6 additions & 10 deletions src/test/java/io/fabric8/maven/docker/util/ImageNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ public void simple() {
.fullName("consol/tomcat-8.0").fullNameWithTag("consol/tomcat-8.0:8.0.9")
};

for (int i = 0; i < data.length; i += 2) {
ImageName name = new ImageName((String) data[i]);
Res res = (Res) data[i+1];
assertEquals("Registry " + i,res.registry,name.getRegistry());
assertEquals("Repository " + i,res.repository,name.getRepository());
assertEquals("Tag " + i,res.tag,name.getTag());
assertEquals("RepoWithRegistry " + i,res.fullName, name.getNameWithoutTag(null));
assertEquals("FullName " + i,res.fullNameWithTag,name.getFullName(null));
}
verifyData(data);
}

@Test
Expand Down Expand Up @@ -87,9 +79,13 @@ public void testMultipleSubComponents() {
.fullName("org/jolokia_demo").fullNameWithTag("org/jolokia_demo:0.9.6"),
};

verifyData(data);
}

private void verifyData(Object[] data) {
for (int i = 0; i < data.length; i += 2) {
ImageName name = new ImageName((String) data[i]);
Res res = (Res) data[i+1];
Res res = (Res) data[i + 1];
assertEquals("Registry " + i,res.registry,name.getRegistry());
assertEquals("Repository " + i,res.repository,name.getRepository());
assertEquals("Tag " + i,res.tag,name.getTag());
Expand Down

0 comments on commit 18f27b3

Please sign in to comment.