use of org.apache.curator.test.TestingServer in project testcases by coheigea.
the class KafkaTest method setup.
@org.junit.BeforeClass
public static void setup() throws Exception {
zkServer = new TestingServer();
// Get a random port
ServerSocket serverSocket = new ServerSocket(0);
port = serverSocket.getLocalPort();
serverSocket.close();
Properties props = new Properties();
props.put("broker.id", 1);
props.put("host.name", "localhost");
props.put("port", port);
props.put("log.dir", "/tmp/kafka");
props.put("zookeeper.connect", zkServer.getConnectString());
props.put("replica.socket.timeout.ms", "1500");
props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
KafkaConfig config = new KafkaConfig(props);
kafkaServer = new KafkaServerStartable(config);
kafkaServer.startup();
}
use of org.apache.curator.test.TestingServer in project testcases by coheigea.
the class KafkaAuthorizerTest method setup.
@org.junit.BeforeClass
public static void setup() throws Exception {
zkServer = new TestingServer();
// Get a random port
ServerSocket serverSocket = new ServerSocket(0);
port = serverSocket.getLocalPort();
serverSocket.close();
final Properties props = new Properties();
props.put("broker.id", 1);
props.put("host.name", "localhost");
props.put("port", port);
props.put("log.dir", "/tmp/kafka");
props.put("zookeeper.connect", zkServer.getConnectString());
props.put("replica.socket.timeout.ms", "1500");
props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
// Enable SSL
props.put("listeners", "SSL://localhost:" + port);
props.put("ssl.keystore.location", KafkaAuthorizerTest.class.getResource("/brokerstore.jks").getPath());
props.put("ssl.keystore.password", "bspass");
props.put("ssl.key.password", "bkpass");
props.put("ssl.truststore.location", KafkaAuthorizerTest.class.getResource("/truststore.jks").getPath());
props.put("ssl.truststore.password", "security");
props.put("security.inter.broker.protocol", "SSL");
props.put("ssl.client.auth", "required");
// Plug in custom authorizer
props.put("authorizer.class.name", "org.apache.coheigea.bigdata.kafka.CustomAuthorizer");
KafkaConfig config = new KafkaConfig(props);
kafkaServer = new KafkaServerStartable(config);
kafkaServer.startup();
// Create some topics
ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
AdminUtils.createTopic(zkUtils, "dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}
use of org.apache.curator.test.TestingServer in project testcases by coheigea.
the class KafkaRangerGSSAuthorizerTest method setup.
@org.junit.BeforeClass
public static void setup() throws Exception {
String basedir = System.getProperty("basedir");
if (basedir == null) {
basedir = new File(".").getCanonicalPath();
}
configureKerby(basedir);
// JAAS Config file - We need to point to the correct keytab files
Path path = FileSystems.getDefault().getPath(basedir, "/src/test/resources/kafka_kerberos.jaas");
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
content = content.replaceAll("<basedir>", basedir);
Path path2 = FileSystems.getDefault().getPath(basedir, "/target/test-classes/kafka_kerberos.jaas");
Files.write(path2, content.getBytes(StandardCharsets.UTF_8));
System.setProperty("java.security.auth.login.config", path2.toString());
// Set up Zookeeper to require SASL
Map<String, Object> zookeeperProperties = new HashMap<>();
zookeeperProperties.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
zookeeperProperties.put("requireClientAuthScheme", "sasl");
zookeeperProperties.put("jaasLoginRenew", "3600000");
InstanceSpec instanceSpec = new InstanceSpec(null, -1, -1, -1, true, 1, -1, -1, zookeeperProperties);
zkServer = new TestingServer(instanceSpec, true);
// Get a random port
ServerSocket serverSocket = new ServerSocket(0);
port = serverSocket.getLocalPort();
serverSocket.close();
final Properties props = new Properties();
props.put("broker.id", 1);
props.put("host.name", "localhost");
props.put("port", port);
props.put("log.dir", "/tmp/kafka");
props.put("zookeeper.connect", zkServer.getConnectString());
props.put("replica.socket.timeout.ms", "1500");
props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
// Enable SASL_PLAINTEXT
props.put("listeners", "SASL_PLAINTEXT://localhost:" + port);
props.put("security.inter.broker.protocol", "SASL_PLAINTEXT");
props.put("sasl.enabled.mechanisms", "GSSAPI");
props.put("sasl.mechanism.inter.broker.protocol", "GSSAPI");
props.put("sasl.kerberos.service.name", "kafka");
// Plug in Apache Ranger authorizer
props.put("authorizer.class.name", "org.apache.ranger.authorization.kafka.authorizer.RangerKafkaAuthorizer");
// Create users for testing
UserGroupInformation.createUserForTesting("client@kafka.apache.org", new String[] { "public" });
UserGroupInformation.createUserForTesting("kafka/localhost@kafka.apache.org", new String[] { "IT" });
KafkaConfig config = new KafkaConfig(props);
kafkaServer = new KafkaServerStartable(config);
kafkaServer.startup();
// Create some topics
ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
AdminUtils.createTopic(zkUtils, "dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
AdminUtils.createTopic(zkUtils, "messages", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}
use of org.apache.curator.test.TestingServer in project drill by apache.
the class TestZkRegistry method testZKRegistry.
@Test
public void testZKRegistry() throws Exception {
TestingServer server = new TestingServer();
server.start();
String connStr = server.getConnectString();
CuratorFramework probeZk = connectToZk(connStr);
addDrillbit(probeZk, FRED_HOST);
ZKClusterCoordinatorDriver driver = new ZKClusterCoordinatorDriver().setConnect(connStr, ZK_ROOT, CLUSTER_ID).setPorts(TEST_USER_PORT, TEST_CONTROL_PORT, TEST_DATA_PORT).build();
ZKRegistry registry = new ZKRegistry(driver);
TestRegistryHandler handler = new TestRegistryHandler();
registry.start(handler);
// We started with one "stray" drillbit that will be reported as unmanaged.
assertEquals(FRED_HOST, handler.reserved);
List<String> unmanaged = registry.listUnmanagedDrillits();
assertEquals(1, unmanaged.size());
String fredsKey = makeKey(FRED_HOST);
assertEquals(fredsKey, unmanaged.get(0));
Map<String, DrillbitTracker> trackers = registry.getRegistryForTesting();
assertEquals(1, trackers.size());
assertTrue(trackers.containsKey(fredsKey));
DrillbitTracker fredsTracker = trackers.get(fredsKey);
assertEquals(fredsKey, fredsTracker.key);
assertEquals(DrillbitTracker.State.UNMANAGED, fredsTracker.state);
assertNull(fredsTracker.task);
assertEquals(fredsKey, ZKClusterCoordinatorDriver.asString(fredsTracker.endpoint));
// The handler should have been told about the initial stray.
assertEquals(FRED_HOST, handler.reserved);
// Pretend to start a new Drillbit.
Task wilmasTask = new TestTask(WILMA_HOST);
EventContext context = new EventContext(wilmasTask);
// Registry ignores the created event.
registry.stateChange(Event.CREATED, context);
assertEquals(1, registry.getRegistryForTesting().size());
// But, does care about the allocated event.
registry.stateChange(Event.ALLOCATED, context);
assertEquals(2, registry.getRegistryForTesting().size());
String wilmasKey = makeKey(WILMA_HOST);
DrillbitTracker wilmasTracker = registry.getRegistryForTesting().get(wilmasKey);
assertNotNull(wilmasTracker);
assertEquals(wilmasTask, wilmasTracker.task);
assertNull(wilmasTracker.endpoint);
assertEquals(wilmasKey, wilmasTracker.key);
assertEquals(DrillbitTracker.State.NEW, wilmasTracker.state);
handler.clear();
// Time goes on. The Drillbit starts and registers itself.
addDrillbit(probeZk, WILMA_HOST);
Thread.sleep(100);
assertEquals(wilmasTask, handler.start);
assertEquals(DrillbitTracker.State.REGISTERED, wilmasTracker.state);
assertEquals(handler.start, wilmasTask);
// Create another task: Barney
Task barneysTask = new TestTask(BARNEY_HOST);
context = new EventContext(barneysTask);
registry.stateChange(Event.CREATED, context);
// Start Barney, but assume a latency in Yarn, but not ZK.
// We get the ZK registration before the YARN launch confirmation.
handler.clear();
addDrillbit(probeZk, BARNEY_HOST);
Thread.sleep(100);
assertEquals(BARNEY_HOST, handler.reserved);
String barneysKey = makeKey(BARNEY_HOST);
DrillbitTracker barneysTracker = registry.getRegistryForTesting().get(barneysKey);
assertNotNull(barneysTracker);
assertEquals(DrillbitTracker.State.UNMANAGED, barneysTracker.state);
assertNull(barneysTracker.task);
assertEquals(2, registry.listUnmanagedDrillits().size());
handler.clear();
registry.stateChange(Event.ALLOCATED, context);
assertEquals(DrillbitTracker.State.REGISTERED, barneysTracker.state);
assertEquals(handler.start, barneysTask);
assertEquals(barneysTask, barneysTracker.task);
assertEquals(1, registry.listUnmanagedDrillits().size());
// Barney is having problems, it it drops out of ZK.
handler.clear();
removeDrillbit(probeZk, BARNEY_HOST);
Thread.sleep(100);
assertEquals(barneysTask, handler.end);
assertEquals(DrillbitTracker.State.DEREGISTERED, barneysTracker.state);
// Barney comes alive (presumably before the controller gives up and kills
// the Drillbit.)
handler.clear();
addDrillbit(probeZk, BARNEY_HOST);
Thread.sleep(100);
assertEquals(barneysTask, handler.start);
assertEquals(DrillbitTracker.State.REGISTERED, barneysTracker.state);
// Barney is killed by the controller.
// ZK entry drops. Tracker is removed, controller is notified.
handler.clear();
removeDrillbit(probeZk, BARNEY_HOST);
Thread.sleep(100);
assertNotNull(registry.getRegistryForTesting().get(barneysKey));
assertEquals(barneysTask, handler.end);
// The controller tells the registry to stop tracking the Drillbit.
handler.clear();
registry.stateChange(Event.ENDED, context);
assertNull(handler.end);
assertNull(registry.getRegistryForTesting().get(barneysKey));
// The stray drillbit deregisters from ZK. The tracker is removed.
handler.clear();
removeDrillbit(probeZk, FRED_HOST);
Thread.sleep(100);
assertNull(registry.getRegistryForTesting().get(fredsKey));
assertNull(handler.end);
assertEquals(FRED_HOST, handler.released);
// Wilma is killed by the controller.
handler.clear();
removeDrillbit(probeZk, WILMA_HOST);
Thread.sleep(100);
assertEquals(wilmasTask, handler.end);
assertNull(handler.released);
assertEquals(DrillbitTracker.State.DEREGISTERED, wilmasTracker.state);
assertNotNull(registry.getRegistryForTesting().get(wilmasKey));
handler.clear();
context = new EventContext(wilmasTask);
registry.stateChange(Event.ENDED, context);
assertNull(registry.getRegistryForTesting().get(wilmasKey));
assertNull(handler.released);
assertNull(handler.end);
// All drillbits should be gone.
assertTrue(registry.getRegistryForTesting().isEmpty());
probeZk.close();
driver.close();
server.stop();
server.close();
}
use of org.apache.curator.test.TestingServer in project drill by apache.
the class TestZkRegistry method testBasics.
/**
* Basic setup: start a ZK and verify that the initial endpoint list is empty.
* Also validates the basics of the test setup (mock server, etc.)
*
* @throws Exception
*/
@Test
public void testBasics() throws Exception {
try (TestingServer server = new TestingServer()) {
server.start();
String connStr = server.getConnectString();
ZKClusterCoordinatorDriver driver = new ZKClusterCoordinatorDriver().setConnect(connStr, "drill", "drillbits").build();
assertTrue(driver.getInitialEndpoints().isEmpty());
driver.close();
server.stop();
}
}
Aggregations