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

Add support for label selectors in the mock server #1158

Merged
merged 3 commits into from
Jul 31, 2018

Conversation

drigz
Copy link
Contributor

@drigz drigz commented Jul 27, 2018

This supports simple label selectors (eg foo=bar). The != operator would
go beyond the limits of the AttributeSet-based matching in
CrudDispatcher.

There was a test using labels in ConfigMapCrudTest, but it was only
passing because, at the point of execution, all resources match the
label selector. This change moves changes the order of assertions so
that the effect of the label selector is observable.

This should fix #1125, although I haven't tested that code directly.

This supports simple label selectors (eg foo=bar). The != operator would
go beyond the limits of the AttributeSet-based matching in
CrudDispatcher.

There was a test using labels in ConfigMapCrudTest, but it was only
passing because, at the point of execution, all resources match the
label selector. This change moves changes the order of assertions so
that the effect of the label selector is observable.

This should fix fabric8io#1125, although I haven't tested that code directly.
@drigz drigz force-pushed the label-selector-support branch from 7933ded to d592d2d Compare July 27, 2018 16:47
@rohanKanojia rohanKanojia requested a review from oscerd July 27, 2018 18:35
@rohanKanojia
Copy link
Member

ok to test

It was only passing before because the label selector was ignored.
private static final String VALUE_GROUP = "(?<value>[a-zA-Z0-9-_.]+)";
private static final Pattern LABEL_REQUIREMENT_EQUALITY = Pattern.compile(KEY_GROUP + EQUALITY_GROUP + VALUE_GROUP);

private HttpUrl parseUrlFromPathAndQuery(String s) {
Copy link
Member

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?

Copy link
Contributor

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 between int port, String path it is always a better practice to not merge concepts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks!

private static final String VALUE_GROUP = "(?<value>[a-zA-Z0-9-_.]+)";
private static final Pattern LABEL_REQUIREMENT_EQUALITY = Pattern.compile(KEY_GROUP + EQUALITY_GROUP + VALUE_GROUP);

private HttpUrl parseUrlFromPathAndQuery(String s) {
Copy link
Contributor

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 between int port, String path it is always a better practice to not merge concepts.


AttributeSet expected = new AttributeSet();
expected = expected.add(new Attribute("labels:name", "myname"));
Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected));
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a comment:
The project already imports AssertJ, why use JUnit assertions which are not readable at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I know I know, no worries.

As recommended by @lordofthejars. I renamed PROTOCOL to SCHEME to match
the convention of HttpUrl.
Copy link
Member

@rohanKanojia rohanKanojia left a comment

Choose a reason for hiding this comment

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

Thanks a lot 👍

@rohanKanojia rohanKanojia merged commit 79d38b1 into fabric8io:master Jul 31, 2018
rohanKanojia pushed a commit to rohanKanojia/kubernetes-client that referenced this pull request Aug 28, 2018
* Support label selectors in mock server

This supports simple label selectors (eg foo=bar). The != operator would
go beyond the limits of the AttributeSet-based matching in
CrudDispatcher.

There was a test using labels in ConfigMapCrudTest, but it was only
passing because, at the point of execution, all resources match the
label selector. This change moves changes the order of assertions so
that the effect of the label selector is observable.

This should fix fabric8io#1125, although I haven't tested that code directly.

* Fix PodCrudTest's use of label selector

It was only passing before because the label selector was ignored.

* Improve flexibility of URL parsing

As recommended by @lordofthejars. I renamed PROTOCOL to SCHEME to match
the convention of HttpUrl.
rohanKanojia pushed a commit to rohanKanojia/kubernetes-client that referenced this pull request Aug 28, 2018
* Support label selectors in mock server

This supports simple label selectors (eg foo=bar). The != operator would
go beyond the limits of the AttributeSet-based matching in
CrudDispatcher.

There was a test using labels in ConfigMapCrudTest, but it was only
passing because, at the point of execution, all resources match the
label selector. This change moves changes the order of assertions so
that the effect of the label selector is observable.

This should fix fabric8io#1125, although I haven't tested that code directly.

* Fix PodCrudTest's use of label selector

It was only passing before because the label selector was ignored.

* Improve flexibility of URL parsing

As recommended by @lordofthejars. I renamed PROTOCOL to SCHEME to match
the convention of HttpUrl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConfigMap labels are ignored when using mock KubernetesServer
4 participants