forked from cabify/aceptadora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aceptadora.go
124 lines (106 loc) · 3.49 KB
/
aceptadora.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
package aceptadora
import (
"context"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Config is intended to be loaded by "github.com/colega/envconfig"
type Config struct {
YAMLDir string `default:"./"`
YAMLName string `default:"aceptadora.yml"`
// StopTimeout will be used to stop containers gracefully.
// If zero (default), then containers will be forced to stop immediately saving some tear down time.
StopTimeout time.Duration `default:"0s"`
}
type Aceptadora struct {
cfg Config
t *testing.T
require *require.Assertions
yaml YAML
imagePuller ImagePuller
services map[string]*Runner
order []string
}
// New creates a new Aceptadora. It will try to load the YAML config from the path provided by Config
// If something goes wrong, it will use testing.T to fail.
func New(t *testing.T, imagePuller ImagePuller, cfg Config) *Aceptadora {
if _, ok := os.LookupEnv("TESTER_ADDRESS"); !ok {
os.Setenv("TESTER_ADDRESS", getLocalIP())
}
os.Setenv("YAMLDIR", cfg.YAMLDir)
yamlPath := cfg.YAMLDir + "/" + cfg.YAMLName
yaml, err := LoadYAML(yamlPath)
require.NoError(t, err, "Can't load YAML from %q: %s", yamlPath, err)
return &Aceptadora{
t: t,
require: require.New(t),
cfg: cfg,
yaml: yaml,
imagePuller: imagePuller,
services: map[string]*Runner{},
}
}
// PullImages pulls all the images mentioned in aceptadora.yml
// This allows doing this outside of the context of the test, and avoid unrelated flaky timeouts in the tests
// happening when most of the context has been consumed by pulling the image
func (a *Aceptadora) PullImages(ctx context.Context) {
for _, svc := range a.yaml.Services {
a.imagePuller.Pull(ctx, svc.Image)
}
}
// Run will start a given service (from aceptadora.yml) and register it for stopping later
func (a *Aceptadora) Run(ctx context.Context, name string) {
if _, ok := a.yaml.Services[name]; !ok {
a.t.Fatalf("There's no service with name %q", name)
}
if _, ok := a.services[name]; ok {
a.t.Fatalf("Trying to start again the service %q", name)
}
runner := NewRunner(a.t, name, a.yaml.Services[name], a.imagePuller)
runner.Start(ctx)
a.services[name] = runner
a.order = append(a.order, name)
}
// StopAll will stop all the services in the reverse order
// If you need to explicitly stop some service in first place, use Stop() previously.
func (a *Aceptadora) StopAll(ctx context.Context) {
for i := len(a.order) - 1; i >= 0; i-- {
a.Stop(ctx, a.order[i])
}
}
// Stop will try to stop the service with the name provided
// It will fail fatally if such service isn't defined
// It will skip the service if it's already stopped, and set it to nil once stopped, making this call idempotent
// Stop is not thread safe.
func (a *Aceptadora) Stop(ctx context.Context, name string) {
svc, ok := a.services[name]
if !ok {
a.t.Fatalf("There's no service %q to stop", name)
}
if svc == nil {
return
}
err := svc.StopWithTimeout(ctx, a.cfg.StopTimeout)
assert.NoError(a.t, err, "Can't stop service %q in time: %s", err)
a.services[name] = nil
}
// getLocalIP returns the non loopback local IP of the host
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}