Skip to content

Commit

Permalink
Create 2 public constructors for MockNode
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jan 22, 2025
1 parent 4cf427c commit f9bc2ae
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ private Node startNode() throws NodeValidationException {

Node node = new MockNode(
settings,
Arrays.asList(MockNioTransportPlugin.class, MockHttpTransport.TestPlugin.class, InternalSettingsPlugin.class),
null,
true,
Collections.emptyMap()
Arrays.asList(MockNioTransportPlugin.class, MockHttpTransport.TestPlugin.class, InternalSettingsPlugin.class)
);
node.start();
return node;
Expand Down
44 changes: 19 additions & 25 deletions test/framework/src/main/java/org/opensearch/node/MockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
Expand All @@ -90,31 +89,33 @@
*/
public class MockNode extends Node {

private final Collection<Class<? extends Plugin>> classpathPlugins;
private final Collection<PluginInfo> classpathPlugins;

private MockNode(
final Environment environment,
final Collection<PluginInfo> classpathPlugins,
final boolean forbidPrivateIndexSettings
) {
super(environment, classpathPlugins, forbidPrivateIndexSettings);
this.classpathPlugins = classpathPlugins;
}

public MockNode(
final Settings settings,
final Collection<Class<? extends Plugin>> classpathPlugins,
final Collection<PluginInfo> classpathPlugins,
final Path configPath,
final boolean forbidPrivateIndexSettings,
final Map<Class<? extends Plugin>, Class<? extends Plugin>> extendedPlugins
final boolean forbidPrivateIndexSettings
) {
this(
InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), configPath, () -> "mock_ node"),
classpathPlugins,
forbidPrivateIndexSettings,
extendedPlugins
forbidPrivateIndexSettings
);
}

