test: scan for "Vault server started" instead of fixed delay
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Stefan Kalscheuer 2022-08-29 09:42:23 +02:00
parent a1626aa1c7
commit b5ed7704e3
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
2 changed files with 33 additions and 38 deletions

10
pom.xml
View File

@ -128,7 +128,7 @@
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>4.6.1</version> <version>4.7.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -155,6 +155,12 @@
<version>3.10.1</version> <version>3.10.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
@ -300,7 +306,7 @@
<plugin> <plugin>
<groupId>org.owasp</groupId> <groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId> <artifactId>dependency-check-maven</artifactId>
<version>7.1.1</version> <version>7.1.2</version>
<executions> <executions>
<execution> <execution>
<goals> <goals>

View File

@ -37,6 +37,7 @@ import java.util.regex.Pattern;
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.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue;
@ -80,11 +81,6 @@ class HTTPVaultConnectorIT {
// Initialize Vault. // Initialize Vault.
VaultConfiguration config = initializeVault(tempDir, isTls); VaultConfiguration config = initializeVault(tempDir, isTls);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Initialize connector. // Initialize connector.
HTTPVaultConnectorBuilder builder = HTTPVaultConnector.builder() HTTPVaultConnectorBuilder builder = HTTPVaultConnector.builder()
@ -1222,30 +1218,37 @@ class HTTPVaultConnectorIT {
} }
// Write configuration file. // Write configuration file.
BufferedWriter bw = null; File configFile = new File(dir, "vault.conf");
File configFile; try (BufferedWriter bw = new BufferedWriter(new FileWriter(configFile))) {
try {
configFile = new File(dir, "vault.conf");
bw = new BufferedWriter(new FileWriter(configFile));
bw.write(config.toString()); bw.write(config.toString());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Unable to generate config file", e); throw new IllegalStateException("Unable to generate config file", e);
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
// Start vault process. // Start vault process.
try { try {
vaultProcess = Runtime.getRuntime().exec("vault server -config " + configFile.toString()); vaultProcess = Runtime.getRuntime().exec("vault server -config " + configFile);
} 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);
} }
await().atMost(5, TimeUnit.SECONDS).until(() -> {
try (InputStream stdout = vaultProcess.getInputStream();
InputStreamReader reader = new InputStreamReader(stdout);
BufferedReader br = new BufferedReader(reader)) {
String line = br.readLine();
while (line != null) {
if (line.contains("Vault server started")) {
return true;
} else {
line = br.readLine();
}
}
return false;
}
});
return config; return config;
} }
@ -1270,28 +1273,14 @@ class HTTPVaultConnectorIT {
* @return port number * @return port number
*/ */
private static Integer getFreePort() { private static Integer getFreePort() {
ServerSocket socket = null; try (ServerSocket socket = new ServerSocket(0)) {
try {
socket = new ServerSocket(0);
socket.setReuseAddress(true); socket.setReuseAddress(true);
int port = socket.getLocalPort();
try { return socket.getLocalPort();
socket.close();
} catch (IOException e) {
// Ignore IOException on close()
}
return port;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
}
throw new IllegalStateException("Unable to find a free TCP port"); throw new IllegalStateException("Unable to find a free TCP port");
} }