Skip to content

Commit af9c6be

Browse files
committed
Add unit test
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
1 parent 90ff82c commit af9c6be

File tree

4 files changed

+146
-5
lines changed

4 files changed

+146
-5
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.gateway.remote;
10+
11+
import org.opensearch.action.LatchedActionListener;
12+
import org.opensearch.cluster.block.ClusterBlocks;
13+
import org.opensearch.cluster.node.DiscoveryNodes;
14+
import org.opensearch.common.CheckedRunnable;
15+
import org.opensearch.common.settings.ClusterSettings;
16+
import org.opensearch.common.settings.Settings;
17+
import org.opensearch.core.action.ActionListener;
18+
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
19+
import org.opensearch.core.compress.Compressor;
20+
import org.opensearch.core.compress.NoneCompressor;
21+
import org.opensearch.gateway.remote.model.RemoteClusterBlocks;
22+
import org.opensearch.gateway.remote.model.RemoteDiscoveryNodes;
23+
import org.opensearch.gateway.remote.model.RemoteReadResult;
24+
import org.opensearch.index.translog.transfer.BlobStoreTransferService;
25+
import org.opensearch.repositories.blobstore.BlobStoreRepository;
26+
import org.opensearch.test.OpenSearchTestCase;
27+
import org.opensearch.threadpool.TestThreadPool;
28+
import org.opensearch.threadpool.ThreadPool;
29+
import org.junit.After;
30+
import org.junit.Assert;
31+
import org.junit.Before;
32+
33+
import java.io.IOException;
34+
import java.util.concurrent.CountDownLatch;
35+
import java.util.concurrent.atomic.AtomicReference;
36+
37+
import static java.util.Collections.emptyList;
38+
import static org.opensearch.gateway.remote.RemoteClusterStateAttributesManager.DISCOVERY_NODES;
39+
import static org.opensearch.gateway.remote.model.RemoteClusterBlocks.CLUSTER_BLOCKS;
40+
import static org.opensearch.gateway.remote.model.RemoteClusterBlocks.CLUSTER_BLOCKS_FORMAT;
41+
import static org.opensearch.gateway.remote.model.RemoteClusterBlocksTests.randomClusterBlocks;
42+
import static org.opensearch.gateway.remote.model.RemoteDiscoveryNodes.DISCOVERY_NODES_FORMAT;
43+
import static org.opensearch.gateway.remote.model.RemoteDiscoveryNodesTests.getDiscoveryNodes;
44+
import static org.mockito.ArgumentMatchers.anyIterable;
45+
import static org.mockito.ArgumentMatchers.anyString;
46+
import static org.mockito.Mockito.mock;
47+
import static org.mockito.Mockito.when;
48+
49+
public class RemoteClusterStateAttributesManagerTests extends OpenSearchTestCase {
50+
private RemoteClusterStateAttributesManager remoteClusterStateAttributesManager;
51+
private BlobStoreTransferService blobStoreTransferService;
52+
private BlobStoreRepository blobStoreRepository;
53+
private Compressor compressor;
54+
private ThreadPool threadpool = new TestThreadPool(RemoteClusterStateAttributesManagerTests.class.getName());
55+
56+
@Before
57+
public void setup() throws Exception {
58+
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
59+
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(emptyList());
60+
blobStoreRepository = mock(BlobStoreRepository.class);
61+
blobStoreTransferService = mock(BlobStoreTransferService.class);
62+
compressor = new NoneCompressor();
63+
64+
remoteClusterStateAttributesManager = new RemoteClusterStateAttributesManager(
65+
"test-cluster",
66+
blobStoreRepository,
67+
blobStoreTransferService,
68+
namedWriteableRegistry,
69+
threadpool
70+
);
71+
}
72+
73+
@After
74+
public void tearDown() throws Exception {
75+
super.tearDown();
76+
threadpool.shutdown();
77+
}
78+
79+
public void testGetAsyncMetadataReadAction_DiscoveryNodes() throws IOException {
80+
DiscoveryNodes discoveryNodes = getDiscoveryNodes();
81+
String fileName = randomAlphaOfLength(10);
82+
when(blobStoreTransferService.downloadBlob(anyIterable(), anyString())).thenReturn(
83+
DISCOVERY_NODES_FORMAT.serialize(discoveryNodes, fileName, compressor).streamInput()
84+
);
85+
RemoteDiscoveryNodes remoteObjForDownload = new RemoteDiscoveryNodes(fileName, "cluster-uuid", compressor);
86+
CountDownLatch latch = new CountDownLatch(1);
87+
AtomicReference<DiscoveryNodes> readDiscoveryNodes = new AtomicReference<>();
88+
LatchedActionListener<RemoteReadResult> assertingListener = new LatchedActionListener<>(
89+
ActionListener.wrap(response -> readDiscoveryNodes.set((DiscoveryNodes) response.getObj()), Assert::assertNull),
90+
latch
91+
);
92+
CheckedRunnable<IOException> runnable = remoteClusterStateAttributesManager.getAsyncMetadataReadAction(
93+
DISCOVERY_NODES,
94+
remoteObjForDownload,
95+
assertingListener
96+
);
97+
98+
try {
99+
runnable.run();
100+
latch.await();
101+
assertEquals(discoveryNodes.getSize(), readDiscoveryNodes.get().getSize());
102+
discoveryNodes.getNodes().forEach((nodeId, node) -> assertEquals(readDiscoveryNodes.get().get(nodeId), node));
103+
assertEquals(discoveryNodes.getClusterManagerNodeId(), readDiscoveryNodes.get().getClusterManagerNodeId());
104+
} catch (Exception e) {
105+
throw new RuntimeException(e);
106+
}
107+
}
108+
109+
public void testGetAsyncMetadataReadAction_ClusterBlocks() throws IOException {
110+
ClusterBlocks clusterBlocks = randomClusterBlocks();
111+
String fileName = randomAlphaOfLength(10);
112+
when(blobStoreTransferService.downloadBlob(anyIterable(), anyString())).thenReturn(
113+
CLUSTER_BLOCKS_FORMAT.serialize(clusterBlocks, fileName, compressor).streamInput()
114+
);
115+
RemoteClusterBlocks remoteClusterBlocks = new RemoteClusterBlocks(fileName, "cluster-uuid", compressor);
116+
CountDownLatch latch = new CountDownLatch(1);
117+
AtomicReference<ClusterBlocks> readClusterBlocks = new AtomicReference<>();
118+
LatchedActionListener<RemoteReadResult> assertingListener = new LatchedActionListener<>(
119+
ActionListener.wrap(response -> readClusterBlocks.set((ClusterBlocks) response.getObj()), Assert::assertNull),
120+
latch
121+
);
122+
123+
CheckedRunnable<IOException> runnable = remoteClusterStateAttributesManager.getAsyncMetadataReadAction(
124+
CLUSTER_BLOCKS,
125+
remoteClusterBlocks,
126+
assertingListener
127+
);
128+
129+
try {
130+
runnable.run();
131+
latch.await();
132+
assertEquals(clusterBlocks.global(), readClusterBlocks.get().global());
133+
assertEquals(clusterBlocks.indices().keySet(), readClusterBlocks.get().indices().keySet());
134+
for (String index : clusterBlocks.indices().keySet()) {
135+
assertEquals(clusterBlocks.indices().get(index), readClusterBlocks.get().indices().get(index));
136+
}
137+
} catch (Exception e) {
138+
throw new RuntimeException(e);
139+
}
140+
}
141+
}

