test: minor test code refactoring
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
15ee202167
commit
318186d9e0
@ -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:
|
||||
|
@ -129,15 +129,14 @@ class HTTPVaultConnectorBuilderTest {
|
||||
});
|
||||
|
||||
// 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;
|
||||
});
|
||||
@ -165,11 +164,11 @@ class HTTPVaultConnectorBuilderTest {
|
||||
});
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -31,11 +31,13 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
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;
|
||||
@ -129,13 +131,11 @@ class HTTPVaultConnectorIT {
|
||||
@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(
|
||||
@ -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());
|
||||
|
||||
// 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"
|
||||
);
|
||||
@ -216,7 +216,6 @@ class HTTPVaultConnectorIT {
|
||||
@Test
|
||||
@Order(30)
|
||||
@DisplayName("Write secrets")
|
||||
@SuppressWarnings("deprecation")
|
||||
void writeSecretTest() {
|
||||
authUser();
|
||||
assumeTrue(connector.isAuthorized());
|
||||
@ -610,7 +609,7 @@ class HTTPVaultConnectorIT {
|
||||
assumeFalse(connector.isAuthorized());
|
||||
|
||||
// Authenticate with created credentials.
|
||||
AuthResponse resp = assertDoesNotThrow(
|
||||
assertDoesNotThrow(
|
||||
() -> connector.authAppId(APP_ID, USER_ID),
|
||||
"Failed to authenticate using App-ID"
|
||||
);
|
||||
@ -1234,15 +1233,17 @@ class HTTPVaultConnectorIT {
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user