Skip to content

Commit d7beab2

Browse files
Decompose test bulk/search helper. Fix flaky test - bulk refresh true.
Signed-off-by: Finn Carroll <carrofin@amazon.com>
1 parent 9086ff0 commit d7beab2

File tree

4 files changed

+154
-41
lines changed

4 files changed

+154
-41
lines changed
Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.opensearch.security.grpc;
1111

1212
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
13+
import io.grpc.ManagedChannel;
1314
import io.grpc.StatusRuntimeException;
1415
import org.junit.ClassRule;
1516
import org.junit.Test;
@@ -19,70 +20,57 @@
1920
import org.opensearch.test.framework.cluster.LocalCluster;
2021

2122
import java.io.IOException;
23+
import java.util.UUID;
2224

2325
import static org.hamcrest.MatcherAssert.assertThat;
2426
import static org.junit.Assert.assertEquals;
2527
import static org.junit.Assert.assertThrows;
26-
import static org.opensearch.security.grpc.GrpcHelpers.CLIENT_AUTH_OPT;
28+
import static org.opensearch.security.grpc.GrpcHelpers.CLIENT_AUTH_NONE;
2729
import static org.opensearch.security.grpc.GrpcHelpers.baseGrpcCluster;
2830

2931
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
3032
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
31-
public class GrpcTestsClientAuthOptionalTest {
33+
public class GrpcClientAuthNoneTests {
3234
@ClassRule
3335
public static LocalCluster cluster = baseGrpcCluster()
34-
.nodeSettings(CLIENT_AUTH_OPT)
36+
.nodeSettings(CLIENT_AUTH_NONE)
3537
.build();
3638

37-
@Test
38-
public void testPlaintextChannelFails() {
39-
long testDocs = 1;
40-
String testIndex = "plaintext-test-index";
41-
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
42-
GrpcHelpers.doBulk(GrpcHelpers.plaintextChannel(), testIndex, testDocs);
43-
});
44-
assertEquals("UNAVAILABLE: Network closed for unknown reason", exception.getMessage());
45-
}
39+
public static void assertBulkAndSearchTestIndex(ManagedChannel channel) {
40+
int testDocs = (int)(Math.random() * 101);
41+
String testIndex = UUID.randomUUID().toString().substring(0, 10);
4642

47-
@Test
48-
public void testBulkAndSearchInsecureChannel() {
49-
long testDocs = 9;
50-
String testIndex = "test-index";
51-
52-
BulkResponse bulkResp = GrpcHelpers.doBulk(GrpcHelpers.insecureChannel(), testIndex, testDocs);
43+
BulkResponse bulkResp = GrpcHelpers.doBulk(channel, testIndex, testDocs);
5344
assertThat("Bulk response should not be null", bulkResp != null);
5445
assertThat("Bulk response should not contain errors", !bulkResp.hasBulkErrorResponse());
5546
assertThat("Bulk response should have response for all docs indexed", bulkResp.getBulkResponseBody().getItemsCount() == testDocs);
5647

57-
SearchResponse searchResp = GrpcHelpers.doMatchAll(GrpcHelpers.insecureChannel(), testIndex, 100);
48+
SearchResponse searchResp = GrpcHelpers.doMatchAll(channel, testIndex, 10);
5849
assertThat("Search response should not be null", searchResp != null);
5950
assertThat("Search response should indicate success", searchResp.getResponseCase().getNumber() == SearchResponse.ResponseCase.RESPONSE_BODY.getNumber());
6051
assertThat("Search response has correct hits count", searchResp.getResponseBody().getHits().getTotal().getTotalHits().getValue() == testDocs);
6152
}
6253

6354
@Test
64-
public void testBulkAndSearchSecureChannel() throws IOException {
65-
long testDocs = 9;
66-
String testIndex = "test-index";
55+
public void testPlaintextChannel() {
56+
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
57+
assertBulkAndSearchTestIndex(GrpcHelpers.plaintextChannel());
58+
});
59+
assertEquals("UNAVAILABLE: Network closed for unknown reason", exception.getMessage());
60+
}
6761

