From b34714c58dc150e07c2a67f27363bade16dbb8cd Mon Sep 17 00:00:00 2001 From: Priyambada Roul Date: Mon, 10 Oct 2022 13:30:52 +0530 Subject: [PATCH] Refactor out test-only extension runner contructor Signed-off-by: Priyambada Roul --- .../org/opensearch/sdk/ExtensionSettings.java | 5 -- .../org/opensearch/sdk/ExtensionsRunner.java | 47 +------------------ .../org/opensearch/sdk/TransportActions.java | 2 +- .../sdk/ExtensionsRunnerForTest.java | 32 +++++++++++++ .../opensearch/sdk/TestExtensionSettings.java | 4 +- .../opensearch/sdk/TestExtensionsRunner.java | 17 ++++++- .../sdk/TransportCommunicationIT.java | 2 +- 7 files changed, 54 insertions(+), 55 deletions(-) create mode 100644 src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java diff --git a/src/main/java/org/opensearch/sdk/ExtensionSettings.java b/src/main/java/org/opensearch/sdk/ExtensionSettings.java index 007a8ec2..63e47117 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionSettings.java +++ b/src/main/java/org/opensearch/sdk/ExtensionSettings.java @@ -16,11 +16,6 @@ public class ExtensionSettings { private String hostAddress; private String hostPort; - /** - * Path to extension.yml file for testing. Internal use only. - */ - static final String EXTENSION_DESCRIPTOR = "src/test/resources/extension.yml"; - /** * Jackson requires a default constructor. */ diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 3f0dae5b..12bf99eb 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -7,9 +7,6 @@ */ package org.opensearch.sdk; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.cluster.node.DiscoveryNode; @@ -50,10 +47,8 @@ import org.opensearch.transport.TransportService; import org.opensearch.transport.TransportSettings; -import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -114,28 +109,13 @@ public class ExtensionsRunner { */ UpdateSettingsRequestHandler updateSettingsRequestHandler = new UpdateSettingsRequestHandler(); - /** - * Instantiates a new Extensions Runner using test settings. - * - * @throws IOException if the runner failed to read settings or API. - */ - public ExtensionsRunner() throws IOException { - ExtensionSettings extensionSettings = readExtensionSettings(); - this.settings = Settings.builder() - .put(NODE_NAME_SETTING, extensionSettings.getExtensionName()) - .put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostAddress()) - .put(TransportSettings.PORT.getKey(), extensionSettings.getHostPort()) - .build(); - this.customSettings = Collections.emptyList(); - } - /** * Instantiates a new Extensions Runner using the specified extension. * * @param extension The settings with which to start the runner. * @throws IOException if the runner failed to read settings or API. */ - private ExtensionsRunner(Extension extension) throws IOException { + protected ExtensionsRunner(Extension extension) throws IOException { ExtensionSettings extensionSettings = extension.getExtensionSettings(); this.settings = Settings.builder() .put(NODE_NAME_SETTING, extensionSettings.getExtensionName()) @@ -152,14 +132,6 @@ private ExtensionsRunner(Extension extension) throws IOException { this.customSettings = extension.getSettings(); // initialize the transport service nettyTransport.initializeExtensionTransportService(this.getSettings(), this); - // start listening on configured port and wait for connection from OpenSearch - this.startActionListener(0); - } - - private static ExtensionSettings readExtensionSettings() throws IOException { - File file = new File(ExtensionSettings.EXTENSION_DESCRIPTOR); - ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); - return objectMapper.readValue(file, ExtensionSettings.class); } /** @@ -478,21 +450,6 @@ public static void run(Extension extension) throws IOException { logger.info("Starting extension " + extension.getExtensionSettings().getExtensionName()); @SuppressWarnings("unused") ExtensionsRunner runner = new ExtensionsRunner(extension); - } - - /** - * Run the Extension. For internal/testing purposes only. Imports settings and sets up Transport Service listening for incoming connections. - * - * @param args Unused - * @throws IOException if the runner failed to connect to the OpenSearch cluster. - */ - public static void main(String[] args) throws IOException { - - ExtensionsRunner extensionsRunner = new ExtensionsRunner(); - - // initialize the transport service - extensionsRunner.nettyTransport.initializeExtensionTransportService(extensionsRunner.getSettings(), extensionsRunner); - // start listening on configured port and wait for connection from OpenSearch - extensionsRunner.startActionListener(0); + runner.startActionListener(0); } } diff --git a/src/main/java/org/opensearch/sdk/TransportActions.java b/src/main/java/org/opensearch/sdk/TransportActions.java index 49fc8ee9..398360aa 100644 --- a/src/main/java/org/opensearch/sdk/TransportActions.java +++ b/src/main/java/org/opensearch/sdk/TransportActions.java @@ -54,7 +54,7 @@ public void sendRegisterTransportActionsRequest(TransportService transportServic transportService.sendRequest( opensearchNode, ExtensionsOrchestrator.REQUEST_EXTENSION_REGISTER_TRANSPORT_ACTIONS, - new RegisterTransportActionsRequest(transportActions), + new RegisterTransportActionsRequest("", transportActions), registerTransportActionsResponseHandler ); } catch (Exception e) { diff --git a/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java b/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java new file mode 100644 index 00000000..e3c53f88 --- /dev/null +++ b/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java @@ -0,0 +1,32 @@ +package org.opensearch.sdk; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +/** + * An Extension Runner for testing using test settings. + */ +public class ExtensionsRunnerForTest extends ExtensionsRunner { + + /** + * Instantiates a new Extensions Runner using test settings. + * + * @throws IOException if the runner failed to read settings or API. + */ + public ExtensionsRunnerForTest() throws IOException { + super(new Extension() { + @Override + public ExtensionSettings getExtensionSettings() { + return new ExtensionSettings("sample-extension", "127.0.0.1", "4532"); + } + + @Override + public List getExtensionRestHandlers() { + return Collections.emptyList(); + } + }); + + } + +} diff --git a/src/test/java/org/opensearch/sdk/TestExtensionSettings.java b/src/test/java/org/opensearch/sdk/TestExtensionSettings.java index d1043cd6..18f1d6fe 100644 --- a/src/test/java/org/opensearch/sdk/TestExtensionSettings.java +++ b/src/test/java/org/opensearch/sdk/TestExtensionSettings.java @@ -9,14 +9,14 @@ import java.io.File; public class TestExtensionSettings extends OpenSearchTestCase { - + private static final String EXTENSION_DESCRIPTOR = "src/test/resources/extension.yml"; private ExtensionSettings extensionSettings; @Override @BeforeEach public void setUp() throws Exception { super.setUp(); - File file = new File(ExtensionSettings.EXTENSION_DESCRIPTOR); + File file = new File(EXTENSION_DESCRIPTOR); ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); extensionSettings = objectMapper.readValue(file, ExtensionSettings.class); } diff --git a/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java b/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java index 30d0ab80..dc2fef61 100644 --- a/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java +++ b/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java @@ -31,6 +31,7 @@ import java.util.HashMap; import java.util.List; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.opensearch.Version; @@ -77,11 +78,14 @@ public class TestExtensionsRunner extends OpenSearchTestCase { private ExtensionsRunner extensionsRunner; private TransportService transportService; + private TransportService initialTransportService; + @Override @BeforeEach public void setUp() throws Exception { super.setUp(); - this.extensionsRunner = new ExtensionsRunner(); + this.extensionsRunner = new ExtensionsRunnerForTest(); + this.initialTransportService = extensionsRunner.extensionTransportService; this.transportService = spy( new TransportService( Settings.EMPTY, @@ -95,6 +99,17 @@ public void setUp() throws Exception { ); } + @Override + @AfterEach + public void tearDown() throws Exception { + super.tearDown(); + if (initialTransportService != null) { + this.initialTransportService.stop(); + this.initialTransportService.close(); + Thread.sleep(1000); + } + } + // test manager method invokes start on transport service @Test public void testTransportServiceStarted() { diff --git a/src/test/java/org/opensearch/sdk/TransportCommunicationIT.java b/src/test/java/org/opensearch/sdk/TransportCommunicationIT.java index 8cbfe284..10c31105 100644 --- a/src/test/java/org/opensearch/sdk/TransportCommunicationIT.java +++ b/src/test/java/org/opensearch/sdk/TransportCommunicationIT.java @@ -145,7 +145,7 @@ public void run() { private void startTransportandClient(Settings settings, Thread client) throws IOException, InterruptedException { // retrieve transport service - ExtensionsRunner extensionsRunner = new ExtensionsRunner(); + ExtensionsRunner extensionsRunner = new ExtensionsRunnerForTest(); // start transport service TransportService transportService = nettyTransport.initializeExtensionTransportService(settings, extensionsRunner);