-
Notifications
You must be signed in to change notification settings - Fork 16
/
docker_helpers_test.go
342 lines (294 loc) · 8.53 KB
/
docker_helpers_test.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package traefikkop
import (
"context"
"fmt"
"io"
"log"
"net"
"os"
"path"
"sync"
"testing"
"github.com/docker/cli/cli/compose/loader"
compose "github.com/docker/cli/cli/compose/types"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/go-connections/nat"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/stretchr/testify/assert"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/provider/docker"
"github.com/traefik/traefik/v2/pkg/safe"
"github.com/traefik/traefik/v2/pkg/server"
)
type testStore struct {
kv map[string]interface{}
}
func (s testStore) Ping() error {
return nil
}
func (s *testStore) Store(conf dynamic.Configuration) error {
kv, err := ConfigToKV(conf)
if err != nil {
return err
}
s.kv = kv
return nil
}
type DockerAPIStub struct {
containers []types.Container
containersJSON map[string]types.ContainerJSON
}
func (d DockerAPIStub) ServerVersion(ctx context.Context) (types.Version, error) {
// Implement your logic here
return types.Version{
Version: "1.0.0",
APIVersion: "1.0.0-test",
}, nil
}
func (d DockerAPIStub) Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error) {
// Implement your logic here
fmt.Println("Events")
return nil, nil
}
func (d DockerAPIStub) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
// Implement your logic here
fmt.Println("ContainerList")
return d.containers, nil
}
func (d DockerAPIStub) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
// Implement your logic here
fmt.Println("ContainerInspect", containerID)
return d.containersJSON[containerID], nil
}
func (d DockerAPIStub) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
// Implement your logic here
fmt.Println("ServiceList")
return nil, nil
}
func (d DockerAPIStub) NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
// Implement your logic here
fmt.Println("NetworkList")
return nil, nil
}
func getAvailablePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
func createHTTPServer() (*fiber.App, string) {
app := fiber.New()
app.Use(logger.New())
app.Get("/v1.24/version", func(c *fiber.Ctx) error {
version, err := dockerAPI.ServerVersion(c.Context())
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(version)
})
app.Get("/v1.24/containers/json", func(c *fiber.Ctx) error {
containers, err := dockerAPI.ContainerList(c.Context(), types.ContainerListOptions{})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(containers)
})
app.Get("/v1.24/containers/:id/json", func(c *fiber.Ctx) error {
container, err := dockerAPI.ContainerInspect(c.Context(), c.Params("id"))
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
// fmt.Printf("returning container: %+v\n", container)
// print container as json
// json.NewEncoder((os.Stdout)).Encode(container)
return c.JSON(container)
})
port, err := getAvailablePort()
if err != nil {
log.Fatal(err)
}
// log.Println("Available port:", port)
go app.Listen(fmt.Sprintf(":%d", port))
dockerEndpoint := fmt.Sprintf("http://localhost:%d", port)
return app, dockerEndpoint
}
func buildConfigDetails(source map[string]any, env map[string]string) compose.ConfigDetails {
workingDir, err := os.Getwd()
if err != nil {
panic(err)
}
return compose.ConfigDetails{
WorkingDir: workingDir,
ConfigFiles: []compose.ConfigFile{
{Filename: "filename.yml", Config: source},
},
Environment: env,
}
}
func loadYAML(yaml []byte) (*compose.Config, error) {
return loadYAMLWithEnv(yaml, nil)
}
func loadYAMLWithEnv(yaml []byte, env map[string]string) (*compose.Config, error) {
dict, err := loader.ParseYAML(yaml)
if err != nil {
return nil, err
}
return loader.Load(buildConfigDetails(dict, env))
}
// convert compose services to containers
func createContainers(composeConfig *compose.Config) []types.Container {
containers := make([]types.Container, 0)
for _, service := range composeConfig.Services {
container := types.Container{
ID: service.Name,
Labels: service.Labels,
State: "running",
Status: "running",
}
// convert ports
ports := make([]types.Port, 0)
for _, port := range service.Ports {
ports = append(ports, types.Port{
IP: "172.18.0.2",
PrivatePort: uint16(port.Target),
PublicPort: uint16(port.Published),
Type: port.Protocol,
})
}
container.Ports = ports
containers = append(containers, container)
}
return containers
}
// convert compose services to containersJSON
func createContainersJSON(composeConfig *compose.Config) map[string]types.ContainerJSON {
containersJSON := make(map[string]types.ContainerJSON)
for _, service := range composeConfig.Services {
containerJSON := types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: service.Name,
Name: service.Name,
State: &types.ContainerState{
Status: "running",
Running: true,
},
HostConfig: &container.HostConfig{
NetworkMode: "testing_default", // network name
PortBindings: nat.PortMap{},
},
},
Config: &container.Config{
Labels: service.Labels,
},
NetworkSettings: &types.NetworkSettings{
Networks: map[string]*network.EndpointSettings{
"testing_default": {
NetworkID: "testing_default", // should normally look like a random id but we can reuse the name here
IPAddress: "172.18.0.2",
},
"foobar": {
NetworkID: "foobar",
IPAddress: "10.10.10.5",
},
},
NetworkSettingsBase: types.NetworkSettingsBase{},
},
}
// add port bindings
for _, port := range service.Ports {
portID := nat.Port(fmt.Sprintf("%d/%s", port.Published, port.Protocol))
containerJSON.HostConfig.PortBindings[portID] = []nat.PortBinding{
{
HostIP: "",
HostPort: fmt.Sprintf("%d", port.Published),
},
}
containerJSON.NetworkSettings.Ports = containerJSON.HostConfig.PortBindings
}
containersJSON[service.Name] = containerJSON
}
return containersJSON
}
func doTest(t *testing.T, file string, config *Config) *testStore {
p := path.Join("fixtures", file)
f, err := os.Open(p)
assert.NoError(t, err)
if err != nil {
t.FailNow()
}
b, err := io.ReadAll(f)
assert.NoError(t, err)
composeConfig, err := loadYAML(b)
assert.NoError(t, err)
store := &testStore{}
// fmt.Printf("%+v\n", composeConfig)
dockerAPI.containers = createContainers(composeConfig)
dockerAPI.containersJSON = createContainersJSON(composeConfig)
dp := &docker.Provider{}
dp.Watch = false
dp.Endpoint = dockerEndpoint
if config == nil {
config = &Config{
BindIP: "192.168.100.100",
}
} else {
config.BindIP = "192.168.100.100"
}
handleConfigChange := createConfigHandler(*config, store, dp, dc)
routinesPool := safe.NewPool(context.Background())
watcher := server.NewConfigurationWatcher(
routinesPool,
dp,
[]string{},
"docker",
)
watcher.AddListener(handleConfigChange)
// ensure we get exactly one change
wgChanges := sync.WaitGroup{}
wgChanges.Add(1)
watcher.AddListener(func(c dynamic.Configuration) {
wgChanges.Done()
})
watcher.Start()
defer watcher.Stop()
wgChanges.Wait()
// print the kv store
for k, v := range store.kv {
fmt.Printf("%s: %+v\n", k, v)
}
return store
}
func assertServiceIP(t *testing.T, store *testStore, serviceName string, ip string) {
assert.Equal(t, ip, store.kv[fmt.Sprintf("traefik/http/services/%s/loadBalancer/servers/0/url", serviceName)])
}
type svc struct {
name string
proto string
ip string
}
func assertServiceIPs(t *testing.T, store *testStore, svcs []svc) {
for _, svc := range svcs {
path := "url"
if svc.proto != "http" {
path = "address"
}
key := fmt.Sprintf("traefik/%s/services/%s/loadBalancer/servers/0/%s", svc.proto, svc.name, path)
assert.Equal(t,
svc.ip,
store.kv[key],
"service has wrong IP at key: %s",
key,
)
}
}