Search in sources :

Example 6 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.

the class BrokerInterceptorUtilsTest method testLoadBrokerEventListenerWithBlankListerClass.

@Test(expectedExceptions = IOException.class)
public void testLoadBrokerEventListenerWithBlankListerClass() throws Exception {
    BrokerInterceptorDefinition def = new BrokerInterceptorDefinition();
    def.setDescription("test-broker-listener");
    String archivePath = "/path/to/broker/listener/nar";
    BrokerInterceptorMetadata metadata = new BrokerInterceptorMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(BrokerInterceptorUtils.BROKER_INTERCEPTOR_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = MockBrokerInterceptor.class;
    when(mockLoader.loadClass(eq(MockBrokerInterceptor.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        BrokerInterceptorUtils.load(metadata, "");
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 7 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.

the class ProtocolHandlerUtilsTest method testLoadProtocolHandler.

@Test
public void testLoadProtocolHandler() throws Exception {
    ProtocolHandlerDefinition def = new ProtocolHandlerDefinition();
    def.setHandlerClass(MockProtocolHandler.class.getName());
    def.setDescription("test-protocol-handler");
    String archivePath = "/path/to/protocol/handler/nar";
    ProtocolHandlerMetadata metadata = new ProtocolHandlerMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(PULSAR_PROTOCOL_HANDLER_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class handlerClass = MockProtocolHandler.class;
    when(mockLoader.loadClass(eq(MockProtocolHandler.class.getName()))).thenReturn(handlerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        ProtocolHandlerWithClassLoader returnedPhWithCL = ProtocolHandlerUtils.load(metadata, "");
        ProtocolHandler returnedPh = returnedPhWithCL.getHandler();
        assertSame(mockLoader, returnedPhWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockProtocolHandler);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 8 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.

the class ProtocolHandlerWithClassLoaderTest method testWrapper.

@Test
public void testWrapper() throws Exception {
    ProtocolHandler h = mock(ProtocolHandler.class);
    NarClassLoader loader = mock(NarClassLoader.class);
    ProtocolHandlerWithClassLoader wrapper = new ProtocolHandlerWithClassLoader(h, loader);
    String protocol = "kafka";
    when(h.protocolName()).thenReturn(protocol);
    assertEquals(protocol, wrapper.protocolName());
    verify(h, times(1)).protocolName();
    when(h.accept(eq(protocol))).thenReturn(true);
    assertTrue(wrapper.accept(protocol));
    verify(h, times(1)).accept(same(protocol));
    ServiceConfiguration conf = new ServiceConfiguration();
    wrapper.initialize(conf);
    verify(h, times(1)).initialize(same(conf));
    BrokerService service = mock(BrokerService.class);
    wrapper.start(service);
    verify(h, times(1)).start(service);
    String protocolData = "test-protocol-data";
    when(h.getProtocolDataToAdvertise()).thenReturn(protocolData);
    assertEquals(protocolData, wrapper.getProtocolDataToAdvertise());
    verify(h, times(1)).getProtocolDataToAdvertise();
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) BrokerService(org.apache.pulsar.broker.service.BrokerService) Test(org.testng.annotations.Test)

Example 9 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.

the class ConnectorUtils method searchForConnectors.

public static TreeMap<String, Connector> searchForConnectors(String connectorsDirectory, String narExtractionDirectory) throws IOException {
    Path path = Paths.get(connectorsDirectory).toAbsolutePath();
    log.info("Searching for connectors in {}", path);
    TreeMap<String, Connector> connectors = new TreeMap<>();
    if (!path.toFile().exists()) {
        log.warn("Connectors archive directory not found");
        return connectors;
    }
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.nar")) {
        for (Path archive : stream) {
            try {
                NarClassLoader ncl = NarClassLoaderBuilder.builder().narFile(new File(archive.toString())).extractionDirectory(narExtractionDirectory).build();
                Connector.ConnectorBuilder connectorBuilder = Connector.builder();
                ConnectorDefinition cntDef = ConnectorUtils.getConnectorDefinition(ncl);
                log.info("Found connector {} from {}", cntDef, archive);
                connectorBuilder.archivePath(archive);
                if (!StringUtils.isEmpty(cntDef.getSourceClass())) {
                    if (!StringUtils.isEmpty(cntDef.getSourceConfigClass())) {
                        connectorBuilder.sourceConfigFieldDefinitions(ConnectorUtils.getConnectorConfigDefinition(ncl, cntDef.getSourceConfigClass()));
                    }
                }
                if (!StringUtils.isEmpty(cntDef.getSinkClass())) {
                    if (!StringUtils.isEmpty(cntDef.getSinkConfigClass())) {
                        connectorBuilder.sinkConfigFieldDefinitions(ConnectorUtils.getConnectorConfigDefinition(ncl, cntDef.getSinkConfigClass()));
                    }
                }
                connectorBuilder.classLoader(ncl);
                connectorBuilder.connectorDefinition(cntDef);
                connectors.put(cntDef.getName(), connectorBuilder.build());
            } catch (Throwable t) {
                log.warn("Failed to load connector from {}", archive, t);
            }
        }
        return connectors;
    }
}
Also used : Path(java.nio.file.Path) ConnectorDefinition(org.apache.pulsar.common.io.ConnectorDefinition) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) TreeMap(java.util.TreeMap) File(java.io.File)

Example 10 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader 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)

Aggregations

NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)126 Test (org.testng.annotations.Test)84 IOException (java.io.IOException)53 NarClassLoaderBuilder (org.apache.pulsar.common.nar.NarClassLoaderBuilder)48 File (java.io.File)39 FileInputStream (java.io.FileInputStream)15 UtilityClass (lombok.experimental.UtilityClass)15 Namespace (org.apache.distributedlog.api.namespace.Namespace)15 HashMap (java.util.HashMap)14 Map (java.util.Map)12 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)12 Lists (com.google.common.collect.Lists)9 InputStream (java.io.InputStream)9 Files (java.nio.file.Files)9 Paths (java.nio.file.Paths)9 StandardCopyOption (java.nio.file.StandardCopyOption)9 LinkedList (java.util.LinkedList)9 List (java.util.List)9 Consumer (java.util.function.Consumer)9 Response (javax.ws.rs.core.Response)9