test: minor test code refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Stefan Kalscheuer 2024-06-29 12:17:18 +02:00
parent 15ee202167
commit 318186d9e0
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
3 changed files with 18 additions and 18 deletions

View File

@ -41,7 +41,7 @@ steps:
environment: environment:
VAULT_VERSION: 1.17.0 VAULT_VERSION: 1.17.0
commands: commands:
- export PATH=.bin:$${PATH} - export PATH=$${DRONE_WORKSPACE}/.bin:$${PATH}
- mvn -B -P integration-test verify - mvn -B -P integration-test verify
when: when:
branch: branch:

View File

@ -129,15 +129,14 @@ class HTTPVaultConnectorBuilderTest {
}); });
// Provide CA certificate. // Provide CA certificate.
String VAULT_CACERT = tempDir.toString() + "/doesnotexist"; String vaultCacert = tempDir.toString() + "/doesnotexist";
withVaultEnv(VAULT_ADDR, VAULT_CACERT, VAULT_MAX_RETRIES.toString(), null).execute(() -> { withVaultEnv(VAULT_ADDR, vaultCacert, VAULT_MAX_RETRIES.toString(), null).execute(() -> {
TlsException e = assertThrows( TlsException e = assertThrows(
TlsException.class, TlsException.class,
() -> HTTPVaultConnector.builder().fromEnv(), () -> HTTPVaultConnector.builder().fromEnv(),
"Creation with unknown cert path failed" "Creation with unknown cert path failed"
); );
assertInstanceOf(NoSuchFileException.class, e.getCause()); assertEquals(vaultCacert, assertInstanceOf(NoSuchFileException.class, e.getCause()).getFile());
assertEquals(VAULT_CACERT, ((NoSuchFileException) e.getCause()).getFile());
return null; return null;
}); });
@ -165,11 +164,11 @@ class HTTPVaultConnectorBuilderTest {
}); });
} }
private SystemLambda.WithEnvironmentVariables withVaultEnv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) { private SystemLambda.WithEnvironmentVariables withVaultEnv(String vaultAddr, String vaultCacert, String vaultMaxRetries, String vaultToken) {
return withEnvironmentVariable("VAULT_ADDR", vault_addr) return withEnvironmentVariable("VAULT_ADDR", vaultAddr)
.and("VAULT_CACERT", vault_cacert) .and("VAULT_CACERT", vaultCacert)
.and("VAULT_MAX_RETRIES", vault_max_retries) .and("VAULT_MAX_RETRIES", vaultMaxRetries)
.and("VAULT_TOKEN", vault_token); .and("VAULT_TOKEN", vaultToken);
} }
private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException { private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException {

View File

@ -31,11 +31,13 @@ import org.junit.jupiter.api.io.TempDir;
import java.io.*; import java.io.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static org.apache.commons.io.FileUtils.copyDirectory; import static org.apache.commons.io.FileUtils.copyDirectory;
import static org.awaitility.Awaitility.await; import static org.awaitility.Awaitility.await;
@ -129,13 +131,11 @@ class HTTPVaultConnectorIT {
@Test @Test
@Order(10) @Order(10)
@DisplayName("Read secrets") @DisplayName("Read secrets")
@SuppressWarnings("deprecation")
void readSecretTest() { void readSecretTest() {
authUser(); authUser();
assumeTrue(connector.isAuthorized()); assumeTrue(connector.isAuthorized());
// Try to read path user has no permission to read. // Try to read path user has no permission to read.
SecretResponse res = null;
final String invalidPath = "secret/invalid/path"; final String invalidPath = "secret/invalid/path";
VaultConnectorException e = assertThrows( VaultConnectorException e = assertThrows(
@ -151,7 +151,7 @@ class HTTPVaultConnectorIT {
assertFalse(Pattern.compile("[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}").matcher(stackTrace(e)).find()); 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. // Try to read accessible path with known value.
res = assertDoesNotThrow( SecretResponse res = assertDoesNotThrow(
() -> connector.read(SECRET_PATH + "/" + SECRET_KEY), () -> connector.read(SECRET_PATH + "/" + SECRET_KEY),
"Valid secret path could not be read" "Valid secret path could not be read"
); );
@ -216,7 +216,6 @@ class HTTPVaultConnectorIT {
@Test @Test
@Order(30) @Order(30)
@DisplayName("Write secrets") @DisplayName("Write secrets")
@SuppressWarnings("deprecation")
void writeSecretTest() { void writeSecretTest() {
authUser(); authUser();
assumeTrue(connector.isAuthorized()); assumeTrue(connector.isAuthorized());
@ -610,7 +609,7 @@ class HTTPVaultConnectorIT {
assumeFalse(connector.isAuthorized()); assumeFalse(connector.isAuthorized());
// Authenticate with created credentials. // Authenticate with created credentials.
AuthResponse resp = assertDoesNotThrow( assertDoesNotThrow(
() -> connector.authAppId(APP_ID, USER_ID), () -> connector.authAppId(APP_ID, USER_ID),
"Failed to authenticate using App-ID" "Failed to authenticate using App-ID"
); );
@ -1234,15 +1233,17 @@ class HTTPVaultConnectorIT {
// Write configuration file. // Write configuration file.
File configFile = new File(dir, "vault.conf"); File configFile = new File(dir, "vault.conf");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(configFile))) { try {
bw.write(config.toString()); Files.write(configFile.toPath(), config.toString().getBytes(UTF_8));
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Unable to generate config file", e); throw new IllegalStateException("Unable to generate config file", e);
} }
// Start vault process. // Start vault process.
try { try {
vaultProcess = Runtime.getRuntime().exec("vault server -config " + configFile); vaultProcess = new ProcessBuilder("vault", "server", "-config", configFile.toString())
.directory(dir)
.start();
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Unable to start vault. Make sure vault binary is in your executable path", e); throw new IllegalStateException("Unable to start vault. Make sure vault binary is in your executable path", e);
} }