diff --git a/kubernetes-server-mock/pom.xml b/kubernetes-server-mock/pom.xml index ce17236b2df..01ded81d7da 100644 --- a/kubernetes-server-mock/pom.xml +++ b/kubernetes-server-mock/pom.xml @@ -52,6 +52,13 @@ junit ${junit.version} + + + org.slf4j + slf4j-simple + ${slf4j.version} + test + diff --git a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java index d521637fb38..5b277dc20fe 100644 --- a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java +++ b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java @@ -28,22 +28,25 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class KubernetesAttributesExtractor implements AttributeExtractor { + private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesAttributesExtractor.class); + public static final String KIND = "kind"; public static final String NAME = "name"; public static final String NAMESPACE = "namespace"; - private static final String VERSION_GROUP = "(?[a-zA-z0-9-_\\/]+)"; - private static final String KIND_GROUP = "(?[^/]+)"; - private static final String NAME_GROUP = "(?[^/]+)"; - private static final String NAMESPACE_GROUP = "(?[^/]+)"; - - protected static final Pattern NAMESPACED_NAMED_PATH = Pattern.compile("/api[s]?/" + VERSION_GROUP + "/namespaces/" + NAMESPACE_GROUP + "/" + KIND_GROUP + "/" + NAME_GROUP + "[^ ]*"); - protected static final Pattern NON_NAMESPACED_NAMED_PATH = Pattern.compile("/api[s]?/" + "/" + KIND_GROUP + "/" + NAME_GROUP + "[^ ]*"); + private static final String API_GROUP = "/api[s]?(/extensions)?"; + private static final String VERSION_GROUP = "(/(?[a-zA-z0-9-_]+))?"; + private static final String KIND_GROUP = "/(?[^/?]+)"; + private static final String NAME_GROUP = "(/(?[^/?]+))?"; + private static final String NAMESPACE_GROUP = "(/namespaces/(?[^/]+))?"; + private static final String END_GROUP = "[^ /]*"; - protected static final Pattern NAMESPACED_CREATE_PATH = Pattern.compile("/api[s]?/" + VERSION_GROUP + "/namespaces/" + NAMESPACE_GROUP + "/" + KIND_GROUP + "[^ ]*"); - protected static final Pattern NON_NAMESPACED_CREATE_PATH = Pattern.compile("/api[s]?/" + "/" + KIND_GROUP + "[^ ]*"); + protected static final Pattern PATTERN = Pattern.compile(API_GROUP + VERSION_GROUP + NAMESPACE_GROUP + KIND_GROUP + NAME_GROUP + END_GROUP); @Override public AttributeSet fromPath(String s) { @@ -52,25 +55,11 @@ public AttributeSet fromPath(String s) { } //Get paths - Matcher m = NAMESPACED_NAMED_PATH.matcher(s); - if (m.matches()) { - return extract(m); - } - - m = NON_NAMESPACED_NAMED_PATH.matcher(s); + Matcher m = PATTERN.matcher(s); if (m.matches()) { - return extract(m); - } - - //Create paths - m = NAMESPACED_CREATE_PATH.matcher(s); - if (m.matches()) { - return extract(m); - } - - m = NON_NAMESPACED_CREATE_PATH.matcher(s); - if (m.matches()) { - return extract(m); + AttributeSet set = extract(m); + LOGGER.debug("fromPath {} : {}", s, set); + return set; } return new AttributeSet(); } @@ -102,26 +91,13 @@ public AttributeSet extract(String s) { } //Get paths - Matcher m = NAMESPACED_NAMED_PATH.matcher(s); - if (m.matches()) { - return extract(m); - } - - m = NON_NAMESPACED_NAMED_PATH.matcher(s); - if (m.matches()) { - return extract(m); - } - - //Create paths - m = NAMESPACED_CREATE_PATH.matcher(s); - if (m.matches()) { - return extract(m); - } - - m = NON_NAMESPACED_CREATE_PATH.matcher(s); + Matcher m = PATTERN.matcher(s); if (m.matches()) { - return extract(m); + AttributeSet set = extract(m); + LOGGER.debug("extract {} : {}", s, set); + return set; } + LOGGER.debug("extract {} : no attributes", s); return new AttributeSet(); } @@ -150,7 +126,9 @@ private static AttributeSet extract(Matcher m) { if (!Strings.isNullOrEmpty(kind)) { //Poor mans to singular. - if (kind.endsWith("s")) { + if (kind.endsWith("ses")) { + kind = kind.substring(0, kind.length() - 2); + } else if (kind.endsWith("s")) { kind = kind.substring(0, kind.length() - 1); } attributes = attributes.add(new Attribute(KIND, kind)); diff --git a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java index 22ba0d46857..b31991d3b1a 100644 --- a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java +++ b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java @@ -27,8 +27,12 @@ import java.util.List; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class KubernetesCrudDispatcher extends CrudDispatcher { + private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesCrudDispatcher.class); public KubernetesCrudDispatcher() { this(new KubernetesAttributesExtractor(), new KubernetesResponseComposer()); @@ -52,6 +56,7 @@ public MockResponse handleGet(String path) { for (Map.Entry entry : map.entrySet()) { if (entry.getKey().matches(query)) { + LOGGER.debug("Entry found for query {} : {}", query, entry); items.add(entry.getValue()); } } diff --git a/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java b/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java index c477eec142c..3a5a4a25f81 100644 --- a/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java +++ b/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java @@ -26,15 +26,26 @@ public class KubernetesAttributesExtractorTest { @Test - public void shouldHandleNamespacedPath() { + public void shouldHandleNamespacedPathWithResource() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod"); AttributeSet expected = new AttributeSet(); - expected.add(new Attribute("kind", "pod")); - expected.add(new Attribute("namespace", "myns")); - expected.add(new Attribute("name", "mypod")); - Assert.assertTrue(attributes.matches(expected)); + expected = expected.add(new Attribute("kind", "pod")); + expected = expected.add(new Attribute("namespace", "myns")); + expected = expected.add(new Attribute("name", "mypod")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + + @Test + public void shouldHandleNamespacedPath() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("kind", "pod")); + expected = expected.add(new Attribute("namespace", "myns")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); } @Test @@ -43,12 +54,20 @@ public void shouldHandleNonNamespacedPath() { AttributeSet attributes = extractor.extract("/api/v1/nodes/mynode"); AttributeSet expected = new AttributeSet(); - expected.add(new Attribute("kind", "node")); - expected.add(new Attribute("name", "mynode")); - Assert.assertTrue(attributes.matches(expected)); + expected = expected.add(new Attribute("kind", "node")); + expected = expected.add(new Attribute("name", "mynode")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); } + @Test + public void shouldHandlePathWithParameters() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/api/v1/pods?labelSelector=testKey%3DtestValue"); + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("kind", "pod")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } @Test public void shouldHandleResource() { @@ -58,10 +77,53 @@ public void shouldHandleResource() { AttributeSet attributes = extractor.extract(pod); AttributeSet expected = new AttributeSet(); - expected.add(new Attribute("kind", "pod")); - expected.add(new Attribute("namespace", "myns")); - expected.add(new Attribute("name", "mypod")); - Assert.assertTrue(attributes.matches(expected)); + expected = expected.add(new Attribute("kind", "pod")); + expected = expected.add(new Attribute("namespace", "myns")); + expected = expected.add(new Attribute("name", "mypod")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + + } + + @Test + public void shouldHandleKindWithoutVersion() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/api/pods"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("kind", "pod")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + + @Test + public void shouldHandleExtensions() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/deployments"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("kind", "deployment")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + + @Test + public void shouldHandleIngress() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("kind", "ingress")); + expected = expected.add(new Attribute("namespace", "myns")); + expected = expected.add(new Attribute("name", "myingress")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + @Test + public void shouldHandleIngresses() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("kind", "ingress")); + expected = expected.add(new Attribute("namespace", "myns")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); } }