-
Notifications
You must be signed in to change notification settings - Fork 4
/
mock-docker.go
75 lines (60 loc) · 1.87 KB
/
mock-docker.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
package whalewall
import (
"context"
"errors"
"slices"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
)
type dockerClient interface {
Ping(ctx context.Context) (types.Ping, error)
Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error)
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
Close() error
}
type mockDockerClient struct {
mtx sync.RWMutex
eventCh chan events.Message
containers []types.ContainerJSON
}
func newMockDockerClient(containers []types.ContainerJSON) *mockDockerClient {
return &mockDockerClient{
eventCh: make(chan events.Message),
containers: containers,
}
}
func (m *mockDockerClient) Ping(_ context.Context) (types.Ping, error) {
return types.Ping{}, nil
}
func (m *mockDockerClient) Events(_ context.Context, _ types.EventsOptions) (<-chan events.Message, <-chan error) {
return m.eventCh, nil
}
func (m *mockDockerClient) ContainerList(_ context.Context, _ types.ContainerListOptions) ([]types.Container, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
listedConts := make([]types.Container, len(m.containers))
for i, cont := range m.containers {
listedConts[i] = types.Container{
ID: cont.ID,
Names: []string{cont.Name},
Labels: cont.Config.Labels,
}
}
return listedConts, nil
}
func (m *mockDockerClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
i := slices.IndexFunc(m.containers, func(c types.ContainerJSON) bool {
return c.ID == containerID
})
if i == -1 {
return types.ContainerJSON{}, errors.New("container not found")
}
return m.containers[i], nil
}
func (m *mockDockerClient) Close() error {
return nil
}