-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtypes.go
129 lines (111 loc) · 5.89 KB
/
types.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
package meli
import (
"bytes"
"context"
"io"
"io/ioutil"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
)
type emptyStruct struct{}
// Buildstruct represents a docker-compose' build section
type Buildstruct struct {
// remember to use caps so that they can be exported
Context string `yaml:"context,omitempty"`
Dockerfile string `yaml:"dockerfile,omitempty"`
}
// ComposeService represents a docker-compose' service section
type ComposeService struct {
Image string `yaml:"image,omitempty"`
Ports []string `yaml:"ports,omitempty"`
Labels []string `yaml:"labels,omitempty"`
Environment []string `yaml:"environment,omitempty"`
Command string `yaml:"command,flow,omitempty"`
Restart string `yaml:"restart,omitempty"`
Build Buildstruct `yaml:"build,omitempty"`
Volumes []string `yaml:"volumes,omitempty"`
Links []string `yaml:"links,omitempty"`
EnvFile []string `yaml:"env_file,omitempty"`
}
// DockerComposeConfig represents a docker-compose file
type DockerComposeConfig struct {
Version string `yaml:"version,omitempty"`
Services map[string]ComposeService `yaml:"services"`
Volumes map[string]string `yaml:"volumes,omitempty"`
}
// DockerContainer represents a docker container
type DockerContainer struct {
ServiceName string
ComposeService ComposeService
NetworkID string
NetworkName string
FollowLogs bool
DockerComposeFile string
ContainerID string // this assumes that there can only be one container per docker-compose service
LogMedium io.Writer
CurentDir string
Rebuild bool
EnvFile []string
}
// UpdateContainerID updates a containers ID
func (dc *DockerContainer) UpdateContainerID(containerID string) {
dc.ContainerID = containerID
}
// APIclient is meli's client to interact with the docker daemon server
type APIclient interface {
// we implement this interface so that we can be able to mock it in tests
// https://medium.com/@zach_4342/dependency-injection-in-golang-e587c69478a8
ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error)
ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error)
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error)
ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error
ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error)
NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error)
NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error)
NetworkConnect(ctx context.Context, networkID, containerID string, config *network.EndpointSettings) error
VolumeCreate(ctx context.Context, options volume.VolumeCreateBody) (types.Volume, error)
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error
}
type imageProgress struct {
Status string `json:"status,omitempty"`
Stream string `json:"stream,omitempty"`
Progress string `json:"progress,omitempty"`
ProgressDetail string `json:"progressDetail,omitempty"`
}
type mockDockerClient struct{}
func (m *mockDockerClient) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBuffer([]byte("Pulling from library/testImage"))), nil
}
func (m *mockDockerClient) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
return types.ImageBuildResponse{Body: ioutil.NopCloser(bytes.NewBuffer([]byte("BUILT library/testImage"))), OSType: "linux baby!"}, nil
}
func (m *mockDockerClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
return container.ContainerCreateCreatedBody{ID: "myContainerId001"}, nil
}
func (m *mockDockerClient) ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error {
return nil
}
func (m *mockDockerClient) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBuffer([]byte("SHOWING LOGS for library/testImage"))), nil
}
func (m *mockDockerClient) NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
return []types.NetworkResource{}, nil
}
func (m *mockDockerClient) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) {
return types.NetworkCreateResponse{ID: "myNetworkId002"}, nil
}
func (m *mockDockerClient) NetworkConnect(ctx context.Context, networkID, containerID string, config *network.EndpointSettings) error {
return nil
}
func (m *mockDockerClient) VolumeCreate(ctx context.Context, options volume.VolumeCreateBody) (types.Volume, error) {
return types.Volume{Name: "MyVolume007"}, nil
}
func (m *mockDockerClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
return []types.Container{{ID: "myExistingContainerId00912"}}, nil
}
func (m *mockDockerClient) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {
return nil
}