/
scenario.go
301 lines (255 loc) · 7.38 KB
/
scenario.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package e2e
import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
)
const ContainerSharedDir = "/shared"
// Service is unified service interface that Scenario can manage.
type Service interface {
Name() string
Start(logger log.Logger, networkName, dir string) error
WaitReady() error
// It should be ok to Stop and Kill more than once, with next invokes being noop.
Kill() error
Stop() error
}
type logger struct {
w io.Writer
}
func NewLogger(w io.Writer) *logger {
return &logger{
w: w,
}
}
func (l *logger) Log(keyvals ...interface{}) error {
b := strings.Builder{}
b.WriteString(time.Now().Format("15:04:05"))
for _, v := range keyvals {
b.WriteString(" " + fmt.Sprintf("%v", v))
}
b.WriteString("\n")
_, err := l.w.Write([]byte(b.String()))
return err
}
// Scenario allows to manage deployments for single testing scenario.
type Scenario struct {
o scenarioOptions
sharedDir string
services []Service
}
// ScenarioOption defined the signature of a function used to manipulate options.
type ScenarioOption func(*scenarioOptions)
type scenarioOptions struct {
networkName string
logger log.Logger
}
// WithNetworkName tells scenario to use custom network name instead of UUID.
func WithNetworkName(networkName string) ScenarioOption {
return func(o *scenarioOptions) {
o.networkName = networkName
}
}
// WithLogger tells scenario to use custom logger default one (stdout).
func WithLogger(logger log.Logger) ScenarioOption {
return func(o *scenarioOptions) {
o.logger = logger
}
}
// NewScenario creates new Scenario.
func NewScenario(opts ...ScenarioOption) (_ *Scenario, err error) {
s := &Scenario{}
for _, o := range opts {
o(&s.o)
}
if s.o.networkName == "" {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return nil, err
}
s.o.networkName = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
if s.o.logger == nil {
s.o.logger = NewLogger(os.Stdout)
}
s.sharedDir, err = getTempDirectory()
if err != nil {
return nil, err
}
// Force a shutdown in order to cleanup from a spurious situation in case
// the previous tests run didn't cleanup correctly.
s.shutdown()
// Setup the docker network.
if out, err := RunCommandAndGetOutput("docker", "network", "create", s.o.networkName); err != nil {
s.o.logger.Log(string(out))
s.clean()
return nil, errors.Wrapf(err, "create docker network '%s'", s.o.networkName)
}
return s, nil
}
// SharedDir returns the absolute path of the directory on the host that is shared with all services in docker.
func (s *Scenario) SharedDir() string {
return s.sharedDir
}
// NetworkName returns the network name that scenario is responsible for.
func (s *Scenario) NetworkName() string {
return s.o.networkName
}
func (s *Scenario) isRegistered(name string) bool {
for _, service := range s.services {
if service.Name() == name {
return true
}
}
return false
}
func (s *Scenario) StartAndWaitReady(services ...Service) error {
if err := s.Start(services...); err != nil {
return err
}
return s.WaitReady(services...)
}
func (s *Scenario) Start(services ...Service) error {
for _, service := range services {
s.o.logger.Log("Starting", service.Name())
// Ensure another service with the same name doesn't exist.
if s.isRegistered(service.Name()) {
return fmt.Errorf("another service with the same name '%s' has already been started", service.Name())
}
// Start the service.
if err := service.Start(s.o.logger, s.o.networkName, s.SharedDir()); err != nil {
return err
}
// Add to the list of services.
s.services = append(s.services, service)
}
return nil
}
func (s *Scenario) Stop(services ...Service) error {
for _, service := range services {
if !s.isRegistered(service.Name()) {
return fmt.Errorf("unable to stop service %s because it does not exist", service.Name())
}
if err := service.Stop(); err != nil {
return err
}
// Remove the service from the list of services.
for i, entry := range s.services {
if entry.Name() == service.Name() {
s.services = append(s.services[:i], s.services[i+1:]...)
break
}
}
}
return nil
}
func (s *Scenario) WaitReady(services ...Service) error {
for _, service := range services {
if !s.isRegistered(service.Name()) {
return fmt.Errorf("unable to wait for service %s because it does not exist", service.Name())
}
if err := service.WaitReady(); err != nil {
return err
}
}
return nil
}
func (s *Scenario) Close() {
if s == nil {
return
}
s.shutdown()
s.clean()
}
// TODO(bwplotka): Add comments.
func (s *Scenario) clean() {
if err := os.RemoveAll(s.sharedDir); err != nil {
s.o.logger.Log("error while removing sharedDir", s.sharedDir, "err:", err)
}
}
func (s *Scenario) shutdown() {
// Kill the services in the opposite order.
for i := len(s.services) - 1; i >= 0; i-- {
if err := s.services[i].Kill(); err != nil {
s.o.logger.Log("Unable to kill service", s.services[i].Name(), ":", err.Error())
}
}
// Ensure there are no leftover containers.
if out, err := RunCommandAndGetOutput(
"docker",
"ps",
"-a",
"--quiet",
"--filter",
fmt.Sprintf("network=%s", s.o.networkName),
); err == nil {
for _, containerID := range strings.Split(string(out), "\n") {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
continue
}
if out, err = RunCommandAndGetOutput("docker", "rm", "--force", containerID); err != nil {
s.o.logger.Log(string(out))
s.o.logger.Log("Unable to cleanup leftover container", containerID, ":", err.Error())
}
}
} else {
s.o.logger.Log(string(out))
s.o.logger.Log("Unable to cleanup leftover containers:", err.Error())
}
// Teardown the docker network. In case the network does not exists (ie. this function
// is called during the setup of the scenario) we skip the removal in order to not log
// an error which may be misleading.
if ok, err := existDockerNetwork(s.o.logger, s.o.networkName); ok || err != nil {
if out, err := RunCommandAndGetOutput("docker", "network", "rm", s.o.networkName); err != nil {
s.o.logger.Log(string(out))
s.o.logger.Log("Unable to remove docker network", s.o.networkName, ":", err.Error())
}
}
}
func existDockerNetwork(logger log.Logger, networkName string) (bool, error) {
out, err := RunCommandAndGetOutput("docker", "network", "ls", "--quiet", "--filter", fmt.Sprintf("name=%s", networkName))
if err != nil {
logger.Log(string(out))
logger.Log("Unable to check if docker network", networkName, "exists:", err.Error())
return false, err
}
return strings.TrimSpace(string(out)) != "", nil
}
// getTempDirectory creates a temporary directory for shared integration
// test files, either in the working directory or a directory referenced by
// the E2E_TEMP_DIR environment variable.
func getTempDirectory() (string, error) {
var (
dir string
err error
)
// If a temp dir is referenced, return that.
if os.Getenv("E2E_TEMP_DIR") != "" {
dir = os.Getenv("E2E_TEMP_DIR")
} else {
dir, err = os.Getwd()
if err != nil {
return "", err
}
}
tmpDir, err := ioutil.TempDir(dir, "e2e_integration_test")
if err != nil {
return "", err
}
absDir, err := filepath.Abs(tmpDir)
if err != nil {
_ = os.RemoveAll(tmpDir)
return "", err
}
return absDir, nil
}