Skip to content

Commit

Permalink
test: minor test code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Jun 29, 2024
1 parent 15ee202 commit 318186d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ steps:
environment:
VAULT_VERSION: 1.17.0
commands:
- export PATH=.bin:$${PATH}
- export PATH=$${DRONE_WORKSPACE}/.bin:$${PATH}
- mvn -B -P integration-test verify
when:
branch:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ void testFromEnv() throws Exception {
});

// Provide CA certificate.
String VAULT_CACERT = tempDir.toString() + "/doesnotexist";
withVaultEnv(VAULT_ADDR, VAULT_CACERT, VAULT_MAX_RETRIES.toString(), null).execute(() -> {
String vaultCacert = tempDir.toString() + "/doesnotexist";
withVaultEnv(VAULT_ADDR, vaultCacert, VAULT_MAX_RETRIES.toString(), null).execute(() -> {
TlsException e = assertThrows(
TlsException.class,
() -> HTTPVaultConnector.builder().fromEnv(),
"Creation with unknown cert path failed"
);
assertInstanceOf(NoSuchFileException.class, e.getCause());
assertEquals(VAULT_CACERT, ((NoSuchFileException) e.getCause()).getFile());
assertEquals(vaultCacert, assertInstanceOf(NoSuchFileException.class, e.getCause()).getFile());

return null;
});
Expand Down Expand Up @@ -165,11 +164,11 @@ void testFromEnv() throws Exception {
});
}

private SystemLambda.WithEnvironmentVariables withVaultEnv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) {
return withEnvironmentVariable("VAULT_ADDR", vault_addr)
.and("VAULT_CACERT", vault_cacert)
.and("VAULT_MAX_RETRIES", vault_max_retries)
.and("VAULT_TOKEN", vault_token);
private SystemLambda.WithEnvironmentVariables withVaultEnv(String vaultAddr, String vaultCacert, String vaultMaxRetries, String vaultToken) {
return withEnvironmentVariable("VAULT_ADDR", vaultAddr)
.and("VAULT_CACERT", vaultCacert)
.and("VAULT_MAX_RETRIES", vaultMaxRetries)
.and("VAULT_TOKEN", vaultToken);
}

private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import java.io.*;
import java.lang.reflect.Field;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
import static org.apache.commons.io.FileUtils.copyDirectory;
import static org.awaitility.Awaitility.await;
Expand Down Expand Up @@ -129,13 +131,11 @@ class ReadWriteTests {
@Test
@Order(10)
@DisplayName("Read secrets")
@SuppressWarnings("deprecation")
void readSecretTest() {
authUser();
assumeTrue(connector.isAuthorized());

// Try to read path user has no permission to read.
SecretResponse res = null;
final String invalidPath = "secret/invalid/path";

VaultConnectorException e = assertThrows(
Expand All @@ -151,7 +151,7 @@ void readSecretTest() {
assertFalse(Pattern.compile("[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}").matcher(stackTrace(e)).find());

// Try to read accessible path with known value.
res = assertDoesNotThrow(
SecretResponse res = assertDoesNotThrow(
() -> connector.read(SECRET_PATH + "/" + SECRET_KEY),
"Valid secret path could not be read"
);
Expand Down Expand Up @@ -216,7 +216,6 @@ void listSecretsTest() {
@Test
@Order(30)
@DisplayName("Write secrets")
@SuppressWarnings("deprecation")
void writeSecretTest() {
authUser();
assumeTrue(connector.isAuthorized());
Expand Down Expand Up @@ -610,7 +609,7 @@ void registerAppIdTest() {
assumeFalse(connector.isAuthorized());

// Authenticate with created credentials.
AuthResponse resp = assertDoesNotThrow(
assertDoesNotThrow(
() -> connector.authAppId(APP_ID, USER_ID),
"Failed to authenticate using App-ID"
);
Expand Down Expand Up @@ -1234,15 +1233,17 @@ private VaultConfiguration initializeVault(File dir, boolean tls) throws Illegal

// Write configuration file.
File configFile = new File(dir, "vault.conf");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(configFile))) {
bw.write(config.toString());
try {
Files.write(configFile.toPath(), config.toString().getBytes(UTF_8));
} catch (IOException e) {
throw new IllegalStateException("Unable to generate config file", e);
}

// Start vault process.
try {
vaultProcess = Runtime.getRuntime().exec("vault server -config " + configFile);
vaultProcess = new ProcessBuilder("vault", "server", "-config", configFile.toString())
.directory(dir)
.start();
} catch (IOException e) {
throw new IllegalStateException("Unable to start vault. Make sure vault binary is in your executable path", e);
}
Expand Down

0 comments on commit 318186d

Please sign in to comment.