Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KubernetesAttributesExtractor is not testing anything #977

Merged
merged 7 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions kubernetes-server-mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<HasMetadata> {

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 = "(?<version>[a-zA-z0-9-_\\/]+)";
private static final String KIND_GROUP = "(?<kind>[^/]+)";
private static final String NAME_GROUP = "(?<name>[^/]+)";
private static final String NAMESPACE_GROUP = "(?<namespace>[^/]+)";

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 = "(/(?<version>[a-zA-z0-9-_]+))?";
private static final String KIND_GROUP = "/(?<kind>[^/?]+)";
private static final String NAME_GROUP = "(/(?<name>[^/?]+))?";
private static final String NAMESPACE_GROUP = "(/namespaces/(?<namespace>[^/]+))?";
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


@Override
public AttributeSet fromPath(String s) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for now this will do.

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -52,6 +56,7 @@ public MockResponse handleGet(String path) {

for (Map.Entry<AttributeSet, String> entry : map.entrySet()) {
if (entry.getKey().matches(query)) {
LOGGER.debug("Entry found for query {} : {}", query, entry);
items.add(entry.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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));
}
}