server/src/test/java/org/opensearch/gateway/remote/model/RemoteClusterBlocksTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void testSerDe() throws IOException {
136136
}
137137
}
138138

139-
static ClusterBlocks randomClusterBlocks() {
139+
public static ClusterBlocks randomClusterBlocks() {
140140
ClusterBlocks.Builder builder = ClusterBlocks.builder();
141141
int randomGlobalBlocks = randomIntBetween(1, 10);
142142
for (int i = 0; i < randomGlobalBlocks; i++) {

server/src/test/java/org/opensearch/gateway/remote/model/RemoteClusterStateCustomsTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ public void testSerDe() throws IOException {
232232
try (InputStream inputStream = remoteObjectForUpload.serialize()) {
233233
remoteObjectForUpload.setFullBlobName(BlobPath.cleanPath());
234234
assertThat(inputStream.available(), greaterThan(0));
235-
Custom readclusterStateCustoms = remoteObjectForUpload.deserialize(inputStream);
236-
assertThat(readclusterStateCustoms, is(clusterStateCustoms));
235+
Custom readClusterStateCustoms = remoteObjectForUpload.deserialize(inputStream);
236+
assertThat(readClusterStateCustoms, is(clusterStateCustoms));
237237
}
238238
}
239239

240-
private Custom getClusterStateCustom() {
240+
public static SnapshotsInProgress getClusterStateCustom() {
241241
return SnapshotsInProgress.of(
242242
List.of(
243243
new SnapshotsInProgress.Entry(

server/src/test/java/org/opensearch/gateway/remote/model/RemoteDiscoveryNodesTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void testExceptionDuringDeserialize() throws IOException {
156156
IOException ioe = assertThrows(IOException.class, () -> remoteObjectForDownload.deserialize(in));
157157
}
158158

159-
private DiscoveryNodes getDiscoveryNodes() {
159+
public static DiscoveryNodes getDiscoveryNodes() {
160160
return DiscoveryNodes.builder()
161161
.add(
162162
new DiscoveryNode(

0 commit comments

Comments
 (0)