Skip to content

Commit

Permalink
Make unit tests to only use dynamically allocated ports (apache#5486)
Browse files Browse the repository at this point in the history
* Make unit tests to only use dynamically allocated ports

* Fixed proxy tests

* Fixed zk test utils after merge

* Fixed multi host client test

* Fix for testConcurrentConsumerReceiveWhileReconnect

* Increased timeout on testPulsarSourceLocalRunWithFile

* Fixed newer test to also avoid port manager

* Fixed test race condition with thread starting in PulsarFunctionLocalRunTest

* Fixed ProxyWithAuthorizationNegTest

* Fixed ProxySaslAuthenticationTest
  • Loading branch information
merlimat authored Jan 5, 2020
1 parent 23ae5ec commit f141efa
Show file tree
Hide file tree
Showing 118 changed files with 1,347 additions and 1,748 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ protected ServerConfiguration newServerConfiguration(String ledgerRootPath) thro
tmpDirs.add(f);
f.delete();
f.mkdir();

int port = PortManager.nextFreePort();
int port = 0;
return newServerConfiguration(port, zkUtil.getZooKeeperConnectString(), f, new File[] { f }, ledgerRootPath);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,16 @@ public class ZooKeeperUtil {
static final Logger LOG = LoggerFactory.getLogger(ZooKeeperUtil.class);

// ZooKeeper related variables
protected final static Integer zooKeeperPort = PortManager.nextFreePort();
private final InetSocketAddress zkaddr;
protected int zooKeeperPort;
private InetSocketAddress zkaddr;

protected ZooKeeperServer zks;
protected ZooKeeper zkc; // zookeeper client
protected NIOServerCnxnFactory serverFactory;
protected File ZkTmpDir;
private final String connectString;
private String connectString;

public ZooKeeperUtil() {
zkaddr = new InetSocketAddress(zooKeeperPort);
connectString = "localhost:" + zooKeeperPort;
}

public ZooKeeper getZooKeeperClient() {
Expand All @@ -81,6 +79,10 @@ public void startServer() throws Exception {
serverFactory.configure(zkaddr, 100);
serverFactory.startup(zks);

zooKeeperPort = serverFactory.getLocalPort();
zkaddr = new InetSocketAddress(zooKeeperPort);
connectString = "localhost:" + zooKeeperPort;

boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
LOG.debug("Server up: " + b);

Expand All @@ -106,6 +108,8 @@ public void startServer(String path) throws Exception {
serverFactory.configure(zkaddr, 100);
serverFactory.startup(zks);

zooKeeperPort = serverFactory.getLocalPort();
connectString = "localhost:" + zooKeeperPort;
boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
LOG.debug("Server up: " + b);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,22 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

import org.apache.bookkeeper.test.PortManager;
import org.apache.pulsar.broker.authentication.AuthenticationDataCommand;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.broker.authentication.AuthenticationProviderAthenz;
import com.yahoo.athenz.auth.token.RoleToken;
import com.yahoo.athenz.zpe.ZpeConsts;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.AuthenticationException;

import com.yahoo.athenz.zpe.ZpeConsts;
import com.yahoo.athenz.auth.token.RoleToken;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class AuthenticationProviderAthenzTest {

Expand Down Expand Up @@ -117,7 +113,7 @@ public void testAuthenticateSignedToken() throws Exception {
String privateKey = new String(Files.readAllBytes(Paths.get("./src/test/resources/zts_private.pem")));
token.sign(privateKey);
AuthenticationDataSource authData = new AuthenticationDataCommand(token.getSignedToken(),
new InetSocketAddress("localhost", PortManager.nextFreePort()), null);
new InetSocketAddress("localhost", 0), null);
assertEquals(provider.authenticate(authData), "test_app");
}

Expand All @@ -131,7 +127,7 @@ public void testAuthenticateUnsignedToken() throws Exception {
};
RoleToken token = new RoleToken.Builder("Z1", "test_provider", roles).principal("test_app").build();
AuthenticationDataSource authData = new AuthenticationDataCommand(token.getUnsignedToken(),
new InetSocketAddress("localhost", PortManager.nextFreePort()), null);
new InetSocketAddress("localhost", 0), null);
try {
provider.authenticate(authData);
fail("Unsigned token should not be authenticated");
Expand All @@ -152,7 +148,7 @@ public void testAuthenticateSignedTokenWithDifferentDomain() throws Exception {
String privateKey = new String(Files.readAllBytes(Paths.get("./src/test/resources/zts_private.pem")));
token.sign(privateKey);
AuthenticationDataSource authData = new AuthenticationDataCommand(token.getSignedToken(),
new InetSocketAddress("localhost", PortManager.nextFreePort()), null);
new InetSocketAddress("localhost", 0), null);
try {
provider.authenticate(authData);
fail("Token which has different domain should not be authenticated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.pulsar.broker.authentication;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import java.io.File;
import java.io.FileWriter;
import java.net.URI;
Expand All @@ -31,9 +34,6 @@

import javax.security.auth.login.Configuration;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.apache.bookkeeper.test.PortManager;
import org.apache.commons.io.FileUtils;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.apache.pulsar.client.admin.PulsarAdmin;
Expand Down Expand Up @@ -61,8 +61,6 @@

public class ProxySaslAuthenticationTest extends ProducerConsumerBase {
private static final Logger log = LoggerFactory.getLogger(ProxySaslAuthenticationTest.class);
private int webServicePort;
private int servicePort;

public static File kdcDir;
public static File kerberosWorkDir;
Expand Down Expand Up @@ -179,8 +177,6 @@ public static void stopMiniKdc() {
@Override
protected void setup() throws Exception {
log.info("-- {} --, start at host: {}", methodName, localHostname);
webServicePort = PortManager.nextFreePort();
servicePort = PortManager.nextFreePort();
isTcpLookup = true;
conf.setAdvertisedAddress(localHostname);
conf.setAuthenticationEnabled(true);
Expand All @@ -194,7 +190,7 @@ protected void setup() throws Exception {

super.init();

lookupUrl = new URI("broker://" + "localhost" + ":" + BROKER_PORT);
lookupUrl = new URI(pulsar.getBrokerServiceUrl());

// set admin auth, to verify admin web resources
Map<String, String> clientSaslConfig = Maps.newHashMap();
Expand All @@ -220,15 +216,15 @@ void testAuthentication() throws Exception {
log.info("-- Starting {} test --", methodName);

// Step 1: Create Admin Client
final String proxyServiceUrl = "pulsar://localhost:" + servicePort;

// create a client which connects to proxy and pass authData
String topicName = "persistent://my-property/my-ns/my-topic1";

ProxyConfiguration proxyConfig = new ProxyConfiguration();
proxyConfig.setAuthenticationEnabled(true);
proxyConfig.setServicePort(Optional.of(servicePort));
proxyConfig.setWebServicePort(Optional.of(webServicePort));
proxyConfig.setBrokerServiceURL("pulsar://localhost:" + BROKER_PORT);
proxyConfig.setServicePort(Optional.of(0));
proxyConfig.setWebServicePort(Optional.of(0));
proxyConfig.setBrokerServiceURL(pulsar.getBrokerServiceUrl());
proxyConfig.setSaslJaasClientAllowedIds(".*" + localHostname + ".*");
proxyConfig.setSaslJaasServerSectionName("PulsarProxy");

Expand All @@ -249,6 +245,7 @@ void testAuthentication() throws Exception {
ProxyService proxyService = new ProxyService(proxyConfig, authenticationService);

proxyService.start();
final String proxyServiceUrl = "pulsar://localhost:" + proxyService.getListenPort().get();
log.info("1 proxy service started {}", proxyService);

// Step 3: Pass correct client params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected void setup() throws Exception {

super.init();

lookupUrl = new URI("http://" + "localhost" + ":" + BROKER_WEBSERVICE_PORT);
lookupUrl = new URI(pulsar.getWebServiceAddress());

pulsarClient = PulsarClient.builder()
.serviceUrl(lookupUrl.toString())
Expand Down
Loading

0 comments on commit f141efa

Please sign in to comment.