-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathcompose.go
194 lines (166 loc) · 4.8 KB
/
compose.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 compose
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
// HostInfo exposes information about started scenario
type HostInfo interface {
// Host returns an address as host:port that can be used to connect to
// a running service.
Host() string
// HostForPort returns an address as host:port that can be used to
// connect to a running service that has multiple exposed ports. The
// address returned is the one that can be used to connect to the
// indicated exposed port.
HostForPort(port int) string
}
// EnsureUp starts all the requested services (must be defined in docker-compose.yml)
// with a default timeout of 300 seconds
func EnsureUp(t testing.TB, service string, options ...UpOption) HostInfo {
t.Helper()
if hostInfo := HostInfoFromEnv(t, service); hostInfo != nil {
return hostInfo
}
compose, err := getComposeProject(os.Getenv("DOCKER_COMPOSE_PROJECT_NAME"))
if err != nil {
t.Fatal(err)
}
defer compose.Close()
// Kill no longer used containers
err = compose.KillOld([]string{service})
if err != nil {
t.Fatal(err)
}
upOptions := UpOptions{
Timeout: 60 * time.Second,
Create: CreateOptions{
ForceRecreate: true,
},
}
for _, option := range options {
option(&upOptions)
}
start := func() error {
// Start container
err := compose.Start(service, upOptions)
if err != nil {
return fmt.Errorf("failed to start service '%s: %v", service, err)
}
// Wait for health
err = compose.Wait(upOptions.Timeout, service)
if err != nil {
inspected, inspectErr := compose.Inspect(service)
if inspectErr != nil {
t.Logf("inspection error: %v", err)
} else {
t.Logf("Container state (service: '%s'): %s", service, inspected)
}
return err
}
return nil
}
err = nil
for retries := 0; retries < 3; retries++ {
err := start()
if err == nil {
break
}
t.Log(err)
// Ignore errors here
compose.Kill(service)
}
if err != nil {
t.FailNow()
}
// Get host information
host, err := compose.HostInformation(service)
if err != nil {
t.Fatalf("getting host for %s: %v", service, err)
}
return host
}
// EnsureUpWithTimeout starts all the requested services (must be defined in docker-compose.yml)
// Wait for `timeout` seconds for health
func EnsureUpWithTimeout(t testing.TB, timeout int, service string) HostInfo {
return EnsureUp(t, service, UpWithTimeout(time.Duration(timeout)*time.Second))
}
// HostInfoFromEnv gets the host information to use for the test from environment variables.
func HostInfoFromEnv(t testing.TB, service string) HostInfo {
// If an environment variable with the form <SERVICE>_HOST is used, its value
// is used as host instead of starting a new service.
envVar := fmt.Sprintf("%s_HOST", strings.ToUpper(service))
host := os.Getenv(envVar)
if host != "" {
return &staticHostInfo{host: host}
}
// The NO_COMPOSE env variables makes it possible to skip the starting of the environment.
// This is useful if the service is already running locally.
// Kept for historical reasons, now it only complains if the host environment
// variable is not set.
noCompose, err := strconv.ParseBool(os.Getenv("NO_COMPOSE"))
if err == nil && noCompose {
t.Fatalf("%s environment variable must be set as the host:port where %s is running", envVar, service)
}
return nil
}
type staticHostInfo struct {
host string
}
func (i *staticHostInfo) Host() string {
return i.host
}
func (i *staticHostInfo) HostForPort(int) string {
return i.host
}
func findComposePath() (string, error) {
// find docker-compose
path, err := os.Getwd()
if err != nil {
return "", err
}
for {
if path == "/" {
break
}
composePath := filepath.Join(path, "docker-compose.yml")
if _, err = os.Stat(composePath); err == nil {
return composePath, nil
}
path = filepath.Dir(path)
}
return "", errors.New("docker-compose.yml not found")
}
func getComposeProject(name string) (*Project, error) {
path, err := findComposePath()
if err != nil {
return nil, err
}
return NewProject(
name,
[]string{
path,
},
)
}