Skip to content

Commit

Permalink
EQL: [Tests] Simplify toml integration tests (elastic#71064) (elastic…
Browse files Browse the repository at this point in the history
…#71095)

Previously, two files were used, `test_queries.toml` and
`test_queries_unsupported.toml`. The queries where read from both files
and each query in the 1st file but not in the 2nd was executed, but if
it was also included in the 2nd file (unsupported) was skipped. This
approach helped in first phase of developing EQL since those tests came
from the EQL-endpoint implementation and we wanted to keep those files
in sync. Now, we no longer need to keep this sync approach, so the
unsupported queries are simply commented out in the original file:
`test_queries.toml` and the `test_queries_unsupported.toml` is removed.

Moreover, one more query is now enabled (previously was
incorrectly still in the `test_queries_unsupported.toml` file) and a few
more were added as commented out, which previously only existed in the
unsupported file.

Fixes: elastic#61841
(cherry picked from commit 8537b2d)
  • Loading branch information
matriv authored Mar 31, 2021
1 parent 0c42863 commit d8125bd
Show file tree
Hide file tree
Showing 6 changed files with 937 additions and 1,983 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class EqlDateNanosSpecTestCase extends BaseEqlSpecTestCase {

@ParametersFactory(shuffle = false, argumentFormatting = PARAM_FORMATTING)
public static List<Object[]> readTestSpecs() throws Exception {
return asArray(EqlSpecLoader.load("/test_queries_date_nanos.toml", true, new HashSet<>()));
return asArray(EqlSpecLoader.load("/test_queries_date_nanos.toml", new HashSet<>()));
}

public EqlDateNanosSpecTestCase(String query, String name, long[] eventIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class EqlExtraSpecTestCase extends BaseEqlSpecTestCase {

@ParametersFactory(shuffle = false, argumentFormatting = PARAM_FORMATTING)
public static List<Object[]> readTestSpecs() throws Exception {
return asArray(EqlSpecLoader.load("/test_extra.toml", true, new HashSet<>()));
return asArray(EqlSpecLoader.load("/test_extra.toml", new HashSet<>()));
}

public EqlExtraSpecTestCase(String query, String name, long[] eventIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

public class EqlSpecLoader {

public static List<EqlSpec> load(String path, boolean supported, Set<String> uniqueTestNames) throws Exception {
public static List<EqlSpec> load(String path, Set<String> uniqueTestNames) throws Exception {
try (InputStream is = EqlSpecLoader.class.getResourceAsStream(path)) {
if (is == null) {
throw new IllegalAccessException("Cannot find classpath resource " + path);
}
return readFromStream(is, supported, uniqueTestNames);
return readFromStream(is, uniqueTestNames);
}
}

private static void validateAndAddSpec(List<EqlSpec> specs, EqlSpec spec, boolean supported, Set<String> uniqueTestNames) {
private static void validateAndAddSpec(List<EqlSpec> specs, EqlSpec spec, Set<String> uniqueTestNames) {
if (Strings.isNullOrEmpty(spec.name())) {
throw new IllegalArgumentException("Read a test without a name value");
}
Expand All @@ -38,15 +38,13 @@ private static void validateAndAddSpec(List<EqlSpec> specs, EqlSpec spec, boolea
throw new IllegalArgumentException("Read a test without a query value");
}

if (supported) {
if (spec.expectedEventIds() == null) {
throw new IllegalArgumentException("Read a test without a expected_event_ids value");
}
if (uniqueTestNames.contains(spec.name())) {
throw new IllegalArgumentException("Found a test with the same name as another test: " + spec.name());
} else {
uniqueTestNames.add(spec.name());
}
if (spec.expectedEventIds() == null) {
throw new IllegalArgumentException("Read a test without a expected_event_ids value");
}
if (uniqueTestNames.contains(spec.name())) {
throw new IllegalArgumentException("Found a test with the same name as another test: " + spec.name());
} else {
uniqueTestNames.add(spec.name());
}

specs.add(spec);
Expand All @@ -60,7 +58,7 @@ private static String getTrimmedString(TomlTable table, String key) {
return null;
}

private static List<EqlSpec> readFromStream(InputStream is, boolean supported, Set<String> uniqueTestNames) throws Exception {
private static List<EqlSpec> readFromStream(InputStream is, Set<String> uniqueTestNames) throws Exception {
List<EqlSpec> testSpecs = new ArrayList<>();

EqlSpec spec;
Expand Down Expand Up @@ -93,7 +91,7 @@ private static List<EqlSpec> readFromStream(InputStream is, boolean supported, S
}
spec.expectedEventIds(expectedEventIds);
}
validateAndAddSpec(testSpecs, spec, supported, uniqueTestNames);
validateAndAddSpec(testSpecs, spec, uniqueTestNames);
}

return testSpecs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -23,29 +22,11 @@ public static List<Object[]> readTestSpecs() throws Exception {

// Load EQL validation specs
Set<String> uniqueTestNames = new HashSet<>();
List<EqlSpec> specs = EqlSpecLoader.load("/test_queries.toml", true, uniqueTestNames);
specs.addAll(EqlSpecLoader.load("/additional_test_queries.toml", true, uniqueTestNames));
specs.addAll(EqlSpecLoader.load("/test_queries_date.toml", true, uniqueTestNames));
List<EqlSpec> unsupportedSpecs = EqlSpecLoader.load("/test_queries_unsupported.toml", false, uniqueTestNames);

// Validate only currently supported specs
List<EqlSpec> filteredSpecs = new ArrayList<>();

for (EqlSpec spec : specs) {
boolean supported = true;
// Check if spec is supported, simple iteration, cause the list is short.
for (EqlSpec unSpec : unsupportedSpecs) {
if (spec.equals(unSpec)) {
supported = false;
break;
}
}

if (supported) {
filteredSpecs.add(spec);
}
}
return asArray(filteredSpecs);
List<EqlSpec> specs = EqlSpecLoader.load("/test_queries.toml", uniqueTestNames);
specs.addAll(EqlSpecLoader.load("/additional_test_queries.toml", uniqueTestNames));
specs.addAll(EqlSpecLoader.load("/test_queries_date.toml", uniqueTestNames));

return asArray(specs);
}

@Override
Expand Down
Loading

0 comments on commit d8125bd

Please sign in to comment.