com.spotify.helios.ZooKeeperTestingClusterManager Maven / Gradle / Ivy
/*-
* -\-\-
* Helios Testing Common Library
* --
* Copyright (C) 2016 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.helios;
import static com.google.common.collect.Iterables.transform;
import static java.util.Arrays.asList;
import static org.apache.commons.io.FileUtils.deleteDirectory;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.InstanceSpec;
import org.apache.curator.test.TestingCluster;
import org.apache.curator.test.TestingZooKeeperServer;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
import org.junit.Rule;
/**
* A ZooKeeperTestManager that uses the {@link org.apache.curator.test.TestingServer}
* to run an in-process ZooKeeper cluster.
*/
public class ZooKeeperTestingClusterManager implements ZooKeeperTestManager {
@Rule public final TemporaryPorts temporaryPorts = TemporaryPorts.create();
private static final String SUPER_USER = "super";
private static final String SUPER_PASSWORD = "hunter2****";
private final Path tempDir;
private List zkPeers;
private List zkAddresses;
private List zkServers;
private List peerCurators;
private CuratorFramework curator;
private TestingCluster cluster;
static {
try {
final String superDigest = DigestAuthenticationProvider.generateDigest(
SUPER_USER + ":" + SUPER_PASSWORD);
final Field digestField = DigestAuthenticationProvider.class.getDeclaredField("superDigest");
digestField.setAccessible(true);
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(digestField, digestField.getModifiers() & ~Modifier.FINAL);
digestField.set(null, superDigest);
} catch (NoSuchAlgorithmException | IllegalAccessException | NoSuchFieldException e) {
// i mean, for real?
throw new RuntimeException(e);
}
}
public ZooKeeperTestingClusterManager() {
try {
tempDir = Files.createTempDirectory("helios-zk");
} catch (IOException e) {
throw new RuntimeException(e);
}
start();
}
@Override
public void ensure(String path) throws Exception {
curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient());
}
@Override
public void close() {
try {
for (final CuratorFramework curator : peerCurators) {
curator.close();
}
curator.close();
stop();
deleteDirectory(tempDir.toFile());
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
@Override
public String connectString() {
return connectString(zkAddresses);
}
private String connectString(final InetSocketAddress... addresses) {
return connectString(asList(addresses));
}
private String connectString(final Iterable addresses) {
return Joiner.on(',').join(endpoints(addresses));
}
@Override
public CuratorFramework curatorWithSuperAuth() {
return curator;
}
@Override
public void awaitUp(long timeout, TimeUnit timeunit) throws TimeoutException {
Polling.awaitUnchecked(timeout, timeunit, new Callable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy