|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.conformance.policy; |
| 16 | + |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | + |
| 19 | +import com.google.auto.value.AutoValue; |
| 20 | +import com.google.common.base.Splitter; |
| 21 | +import com.google.common.base.Strings; |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.io.Files; |
| 24 | +import com.google.protobuf.ListValue; |
| 25 | +import com.google.protobuf.Struct; |
| 26 | +import com.google.protobuf.TypeRegistry; |
| 27 | +import com.google.protobuf.Value; |
| 28 | +import dev.cel.testing.testrunner.CelTestSuite; |
| 29 | +import dev.cel.testing.testrunner.CelTestSuite.CelTestSection; |
| 30 | +import dev.cel.testing.testrunner.CelTestSuite.CelTestSection.CelTestCase; |
| 31 | +import dev.cel.testing.testrunner.CelTestSuiteTextProtoParser; |
| 32 | +import dev.cel.testing.testrunner.CelTestSuiteYamlParser; |
| 33 | +import java.io.File; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.List; |
| 36 | +import org.junit.runner.Description; |
| 37 | +import org.junit.runner.notification.RunNotifier; |
| 38 | +import org.junit.runners.ParentRunner; |
| 39 | +import org.junit.runners.model.InitializationError; |
| 40 | + |
| 41 | +/** Custom JUnit runner for CEL policy conformance tests. */ |
| 42 | +public final class PolicyConformanceTestRunner extends ParentRunner<PolicyConformanceTest> { |
| 43 | + |
| 44 | + private static final Splitter SPLITTER = Splitter.on(",").omitEmptyStrings(); |
| 45 | + private static final String TESTS_YAML_FILE_NAME = "tests.yaml"; |
| 46 | + private static final String TESTS_TEXTPROTO_FILE_NAME = "tests.textproto"; |
| 47 | + private static final TypeRegistry TYPE_REGISTRY = |
| 48 | + TypeRegistry.newBuilder() |
| 49 | + .add(Struct.getDescriptor()) |
| 50 | + .add(Value.getDescriptor()) |
| 51 | + .add(ListValue.getDescriptor()) |
| 52 | + .build(); |
| 53 | + |
| 54 | + private static final String TEST_DIRS_PROP = |
| 55 | + System.getProperty("dev.cel.policy.conformance.tests"); |
| 56 | + private static final String TESTDATA_DIR = |
| 57 | + System.getProperty("dev.cel.policy.conformance.testdata_dir", "testdata"); |
| 58 | + private static final String SKIP_TESTS_PROP = |
| 59 | + System.getProperty("dev.cel.policy.conformance.skip_tests"); |
| 60 | + |
| 61 | + private static final ImmutableList<String> TESTS_TO_SKIP = |
| 62 | + Strings.isNullOrEmpty(SKIP_TESTS_PROP) |
| 63 | + ? ImmutableList.of() |
| 64 | + : ImmutableList.copyOf(SPLITTER.splitToList(SKIP_TESTS_PROP)); |
| 65 | + |
| 66 | + private static final ImmutableList<String> TEST_DIRS = |
| 67 | + Strings.isNullOrEmpty(TEST_DIRS_PROP) |
| 68 | + ? discoverTestDirs(TESTDATA_DIR) |
| 69 | + : ImmutableList.copyOf(SPLITTER.splitToList(TEST_DIRS_PROP)); |
| 70 | + |
| 71 | + private static ImmutableList<String> discoverTestDirs(String testdataDir) { |
| 72 | + File dir = new File(testdataDir); |
| 73 | + if (!dir.exists() || !dir.isDirectory()) { |
| 74 | + return ImmutableList.of(); |
| 75 | + } |
| 76 | + String[] directories = dir.list((current, name) -> new File(current, name).isDirectory()); |
| 77 | + if (directories == null) { |
| 78 | + return ImmutableList.of(); |
| 79 | + } |
| 80 | + Arrays.sort(directories); |
| 81 | + return ImmutableList.copyOf(directories); |
| 82 | + } |
| 83 | + |
| 84 | + private final ImmutableList<PolicyConformanceTest> tests; |
| 85 | + |
| 86 | + private ImmutableList<PolicyConformanceTest> loadTests() { |
| 87 | + if (TEST_DIRS.isEmpty()) { |
| 88 | + return ImmutableList.of(); |
| 89 | + } |
| 90 | + |
| 91 | + ImmutableList.Builder<PolicyConformanceTest> testsBuilder = ImmutableList.builder(); |
| 92 | + |
| 93 | + for (String dir : TEST_DIRS) { |
| 94 | + String fullDirPath = TESTDATA_DIR + "/" + dir; |
| 95 | + try { |
| 96 | + ImmutableList<CelTestSuiteContext> suites = readTestSuites(fullDirPath); |
| 97 | + for (CelTestSuiteContext namedSuite : suites) { |
| 98 | + for (CelTestSection section : namedSuite.testSuite().sections()) { |
| 99 | + for (CelTestCase testCase : section.tests()) { |
| 100 | + String baseName = String.format("%s/%s/%s", dir, section.name(), testCase.name()); |
| 101 | + String displayName = baseName + namedSuite.formatSuffix(); |
| 102 | + if (!shouldSkipTest(baseName, TESTS_TO_SKIP)) { |
| 103 | + testsBuilder.add(new PolicyConformanceTest(displayName, testCase, fullDirPath)); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } catch (Exception e) { |
| 109 | + throw new RuntimeException("Failed to load test suite in " + fullDirPath, e); |
| 110 | + } |
| 111 | + } |
| 112 | + return testsBuilder.build(); |
| 113 | + } |
| 114 | + |
| 115 | + private static boolean shouldSkipTest(String name, List<String> testsToSkip) { |
| 116 | + for (String testToSkip : testsToSkip) { |
| 117 | + if (name.startsWith(testToSkip)) { |
| 118 | + String consumedName = name.substring(testToSkip.length()); |
| 119 | + if (consumedName.isEmpty() || consumedName.startsWith("/")) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + private static ImmutableList<CelTestSuiteContext> readTestSuites(String dirPath) |
| 128 | + throws Exception { |
| 129 | + File dir = new File(dirPath); |
| 130 | + File yamlFile = new File(dir, TESTS_YAML_FILE_NAME); |
| 131 | + File textprotoFile = new File(dir, TESTS_TEXTPROTO_FILE_NAME); |
| 132 | + |
| 133 | + boolean bothExist = yamlFile.exists() && textprotoFile.exists(); |
| 134 | + ImmutableList.Builder<CelTestSuiteContext> suitesBuilder = ImmutableList.builder(); |
| 135 | + |
| 136 | + if (yamlFile.exists()) { |
| 137 | + suitesBuilder.add( |
| 138 | + CelTestSuiteContext.create( |
| 139 | + CelTestSuiteYamlParser.newInstance() |
| 140 | + .parse(Files.asCharSource(yamlFile, UTF_8).read()), |
| 141 | + bothExist ? " (yaml)" : "")); |
| 142 | + } |
| 143 | + if (textprotoFile.exists()) { |
| 144 | + suitesBuilder.add( |
| 145 | + CelTestSuiteContext.create( |
| 146 | + CelTestSuiteTextProtoParser.newInstance() |
| 147 | + .parse(Files.asCharSource(textprotoFile, UTF_8).read(), TYPE_REGISTRY), |
| 148 | + bothExist ? " (textproto)" : "")); |
| 149 | + } |
| 150 | + |
| 151 | + ImmutableList<CelTestSuiteContext> suites = suitesBuilder.build(); |
| 152 | + if (suites.isEmpty()) { |
| 153 | + throw new IllegalArgumentException( |
| 154 | + String.format( |
| 155 | + "No %s or %s found in %s", TESTS_YAML_FILE_NAME, TESTS_TEXTPROTO_FILE_NAME, dirPath)); |
| 156 | + } |
| 157 | + return suites; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected ImmutableList<PolicyConformanceTest> getChildren() { |
| 162 | + return tests; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + protected Description describeChild(PolicyConformanceTest child) { |
| 167 | + return Description.createTestDescription(getTestClass().getJavaClass(), child.getName()); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + protected void runChild(PolicyConformanceTest child, RunNotifier notifier) { |
| 172 | + runLeaf(child, describeChild(child), notifier); |
| 173 | + } |
| 174 | + |
| 175 | + public PolicyConformanceTestRunner(Class<?> clazz) throws InitializationError { |
| 176 | + super(clazz); |
| 177 | + this.tests = loadTests(); |
| 178 | + } |
| 179 | + |
| 180 | + @AutoValue |
| 181 | + abstract static class CelTestSuiteContext { |
| 182 | + abstract CelTestSuite testSuite(); |
| 183 | + |
| 184 | + abstract String formatSuffix(); |
| 185 | + |
| 186 | + static CelTestSuiteContext create(CelTestSuite testSuite, String formatSuffix) { |
| 187 | + return new AutoValue_PolicyConformanceTestRunner_CelTestSuiteContext(testSuite, formatSuffix); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments