From 18f27b3ca1a9c67c35d1a3d2749a94c530711a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Thu, 26 May 2016 21:10:25 +0200 Subject: [PATCH] #459 : Adapted regex to the 'official' regexps used by docker. 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. --- .../io/fabric8/maven/docker/util/ImageName.java | 14 ++++++++------ .../fabric8/maven/docker/util/ImageNameTest.java | 16 ++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/fabric8/maven/docker/util/ImageName.java b/src/main/java/io/fabric8/maven/docker/util/ImageName.java index 588ced916..2f46de088 100644 --- a/src/main/java/io/fabric8/maven/docker/util/ImageName.java +++ b/src/main/java/io/fabric8/maven/docker/util/ImageName.java @@ -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 @@ -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 }; diff --git a/src/test/java/io/fabric8/maven/docker/util/ImageNameTest.java b/src/test/java/io/fabric8/maven/docker/util/ImageNameTest.java index 0acf26eaf..be3ad7908 100644 --- a/src/test/java/io/fabric8/maven/docker/util/ImageNameTest.java +++ b/src/test/java/io/fabric8/maven/docker/util/ImageNameTest.java @@ -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 @@ -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());