diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java index 1f6cc3340242a..99da60482f70a 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java @@ -43,7 +43,6 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; -import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexMode; import org.elasticsearch.logging.LogManager; import org.elasticsearch.logging.Logger; @@ -148,6 +147,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.net.URI; import java.net.URL; import java.nio.file.Files; @@ -967,13 +967,6 @@ void executeSubPlan( ExchangeSourceHandler exchangeSource = new ExchangeSourceHandler(between(1, 64), executor); ExchangeSinkHandler exchangeSink = new ExchangeSinkHandler(blockFactory, between(1, 64), threadPool::relativeTimeInMillis); - UserAgentParserRegistry userAgentRegistry; - try { - userAgentRegistry = createUserAgentRegistry(); - } catch (IOException e) { - throw new IllegalStateException("Failed to create UserAgentParserRegistry", e); - } - LocalExecutionPlanner executionPlanner = new LocalExecutionPlanner( getTestName(), "", @@ -987,7 +980,7 @@ void executeSubPlan( mock(EnrichLookupService.class), mock(LookupFromIndexService.class), mock(InferenceService.class), - userAgentRegistry, + createUserAgentRegistry(), physicalOperationProviders, operatorFactoryRegistry ); @@ -1105,17 +1098,19 @@ private static List coordinatorSplits(OperatorFactoryRegistry ope * Creates a {@link UserAgentParserRegistry} with a config directory * containing the custom-regexes.yml test resource, so csv-spec tests can exercise the {@code regex_file} option. */ - private static UserAgentParserRegistry createUserAgentRegistry() throws IOException { - Path homeDir = createTempDir(); - Path userAgentConfigDir = homeDir.resolve("config").resolve("user-agent"); - Files.createDirectories(userAgentConfigDir); - try (InputStream is = CsvTests.class.getResourceAsStream("/custom-regexes.yml")) { - assert is != null : "custom-regexes.yml not found on classpath"; - Files.copy(is, userAgentConfigDir.resolve("custom-regexes.yml")); + private static UserAgentParserRegistry createUserAgentRegistry() { + Environment env = TestPhysicalOperationProviders.TEST_ENV.getOrCompute(); + Path userAgentConfigDir = env.configDir().resolve("user-agent"); + Path customRegexes = userAgentConfigDir.resolve("custom-regexes.yml"); + if (Files.exists(customRegexes) == false) { + try (InputStream is = CsvTests.class.getResourceAsStream("/custom-regexes.yml")) { + assert is != null : "custom-regexes.yml not found on classpath"; + Files.createDirectories(userAgentConfigDir); + Files.copy(is, customRegexes); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } - return UserAgentPlugin.createRegistry( - TestEnvironment.newEnvironment(Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), homeDir).build()), - Settings.EMPTY - ); + return UserAgentPlugin.createRegistry(env, Settings.EMPTY); } } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java index 4cce7aa85e9b7..154f7cbefc365 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java @@ -10,6 +10,7 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.analysis.common.CommonAnalysisPlugin; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.LazyInitializable; import org.elasticsearch.compute.aggregation.AggregatorMode; import org.elasticsearch.compute.aggregation.GroupingAggregator; import org.elasticsearch.compute.aggregation.blockhash.BlockHash; @@ -81,6 +82,13 @@ import static org.elasticsearch.index.mapper.MappedFieldType.FieldExtractPreference.EXTRACT_SPATIAL_CENTROID; public class TestPhysicalOperationProviders extends AbstractPhysicalOperationProviders { + + public static final LazyInitializable TEST_ENV = new LazyInitializable<>( + () -> TestEnvironment.newEnvironment( + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ) + ); + private final List indexPages; private final UnmappedResolution unmappedResolution; @@ -112,9 +120,7 @@ Optional columnIndex(String columnName) { private static AnalysisRegistry createAnalysisRegistry() throws IOException { return new AnalysisModule( - TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() - ), + TEST_ENV.getOrCompute(), List.of(new MachineLearning(Settings.EMPTY), new CommonAnalysisPlugin()), new StablePluginsRegistry() ).getAnalysisRegistry();