Skip to content

fix(matchexpr): include jvmId in js binding #1393

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

Merged
merged 2 commits into from
Mar 8, 2023
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
26 changes: 12 additions & 14 deletions src/main/java/io/cryostat/rules/MatchExpressionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,19 @@ Bindings createBindings(ServiceRef serviceRef) {
serviceRef.getCryostatAnnotations().entrySet()) {
cryostatAnnotations.put(entry.getKey().name(), entry.getValue());
}
bindings.put(
"target",
Map<String, Object> target = new HashMap<>();
target.put("connectUrl", serviceRef.getServiceUri().toString());
target.put("jvmId", serviceRef.getJvmId());
target.put("alias", serviceRef.getAlias().orElse(null));
target.put("labels", serviceRef.getLabels());
target.put(
"annotations",
Map.of(
"connectUrl",
serviceRef.getServiceUri(),
"alias",
serviceRef.getAlias().orElse(null),
"labels",
serviceRef.getLabels(),
"annotations",
Map.of(
"platform",
serviceRef.getPlatformAnnotations(),
"cryostat",
cryostatAnnotations)));
"platform",
serviceRef.getPlatformAnnotations(),
"cryostat",
cryostatAnnotations));
bindings.put("target", target);
return bindings;
} finally {
evt.end();
Expand Down
31 changes: 28 additions & 3 deletions src/test/java/io/cryostat/rules/MatchExpressionEvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class MatchExpressionEvaluatorTest {
@Mock RuleRegistry rules;

URI serviceUri;
String jvmId;
String alias;
Map<String, String> labels;
Map<String, String> platformAnnotations;
Expand All @@ -88,12 +89,14 @@ void setup() throws Exception {
MainModule.provideScriptEngine(), credentials, rules, logger);

this.serviceUri = new URI("service:jmx:rmi:///jndi/rmi://cryostat:9091/jmxrmi");
this.jvmId = "-some1234HashId=";
this.alias = "someAlias";
this.labels = Map.of("label1", "someLabel");
this.platformAnnotations = Map.of("annotation1", "someAnnotation");
this.cryostatAnnotations = Map.of(AnnotationKey.JAVA_MAIN, "io.cryostat.Cryostat");

Mockito.when(serviceRef.getServiceUri()).thenReturn(this.serviceUri);
Mockito.when(serviceRef.getJvmId()).thenReturn(this.jvmId);
Mockito.when(serviceRef.getAlias()).thenReturn(Optional.of(this.alias));
Mockito.when(serviceRef.getLabels()).thenReturn(this.labels);
Mockito.when(serviceRef.getPlatformAnnotations()).thenReturn(this.platformAnnotations);
Expand All @@ -119,14 +122,16 @@ void bindingsShouldContainTargetReference() {
void targetShouldHaveExpectedKeys() {
Set<String> keys = ((Map<String, Object>) bindings.get("target")).keySet();
MatcherAssert.assertThat(
keys, Matchers.equalTo(Set.of("connectUrl", "alias", "labels", "annotations")));
keys,
Matchers.equalTo(
Set.of("connectUrl", "jvmId", "alias", "labels", "annotations")));
}

@Test
void targetShouldHaveServiceUriAsUri() {
URI uri = (URI) ((Map<String, Object>) bindings.get("target")).get("connectUrl");
String uri = (String) ((Map<String, Object>) bindings.get("target")).get("connectUrl");
MatcherAssert.assertThat(
uri, Matchers.equalTo(MatchExpressionEvaluatorTest.this.serviceUri));
uri, Matchers.equalTo(MatchExpressionEvaluatorTest.this.serviceUri.toString()));
}

@Test
Expand All @@ -136,6 +141,13 @@ void targetShouldHaveAliasAsString() {
alias, Matchers.equalTo(MatchExpressionEvaluatorTest.this.alias));
}

@Test
void targetShouldHaveJvmIdAsString() {
String jvmId = (String) (((Map<String, Object>) bindings.get("target")).get("jvmId"));
MatcherAssert.assertThat(
jvmId, Matchers.equalTo(MatchExpressionEvaluatorTest.this.jvmId));
}

@Test
void targetShouldHaveLabels() {
Map<String, String> labels =
Expand Down Expand Up @@ -205,6 +217,19 @@ void shouldMatchOnAlias() throws Exception {
Assertions.assertTrue(ruleMatcher.applies(expr, serviceRef));
}

@Test
void shouldMatchOnJvmId() throws Exception {
String expr =
String.format("target.jvmId == '%s'", MatchExpressionEvaluatorTest.this.jvmId);
Assertions.assertTrue(ruleMatcher.applies(expr, serviceRef));
}

@Test
void shouldNotMatchOnWrongJvmId() throws Exception {
String expr = "target.jvmId == \"hello-world\"";
Assertions.assertFalse(ruleMatcher.applies(expr, serviceRef));
}

@ParameterizedTest
@ValueSource(strings = {"foo", "somethingelse", "true"})
@NullAndEmptySource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ void setup() {
"target.alias == 'io.cryostat.Cryostat' || target.annotations.cryostat.JAVA_MAIN =="
+ " 'io.cryostat.Cryostat'",
"target.connectUrl != '' && target.labels.SOMETHING == 'other'",
"taret.jvmId == \"abcd1234\"",
"taret.jvmId != \"hello world\"",
"/^[a-z]+$/.test(target.alias)",
"/^[a-z]+$/.test(target.noSuchProperty)",
"/^[a-z]+$/.test([].length)",
Expand Down