/
vault.go
62 lines (48 loc) · 1.56 KB
/
vault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dockertest
import (
"context"
"testing"
"github.com/docker/docker/api/types"
dockhelper "github.com/hashicorp/vault/sdk/helper/docker"
)
const (
benchmarkImageRepo = "docker.mirror.hashicorp.services/hashicorp/vault"
benchmarkImageTag = "latest"
)
func CreateVaultContainer(t *testing.T) (func(), string) {
ctx := context.Background()
runOpts := dockhelper.RunOptions{
ContainerName: "vault",
ImageRepo: benchmarkImageRepo,
ImageTag: benchmarkImageTag,
Cmd: []string{
"server", "-log-level=trace", "-dev", "-dev-root-token-id=root",
"-dev-listen-address=0.0.0.0:8200",
},
Ports: []string{"8200/tcp"},
}
runner, err := dockhelper.NewServiceRunner(runOpts)
if err != nil {
t.Fatalf("Error starting docker client for vault: %s", err)
}
svc, err := runner.Start(ctx, true, false)
if err != nil {
t.Fatalf("Error starting vault container: %s", err)
}
var netName string
for netName = range svc.Container.NetworkSettings.Networks {
// Networks above is a map; we just need to find the first and
// only key of this map (network name). The range handles this
// for us, but we need a loop construction in order to use range.
}
containerIPAddress := svc.Container.NetworkSettings.Networks[netName].IPAddress
cleanup := func() {
err := runner.DockerAPI.ContainerRemove(ctx, svc.Container.ID, types.ContainerRemoveOptions{Force: true})
if err != nil {
t.Fatalf("Error removing vault container: %s", err)
}
}
return cleanup, containerIPAddress
}