-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add support for label selectors in the mock server #1158
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
import io.fabric8.kubernetes.api.model.PodBuilder; | ||
import io.fabric8.mockwebserver.crud.Attribute; | ||
import io.fabric8.mockwebserver.crud.AttributeSet; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
@@ -84,6 +86,20 @@ public void shouldHandleResource() { | |
|
||
} | ||
|
||
@Test | ||
public void shouldHandleResourceWithLabel() { | ||
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); | ||
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")); | ||
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a comment: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JUnit assertions are used through the rest of the file. I'm new to the project so I couldn't say why - I just aim for consistency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know I know, no worries. |
||
} | ||
|
||
@Test | ||
public void shouldHandleKindWithoutVersion() { | ||
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); | ||
|
@@ -151,4 +167,45 @@ public void shouldHandleCrds() { | |
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleLabelSelectorsWithOneLabel() { | ||
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); | ||
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")); | ||
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleLabelSelectorsWithDoubleEquals() { | ||
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); | ||
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")); | ||
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleLabelSelectorsWithTwoLabels() { | ||
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); | ||
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")); | ||
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleLabelSelectorsWithADomain() { | ||
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); | ||
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")); | ||
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to get rid of this hard coded string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from what @rohanKanojia suggests to have this
localhost
as constant I will do next things in this case. I know they might sounds weird but I have done this in several projects and works pretty well in terms of readable code.. Create a
private static final String PROTOCOL = http
. Create a
private static final String HOST = localhost
. Check if
s
starts with/
or not and in case of not, append it.. Do
HttpUrl.parse(String.format("%s://%s%s", PROTOCOL, HOST, s))
if
s
can contain the port as well, split the method betweenint port, String path
it is always a better practice to not merge concepts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!