refactoring to introduce deleteOnExit() for temp files (#920)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Sally MacFarlane 6 years ago committed by GitHub
parent b1b72010f8
commit 6d90cffccf
  1. 1
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/ProcessPantheonNodeRunner.java
  2. 1
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java
  3. 7
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/NettyP2PNetworkTest.java
  4. 6
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/PeerDiscoveryControllerTest.java
  5. 6
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/RecursivePeerRefreshStateTest.java
  6. 1
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistControllerTest.java
  7. 1
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/NodeWhitelistControllerTest.java
  8. 38
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/PermissioningConfigurationBuilderTest.java
  9. 1
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/WhitelistPersistorTest.java
  10. 90
      pantheon/src/test/java/tech/pegasys/pantheon/cli/PantheonCommandTest.java
  11. 1
      pantheon/src/test/java/tech/pegasys/pantheon/util/PermissioningConfigurationValidatorTest.java

@ -136,6 +136,7 @@ public class ProcessPantheonNodeRunner implements PantheonNodeRunner {
private Path createGenesisFile(final PantheonNode node, final EthNetworkConfig ethNetworkConfig) {
try {
Path genesisFile = Files.createTempFile(node.homeDirectory(), "genesis", "");
genesisFile.toFile().deleteOnExit();
Files.write(genesisFile, ethNetworkConfig.getGenesisConfig().getBytes(UTF_8));
return genesisFile;
} catch (IOException e) {

@ -223,6 +223,7 @@ public class PantheonNodeFactory {
PermissioningConfiguration.createDefault();
permissioningConfiguration.setNodeWhitelist(nodesWhitelist);
File tempFile = createTempPermissioningConfigurationFile();
tempFile.deleteOnExit();
permissioningConfiguration.setConfigurationFilePath(tempFile.getPath());
initPermissioningConfigurationFile(
WhitelistPersistor.WHITELIST_TYPE.NODES,

@ -46,6 +46,7 @@ import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -414,8 +415,10 @@ public final class NettyP2PNetworkTest {
final PeerBlacklist localBlacklist = new PeerBlacklist();
final PeerBlacklist remoteBlacklist = new PeerBlacklist();
final PermissioningConfiguration config = PermissioningConfiguration.createDefault();
config.setConfigurationFilePath(
Files.createTempFile("test", "test").toAbsolutePath().toString());
final Path tempFile = Files.createTempFile("test", "test");
tempFile.toFile().deleteOnExit();
config.setConfigurationFilePath(tempFile.toAbsolutePath().toString());
final NodeWhitelistController localWhitelistController =
new NodeWhitelistController(config, Collections.emptyList());
// turn on whitelisting by adding a different node NOT remote node

@ -51,6 +51,7 @@ import tech.pegasys.pantheon.util.uint.UInt256Value;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -1212,8 +1213,9 @@ public class PeerDiscoveryControllerTest {
private PermissioningConfiguration permissioningConfigurationWithTempFile() throws IOException {
final PermissioningConfiguration config = PermissioningConfiguration.createDefault();
config.setConfigurationFilePath(
Files.createTempFile("test", "test").toAbsolutePath().toString());
Path tempFile = Files.createTempFile("test", "test");
tempFile.toFile().deleteOnExit();
config.setConfigurationFilePath(tempFile.toAbsolutePath().toString());
return config;
}

@ -32,6 +32,7 @@ import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -480,10 +481,11 @@ public class RecursivePeerRefreshStateTest {
final DiscoveryPeer peerA = new DiscoveryPeer(createId(1), "127.0.0.1", 1, 1);
final DiscoveryPeer peerB = new DiscoveryPeer(createId(2), "127.0.0.2", 2, 2);
final Path tempFile = Files.createTempFile("test", "test");
tempFile.toFile().deleteOnExit();
final PermissioningConfiguration permissioningConfiguration =
PermissioningConfiguration.createDefault();
permissioningConfiguration.setConfigurationFilePath(
Files.createTempFile("test", "test").toAbsolutePath().toString());
permissioningConfiguration.setConfigurationFilePath(tempFile.toAbsolutePath().toString());
final NodeWhitelistController peerWhitelist =
new NodeWhitelistController(permissioningConfiguration, Collections.emptyList());

@ -234,6 +234,7 @@ public class AccountWhitelistControllerTest {
private Path createPermissionsFileWithAccount(final String account) throws IOException {
final String nodePermissionsFileContent = "accounts-whitelist=[\"" + account + "\"]";
final Path permissionsFile = Files.createTempFile("account_permissions", "");
permissionsFile.toFile().deleteOnExit();
Files.write(permissionsFile, nodePermissionsFileContent.getBytes(StandardCharsets.UTF_8));
return permissionsFile;
}

@ -388,6 +388,7 @@ public class NodeWhitelistControllerTest {
private Path createPermissionsFileWithNode(final String node) throws IOException {
final String nodePermissionsFileContent = "nodes-whitelist=[\"" + node + "\"]";
final Path permissionsFile = Files.createTempFile("node_permissions", "");
permissionsFile.toFile().deleteOnExit();
Files.write(permissionsFile, nodePermissionsFileContent.getBytes(StandardCharsets.UTF_8));
return permissionsFile;
}

@ -12,9 +12,11 @@
*/
package tech.pegasys.pantheon.ethereum.permissioning;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
@ -53,8 +55,7 @@ public class PermissioningConfigurationBuilderTest {
final String uri2 = "enode://" + VALID_NODE_ID + "@192.169.0.9:4568";
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_VALID);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
PermissioningConfiguration permissioningConfiguration = permissioningConfig(toml);
@ -71,8 +72,7 @@ public class PermissioningConfigurationBuilderTest {
final String uri = "enode://" + VALID_NODE_ID + "@192.168.0.9:4567";
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
PermissioningConfiguration permissioningConfiguration =
PermissioningConfigurationBuilder.permissioningConfiguration(
@ -86,8 +86,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithOnlyAccountWhitelistSet() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_ACCOUNT_WHITELIST_ONLY);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
PermissioningConfiguration permissioningConfiguration =
PermissioningConfigurationBuilder.permissioningConfiguration(
@ -102,8 +101,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithInvalidAccount() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_INVALID_ACCOUNT);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
@ -115,8 +113,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithInvalidEnode() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_INVALID_ENODE);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
@ -128,8 +125,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithEmptyWhitelistMustNotError() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_EMPTY_WHITELISTS);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
PermissioningConfiguration permissioningConfiguration = permissioningConfig(toml);
@ -142,8 +138,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithAbsentWhitelistMustThrowException() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_ABSENT_WHITELISTS);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
@ -153,8 +148,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithUnrecognizedKeyMustThrowException() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_UNRECOGNIZED_KEY);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
@ -167,7 +161,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigWithEmptyFileMustThrowException() throws Exception {
// write an empty file
final Path toml = Files.createTempFile("toml", "");
final Path toml = createTempFile("toml", "".getBytes(UTF_8));
final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
@ -177,8 +171,7 @@ public class PermissioningConfigurationBuilderTest {
@Test
public void permissioningConfigFromFileMustSetFilePath() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_VALID);
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
PermissioningConfiguration permissioningConfiguration =
PermissioningConfigurationBuilder.permissioningConfigurationFromToml(
@ -213,4 +206,11 @@ public class PermissioningConfigurationBuilderTest {
return PermissioningConfigurationBuilder.permissioningConfiguration(
toml.toAbsolutePath().toString(), true, true);
}
private Path createTempFile(final String filename, final byte[] contents) throws IOException {
final Path file = Files.createTempFile(filename, "");
Files.write(file, contents);
file.toFile().deleteOnExit();
return file;
}
}

@ -43,6 +43,7 @@ public class WhitelistPersistorTest {
public void setUp() throws IOException {
List<String> lines = Lists.newArrayList(nodesWhitelist, accountsWhitelist);
tempFile = File.createTempFile("test", "test");
tempFile.deleteOnExit();
Files.write(tempFile.toPath(), lines, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
whitelistPersistor = new WhitelistPersistor(tempFile.getAbsolutePath());
}

@ -48,7 +48,6 @@ import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
@ -184,10 +183,11 @@ public class PantheonCommandTest extends CommandTestAbstract {
public void callingWithConfigOptionButNonExistingFileShouldDisplayHelp() throws IOException {
assumeTrue(isFullInstantiation());
final File tempConfigFile = temp.newFile("an-invalid-file-name-without-extension");
parseCommand("--config-file", tempConfigFile.getPath());
final Path tempConfigFilePath = createTempFile("an-invalid-file-name-without-extension", "");
parseCommand("--config-file", tempConfigFilePath.toString());
final String expectedOutputStart = "Unable to read TOML configuration file " + tempConfigFile;
final String expectedOutputStart =
"Unable to read TOML configuration file " + tempConfigFilePath;
assertThat(commandErrorOutput.toString()).startsWith(expectedOutputStart);
assertThat(commandOutput.toString()).isEmpty();
}
@ -209,20 +209,15 @@ public class PantheonCommandTest extends CommandTestAbstract {
// We write a config file to prevent an invalid file in resource folder to raise errors in
// code checks (CI + IDE)
final File tempConfigFile = temp.newFile("invalid_config.toml");
try (final Writer fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8)) {
fileWriter.write("."); // an invalid toml content
fileWriter.flush();
final Path tempConfigFile = createTempFile("invalid_config.toml", ".");
parseCommand("--config-file", tempConfigFile.getPath());
parseCommand("--config-file", tempConfigFile.toString());
final String expectedOutputStart =
"Invalid TOML configuration : Unexpected '.', expected a-z, A-Z, 0-9, ', \", a table key, "
+ "a newline, or end-of-input (line 1, column 1)";
assertThat(commandErrorOutput.toString()).startsWith(expectedOutputStart);
assertThat(commandOutput.toString()).isEmpty();
}
final String expectedOutputStart =
"Invalid TOML configuration : Unexpected '.', expected a-z, A-Z, 0-9, ', \", a table key, "
+ "a newline, or end-of-input (line 1, column 1)";
assertThat(commandErrorOutput.toString()).startsWith(expectedOutputStart);
assertThat(commandOutput.toString()).isEmpty();
}
@Test
@ -231,20 +226,14 @@ public class PantheonCommandTest extends CommandTestAbstract {
// We write a config file to prevent an invalid file in resource folder to raise errors in
// code checks (CI + IDE)
final File tempConfigFile = temp.newFile("invalid_config.toml");
try (final Writer fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8)) {
final Path tempConfigFile = createTempFile("invalid_config.toml", "tester===========.......");
parseCommand("--config-file", tempConfigFile.toString());
fileWriter.write("tester===========......."); // an invalid toml content
fileWriter.flush();
parseCommand("--config-file", tempConfigFile.getPath());
final String expectedOutputStart =
"Invalid TOML configuration : Unexpected '=', expected ', \", ''', \"\"\", a number, "
+ "a boolean, a date/time, an array, or a table (line 1, column 8)";
assertThat(commandErrorOutput.toString()).startsWith(expectedOutputStart);
assertThat(commandOutput.toString()).isEmpty();
}
final String expectedOutputStart =
"Invalid TOML configuration : Unexpected '=', expected ', \", ''', \"\"\", a number, "
+ "a boolean, a date/time, an array, or a table (line 1, column 8)";
assertThat(commandErrorOutput.toString()).startsWith(expectedOutputStart);
assertThat(commandOutput.toString()).isEmpty();
}
@Test
@ -256,8 +245,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
final String updatedConfig =
Resources.toString(configFile, UTF_8)
.replace("~/genesis.json", escapeTomlString(genesisFile.toString()));
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, updatedConfig.getBytes(UTF_8));
final Path toml = createTempFile("toml", updatedConfig.getBytes(UTF_8));
Collection<RpcApi> expectedApis = asList(ETH, WEB3);
@ -341,10 +329,8 @@ public class PantheonCommandTest extends CommandTestAbstract {
public void permissionsTomlFileWithNoPermissionsEnabledMustError() throws IOException {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_TOML);
final Path permToml = Files.createTempFile("toml", "");
Files.write(permToml, Resources.toByteArray(configFile));
final Path permToml = createTempFile("toml", Resources.toByteArray(configFile));
parseCommand("--permissions-config-file", permToml.toString());
permToml.toFile().deleteOnExit();
verify(mockRunnerBuilder).build();
@ -353,7 +339,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
}
@Test
public void defaultPermissionsTomlFileWithNoPermissionsEnabledMustError() throws IOException {
public void defaultPermissionsTomlFileWithNoPermissionsEnabledMustError() {
parseCommand("--p2p-enabled", "false");
verify(mockRunnerBuilder).build();
@ -366,8 +352,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
public void permissionsTomlPathMustUseOption() throws IOException {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_TOML);
final Path permToml = Files.createTempFile("toml", "");
Files.write(permToml, Resources.toByteArray(configFile));
final Path permToml = createTempFile("toml", Resources.toByteArray(configFile));
parseCommand(
"--permissions-accounts-enabled", "--permissions-config-file", permToml.toString());
@ -394,8 +379,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
// Load a TOML that configures literally everything (except permissioning TOML config)
final URL configFile = Resources.getResource("everything_config.toml");
final Path toml = Files.createTempFile("toml", "");
Files.write(toml, Resources.toByteArray(configFile));
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
// Parse it.
final CommandLine.Model.CommandSpec spec = parseCommand("--config-file", toml.toString());
@ -480,6 +464,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
@Test
public void nodekeyOptionMustBeUsed() throws Exception {
final File file = new File("./specific/key");
file.deleteOnExit();
parseCommand("--node-private-key-file", file.getPath());
@ -501,6 +486,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
assumeFalse(isFullInstantiation());
final File file = new File("./specific/key");
file.deleteOnExit();
parseCommand("--node-private-key-file", file.getPath());
@ -591,8 +577,6 @@ public class PantheonCommandTest extends CommandTestAbstract {
assumeTrue(isFullInstantiation());
final Path genesisFile = createFakeGenesisFile(GENESIS_VALID_JSON);
final ArgumentCaptor<EthNetworkConfig> networkArg =
ArgumentCaptor.forClass(EthNetworkConfig.class);
parseCommand("--genesis-file", genesisFile.toString(), "--network", "rinkeby");
@ -1639,10 +1623,6 @@ public class PantheonCommandTest extends CommandTestAbstract {
public void metricsAndMetricsPushMustNotBeUsedTogether() throws Exception {
assumeTrue(isFullInstantiation());
final Path genesisFile = createFakeGenesisFile(GENESIS_VALID_JSON);
final ArgumentCaptor<EthNetworkConfig> networkArg =
ArgumentCaptor.forClass(EthNetworkConfig.class);
parseCommand("--metrics-enabled", "--metrics-push-enabled");
verifyZeroInteractions(mockRunnerBuilder);
@ -1880,15 +1860,16 @@ public class PantheonCommandTest extends CommandTestAbstract {
}
@Test
public void privacyOptionsRequiresServiceToBeEnabled() {
public void privacyOptionsRequiresServiceToBeEnabled() throws IOException {
final File file = new File("./specific/public_key");
file.deleteOnExit();
parseCommand(
"--privacy-url",
ENCLAVE_URI,
"--privacy-public-key-file",
file.getPath(),
file.toString(),
"--privacy-precompiled-address",
String.valueOf(Byte.MAX_VALUE - 1));
@ -1918,9 +1899,24 @@ public class PantheonCommandTest extends CommandTestAbstract {
private Path createFakeGenesisFile(final JsonObject jsonGenesis) throws IOException {
final Path genesisFile = Files.createTempFile("genesisFile", "");
Files.write(genesisFile, encodeJsonGenesis(jsonGenesis).getBytes(UTF_8));
genesisFile.toFile().deleteOnExit();
return genesisFile;
}
private Path createTempFile(final String filename, final String contents) throws IOException {
final Path file = Files.createTempFile(filename, "");
Files.write(file, contents.getBytes(UTF_8));
file.toFile().deleteOnExit();
return file;
}
private Path createTempFile(final String filename, final byte[] contents) throws IOException {
final Path file = Files.createTempFile(filename, "");
Files.write(file, contents);
file.toFile().deleteOnExit();
return file;
}
private String encodeJsonGenesis(final JsonObject jsonGenesis) {
return jsonGenesis.encodePrettily();
}

@ -58,6 +58,7 @@ public class PermissioningConfigurationValidatorTest {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG);
final Path toml = Files.createTempFile("toml", "");
toml.toFile().deleteOnExit();
Files.write(toml, Resources.toByteArray(configFile));
PermissioningConfiguration permissioningConfiguration =

Loading…
Cancel
Save