68-
BulkResponse bulkResp = GrpcHelpers.doBulk(GrpcHelpers.secureChannel(), testIndex, testDocs);
69-
assertThat("Bulk response should not be null", bulkResp != null);
70-
assertThat("Bulk response should not contain errors", !bulkResp.hasBulkErrorResponse());
71-
assertThat("Bulk response should have response for all docs indexed", bulkResp.getBulkResponseBody().getItemsCount() == testDocs);
62+
@Test
63+
public void testBulkAndSearchInsecureChannel() {
64+
assertBulkAndSearchTestIndex(GrpcHelpers.insecureChannel());
65+
}
7266

73-
SearchResponse searchResp = GrpcHelpers.doMatchAll(GrpcHelpers.secureChannel(), testIndex, 100);
74-
assertThat("Search response should not be null", searchResp != null);
75-
assertThat("Search response should indicate success", searchResp.getResponseCase().getNumber() == SearchResponse.ResponseCase.RESPONSE_BODY.getNumber());
76-
assertThat("Search response has correct hits count", searchResp.getResponseBody().getHits().getTotal().getTotalHits().getValue() == testDocs);
67+
@Test
68+
public void testBulkAndSearchSecureChannel() throws IOException {
69+
assertBulkAndSearchTestIndex(GrpcHelpers.secureChannel());
7770
}
7871

7972
@Test
80-
public void testBulkAndSearchUntrustedSecureChannelFails() {
81-
long testDocs = 1;
82-
String testIndex = "plaintext-test-index";
83-
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
84-
GrpcHelpers.doBulk(GrpcHelpers.secureUntrustedChannel(), testIndex, testDocs);
85-
});
86-
assertEquals("UNAVAILABLE: ssl exception", exception.getMessage());
73+
public void testBulkAndSearchUntrustedSecureChannel() throws IOException {
74+
assertBulkAndSearchTestIndex(GrpcHelpers.secureUntrustedChannel());
8775
}
8876
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* The OpenSearch Contributors require contributions made to
6+
* this file be licensed under the Apache-2.0 license or a
7+
* compatible open source license.
8+
*
9+
*/
10+
package org.opensearch.security.grpc;
11+
12+
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
13+
import io.grpc.StatusRuntimeException;
14+
import org.junit.ClassRule;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.opensearch.protobufs.BulkResponse;
18+
import org.opensearch.protobufs.SearchResponse;
19+
import org.opensearch.test.framework.cluster.LocalCluster;
20+
21+
import java.io.IOException;
22+
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertThrows;
26+
import static org.opensearch.security.grpc.GrpcClientAuthNoneTests.assertBulkAndSearchTestIndex;
27+
import static org.opensearch.security.grpc.GrpcHelpers.CLIENT_AUTH_OPT;
28+
import static org.opensearch.security.grpc.GrpcHelpers.baseGrpcCluster;
29+
30+
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
31+
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
32+
public class GrpcClientAuthOptionalTests {
33+
@ClassRule
34+
public static LocalCluster cluster = baseGrpcCluster()
35+
.nodeSettings(CLIENT_AUTH_OPT)
36+
.build();
37+
38+
@Test
39+
public void testPlaintextChannel() {
40+
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
41+
assertBulkAndSearchTestIndex(GrpcHelpers.plaintextChannel());
42+
});
43+
assertEquals("UNAVAILABLE: Network closed for unknown reason", exception.getMessage());
44+
}
45+
46+
@Test
47+
public void testBulkAndSearchInsecureChannel() {
48+
assertBulkAndSearchTestIndex(GrpcHelpers.insecureChannel());
49+
}
50+
51+
@Test
52+
public void testBulkAndSearchSecureChannel() throws IOException {
53+
assertBulkAndSearchTestIndex(GrpcHelpers.secureChannel());
54+
}
55+
56+
@Test
57+
public void testBulkAndSearchUntrustedSecureChannel() {
58+
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
59+
assertBulkAndSearchTestIndex(GrpcHelpers.secureUntrustedChannel());
60+
});
61+
assertEquals("UNAVAILABLE: ssl exception", exception.getMessage());
62+
}
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* The OpenSearch Contributors require contributions made to
6+
* this file be licensed under the Apache-2.0 license or a
7+
* compatible open source license.
8+
*
9+
*/
10+
package org.opensearch.security.grpc;
11+
12+
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
13+
import io.grpc.StatusRuntimeException;
14+
import org.junit.ClassRule;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.opensearch.test.framework.cluster.LocalCluster;
18+
19+
import java.io.IOException;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThrows;
23+
import static org.opensearch.security.grpc.GrpcClientAuthNoneTests.assertBulkAndSearchTestIndex;
24+
import static org.opensearch.security.grpc.GrpcHelpers.CLIENT_AUTH_REQUIRE;
25+
import static org.opensearch.security.grpc.GrpcHelpers.baseGrpcCluster;
26+
27+
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
28+
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
29+
public class GrpcClientAuthRequireTests {
30+
@ClassRule
31+
public static LocalCluster cluster = baseGrpcCluster()
32+
.nodeSettings(CLIENT_AUTH_REQUIRE)
33+
.build();
34+
35+
@Test
36+
public void testPlaintextChannel() {
37+
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
38+
assertBulkAndSearchTestIndex(GrpcHelpers.plaintextChannel());
39+
});
40+
assertEquals("UNAVAILABLE: Network closed for unknown reason", exception.getMessage());
41+
}
42+
43+
@Test
44+
public void testBulkAndSearchInsecureChannel() {
45+
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
46+
assertBulkAndSearchTestIndex(GrpcHelpers.secureUntrustedChannel());
47+
});
48+
assertEquals("UNAVAILABLE: ssl exception", exception.getMessage());
49+
}
50+
51+
@Test
52+
public void testBulkAndSearchSecureChannel() throws IOException {
53+
assertBulkAndSearchTestIndex(GrpcHelpers.secureChannel());
54+
}
55+
56+
@Test
57+
public void testBulkAndSearchUntrustedSecureChannel() {
58+
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> {
59+
assertBulkAndSearchTestIndex(GrpcHelpers.secureUntrustedChannel());
60+
});
61+
assertEquals("UNAVAILABLE: ssl exception", exception.getMessage());
62+
}
63+
}

