Skip to content

Commit 977fd65

Browse files
committed
add tests for method
1 parent 2602253 commit 977fd65

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

storm-server/src/test/java/org/apache/storm/daemon/nimbus/NimbusTest.java

+88-1
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,74 @@
2525

2626
import org.apache.storm.Config;
2727
import org.apache.storm.DaemonConfig;
28+
import org.apache.storm.blobstore.BlobStore;
29+
import org.apache.storm.blobstore.KeySequenceNumber;
30+
import org.apache.storm.blobstore.LocalFsBlobStore;
31+
import org.apache.storm.cluster.IStormClusterState;
2832
import org.apache.storm.generated.InvalidTopologyException;
33+
import org.apache.storm.generated.KeyNotFoundException;
2934
import org.apache.storm.generated.StormTopology;
35+
import org.apache.storm.metric.StormMetricsRegistry;
36+
import org.apache.storm.nimbus.ILeaderElector;
37+
import org.apache.storm.nimbus.NimbusInfo;
38+
import org.apache.storm.scheduler.INimbus;
3039
import org.apache.storm.scheduler.resource.strategies.priority.DefaultSchedulingPriorityStrategy;
3140
import org.apache.storm.scheduler.resource.strategies.scheduling.DefaultResourceAwareStrategy;
3241
import org.apache.storm.scheduler.resource.strategies.scheduling.GenericResourceAwareStrategyOld;
3342
import org.apache.storm.scheduler.resource.strategies.scheduling.RoundRobinResourceAwareStrategy;
43+
import org.apache.storm.security.auth.IGroupMappingServiceProvider;
3444
import org.apache.storm.testing.TestWordSpout;
3545
import org.apache.storm.topology.TopologyBuilder;
3646
import org.apache.storm.utils.ServerUtils;
3747
import org.apache.storm.utils.Time;
48+
import org.junit.jupiter.api.BeforeEach;
3849
import org.junit.jupiter.api.Test;
50+
import org.mockito.Mock;
51+
import org.mockito.MockedConstruction;
52+
import org.mockito.MockitoAnnotations;
3953

4054
import static org.junit.jupiter.api.Assertions.assertEquals;
55+
import static org.junit.jupiter.api.Assertions.assertThrows;
4156
import static org.junit.jupiter.api.Assertions.assertTrue;
4257
import static org.junit.jupiter.api.Assertions.fail;
4358
import static org.junit.jupiter.api.Assertions.assertNull;
59+
import static org.mockito.ArgumentMatchers.any;
60+
import static org.mockito.ArgumentMatchers.eq;
61+
import static org.mockito.Mockito.doThrow;
62+
import static org.mockito.Mockito.mock;
63+
import static org.mockito.Mockito.mockConstruction;
64+
import static org.mockito.Mockito.never;
65+
import static org.mockito.Mockito.verify;
66+
import static org.mockito.Mockito.when;
67+
68+
class NimbusTest {
69+
private static final String BLOB_FILE_KEY = "file-key";
70+
71+
@Mock
72+
private StormMetricsRegistry metricRegistry;
73+
@Mock
74+
private INimbus iNimbus;
75+
@Mock
76+
private IStormClusterState stormClusterState;
77+
@Mock
78+
private NimbusInfo nimbusInfo;
79+
@Mock
80+
private LocalFsBlobStore localBlobStore;
81+
@Mock
82+
private ILeaderElector leaderElector;
83+
@Mock
84+
private IGroupMappingServiceProvider groupMapper;
85+
86+
private Nimbus nimbus;
87+
88+
@BeforeEach
89+
public void setUp() throws Exception {
90+
MockitoAnnotations.openMocks(this).close();
91+
92+
Map<String, Object> conf = Map.of(DaemonConfig.NIMBUS_MONITOR_FREQ_SECS, 10);
93+
nimbus = new Nimbus(conf, iNimbus, stormClusterState, nimbusInfo, localBlobStore, leaderElector, groupMapper, metricRegistry);
94+
}
4495

45-
public class NimbusTest {
4696
@Test
4797
public void testMemoryLoadLargerThanMaxHeapSize() {
4898
// Topology will not be able to be successfully scheduled: Config TOPOLOGY_WORKER_MAX_HEAP_SIZE_MB=128.0 < 129.0,
@@ -112,4 +162,41 @@ public void validateNoTopoConfOverrides() {
112162
Map<String, Object> normalized = Nimbus.normalizeConf(conf, topoConf, topology);
113163
assertNull(normalized.get(Config.STORM_WORKERS_ARTIFACTS_DIR));
114164
}
165+
166+
@Test
167+
void testCreateStateInZookeeper() {
168+
nimbus.createStateInZookeeper(BLOB_FILE_KEY);
169+
170+
verify(stormClusterState).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
171+
}
172+
173+
@Test
174+
void testCreateStateInZookeeperWithoutLocalFsBlobStoreInstanceShouldNotCreate() throws Exception {
175+
BlobStore blobStore = mock(BlobStore.class);
176+
Map<String, Object> conf = Map.of(DaemonConfig.NIMBUS_MONITOR_FREQ_SECS, 10);
177+
nimbus = new Nimbus(conf, iNimbus, stormClusterState, nimbusInfo, blobStore, leaderElector, groupMapper, metricRegistry);
178+
179+
nimbus.createStateInZookeeper(BLOB_FILE_KEY);
180+
181+
verify(stormClusterState, never()).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
182+
}
183+
184+
@Test
185+
void testCreateStateInZookeeperWhenFailToSetupBlobWithRuntimeExceptionThrowsRuntimeException() {
186+
doThrow(new RuntimeException("Failed to setup blob")).when(stormClusterState).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
187+
188+
assertThrows(RuntimeException.class, () -> nimbus.createStateInZookeeper(BLOB_FILE_KEY));
189+
verify(stormClusterState).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
190+
}
191+
192+
@Test
193+
void testCreateStateInZookeeperWhenKeyNotFoundHandlesException() throws Exception {
194+
try (MockedConstruction<KeySequenceNumber> keySequenceNumber = mockConstruction(KeySequenceNumber.class, (mock, context) ->
195+
when(mock.getKeySequenceNumber(any())).thenThrow(new KeyNotFoundException("Failed to setup blob")))) {
196+
nimbus.createStateInZookeeper(BLOB_FILE_KEY);
197+
198+
verify(keySequenceNumber.constructed().get(0)).getKeySequenceNumber(any());
199+
verify(stormClusterState, never()).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
200+
}
201+
}
115202
}

0 commit comments

Comments
 (0)