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, "");
}
}
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);
}
}
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();
}
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;
}
}
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);
}
}
}
Aggregations