Skip to content

Commit 90b5d0f

Browse files
committed
Modifies the integration test suite to use TestContainers
1 parent 448c577 commit 90b5d0f

File tree

9 files changed

+141
-306
lines changed

9 files changed

+141
-306
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ should represent non-breaking changes. The third number represents any very min
145145
* Includes the REST response body in `VaultException` messages for basic read and write operations.
146146
* Implements the `/v1/auth/token/lookup-self` endpoint.
147147
* Makes numerous classes implement `Serializable`.
148+
* Re-works the integration test suite, so that you no longer have to manually configure and run a Vault server
149+
instance. The tests now use the [TestContainers](https://www.testcontainers.org/) to setup and launch a
150+
Vault server instance from within a Docker container, in a completely automated manner.
148151
* **2.0.0**: This is breaking-change release, with numerous deprecated items cleaned up.
149152
* Adds support for authentication via the AppRole auth backend.
150153
* Adds support for renewing secret leases.

build.gradle

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,27 @@ archivesBaseName = 'vault-java-driver'
77
version '3.0.0-SNAPSHOT'
88
ext.isReleaseVersion = !version.endsWith('SNAPSHOT')
99

10-
sourceCompatibility = 1.7
11-
targetCompatibility = 1.7
10+
compileJava {
11+
sourceCompatibility = 1.7
12+
targetCompatibility = 1.7
13+
}
14+
15+
compileTestJava {
16+
sourceCompatibility = 1.8
17+
targetCompatibility = 1.8
18+
}
1219

1320
repositories {
1421
mavenCentral()
1522
}
1623

1724
dependencies {
25+
testCompile('org.testcontainers:testcontainers:1.3.0')
1826
testCompile('junit:junit:4.11')
1927
testCompile('org.mockito:mockito-core:1.10.19')
2028
testCompile('org.eclipse.jetty:jetty-server:9.3.7.v20160115')
29+
testCompile('org.slf4j:slf4j-api:1.7.25')
30+
testRuntime('org.slf4j:slf4j-simple:1.7.25')
2131
}
2232

2333
task wrapper(type: Wrapper) {
@@ -73,13 +83,6 @@ task integrationTest(type: Test) {
7383
testLogging {
7484
events "passed", "skipped", "failed"
7585
}
76-
systemProperties = [
77-
VAULT_ADDR: System.getProperty("VAULT_ADDR"),
78-
VAULT_TOKEN: System.getProperty("VAULT_TOKEN"),
79-
VAULT_APP_ID: System.getProperty("VAULT_APP_ID"),
80-
VAULT_USER_ID: System.getProperty("VAULT_USER_ID"),
81-
VAULT_PASSWORD: System.getProperty("VAULT_PASSWORD")
82-
]
8386
}
8487

8588
//

src/main/java/com/bettercloud/vault/api/Auth.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import com.bettercloud.vault.rest.Rest;
1111

1212
import java.io.Serializable;
13-
import java.io.UnsupportedEncodingException;
14-
import java.util.ArrayList;
1513
import java.util.List;
1614
import java.util.Map;
1715
import java.util.UUID;
@@ -78,71 +76,71 @@ public static class TokenRequest implements Serializable {
7876
/**
7977
* {@link #id}
8078
*/
81-
public TokenRequest withId(final UUID id) {
79+
public TokenRequest id(final UUID id) {
8280
this.id = id;
8381
return this;
8482
}
8583

8684
/**
8785
* {@link #polices}
8886
*/
89-
public TokenRequest withPolices(final List<String> polices) {
87+
public TokenRequest polices(final List<String> polices) {
9088
this.polices = polices;
9189
return this;
9290
}
9391

9492
/**
9593
* {@link #meta}
9694
*/
97-
public TokenRequest withMeta(final Map<String, String> meta) {
95+
public TokenRequest meta(final Map<String, String> meta) {
9896
this.meta = meta;
9997
return this;
10098
}
10199

102100
/**
103101
* {@link #noParent}
104102
*/
105-
public TokenRequest withNoParent(final Boolean noParent) {
103+
public TokenRequest noParent(final Boolean noParent) {
106104
this.noParent = noParent;
107105
return this;
108106
}
109107

110108
/**
111109
* {@link #noDefaultPolicy}
112110
*/
113-
public TokenRequest withNoDefaultPolicy(final Boolean noDefaultPolicy) {
111+
public TokenRequest noDefaultPolicy(final Boolean noDefaultPolicy) {
114112
this.noDefaultPolicy = noDefaultPolicy;
115113
return this;
116114
}
117115

118116
/**
119117
* {@link #ttl}
120118
*/
121-
public TokenRequest withTtl(final String ttl) {
119+
public TokenRequest ttl(final String ttl) {
122120
this.ttl = ttl;
123121
return this;
124122
}
125123

126124
/**
127125
* {@link #displayName}
128126
*/
129-
public TokenRequest withDisplayName(final String displayName) {
127+
public TokenRequest displayName(final String displayName) {
130128
this.displayName = displayName;
131129
return this;
132130
}
133131

134132
/**
135133
* {@link #numUses}
136134
*/
137-
public TokenRequest withNumUses(final Long numUses) {
135+
public TokenRequest numUses(final Long numUses) {
138136
this.numUses = numUses;
139137
return this;
140138
}
141139

142140
/**
143141
* {@link #role}
144142
*/
145-
public TokenRequest withRole(final String role) {
143+
public TokenRequest role(final String role) {
146144
this.role = role;
147145
return this;
148146
}
@@ -259,14 +257,14 @@ public AuthResponse createToken(
259257
) throws VaultException {
260258
return createToken(
261259
new TokenRequest()
262-
.withId(id)
263-
.withPolices(policies)
264-
.withMeta(meta)
265-
.withNoParent(noParent)
266-
.withNoDefaultPolicy(noDefaultPolicy)
267-
.withTtl(ttl)
268-
.withDisplayName(displayName)
269-
.withNumUses(numUses));
260+
.id(id)
261+
.polices(policies)
262+
.meta(meta)
263+
.noParent(noParent)
264+
.noDefaultPolicy(noDefaultPolicy)
265+
.ttl(ttl)
266+
.displayName(displayName)
267+
.numUses(numUses));
270268
}
271269

272270

src/test-integration/README.md

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,21 @@ Intro
33
Unit tests, which do not rely on a Vault server being available, are separated from
44
these integration tests, which do require a Vault instance.
55

6-
Configuring a Vault Server
7-
==========================
8-
It's not necessary to have a production-grade Vault server. To run these tests, you
9-
can simply run a [Dev Server](https://www.vaultproject.io/intro/getting-started/dev-server.html)
10-
process:
6+
Running the Integration Tests
7+
=============================
8+
Originally this test suite required a decent amount of manual setup. You had to run and configure a Vault server
9+
instance on your machine, and populate several environment variables with values that would be picked up by the
10+
tests.
1111

12-
```
13-
$ vault server -dev`
12+
Since then, the tests have been modified to work with [TestContainers](https://www.testcontainers.org/), a Java
13+
library that efficiently manages Docker containers and makes them available to JUnit tests. So now, setup of the
14+
Vault server instance is entirely automated, and dealt with by the test suite itself.
1415

15-
==> WARNING: Dev mode is enabled!
16+
However, to run these tests you do need to have a current version of Docker installed on your machine. This is
17+
supported for Linux, OS X, and Windows, although the details vary significantly between those operating systems.
18+
See the [Docker website](https://www.docker.com/) for information on installing Docker on your OS, after checking
19+
also with the TestContainers website for OS-specific caveats (Windows in particular).
1620

17-
In this mode, Vault is completely in-memory and unsealed.
18-
Vault is configured to only have a single unseal key. The root
19-
token has already been authenticated with the CLI, so you can
20-
immediately begin using the Vault CLI.
21+
With Docker installed on your machine, you can run this test suite using the `integrationTest` Gradle task:
2122

22-
The only step you need to take is to set the following
23-
environment variables:
24-
25-
set VAULT_ADDR=http://127.0.0.1:8200
26-
27-
The unseal key and root token are reproduced below in case you
28-
want to seal/unseal the Vault or play with authentication.
29-
30-
Unseal Key: 642e33b1c397c292743df56da6129a25df6a6349934931f55a2baac34a6e2c80
31-
Root Token: 764cf317-d3b9-3d52-dc7d-e4f0198f6a8c
32-
33-
...
34-
```
35-
36-
Some of the integration tests verify that an authentication token can be retrieved
37-
from various auth backends (e.g. [App Id](https://www.vaultproject.io/docs/auth/app-id.html),
38-
[Username & Password](https://www.vaultproject.io/docs/auth/userpass.html), etc).
39-
So prior to running these tests, you will need to run some Vault CLI commands to
40-
enable auth backends and populate user and app data:
41-
42-
```
43-
vault auth-enable app-id
44-
vault write auth/app-id/map/app-id/fake_app display_name=fake_app
45-
vault write auth/app-id/map/user-id/fake_user value=fake_app
46-
47-
vault auth-enable userpass
48-
vault write auth/userpass/users/fake_user password=fake_password
49-
50-
vault mount -path=pki pki
51-
vault mount -path=other-pki pki
52-
vault write pki/root/generate/internal common_name=myvault.com ttl=99h
53-
54-
vault auth-enable approle
55-
vault write auth/approle/role/testrole secret_id_ttl=10m token_ttl=20m token_max_ttl=30m secret_id_num_uses=40
56-
```
57-
58-
Configuring and Running the Integration Tests
59-
=============================================
60-
The Gradle `integrationTest` task is used to execute the integration test suite.
61-
When running this Gradle task, you need to pass several JVM options so that Gradle
62-
will make them available to the tests:
63-
64-
* `VAULT_ADDR`: The connection URL for the Vault server. The Dev Server displays
65-
this when the server starts up (e.g. `http://127.0.0.1:8200`) in the example above.
66-
* `VAULT_TOKEN`: The root token, to enable Vault API calls. The Dev Server also
67-
displays this at startup (e.g. `764cf317-d3b9-3d52-dc7d-e4f0198f6a8c` in the
68-
example above).
69-
* `VAULT_APP_ID`: An application ID that has been created in the Vault server,
70-
for testing the App Id auth backend. This can be whatever you populate (e.g.
71-
`fake_app` in the example CLI command above).
72-
* `VAULT_USER_ID`: A user ID that has been created in the Vault server, for testing
73-
the Username and Password auth backend. This can be whatever you populate (e.g.
74-
`fake_user` in the example CLI command above).
75-
* `VAULT_PASSWORD`: The password corresponding to the above user (e.g. `fake_password`
76-
in the CLI command above).
77-
78-
Example Gradle invocation:
79-
80-
`$ gradle integrationTest -DVAULT_ADDR=http://127.0.0.1:8200 -DVAULT_TOKEN=764cf317-d3b9-3d52-dc7d-e4f0198f6a8c -DVAULT_APP_ID=fake_app -DVAULT_USER_ID=fake_user -DVAULT_PASSWORD=fake_password`
23+
`$ ./gradlew integrationTest`

0 commit comments

Comments
 (0)