Skip to content

Commit

Permalink
Refactor uses of old Plugin.createComponents method to new services m…
Browse files Browse the repository at this point in the history
…ethod (elastic#101376)
  • Loading branch information
thecoop authored Oct 26, 2023
1 parent f68cd1f commit 3e2f17f
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.scanners.StablePluginsRegistry;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.telemetry.TelemetryProvider;
import org.elasticsearch.test.ESTokenStreamTestCase;
import org.elasticsearch.test.IndexSettingsModule;
import org.elasticsearch.threadpool.ThreadPool;

import java.io.IOException;
import java.util.Collections;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PredicateTokenScriptFilterTests extends ESTokenStreamTestCase {

public void testSimpleFilter() throws IOException {
Expand All @@ -57,33 +60,23 @@ public boolean execute(Token token) {
}
};

@SuppressWarnings("unchecked")
ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap(), () -> 1L) {
@Override
@SuppressWarnings("unchecked")
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
assertEquals(context, AnalysisPredicateScript.CONTEXT);
assertEquals(new Script("my_script"), script);
return (FactoryType) factory;
}
};
Client client = new MockClient(Settings.EMPTY, null);

CommonAnalysisPlugin plugin = new CommonAnalysisPlugin();
plugin.createComponents(
client,
null,
null,
null,
scriptService,
null,
null,
null,
null,
null,
null,
TelemetryProvider.NOOP,
null,
null
);
Plugin.PluginServices services = mock(Plugin.PluginServices.class);
when(services.client()).thenReturn(client);
when(services.scriptService()).thenReturn(scriptService);
plugin.createComponents(services);

AnalysisModule module = new AnalysisModule(
TestEnvironment.newEnvironment(settings),
Collections.singletonList(plugin),
Expand All @@ -99,7 +92,7 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp

}

private class MockClient extends AbstractClient {
private static class MockClient extends AbstractClient {
MockClient(Settings settings, ThreadPool threadPool) {
super(settings, threadPool);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.scanners.StablePluginsRegistry;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.telemetry.TelemetryProvider;
import org.elasticsearch.test.ESTokenStreamTestCase;
import org.elasticsearch.test.IndexSettingsModule;
import org.elasticsearch.threadpool.ThreadPool;

import java.util.Collections;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ScriptedConditionTokenFilterTests extends ESTokenStreamTestCase {

public void testSimpleCondition() throws Exception {
Expand Down Expand Up @@ -67,23 +70,13 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp
}
};
Client client = new MockClient(Settings.EMPTY, null);

CommonAnalysisPlugin plugin = new CommonAnalysisPlugin();
plugin.createComponents(
client,
null,
null,
null,
scriptService,
null,
null,
null,
null,
null,
null,
TelemetryProvider.NOOP,
null,
null
);
Plugin.PluginServices services = mock(Plugin.PluginServices.class);
when(services.client()).thenReturn(client);
when(services.scriptService()).thenReturn(scriptService);
plugin.createComponents(services);

AnalysisModule module = new AnalysisModule(
TestEnvironment.newEnvironment(settings),
Collections.singletonList(plugin),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.settings.SettingsModule;
import org.elasticsearch.telemetry.TelemetryProvider;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
Expand All @@ -38,6 +38,8 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AzureStorageServiceTests extends ESTestCase {
private ThreadPool threadPool;
Expand Down Expand Up @@ -73,7 +75,9 @@ public void testReadSecuredSettings() {
private AzureRepositoryPlugin pluginWithSettingsValidation(Settings settings) {
final AzureRepositoryPlugin plugin = new AzureRepositoryPlugin(settings);
new SettingsModule(settings, plugin.getSettings(), Collections.emptyList());
plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null, TelemetryProvider.NOOP, null, null);
Plugin.PluginServices services = mock(Plugin.PluginServices.class);
when(services.threadPool()).thenReturn(threadPool);
plugin.createComponents(services);
return plugin;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.telemetry.TelemetryProvider;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.hamcrest.OptionalMatchers;
import org.elasticsearch.threadpool.Scheduler;
Expand Down Expand Up @@ -58,30 +58,36 @@ public class SystemdPluginTests extends ESTestCase {
).thenReturn(extender);
}

private void startPlugin(SystemdPlugin plugin) {
Plugin.PluginServices services = mock(Plugin.PluginServices.class);
when(services.threadPool()).thenReturn(threadPool);
plugin.createComponents(services);
}

public void testIsEnabled() {
final SystemdPlugin plugin = new SystemdPlugin(false, randomPackageBuildType, Boolean.TRUE.toString());
plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null, TelemetryProvider.NOOP, null, null);
startPlugin(plugin);
assertTrue(plugin.isEnabled());
assertNotNull(plugin.extender());
}

public void testIsNotPackageDistribution() {
final SystemdPlugin plugin = new SystemdPlugin(false, randomNonPackageBuildType, Boolean.TRUE.toString());
plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null, TelemetryProvider.NOOP, null, null);
startPlugin(plugin);
assertFalse(plugin.isEnabled());
assertNull(plugin.extender());
}

public void testIsImplicitlyNotEnabled() {
final SystemdPlugin plugin = new SystemdPlugin(false, randomPackageBuildType, null);
plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null, TelemetryProvider.NOOP, null, null);
startPlugin(plugin);
assertFalse(plugin.isEnabled());
assertNull(plugin.extender());
}

public void testIsExplicitlyNotEnabled() {
final SystemdPlugin plugin = new SystemdPlugin(false, randomPackageBuildType, Boolean.FALSE.toString());
plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null, TelemetryProvider.NOOP, null, null);
startPlugin(plugin);
assertFalse(plugin.isEnabled());
assertNull(plugin.extender());
}
Expand Down Expand Up @@ -169,7 +175,7 @@ int sd_notify(final int unset_environment, final String state) {
}

};
plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null, TelemetryProvider.NOOP, null, null);
startPlugin(plugin);
if (Boolean.TRUE.toString().equals(esSDNotify)) {
assertNotNull(plugin.extender());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,16 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.telemetry.TelemetryProvider;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.xcontent.NamedXContentRegistry;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
Expand Down Expand Up @@ -65,41 +51,11 @@ public TestPlugin(Settings settings) {
}

@Override
public Collection<Object> createComponents(
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
ScriptService scriptService,
NamedXContentRegistry xContentRegistry,
Environment environment,
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver expressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier,
TelemetryProvider telemetryProvider,
AllocationService allocationService,
IndicesService indicesService
) {
clusterService.getClusterSettings().addSettingsUpdateConsumer(UPDATE_TEMPLATE_DUMMY_SETTING, integer -> {
public Collection<?> createComponents(PluginServices services) {
services.clusterService().getClusterSettings().addSettingsUpdateConsumer(UPDATE_TEMPLATE_DUMMY_SETTING, integer -> {
logger.debug("the template dummy setting was updated to {}", integer);
});
return super.createComponents(
client,
clusterService,
threadPool,
resourceWatcherService,
scriptService,
xContentRegistry,
environment,
nodeEnvironment,
namedWriteableRegistry,
expressionResolver,
repositoriesServiceSupplier,
telemetryProvider,
allocationService,
indicesService
);
return super.createComponents(services);
}

@Override
Expand Down
Loading

0 comments on commit 3e2f17f

Please sign in to comment.