Skip to content

Commit

Permalink
fix(itest): fix GraphQLTest.java (#395)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Azores <[email protected]>
  • Loading branch information
aali309 and andrewazores authored Jun 26, 2024
1 parent 8ce2568 commit c5ea2a3
Show file tree
Hide file tree
Showing 6 changed files with 1,679 additions and 839 deletions.
29 changes: 14 additions & 15 deletions src/main/java/io/cryostat/discovery/DiscoveryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,26 @@ public boolean equals(Object obj) {
&& Objects.equals(children, other.children);
}

@Override
public String toString() {
return "DiscoveryNode{"
+ "name='"
+ name
+ '\''
+ ", nodeType='"
+ nodeType
+ '\''
+ ", children="
+ children
+ '}';
}

@ApplicationScoped
static class Listener {

@Inject Logger logger;
@Inject EventBus bus;

// @Transactional
// @Blocking
// @ConsumeEvent(Target.TARGET_JVM_DISCOVERY)
// void onMessage(TargetDiscovery event) {
// switch (event.kind()) {
// case LOST:
// break;
// case FOUND:
// break;
// default:
// // no-op
// break;
// }
// }

@PrePersist
void prePersist(DiscoveryNode node) {}

Expand Down
17 changes: 10 additions & 7 deletions src/main/java/io/cryostat/graphql/RootNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ public boolean test(DiscoveryNode t) {
Predicate<DiscoveryNode> matchesAnnotations =
n ->
annotations == null
|| annotations.stream()
.allMatch(
annotation ->
LabelSelectorMatcher.parse(annotation)
.test(
n.target.annotations
.merged()));
|| (n.target != null
&& annotations.stream()
.allMatch(
annotation ->
LabelSelectorMatcher.parse(
annotation)
.test(
n.target
.annotations
.merged())));

return List.of(
matchesId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LabelSelectorMatcher implements Predicate<Map<String, String>> {
// must loosely look like a k8s label (not strictly enforced here), right side must loosely look
// like a k8s label value, which may be empty. Allowed operators are "=", "==", "!=".
static final Pattern EQUALITY_PATTERN =
Pattern.compile("^(?<key>[^!=\\s]+)\\s*(?<op>=|==|!=)\\s*(?<value>[^!=\\s]*)$");
Pattern.compile("^(?<key>[^!=\\s]+)\\s*(?<op>=|==|!=)\\s*(?<value>[^!=]*)\\s*$");

// ex. "environment in (production, qa)" or "tier NotIn (frontend, backend)". Tests if the given
// label has or does not have any of the specified values.
Expand Down Expand Up @@ -74,7 +74,9 @@ public static LabelSelectorMatcher parse(String clause) throws IllegalArgumentEx
return new LabelSelectorMatcher(List.of(matcher));
}
}
return new LabelSelectorMatcher();
throw new IllegalArgumentException(
String.format(
"No LabelSelectorMatcher case matched given expression: \"%s\"", clause));
}

private static LabelMatcher parseEqualities(String clause) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.graphql.matchers;

import java.util.Map;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

class LabelSelectorMatcherTest {

private static final Map<String, String> TEST_LABELS =
Map.of(
"foo", "bar",
"something", "else",
"my.prefixed/label", "expectedValue",
"env", "prod",
"present", "irrelevant");

@ParameterizedTest
@ValueSource(strings = {"this is not a valid expression"})
void testDoesNotMatchBadSyntax(String expr) {
Assertions.assertThrows(
IllegalArgumentException.class, () -> LabelSelectorMatcher.parse(expr));
}

@ParameterizedTest
@CsvSource({
"foo=bar, true",
"something=wrong, false",
"my.prefixed/label = expectedValue, true",
"env = Expected Value, false",
"env = prod, true",
"env = prod , true",
"env = dev, false",
"env = dev , false",
})
void testSingleEquality(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}

@ParameterizedTest
@CsvSource({
"foo==bar, true",
"something==wrong, false",
"my.prefixed/label == expectedValue, true"
})
void testDoubleEquality(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}

@ParameterizedTest
@CsvSource({
"foo!=bar, false",
"something!=wrong, true",
"my.prefixed/label != expectedValue, false"
})
void testInequality(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}

@ParameterizedTest
@CsvSource(
value = {
"foo in (bar, baz) : true",
"something In (else, orother) : true",
"env IN (stage,qa) : false",
},
delimiter = ':')
void testSetIn(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}

@ParameterizedTest
@CsvSource(
value = {
"foo notin (bar, baz) : false",
"something NotIn (orother, else, third) : false",
"env NOTIN (stage,qa) : true",
},
delimiter = ':')
void testSetNotIn(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}

@ParameterizedTest
@CsvSource({
"foo, true",
"something, true",
"my.prefixed/label, true",
"another/missing-label, false",
"present, true"
})
void testExists(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}

@ParameterizedTest
@CsvSource({"!foo, false", "!something, false", "!present, false"})
void testNotExists(String expr, boolean pass) {
LabelSelectorMatcher matcher = LabelSelectorMatcher.parse(expr);
MatcherAssert.assertThat(expr, matcher.test(TEST_LABELS), Matchers.is(pass));
}
}
Loading

0 comments on commit c5ea2a3

Please sign in to comment.