src/integrationTest/java/org/opensearch/security/grpc/GrpcHelpers.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.opensearch.protobufs.IndexOperation;
2929
import org.opensearch.protobufs.MatchAllQuery;
3030
import org.opensearch.protobufs.QueryContainer;
31+
import org.opensearch.protobufs.Refresh;
3132
import org.opensearch.protobufs.SearchRequest;
3233
import org.opensearch.protobufs.SearchRequestBody;
3334
import org.opensearch.protobufs.SearchResponse;
@@ -38,8 +39,6 @@
3839
import org.opensearch.test.framework.cluster.ClusterManager;
3940
import org.opensearch.test.framework.cluster.LocalCluster;
4041

41-
import javax.net.ssl.SSLException;
42-
4342
import static io.grpc.internal.GrpcUtil.NOOP_PROXY_DETECTOR;
4443
import static org.opensearch.plugin.transport.grpc.ssl.SecureNetty4GrpcServerTransport.GRPC_SECURE_TRANSPORT_SETTING_KEY;
4544
import static org.opensearch.plugin.transport.grpc.ssl.SecureNetty4GrpcServerTransport.SETTING_GRPC_SECURE_PORT;
@@ -130,7 +129,8 @@ public static ManagedChannel secureUntrustedChannel() throws IOException {
130129
}
131130

132131
public static BulkResponse doBulk(ManagedChannel channel, String index, long numDocs) {
133-
BulkRequest.Builder requestBuilder = BulkRequest.newBuilder();
132+
BulkRequest.Builder requestBuilder = BulkRequest.newBuilder()
133+
.setRefresh(Refresh.REFRESH_TRUE);
134134
for (int i = 0; i < numDocs; i++) {
135135
String docBody = "{\"field\": \"doc " + i + " body\"}";
136136
IndexOperation indexOp = IndexOperation.newBuilder()
@@ -152,7 +152,6 @@ public static SearchResponse doMatchAll(ManagedChannel channel, String index, in
152152
.setMatchAll(MatchAllQuery.newBuilder().build())
153153
.build();
154154
SearchRequestBody requestBody = SearchRequestBody.newBuilder()
155-
.setFrom(0)
156155
.setSize(size)
157156
.setQuery(query)
158157
.build();

0 commit comments

Comments
 (0)