private MockNode(
final Environment environment,
final Collection<Class<? extends Plugin>> classpathPlugins,
final boolean forbidPrivateIndexSettings,
final Map<Class<? extends Plugin>, Class<? extends Plugin>> extendedPlugins
) {
super(
environment,
public MockNode(final Settings settings, final Collection<Class<? extends Plugin>> classpathPlugins) {
this(
InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), null, () -> "mock_ node"),
classpathPlugins.stream()
.map(
p -> new PluginInfo(
Expand All @@ -125,26 +126,19 @@ private MockNode(
"1.8",
p.getName(),
null,
(extendedPlugins != null && extendedPlugins.containsKey(p))
? List.of(extendedPlugins.get(p).getName())
: Collections.emptyList(),
Collections.emptyList(),
false
)
)
.collect(Collectors.toList()),
forbidPrivateIndexSettings
true
);
this.classpathPlugins = classpathPlugins;
}

public MockNode(final Settings settings, final Collection<Class<? extends Plugin>> classpathPlugins) {
this(settings, classpathPlugins, null, true, Collections.emptyMap());
}

/**
* The classpath plugins this node was constructed with.
*/
public Collection<Class<? extends Plugin>> getClasspathPlugins() {
public Collection<PluginInfo> getClasspathPlugins() {
return classpathPlugins;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException;
import org.opensearch.Version;
import org.opensearch.action.admin.cluster.node.info.NodeInfo;
import org.opensearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.opensearch.action.admin.cluster.node.stats.NodeStats;
Expand All @@ -50,6 +51,7 @@
import org.opensearch.http.HttpInfo;
import org.opensearch.node.MockNode;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.PluginInfo;
import org.opensearch.transport.nio.MockNioTransportPlugin;

import java.io.IOException;
Expand All @@ -59,6 +61,7 @@
import java.util.Arrays;
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.Function;
Expand Down Expand Up @@ -131,7 +134,28 @@ public ExternalTestCluster(
pluginClasses = new ArrayList<>(pluginClasses);
pluginClasses.add(MockHttpTransport.TestPlugin.class);
Settings clientSettings = clientSettingsBuilder.build();
MockNode node = new MockNode(clientSettings, pluginClasses, null, true, extendedPlugins);
MockNode node = new MockNode(
clientSettings,
pluginClasses.stream()
.map(
p -> new PluginInfo(
p.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
p.getName(),
null,
(extendedPlugins != null && extendedPlugins.containsKey(p))
? List.of(extendedPlugins.get(p).getName())
: Collections.emptyList(),
false
)
)
.collect(Collectors.toList()),
null,
true
);
Client client = clientWrapper.apply(node.client());
try {
node.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.store.AlreadyClosedException;
import org.opensearch.Version;
import org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsAction;
import org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest;
import org.opensearch.action.admin.cluster.configuration.ClearVotingConfigExclusionsAction;
Expand Down Expand Up @@ -113,6 +114,7 @@
import org.opensearch.node.NodeService;
import org.opensearch.node.NodeValidationException;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.PluginInfo;
import org.opensearch.script.ScriptModule;
import org.opensearch.script.ScriptService;
import org.opensearch.search.SearchService;
Expand Down Expand Up @@ -824,10 +826,25 @@ private synchronized NodeAndClient buildNode(int nodeId, Settings settings, bool
}
MockNode node = new MockNode(
settings,
plugins,
plugins.stream()
.map(
p -> new PluginInfo(
p.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
p.getName(),
null,
(extendedPlugins != null && extendedPlugins.containsKey(p))
? List.of(extendedPlugins.get(p).getName())
: Collections.emptyList(),
false
)
)
.collect(Collectors.toList()),
nodeConfigurationSource.nodeConfigPath(nodeId),
forbidPrivateIndexSettings,
extendedPlugins
forbidPrivateIndexSettings
);
node.injector().getInstance(TransportService.class).addLifecycleListener(new LifecycleListener() {
@Override
Expand Down Expand Up @@ -1120,8 +1137,13 @@ private void recreateNode(final Settings newSettings, final Runnable onTransport
.put(newSettings)
.put(NodeEnvironment.NODE_ID_SEED_SETTING.getKey(), newIdSeed)
.build();
Collection<Class<? extends Plugin>> plugins = node.getClasspathPlugins();
node = new MockNode(finalSettings, plugins);
Collection<PluginInfo> plugins = node.getClasspathPlugins();
node = new MockNode(
finalSettings,
plugins,
nodeConfigurationSource.nodeConfigPath((int) newIdSeed),
forbidPrivateIndexSettings
);
node.injector().getInstance(TransportService.class).addLifecycleListener(new LifecycleListener() {
@Override
public void afterStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.carrotsearch.randomizedtesting.RandomizedContext;

import org.opensearch.Version;
import org.opensearch.action.admin.cluster.health.ClusterHealthRequest;
import org.opensearch.action.admin.cluster.health.ClusterHealthResponse;
import org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder;
Expand Down Expand Up @@ -68,6 +69,7 @@
import org.opensearch.node.Node;
import org.opensearch.node.NodeValidationException;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.PluginInfo;
import org.opensearch.script.MockScriptService;
import org.opensearch.search.SearchService;
import org.opensearch.search.internal.SearchContext;
Expand All @@ -85,6 +87,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.opensearch.cluster.coordination.ClusterBootstrapService.INITIAL_CLUSTER_MANAGER_NODES_SETTING;
import static org.opensearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;
Expand Down Expand Up @@ -272,7 +275,26 @@ private Node newNode() {
plugins.add(MockScriptService.TestPlugin.class);

plugins.add(MockTelemetryPlugin.class);
Node node = new MockNode(settingsBuilder.build(), plugins, null, forbidPrivateIndexSettings(), Collections.emptyMap());
Node node = new MockNode(
settingsBuilder.build(),
plugins.stream()
.map(
p -> new PluginInfo(
p.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
p.getName(),
null,
Collections.emptyList(),
false
)
)
.collect(Collectors.toList()),
null,
forbidPrivateIndexSettings()
);
try {
node.start();
} catch (NodeValidationException e) {
Expand Down

0 comments on commit f9bc2ae

Please sign in to comment.