Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable testing for ExtensiblePlugins using classpath plugins #16908

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.plugins;

import org.opensearch.test.OpenSearchIntegTestCase;

import java.io.IOException;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.Matchers.equalTo;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0)
public class ClasspathPluginIT extends OpenSearchIntegTestCase {

public interface SampleExtension {}

public static class SampleExtensiblePlugin extends Plugin implements ExtensiblePlugin {
public SampleExtensiblePlugin() {}

@Override
public void loadExtensions(ExtensiblePlugin.ExtensionLoader loader) {
int nLoaded = 0;
for (SampleExtension e : loader.loadExtensions(SampleExtension.class)) {
nLoaded++;
}

assertThat(nLoaded, equalTo(1));
}
}

public static class SampleExtendingPlugin extends Plugin implements SampleExtension {
public SampleExtendingPlugin() {}
};

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Stream.concat(super.nodePlugins().stream(), Stream.of(SampleExtensiblePlugin.class, SampleExtendingPlugin.class))
.collect(Collectors.toList());
}

public void testPluginExtensionWithClasspathPlugins() throws IOException {
internalCluster().startNode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

org.opensearch.plugins.ClasspathPluginIT$SampleExtendingPlugin
13 changes: 11 additions & 2 deletions server/src/main/java/org/opensearch/plugins/PluginsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public PluginsService(
"1.8",
pluginClass.getName(),
null,
Collections.emptyList(),
classpathPlugins.stream().map(Class::getName).filter(cp -> !pluginClass.getName().equals(cp)).collect(Collectors.toList()),
false
);
if (logger.isTraceEnabled()) {
Expand All @@ -160,6 +160,7 @@ public PluginsService(
pluginsList.add(pluginInfo);
pluginsNames.add(pluginInfo.getName());
}
loadExtensions(pluginsLoaded);

Set<Bundle> seenBundles = new LinkedHashSet<>();
List<PluginInfo> modulesList = new ArrayList<>();
Expand Down Expand Up @@ -569,9 +570,17 @@ private static void loadExtensionsForPlugin(ExtensiblePlugin extensiblePlugin, L
ExtensiblePlugin.ExtensionLoader extensionLoader = new ExtensiblePlugin.ExtensionLoader() {
@Override
public <T> List<T> loadExtensions(Class<T> extensionPointType) {
Set<Class<?>> seenClasses = new LinkedHashSet<>();
List<T> result = new ArrayList<>();

for (Plugin extendingPlugin : extendingPlugins) {
result.addAll(createExtensions(extensionPointType, extendingPlugin));
List<? extends T> extensions = createExtensions(extensionPointType, extendingPlugin);
for (T extension : extensions) {
// Only add if we haven't seen this class before, needed for classpath extensions for testing
if (seenClasses.add(extension.getClass())) {
result.add(extension);
}
}
}
return Collections.unmodifiableList(result);
}
Expand Down
Loading