Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-4780: Delegate temp directory creation to Junit in tests. #2100

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,12 @@
import java.util.Properties;
import org.apache.zookeeper.common.ZKConfig;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.io.TempDir;

public class ZKClientConfigTest {

private static final File testData = new File(System.getProperty("test.data.dir", "src/test/resources/data"));

@BeforeAll
public static void init() {
if (!testData.exists()) {
testData.mkdirs();
}
}

@Test
@Timeout(value = 10)
public void testDefaultConfiguration() {
Expand Down Expand Up @@ -109,9 +100,8 @@ public void testSystemPropertyValue() {

@Test
@Timeout(value = 10)
public void testReadConfigurationFile() throws IOException, ConfigException {
File file = File.createTempFile("clientConfig", ".conf", testData);
file.deleteOnExit();
public void testReadConfigurationFile(@TempDir File testDataDir) throws IOException, ConfigException {
File file = File.createTempFile("clientConfig", ".conf", testDataDir);
Properties clientConfProp = new Properties();
clientConfProp.setProperty(ENABLE_CLIENT_SASL_KEY, "true");
clientConfProp.setProperty(ZK_SASL_CLIENT_USERNAME, "ZK");
Expand All @@ -132,10 +122,6 @@ public void testReadConfigurationFile() throws IOException, ConfigException {
assertEquals(conf.getProperty(LOGIN_CONTEXT_NAME_KEY), "MyClient");
assertEquals(conf.getProperty(ZOOKEEPER_SERVER_REALM), "HADOOP.COM");
assertEquals(conf.getProperty("dummyProperty"), "dummyValue");

// try to delete it now as we have done with the created file, why to
// wait for deleteOnExit() deletion
file.delete();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
package org.apache.zookeeper.common;

import java.io.File;
import java.io.IOException;
import java.security.Security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.test.ClientBase;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.provider.Arguments;

/**
Expand Down Expand Up @@ -70,6 +68,7 @@ public static Stream<Arguments> data() {
* caching makes all test cases after the first one for a given parameter combination complete almost instantly.
*/
protected static Map<Integer, X509TestContext> cachedTestContexts;
@TempDir
protected static File tempDir;

protected X509TestContext x509TestContext;
Expand All @@ -78,19 +77,13 @@ public static Stream<Arguments> data() {
public static void setUpBaseClass() throws Exception {
Security.addProvider(new BouncyCastleProvider());
cachedTestContexts = new HashMap<>();
tempDir = ClientBase.createEmptyTestDir();
}

@AfterAll
public static void cleanUpBaseClass() {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
cachedTestContexts.clear();
cachedTestContexts = null;
try {
FileUtils.deleteDirectory(tempDir);
} catch (IOException e) {
// ignore
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,25 @@
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.test.ClientBase;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileChangeWatcherTest extends ZKTestCase {

private static File tempDir;
private static File tempFile;
@TempDir
static File tempDir;
static File tempFile;
muthu90tech marked this conversation as resolved.
Show resolved Hide resolved

private static final Logger LOG = LoggerFactory.getLogger(FileChangeWatcherTest.class);

private static final long FS_TIMEOUT = 30000L;

@BeforeAll
public static void createTempFile() throws IOException {
tempDir = ClientBase.createEmptyTestDir();
tempFile = File.createTempFile("zk_test_", "", tempDir);
tempFile.deleteOnExit();
}

@AfterAll
public static void cleanupTempDir() {
try {
FileUtils.deleteDirectory(tempDir);
} catch (IOException e) {
// ignore
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class SecretUtilsTest {

@TempDir
static File tempDir;

@ParameterizedTest
@ValueSource (strings = {"test secret", ""})
public void testReadSecret(final String secretTxt) throws Exception {
Expand Down Expand Up @@ -58,13 +62,13 @@ public void testReadSecret_fileNotExist() {
}

public static Path createSecretFile(final String secretTxt) throws IOException {
final Path path = Files.createTempFile("test_", ".secrete");
final File tempFile = File.createTempFile("test_", ".secrete", tempDir);
final Path path = tempFile.toPath();

final BufferedWriter writer = new BufferedWriter(new FileWriter(path.toString()));
writer.append(secretTxt);
writer.close();

path.toFile().deleteOnExit();
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator;
import org.apache.zookeeper.test.ClientBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -102,8 +103,7 @@ private boolean getCheckSum(File snapFile) throws IOException {
* @throws Exception
*/
@Test
public void testChecksums() throws Exception {
File tmpDir = ClientBase.createTmpDir();
public void testChecksums(@TempDir File tmpDir) throws Exception {
muthu90tech marked this conversation as resolved.
Show resolved Hide resolved
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(150);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -52,8 +53,10 @@ public class ClientSSLReloadTest extends ZKTestCase {
private X509TestContext x509TestContext1;
private X509TestContext x509TestContext2;

private File dir1;
private File dir2;
@TempDir
File dir1;
@TempDir
File dir2;

private File keyStoreFile1;
private File trustStoreFile1;
Expand All @@ -64,9 +67,6 @@ public class ClientSSLReloadTest extends ZKTestCase {
@BeforeEach
public void setup() throws Exception {

dir1 = ClientBase.createEmptyTestDir();
dir2 = ClientBase.createEmptyTestDir();

Security.addProvider(new BouncyCastleProvider());

x509TestContext1 = X509TestContext.newBuilder()
Expand All @@ -92,12 +92,6 @@ public void setup() throws Exception {

@AfterEach
public void teardown() throws Exception {
try {
FileUtils.deleteDirectory(dir1);
FileUtils.deleteDirectory(dir2);
} catch (IOException e) {
// ignore
}
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.test.ClientBase;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class DatadirCleanupManagerTest extends ZKTestCase {

private DatadirCleanupManager purgeMgr;
@TempDir
File tmpDir;
private File snapDir;
private File dataLogDir;

@BeforeEach
public void setUp() throws Exception {
File dataDir = ClientBase.createTmpDir();
snapDir = dataDir;
dataLogDir = dataDir;
snapDir = tmpDir;
dataLogDir = tmpDir;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;


public class PrepRequestProcessorTest extends ClientBase {
Expand All @@ -81,8 +82,7 @@ public class PrepRequestProcessorTest extends ClientBase {
private boolean isStandaloneEnabledPreviously;

@BeforeEach
public void setup() throws Exception {
File tmpDir = ClientBase.createTmpDir();
public void setup(@TempDir File tmpDir) throws Exception {
ClientBase.setupTestEnv();
zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import org.apache.jute.Record;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
import org.apache.zookeeper.test.ClientBase;
import org.apache.zookeeper.txn.SetDataTxn;
import org.apache.zookeeper.txn.TxnHeader;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class ZooKeeperServerBeanTest {

Expand All @@ -47,9 +47,8 @@ public void teardown() throws Exception {
}

@Test
public void testTxnLogElapsedSyncTime() throws IOException {
public void testTxnLogElapsedSyncTime(@TempDir File tmpDir) throws IOException {

File tmpDir = ClientBase.createEmptyTestDir();
FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog"));

ZooKeeperServer zks = new ZooKeeperServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.io.File;
import org.apache.zookeeper.proto.ConnectRequest;
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
import org.apache.zookeeper.test.ClientBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class ZooKeeperServerCreationTest {

Expand All @@ -31,8 +31,7 @@ public class ZooKeeperServerCreationTest {
* that all needed fields are initialized properly, etc.
*/
@Test
public void testDefaultConstructor() throws Exception {
File tmpDir = ClientBase.createEmptyTestDir();
public void testDefaultConstructor(@TempDir File tmpDir) throws Exception {
FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog"));

ZooKeeperServer zks = new ZooKeeperServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -73,14 +74,12 @@ public void enableServer() {
}

@BeforeEach
public void setupEncryption() {
public void setupEncryption(@TempDir File tempDir) {
Security.addProvider(new BouncyCastleProvider());
File tmpDir = null;
X509TestContext x509TestContext = null;
try {
tmpDir = ClientBase.createEmptyTestDir();
x509TestContext = X509TestContext.newBuilder()
.setTempDir(tmpDir)
.setTempDir(tempDir)
.setKeyStorePassword("")
.setKeyStoreKeyType(X509KeyType.EC)
.setTrustStorePassword("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.io.TempDir;

public class ControllerConfigTest {

@TempDir
static File configDir;
File configFile;

private static final int AnyTickTime = 1234;
private static final int AnyPort = 1234;
private static final String AnyDataDir = "temp";

public static File createTempFile() throws IOException {
return File.createTempFile("temp", "cfg", new File(System.getProperty("user.dir")));
return File.createTempFile("temp", "cfg", configDir);
}

public static List<Integer> findNAvailablePorts(int n) throws IOException {
Expand Down
Loading
Loading