use of org.apache.pulsar.functions.worker.ConnectorsManager in project pulsar by apache.
the class SinkApiV3ResourceTest method testRegisterSinkSuccessK8sWithUpload.
/*
Externally managed runtime,
uploadBuiltinSinksSources == true
Make sure uploadFileToBookkeeper is called
*/
@Test
public void testRegisterSinkSuccessK8sWithUpload() throws Exception {
final String injectedErrMsg = "uploadFileToBookkeeper triggered";
mockedWorkerService.getWorkerConfig().setUploadBuiltinSinksSources(true);
mockStatic(WorkerUtils.class, ctx -> {
ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new RuntimeException(injectedErrMsg));
});
NarClassLoader mockedClassLoader = mock(NarClassLoader.class);
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.getSinkType(any())).thenReturn(String.class);
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(mockedClassLoader);
});
ConnectorsManager mockedConnManager = mock(ConnectorsManager.class);
Connector connector = Connector.builder().classLoader(mockedClassLoader).build();
when(mockedConnManager.getConnector("cassandra")).thenReturn(connector);
when(mockedConnManager.getSinkArchive(any())).thenReturn(getPulsarIOCassandraNar().toPath());
when(mockedWorkerService.getConnectorsManager()).thenReturn(mockedConnManager);
when(mockedRuntimeFactory.externallyManaged()).thenReturn(true);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(false);
SinkConfig sinkConfig = createDefaultSinkConfig();
sinkConfig.setArchive("builtin://cassandra");
try (FileInputStream inputStream = new FileInputStream(getPulsarIOCassandraNar())) {
try {
resource.registerSink(tenant, namespace, sink, inputStream, mockedFormData, null, sinkConfig, null, null);
Assert.fail();
} catch (RuntimeException e) {
Assert.assertEquals(e.getMessage(), injectedErrMsg);
}
}
}
use of org.apache.pulsar.functions.worker.ConnectorsManager in project pulsar by apache.
the class SinkApiV3ResourceTest method testRegisterSinkSuccessK8sNoUpload.
/*
Externally managed runtime,
uploadBuiltinSinksSources == false
Make sure uploadFileToBookkeeper is not called
*/
@Test
public void testRegisterSinkSuccessK8sNoUpload() throws Exception {
mockedWorkerService.getWorkerConfig().setUploadBuiltinSinksSources(false);
mockStatic(WorkerUtils.class, ctx -> {
ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new RuntimeException("uploadFileToBookkeeper triggered"));
});
NarClassLoader mockedClassLoader = mock(NarClassLoader.class);
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.getSinkType(any())).thenReturn(String.class);
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(mockedClassLoader);
});
ConnectorsManager mockedConnManager = mock(ConnectorsManager.class);
Connector connector = Connector.builder().classLoader(mockedClassLoader).build();
when(mockedConnManager.getConnector("cassandra")).thenReturn(connector);
when(mockedWorkerService.getConnectorsManager()).thenReturn(mockedConnManager);
when(mockedRuntimeFactory.externallyManaged()).thenReturn(true);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(false);
SinkConfig sinkConfig = createDefaultSinkConfig();
sinkConfig.setArchive("builtin://cassandra");
try (FileInputStream inputStream = new FileInputStream(getPulsarIOCassandraNar())) {
resource.registerSink(tenant, namespace, sink, inputStream, mockedFormData, null, sinkConfig, null, null);
}
}
use of org.apache.pulsar.functions.worker.ConnectorsManager in project pulsar by yahoo.
the class SinkApiV3ResourceTest method testRegisterSinkSuccessK8sNoUpload.
/*
Externally managed runtime,
uploadBuiltinSinksSources == false
Make sure uploadFileToBookkeeper is not called
*/
@Test
public void testRegisterSinkSuccessK8sNoUpload() throws Exception {
mockedWorkerService.getWorkerConfig().setUploadBuiltinSinksSources(false);
mockStatic(WorkerUtils.class, ctx -> {
ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new RuntimeException("uploadFileToBookkeeper triggered"));
});
NarClassLoader mockedClassLoader = mock(NarClassLoader.class);
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.getSinkType(any())).thenReturn(String.class);
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(mockedClassLoader);
});
ConnectorsManager mockedConnManager = mock(ConnectorsManager.class);
Connector connector = Connector.builder().classLoader(mockedClassLoader).build();
when(mockedConnManager.getConnector("cassandra")).thenReturn(connector);
when(mockedWorkerService.getConnectorsManager()).thenReturn(mockedConnManager);
when(mockedRuntimeFactory.externallyManaged()).thenReturn(true);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(false);
SinkConfig sinkConfig = createDefaultSinkConfig();
sinkConfig.setArchive("builtin://cassandra");
try (FileInputStream inputStream = new FileInputStream(getPulsarIOCassandraNar())) {
resource.registerSink(tenant, namespace, sink, inputStream, mockedFormData, null, sinkConfig, null, null);
}
}
use of org.apache.pulsar.functions.worker.ConnectorsManager in project pulsar by yahoo.
the class SinkApiV3ResourceTest method testRegisterSinkSuccessK8sWithUpload.
/*
Externally managed runtime,
uploadBuiltinSinksSources == true
Make sure uploadFileToBookkeeper is called
*/
@Test
public void testRegisterSinkSuccessK8sWithUpload() throws Exception {
final String injectedErrMsg = "uploadFileToBookkeeper triggered";
mockedWorkerService.getWorkerConfig().setUploadBuiltinSinksSources(true);
mockStatic(WorkerUtils.class, ctx -> {
ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new RuntimeException(injectedErrMsg));
});
NarClassLoader mockedClassLoader = mock(NarClassLoader.class);
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.getSinkType(any())).thenReturn(String.class);
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(mockedClassLoader);
});
ConnectorsManager mockedConnManager = mock(ConnectorsManager.class);
Connector connector = Connector.builder().classLoader(mockedClassLoader).build();
when(mockedConnManager.getConnector("cassandra")).thenReturn(connector);
when(mockedConnManager.getSinkArchive(any())).thenReturn(getPulsarIOCassandraNar().toPath());
when(mockedWorkerService.getConnectorsManager()).thenReturn(mockedConnManager);
when(mockedRuntimeFactory.externallyManaged()).thenReturn(true);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(false);
SinkConfig sinkConfig = createDefaultSinkConfig();
sinkConfig.setArchive("builtin://cassandra");
try (FileInputStream inputStream = new FileInputStream(getPulsarIOCassandraNar())) {
try {
resource.registerSink(tenant, namespace, sink, inputStream, mockedFormData, null, sinkConfig, null, null);
Assert.fail();
} catch (RuntimeException e) {
Assert.assertEquals(e.getMessage(), injectedErrMsg);
}
}
}
use of org.apache.pulsar.functions.worker.ConnectorsManager in project incubator-pulsar by apache.
the class SinkApiV3ResourceTest method testRegisterSinkSuccessK8sWithUpload.
/*
Externally managed runtime,
uploadBuiltinSinksSources == true
Make sure uploadFileToBookkeeper is called
*/
@Test
public void testRegisterSinkSuccessK8sWithUpload() throws Exception {
final String injectedErrMsg = "uploadFileToBookkeeper triggered";
mockedWorkerService.getWorkerConfig().setUploadBuiltinSinksSources(true);
mockStatic(WorkerUtils.class, ctx -> {
ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new RuntimeException(injectedErrMsg));
});
NarClassLoader mockedClassLoader = mock(NarClassLoader.class);
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.getSinkType(any())).thenReturn(String.class);
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(mockedClassLoader);
});
ConnectorsManager mockedConnManager = mock(ConnectorsManager.class);
Connector connector = Connector.builder().classLoader(mockedClassLoader).build();
when(mockedConnManager.getConnector("cassandra")).thenReturn(connector);
when(mockedConnManager.getSinkArchive(any())).thenReturn(getPulsarIOCassandraNar().toPath());
when(mockedWorkerService.getConnectorsManager()).thenReturn(mockedConnManager);
when(mockedRuntimeFactory.externallyManaged()).thenReturn(true);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(false);
SinkConfig sinkConfig = createDefaultSinkConfig();
sinkConfig.setArchive("builtin://cassandra");
try (FileInputStream inputStream = new FileInputStream(getPulsarIOCassandraNar())) {
try {
resource.registerSink(tenant, namespace, sink, inputStream, mockedFormData, null, sinkConfig, null, null);
Assert.fail();
} catch (RuntimeException e) {
Assert.assertEquals(e.getMessage(), injectedErrMsg);
}
}
}
Aggregations