Skip to content

Commit

Permalink
Fix forbidden API calls check failed issue
Browse files Browse the repository at this point in the history
Signed-off-by: zane-neo <[email protected]>
  • Loading branch information
zane-neo committed Oct 10, 2024
1 parent a12e13b commit 33b8f15
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

package org.opensearch.bootstrap;

import org.mockito.InOrder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.opensearch.cli.UserException;
import org.opensearch.common.logging.LogConfigurator;
import org.opensearch.common.settings.KeyStoreCommandTestCase;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.common.settings.SecureSettings;
Expand All @@ -40,26 +43,29 @@
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.env.Environment;
import org.opensearch.node.Node;
import org.opensearch.node.NodeValidationException;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.After;
import org.junit.Before;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.inOrder;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class BootstrapTests extends OpenSearchTestCase {
Environment env;
Expand Down Expand Up @@ -138,44 +144,28 @@ private void assertPassphraseRead(String source, String expected) {
}
}

public void testInitExecutionOrder() throws Exception {
AtomicInteger order = new AtomicInteger(0);

public void testInitExecutionOrder() {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
try {
// Create mock objects for Thread and Node
Thread mockThread = mock(Thread.class);
Node mockNode = mock(Node.class);

// Use reflection to set the INSTANCE to null before the test
Field instanceField = Bootstrap.class.getDeclaredField("INSTANCE");
instanceField.setAccessible(true);
instanceField.set(null, null);

// Use reflection to replace the private fields with our mocks
Bootstrap bootstrap = new Bootstrap();
Field threadField = Bootstrap.class.getDeclaredField("keepAliveThread");
threadField.setAccessible(true);
threadField.set(bootstrap, mockThread);

Field nodeField = Bootstrap.class.getDeclaredField("node");
nodeField.setAccessible(true);
nodeField.set(bootstrap, mockNode);

// Set the INSTANCE to our modified bootstrap
instanceField.set(null, bootstrap);

// Call the startInstance method
Bootstrap.startInstance(bootstrap);

// Verify the order of execution
InOrder inOrder = inOrder(mockThread, mockNode);
inOrder.verify(mockThread).start();
inOrder.verify(mockNode).start();
} catch (Exception e) {
fail(e.getMessage());
}
return null;
Thread mockThread = new Thread(() -> {
assertEquals(0, order.getAndIncrement());
});

Node mockNode = mock(Node.class);
doAnswer(invocation -> {
assertEquals(1, order.getAndIncrement());
return null;
}).when(mockNode).start();

LogConfigurator.registerErrorListener();
Bootstrap testBootstrap = new Bootstrap(mockThread, mockNode);
testBootstrap.setInstance(testBootstrap);

Bootstrap.startInstance(testBootstrap);

verify(mockNode).start();
assertEquals(2, order.get());
}


}
11 changes: 11 additions & 0 deletions server/src/main/java/org/opensearch/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ final class Bootstrap {
private final Thread keepAliveThread;
private final Spawner spawner = new Spawner();

// For testing purpose
void setInstance(Bootstrap bootstrap) {
INSTANCE = bootstrap;
}

// For testing purpose
Bootstrap(Thread keepAliveThread, Node node) {
this.keepAliveThread = keepAliveThread;
this.node = node;
}

/** creates a new instance */
Bootstrap() {
keepAliveThread = new Thread(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ grant {
// otherwise can be provided only to test libraries
permission java.lang.RuntimePermission "fileSystemProvider";

// needed for UT test in BootstrapTests
permission java.lang.RuntimePermission "shutdownHooks";

// needed by jvminfo for monitoring the jvm
permission java.lang.management.ManagementPermission "monitor";

Expand Down

0 comments on commit 33b8f15

Please sign in to comment.