use of org.apache.storm.utils.ServerUtils in project storm by apache.
the class AsyncLocalizerTest method testRequestDownloadTopologyBlobsLocalMode.
@Test
public void testRequestDownloadTopologyBlobsLocalMode() throws Exception {
// tests download of topology blobs in local mode on a topology without resources folder
ConfigUtils mockedConfigUtils = mock(ConfigUtils.class);
ServerUtils mockedServerUtils = mock(ServerUtils.class);
ConfigUtils previousConfigUtils = ConfigUtils.setInstance(mockedConfigUtils);
ServerUtils previousServerUtils = ServerUtils.setInstance(mockedServerUtils);
AsyncLocalizer victim = null;
try (TmpPath stormLocal = new TmpPath();
TmpPath localizerRoot = new TmpPath()) {
Map<String, Object> conf = new HashMap<>();
conf.put(Config.STORM_LOCAL_DIR, stormLocal.getPath());
conf.put(Config.STORM_CLUSTER_MODE, "local");
StormMetricsRegistry metricsRegistry = new StormMetricsRegistry();
AdvancedFSOps ops = AdvancedFSOps.make(conf);
victim = spy(new AsyncLocalizer(conf, ops, localizerRoot.getPath(), metricsRegistry));
final String topoId = "TOPO-12345";
final String user = "user";
final int port = 8080;
final Path userDir = Paths.get(stormLocal.getPath(), user);
final Path stormRoot = Paths.get(stormLocal.getPath(), topoId);
final String simpleLocalName = "simple.txt";
final String simpleKey = "simple";
Map<String, Map<String, Object>> topoBlobMap = new HashMap<>();
Map<String, Object> simple = new HashMap<>();
simple.put("localname", simpleLocalName);
simple.put("uncompress", false);
topoBlobMap.put(simpleKey, simple);
Map<String, Object> topoConf = new HashMap<>(conf);
topoConf.put(Config.TOPOLOGY_BLOBSTORE_MAP, topoBlobMap);
topoConf.put(Config.TOPOLOGY_NAME, "TOPO");
List<LocalizedResource> localizedList = new ArrayList<>();
LocalizedResource simpleLocal = new LocalizedResource(simpleKey, localizerRoot.getFile().toPath(), false, ops, conf, user, metricsRegistry);
localizedList.add(simpleLocal);
when(mockedConfigUtils.supervisorStormDistRootImpl(conf, topoId)).thenReturn(stormRoot.toString());
when(mockedConfigUtils.readSupervisorStormConfImpl(conf, topoId)).thenReturn(topoConf);
when(mockedConfigUtils.readSupervisorTopologyImpl(conf, topoId, ops)).thenReturn(constructEmptyStormTopology());
doReturn(mockBlobStore).when(victim).getClientBlobStore();
doReturn(userDir.toFile()).when(victim).getLocalUserFileCacheDir(user);
doReturn(localizedList).when(victim).getBlobs(any(List.class), any(), any());
ReadableBlobMeta blobMeta = new ReadableBlobMeta();
blobMeta.set_version(1);
doReturn(blobMeta).when(mockBlobStore).getBlobMeta(any());
when(mockBlobStore.getBlob(any())).thenAnswer(invocation -> new TestInputStreamWithMeta(LOCAL_MODE_JAR_VERSION));
Future<Void> f = victim.requestDownloadTopologyBlobs(constructLocalAssignment(topoId, user), port, null);
f.get(20, TimeUnit.SECONDS);
verify(victim).getLocalUserFileCacheDir(user);
assertTrue(ops.fileExists(userDir));
verify(victim).getBlobs(any(List.class), any(), any());
// make sure resources directory after blob version commit is created.
Path extractionDir = stormRoot.resolve(LocallyCachedTopologyBlob.TopologyBlobType.TOPO_JAR.getExtractionDir());
assertTrue(ops.fileExists(extractionDir));
} finally {
ConfigUtils.setInstance(previousConfigUtils);
ServerUtils.setInstance(previousServerUtils);
if (victim != null) {
victim.close();
}
}
}
use of org.apache.storm.utils.ServerUtils in project storm by apache.
the class AsyncLocalizerTest method testRequestDownloadBaseTopologyBlobs.
@Test
public void testRequestDownloadBaseTopologyBlobs() throws Exception {
ReflectionUtils mockedReflectionUtils = mock(ReflectionUtils.class);
ServerUtils mockedServerUtils = mock(ServerUtils.class);
ReflectionUtils previousReflectionUtils = ReflectionUtils.setInstance(mockedReflectionUtils);
ServerUtils previousServerUtils = ServerUtils.setInstance(mockedServerUtils);
// cannot use automatic resource management here in this try because the AsyncLocalizer depends on a config map,
// which should take the storm local dir, and that storm local dir is declared in the try-with-resources.
AsyncLocalizer victim = null;
try (TmpPath stormRoot = new TmpPath();
TmpPath localizerRoot = new TmpPath()) {
Map<String, Object> conf = new HashMap<>();
conf.put(DaemonConfig.SUPERVISOR_BLOBSTORE, ClientBlobStore.class.getName());
conf.put(Config.STORM_PRINCIPAL_TO_LOCAL_PLUGIN, DefaultPrincipalToLocal.class.getName());
conf.put(Config.STORM_CLUSTER_MODE, "distributed");
conf.put(Config.STORM_LOCAL_DIR, stormRoot.getPath());
AdvancedFSOps ops = AdvancedFSOps.make(conf);
victim = spy(new AsyncLocalizer(conf, ops, localizerRoot.getPath(), new StormMetricsRegistry()));
final String topoId = "TOPO";
final LocalAssignment localAssignment = constructLocalAssignment(topoId, "user");
final int port = 8080;
ClientBlobStore blobStore = mock(ClientBlobStore.class);
when(blobStore.getRemoteBlobstoreUpdateTime()).thenReturn(-1L);
LocallyCachedTopologyBlob jarBlob = mock(LocallyCachedTopologyBlob.class);
doReturn(jarBlob).when(victim).getTopoJar(topoId, localAssignment.get_owner());
when(jarBlob.getLocalVersion()).thenReturn(-1L);
when(jarBlob.getRemoteVersion(any())).thenReturn(100L);
when(jarBlob.fetchUnzipToTemp(any())).thenReturn(100L);
when(jarBlob.isUsed()).thenReturn(true);
LocallyCachedTopologyBlob codeBlob = mock(LocallyCachedTopologyBlob.class);
doReturn(codeBlob).when(victim).getTopoCode(topoId, localAssignment.get_owner());
when(codeBlob.getLocalVersion()).thenReturn(-1L);
when(codeBlob.getRemoteVersion(any())).thenReturn(200L);
when(codeBlob.fetchUnzipToTemp(any())).thenReturn(200L);
when(codeBlob.isUsed()).thenReturn(true);
LocallyCachedTopologyBlob confBlob = mock(LocallyCachedTopologyBlob.class);
doReturn(confBlob).when(victim).getTopoConf(topoId, localAssignment.get_owner());
when(confBlob.getLocalVersion()).thenReturn(-1L);
when(confBlob.getRemoteVersion(any())).thenReturn(300L);
when(confBlob.fetchUnzipToTemp(any())).thenReturn(300L);
when(confBlob.isUsed()).thenReturn(true);
when(mockedReflectionUtils.newInstanceImpl(ClientBlobStore.class)).thenReturn(blobStore);
PortAndAssignment pna = new PortAndAssignmentImpl(port, localAssignment);
Future<Void> f = victim.requestDownloadBaseTopologyBlobs(pna, null);
f.get(20, TimeUnit.SECONDS);
verify(jarBlob).update(eq(blobStore), eq(-1L));
verify(codeBlob).update(eq(blobStore), eq(-1L));
verify(confBlob).update(eq(blobStore), eq(-1L));
} finally {
ReflectionUtils.setInstance(previousReflectionUtils);
ServerUtils.setInstance(previousServerUtils);
if (victim != null) {
victim.close();
}
}
}
Aggregations