Search in sources :

Example 1 with ConnectorsManager

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);
        }
    }
}
Also used : Connector(org.apache.pulsar.functions.utils.io.Connector) SinkConfig(org.apache.pulsar.common.io.SinkConfig) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Mockito.anyString(org.mockito.Mockito.anyString) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) FileInputStream(java.io.FileInputStream) ConnectorsManager(org.apache.pulsar.functions.worker.ConnectorsManager) Test(org.testng.annotations.Test)

Example 2 with ConnectorsManager

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);
    }
}
Also used : Connector(org.apache.pulsar.functions.utils.io.Connector) SinkConfig(org.apache.pulsar.common.io.SinkConfig) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) FileInputStream(java.io.FileInputStream) ConnectorsManager(org.apache.pulsar.functions.worker.ConnectorsManager) Test(org.testng.annotations.Test)

Example 3 with ConnectorsManager

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);
    }
}
Also used : Connector(org.apache.pulsar.functions.utils.io.Connector) SinkConfig(org.apache.pulsar.common.io.SinkConfig) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) FileInputStream(java.io.FileInputStream) ConnectorsManager(org.apache.pulsar.functions.worker.ConnectorsManager) Test(org.testng.annotations.Test)

Example 4 with ConnectorsManager

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);
        }
    }
}
Also used : Connector(org.apache.pulsar.functions.utils.io.Connector) SinkConfig(org.apache.pulsar.common.io.SinkConfig) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Mockito.anyString(org.mockito.Mockito.anyString) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) FileInputStream(java.io.FileInputStream) ConnectorsManager(org.apache.pulsar.functions.worker.ConnectorsManager) Test(org.testng.annotations.Test)

Example 5 with ConnectorsManager

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);
        }
    }
}
Also used : Connector(org.apache.pulsar.functions.utils.io.Connector) SinkConfig(org.apache.pulsar.common.io.SinkConfig) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Mockito.anyString(org.mockito.Mockito.anyString) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) FileInputStream(java.io.FileInputStream) ConnectorsManager(org.apache.pulsar.functions.worker.ConnectorsManager) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)6 FileInputStream (java.io.FileInputStream)6 Namespace (org.apache.distributedlog.api.namespace.Namespace)6 SinkConfig (org.apache.pulsar.common.io.SinkConfig)6 NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)6 Connector (org.apache.pulsar.functions.utils.io.Connector)6 ConnectorsManager (org.apache.pulsar.functions.worker.ConnectorsManager)6 Test (org.testng.annotations.Test)6 Mockito.anyString (org.mockito.Mockito.anyString)3