KubernetesCrudAttributesExtractor extends KubernetesAttributesExtractor { @Override public AttributeSet extract(HasMetadata hasMetadata) { return extractMetadataAttributes(hasMetadata); } KubernetesCrudAttributesExtractor();  KubernetesCrudAttributesExtractor(List<CustomResourceDefinitionContext> crdContexts); @Override AttributeSet extract(HasMetadata hasMetadata);  }
@Test public void shouldHandleNamespacedPathWithResource() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "mypod")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleIngresses() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); 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")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleApiGroups() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor .extract("/apis/autoscaling/v1/namespaces/myns/horizontalpodautoscalers/myhpa"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "horizontalpodautoscaler")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "myhpa")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleCrds() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/apis/test.com/v1/namespaces/myns/crds/mycrd"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "crd")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "mycrd")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleLabelSelectorsWithOneLabel() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleLabelSelectorsWithDoubleEquals() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3D%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleLabelSelectorsWithTwoLabels() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname,age%3D37"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); expected = expected.add(new Attribute("labels:age", "37")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleLabelSelectorsWithADomain() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=example.com/name%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:example.com/name", "myname")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleNamespacedPath() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); 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")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleNonNamespacedPath() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/nodes/mynode"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "node")); expected = expected.add(new Attribute("name", "mynode")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandlePathWithParameters() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/pods?labelSelector=testKey%3DtestValue"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleResource() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); Pod pod = new PodBuilder().withNewMetadata().withName("mypod").withNamespace("myns").endMetadata().build(); AttributeSet attributes = extractor.extract(pod); AttributeSet expected1 = new AttributeSet(); expected1 = expected1.add(new Attribute("kind", "pod")); expected1 = expected1.add(new Attribute("namespace", "myns")); expected1 = expected1.add(new Attribute("name", "mypod")); assertFalse(attributes.matches(expected1)); AttributeSet expected2 = new AttributeSet(); expected2 = expected2.add(new Attribute("namespace", "myns")); expected2 = expected2.add(new Attribute("name", "mypod")); assertTrue(attributes.matches(expected2)); }
@Test public void shouldHandleResourceWithLabel() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); Map<String, String> labels = new HashMap<>(); labels.put("name", "myname"); Pod pod = new PodBuilder().withNewMetadata().withLabels(labels).endMetadata().build(); AttributeSet attributes = extractor.extract(pod); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleKindWithoutVersion() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleExtensions() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/apis/apps/v1/deployments"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "deployment")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleIngress() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); 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")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleEndpoints() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/endpoints"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "endpoints")); expected = expected.add(new Attribute("namespace", "myns")); assertTrue(attributes.matches(expected)); }