Skip to content

Commit

Permalink
basic cel-java integration/test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Aug 14, 2024
1 parent 9d69f97 commit df144f5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ commons-math3 = "org.apache.commons:commons-math3:3.6.1"
conscrypt = "org.conscrypt:conscrypt-openjdk-uber:2.5.2"
cronet-api = "org.chromium.net:cronet-api:119.6045.31"
cronet-embedded = "org.chromium.net:cronet-embedded:119.6045.31"
dev-cel = "dev.cel:cel:0.6.0"
errorprone-annotations = "com.google.errorprone:error_prone_annotations:2.28.0"
errorprone-core = "com.google.errorprone:error_prone_core:2.28.0"
google-api-protos = "com.google.api.grpc:proto-google-common-protos:2.41.0"
Expand Down
1 change: 1 addition & 0 deletions xds/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
project(':grpc-services'),
project(':grpc-auth'),
project(path: ':grpc-alts', configuration: 'shadow'),
libraries.dev.cel,
libraries.guava,
libraries.gson,
libraries.re2j,
Expand Down
35 changes: 30 additions & 5 deletions xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,52 @@

package io.grpc.xds.internal.matchers;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableMap;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeFactory;
import java.util.function.Predicate;

/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
public class CelMatcher implements Predicate<HttpMatchInput> {
private static final CelRuntime CEL_RUNTIME =
CelRuntimeFactory.standardCelRuntimeBuilder().build();

private final CelRuntime.Program program;
private final String description;

public CelMatcher(String description) {
// TODO(sergiitk): cache parsed CEL expressions
private CelMatcher(CelAbstractSyntaxTree ast, String description) throws CelEvaluationException {
this.program = CEL_RUNTIME.createProgram(checkNotNull(ast));
this.description = description != null ? description : "";
}

public static CelMatcher create(CelAbstractSyntaxTree ast) throws CelEvaluationException {
return new CelMatcher(ast, null);
}

public static CelMatcher create(CelAbstractSyntaxTree ast, String description)
throws CelEvaluationException {
return new CelMatcher(ast, description);
}

public String description() {
return description;
}

@Override
public boolean test(HttpMatchInput httpMatchInput) {
if (httpMatchInput.headers().keys().isEmpty()) {
// if (httpMatchInput.headers().keys().isEmpty()) {
// return false;
// }
// TODO(sergiitk): [IMPL] convert headers to cel args
try {
return (boolean) program.eval(ImmutableMap.of("my_var", "Hello World"));
} catch (CelEvaluationException e) {
// TODO(sergiitk): [IMPL] log cel error?
return false;
}
// TODO(sergiitk): [IMPL] convert headers to cel args
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,54 @@

import static com.google.common.truth.Truth.assertThat;

import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelValidationResult;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.runtime.CelEvaluationException;
import io.grpc.Metadata;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CelMatcherTest {
// Construct the compilation and runtime environments.
// These instances are immutable and thus trivially thread-safe and amenable to caching.
private static final CelCompiler CEL_COMPILER =
CelCompilerFactory.standardCelCompilerBuilder().addVar("my_var", SimpleType.STRING).build();
private static final CelValidationResult celProg1 =
CEL_COMPILER.compile("type(my_var) == string");

CelAbstractSyntaxTree ast1;
CelMatcher matcher1;

private static final HttpMatchInput fakeInput = new HttpMatchInput() {
@Override
public Metadata headers() {
return new Metadata();
}
};

@Before
public void setUp() throws Exception {
ast1 = celProg1.getAst();
matcher1 = CelMatcher.create(ast1);
}

@Test
public void construct() {
public void construct() throws CelEvaluationException {
assertThat(matcher1.description()).isEqualTo("");

String description = "Optional description";
CelMatcher matcher = new CelMatcher(description);
CelMatcher matcher = CelMatcher.create(ast1, description);
assertThat(matcher.description()).isEqualTo(description);
}

matcher = new CelMatcher(null);
assertThat(matcher.description()).isEqualTo("");
@Test
public void testProgTrue() {
assertThat(matcher1.test(fakeInput)).isTrue();
}
}

0 comments on commit df144f5

Please sign in to comment.