forked from orlangure/gnomock
/
preset.go
205 lines (164 loc) 路 4.51 KB
/
preset.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
// Package localstack provides a Gnomock Preset for localstack project
// (https://github.com/localstack/localstack). It allows to easily setup local
// AWS stack for testing
package localstack
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/johanhugg/gnomock"
"github.com/johanhugg/gnomock/internal/registry"
)
const (
webPort = "web"
// APIPort should be used to configure AWS SDK endpoint.
APIPort = "api"
)
const defaultVersion = "0.14.0"
func init() {
registry.Register("localstack", func() gnomock.Preset { return &P{} })
}
// Preset creates a new localstack preset to use with gnomock.Start. See
// package docs for a list of exposed ports and services. It is legal to not
// provide any services using WithServices options, but in such case a new
// localstack container will be useless.
//
// This Preset cannot be used with localstack image prior to 0.11.0.
func Preset(opts ...Option) gnomock.Preset {
p := &P{}
for _, opt := range opts {
opt(p)
}
return p
}
// P is a Gnomock Preset localstack implementation.
type P struct {
Services []Service `json:"services"`
S3Path string `json:"s3_path"`
Version string `json:"version"`
}
// Image returns an image that should be pulled to create this container.
func (p *P) Image() string {
return fmt.Sprintf("docker.io/localstack/localstack:%s", p.Version)
}
// Ports returns ports that should be used to access this container.
func (p *P) Ports() gnomock.NamedPorts {
return gnomock.NamedPorts{
webPort: {Protocol: "tcp", Port: 8080},
APIPort: {Protocol: "tcp", Port: 4566},
}
}
// Options returns a list of options to configure this container.
func (p *P) Options() []gnomock.Option {
p.setDefaults()
svcStrings := make([]string, len(p.Services))
for i, svc := range p.Services {
svcStrings[i] = string(svc)
}
svcEnv := strings.Join(svcStrings, ",")
opts := []gnomock.Option{
gnomock.WithHealthCheck(p.healthcheck(svcStrings)),
gnomock.WithEnv("SERVICES=" + svcEnv),
gnomock.WithInit(p.initf()),
}
return opts
}
func (p *P) setDefaults() {
if p.Version == "" {
p.Version = defaultVersion
}
}
func (p *P) healthcheck(services []string) gnomock.HealthcheckFunc {
return func(ctx context.Context, c *gnomock.Container) (err error) {
addr := p.healthCheckAddress(c)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, addr, nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer func() {
closeErr := res.Body.Close()
if err == nil && closeErr != nil {
err = closeErr
}
}()
var hr healthResponse
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&hr)
if err != nil {
return err
}
if len(hr.Services) < len(services) {
return fmt.Errorf(
"not enough active services: want %d got %d [%s]",
len(services), len(hr.Services), hr.Services,
)
}
for _, service := range services {
status := hr.Services[service]
// available status was added in 0.13.0: it allows to lazy load the
// services after the first request
if status != "running" && status != "available" {
return fmt.Errorf("service '%s' is not running", service)
}
}
return nil
}
}
// healthCheckAddress returns the address of `/health` endpoint of a running
// localstack container. Before version 0.11.3, the endpoint was available at
// port 8080. In 0.11.3, the endpoint was moved to the default port (4566).
func (p *P) healthCheckAddress(c *gnomock.Container) string {
defaultPath := fmt.Sprintf("http://%s/health", c.Address(APIPort))
if p.Version == defaultVersion {
return defaultPath
}
versionParts := strings.Split(p.Version, ".")
if len(versionParts) != 3 {
return defaultPath
}
major, err := strconv.Atoi(versionParts[0])
if err != nil {
return defaultPath
}
if major > 0 {
return defaultPath
}
minor, err := strconv.Atoi(versionParts[1])
if err != nil {
return defaultPath
}
if minor > 11 {
return defaultPath
}
patch, err := strconv.Atoi(versionParts[2])
if err != nil {
return defaultPath
}
if minor == 11 && patch > 2 {
return defaultPath
}
return fmt.Sprintf("http://%s/health", c.Address(webPort))
}
type healthResponse struct {
Services map[string]string `json:"services"`
}
func (p *P) initf() gnomock.InitFunc {
return func(ctx context.Context, c *gnomock.Container) error {
for _, s := range p.Services {
if s == S3 {
err := p.initS3(c)
if err != nil {
return fmt.Errorf("can't init s3 storage: %w", err)
}
}
}
return nil
}
}