forked from cockroachdb/cockroach
/
util_docker.go
104 lines (92 loc) · 3.12 KB
/
util_docker.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package acceptance
import (
"fmt"
"testing"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/acceptance/cluster"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
)
func defaultContainerConfig() container.Config {
return container.Config{
Image: postgresTestImage,
Env: []string{
fmt.Sprintf("PGPORT=%s", base.DefaultPort),
"PGSSLCERT=/certs/node.crt",
"PGSSLKEY=/certs/node.key",
},
}
}
// testDockerFail ensures the specified docker cmd fails.
func testDockerFail(ctx context.Context, t *testing.T, name string, cmd []string) {
containerConfig := defaultContainerConfig()
containerConfig.Cmd = cmd
if err := testDockerSingleNode(ctx, t, name, containerConfig); err == nil {
t.Error("expected failure")
}
}
// testDockerSuccess ensures the specified docker cmd succeeds.
func testDockerSuccess(ctx context.Context, t *testing.T, name string, cmd []string) {
containerConfig := defaultContainerConfig()
containerConfig.Cmd = cmd
if err := testDockerSingleNode(ctx, t, name, containerConfig); err != nil {
t.Error(err)
}
}
const (
// Iterating against a locally built version of the docker image can be done
// by changing postgresTestImage to the hash of the container.
postgresTestImage = "docker.io/cockroachdb/postgres-test:20170423-1100"
)
func testDocker(
ctx context.Context, t *testing.T, num int, name string, containerConfig container.Config,
) error {
var err error
RunDocker(t, func(t *testing.T) {
cfg := cluster.TestConfig{
Name: name,
Duration: *flagDuration,
}
for i := 0; i < num; i++ {
cfg.Nodes = append(cfg.Nodes, cluster.NodeConfig{Stores: []cluster.StoreConfig{{}}})
}
l := StartCluster(ctx, t, cfg).(*cluster.DockerCluster)
defer l.AssertAndStop(ctx, t)
if len(l.Nodes) > 0 {
containerConfig.Env = append(containerConfig.Env, "PGHOST="+l.Hostname(0))
}
hostConfig := container.HostConfig{NetworkMode: "host"}
err = l.OneShot(
ctx, postgresTestImage, types.ImagePullOptions{}, containerConfig, hostConfig, "docker-"+name,
)
if err == nil {
// Clean up the log files if the run was successful.
l.Cleanup(ctx)
}
})
return err
}
func testDockerSingleNode(
ctx context.Context, t *testing.T, name string, containerConfig container.Config,
) error {
return testDocker(ctx, t, 1, name, containerConfig)
}
func testDockerOneShot(
ctx context.Context, t *testing.T, name string, containerConfig container.Config,
) error {
return testDocker(ctx, t, 0, name, containerConfig)
}