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

Added tests for createComponent workflow and removed the logs of gradle test #44

Merged
merged 7 commits into from
Jul 15, 2022
Merged
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
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ test {
useJUnitPlatform()
jvmArgs '--enable-preview'
systemProperty 'tests.security.manager', 'false'
testLogging.showStandardStreams = true
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
events "passed", "skipped", "failed"
}
}
35 changes: 15 additions & 20 deletions src/main/java/org/opensearch/sdk/ExtensionSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,37 @@
*/
public class ExtensionSettings {

private String extensionname;
private String hostaddress;
private String hostport;
private String extensionName;
private String hostAddress;
private String hostPort;

/**
* Placeholder field. Change the location to extension.yml file of the extension.
*/
public static final String EXTENSION_DESCRIPTOR = "src/test/resources/extension.yml";
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved

public String getExtensionname() {
return extensionname;
}

public void setExtensionname(String extensionname) {
this.extensionname = extensionname;
}

public String getHostaddress() {
return hostaddress;
/**
* Jackson requires a default constructor.
*/
private ExtensionSettings() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does ObjectMapper use this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does ObjectMapper use this?

Reflection!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses reflection to fill the attributes with the value present in the yml file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which introduces its own problems with Java Modules, if we ever decide to publish a module descriptor we'll have to open this package to Jackson's module. But for now, let's enjoy the classpath...

super();
}

public void setHostaddress(String hostaddress) {
this.hostaddress = hostaddress;
public String getExtensionName() {
return extensionName;
}

public String getHostport() {
return hostport;
public String getHostAddress() {
return hostAddress;
}

public void setHostport(String hostport) {
this.hostport = hostport;
public String getHostPort() {
return hostPort;
}

@Override
public String toString() {
return "\nnodename: " + extensionname + "\nhostaddress: " + hostaddress + "\nhostPort: " + hostport + "\n";
return "\nnodename: " + extensionName + "\nhostaddress: " + hostAddress + "\nhostPort: " + hostPort + "\n";
}

}
12 changes: 6 additions & 6 deletions src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public class ExtensionsRunner {
public ExtensionsRunner() throws IOException {}

private final Settings settings = Settings.builder()
.put("node.name", extensionSettings.getExtensionname())
.put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostaddress())
.put(TransportSettings.PORT.getKey(), extensionSettings.getHostport())
.put("node.name", extensionSettings.getExtensionName())
.put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostAddress())
.put(TransportSettings.PORT.getKey(), extensionSettings.getHostPort())
.build();
private final Logger logger = LogManager.getLogger(ExtensionsRunner.class);
private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() {
Expand All @@ -91,7 +91,7 @@ private void setOpensearchNode(DiscoveryNode opensearchNode) {
this.opensearchNode = opensearchNode;
}

private DiscoveryNode getOpensearchNode() {
public DiscoveryNode getOpensearchNode() {
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
return opensearchNode;
}

Expand All @@ -103,7 +103,7 @@ private DiscoveryNode getOpensearchNode() {
*/
PluginResponse handlePluginsRequest(PluginRequest pluginRequest) {
logger.info("Registering Plugin Request received from OpenSearch");
PluginResponse pluginResponse = new PluginResponse("RealExtension");
PluginResponse pluginResponse = new PluginResponse(extensionSettings.getExtensionName());
opensearchNode = pluginRequest.getSourceNode();
setOpensearchNode(opensearchNode);
return pluginResponse;
Expand Down Expand Up @@ -199,7 +199,7 @@ public TransportService createTransportService(Settings settings) {
threadPool,
NOOP_TRANSPORT_INTERCEPTOR,
boundAddress -> DiscoveryNode.createLocal(
Settings.builder().put("node.name", extensionSettings.getExtensionname()).build(),
Settings.builder().put("node.name", extensionSettings.getExtensionName()).build(),
boundAddress.publishAddress(),
randomBase64UUID()
),
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/opensearch/sdk/TestExtensionSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.opensearch.sdk;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensearch.test.OpenSearchTestCase;

import java.io.File;
import java.io.IOException;

public class TestExtensionSettings extends OpenSearchTestCase {

private ExtensionSettings extensionSettings;

@BeforeEach
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
public void setUp() throws IOException {
File file = new File(ExtensionSettings.EXTENSION_DESCRIPTOR);
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
extensionSettings = objectMapper.readValue(file, ExtensionSettings.class);
}

@Test
public void testExtensionName() {
assertEquals(extensionSettings.getExtensionName(), "extension");
}

@Test
public void testHostAddress() {
assertEquals(extensionSettings.getHostAddress(), "127.0.0.1");
}

@Test
public void testHostPort() {
assertEquals(extensionSettings.getHostPort(), "4532");
}
}
102 changes: 86 additions & 16 deletions src/test/java/org/opensearch/sdk/TestExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,125 @@

package org.opensearch.sdk;

import static java.util.Collections.emptySet;
import static java.util.Collections.emptyMap;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;

import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.opensearch.Version;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.transport.TransportAddress;
import org.opensearch.discovery.PluginRequest;
import org.opensearch.discovery.PluginResponse;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.transport.TransportService;
import org.opensearch.transport.Transport;

public class TestExtensionsRunner extends OpenSearchTestCase {

private ExtensionsRunner extensionsRunner;
private Settings settings;
private TransportService transportService;

@BeforeEach
public void setUp() throws IOException {

public void setUp() throws Exception {
this.extensionsRunner = new ExtensionsRunner();
this.settings = Settings.builder().put("node.name", "MainScriptTests").build();
this.settings = Settings.builder().put("node.name", "sdk").build();
this.transportService = spy(
new TransportService(
Settings.EMPTY,
mock(Transport.class),
null,
TransportService.NOOP_TRANSPORT_INTERCEPTOR,
x -> null,
null,
Collections.emptySet()
)
);
}

// test ExtensionsRunner getTransportService return type is transport service
@Test
public void testGetTransportService() throws IOException {
public void testGetTransportService() {
assert (extensionsRunner.createTransportService(settings) instanceof TransportService);
}

// test manager method invokes start on transport service
@Test
public void testTransportServiceStarted() throws IOException {

// retrieve and mock transport service
TransportService transportService = Mockito.spy(extensionsRunner.createTransportService(settings));
public void testTransportServiceStarted() {

// verify mocked object interaction in manager method
extensionsRunner.startTransportService(transportService);
Mockito.verify(transportService, times(1)).start();
verify(transportService, times(1)).start();
}

// test manager method invokes accept incoming requests on transport service
@Test
public void testTransportServiceAcceptedIncomingRequests() throws IOException {

// retrieve and mock transport service
TransportService transportService = Mockito.spy(extensionsRunner.createTransportService(settings));
public void testTransportServiceAcceptedIncomingRequests() {

// verify mocked object interaction in manager method
extensionsRunner.startTransportService(transportService);
Mockito.verify(transportService, times(1)).acceptIncomingRequests();
verify(transportService, times(1)).acceptIncomingRequests();
}

@Test
public void testRegisterRequestHandler() {

extensionsRunner.startTransportService(transportService);
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
verify(transportService, times(3)).registerRequestHandler(anyString(), anyString(), anyBoolean(), anyBoolean(), any(), any());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of any, lets verify the respective handlers are being registered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we invoke startTransportService above it expects all the 3 handlers to be present at the same time. If we try to match it one by one, it causes MatchersException. I tried this before using any. I'm open to suggestions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I didn't understand it. Why does it expect all 3 handlers at the same time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As startTransportService is registering all 3 handlers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure there is a way. I dont want to block this PR for this one.
See if you find a solution.

}

@Test
public void testHandlePluginsRequest() throws UnknownHostException {
DiscoveryNode sourceNode = new DiscoveryNode(
"test_node",
new TransportAddress(InetAddress.getByName("localhost"), 9876),
emptyMap(),
emptySet(),
Version.CURRENT
);
PluginRequest pluginRequest = new PluginRequest(sourceNode, null);
PluginResponse response = extensionsRunner.handlePluginsRequest(pluginRequest);
assertEquals(response.getName(), "extension");

// Test if the source node is set after handlePluginRequest() is called during OpenSearch bootstrap
assertEquals(extensionsRunner.getOpensearchNode(), sourceNode);
}

@Test
public void testClusterStateRequest() {

extensionsRunner.sendClusterStateRequest(transportService);

verify(transportService, times(1)).sendRequest(any(), anyString(), any(), any(ClusterStateResponseHandler.class));
}

@Test
public void testClusterSettingRequest() {

extensionsRunner.sendClusterSettingsRequest(transportService);

verify(transportService, times(1)).sendRequest(any(), anyString(), any(), any(ClusterSettingsResponseHandler.class));
}

@Test
public void testLocalNodeRequest() {

extensionsRunner.sendLocalNodeRequest(transportService);

verify(transportService, times(1)).sendRequest(any(), anyString(), any(), any(LocalNodeResponseHandler.class));
}

}
6 changes: 3 additions & 3 deletions src/test/resources/extension.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
extensionname: extension
hostaddress: 127.0.0.1
hostport: 4532
extensionName: extension
hostAddress: 127.0.0.1
hostPort: 4532