forked from mijia/adoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
230 lines (205 loc) · 5.8 KB
/
client_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
package adoc
import (
"fmt"
"testing"
)
func TestVersionAndInfo(t *testing.T) {
version, err := docker.Version()
if err != nil {
t.Fatalf("Cannot get docker version, %s", err)
}
d("ApiVersion", version)
if docker.IsSwarm() {
info, err := docker.SwarmInfo()
if err != nil {
t.Fatalf("Cannot get docker info, %s", err)
}
d("SwarmInfo", info)
} else {
info, err := docker.Info()
if err != nil {
t.Fatalf("Cannot get docker info, %s", err)
}
d("DockerInfo", info)
}
pong, err := docker.Ping()
if err != nil || !pong {
t.Fatalf("Cannot ping the docker, %s", err)
}
}
func TestContainers(t *testing.T) {
fmt.Println("\n\n\n")
containerConf := ContainerConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"python", "app.py"},
Image: "training/webapp",
}
id, err := docker.CreateContainer(containerConf, HostConfig{})
if err != nil {
t.Fatalf("Cannot create the container, %s", err)
}
if err := docker.StartContainer(id); err != nil {
t.Fatalf("Cannot start the container, %s", err)
}
containers, err := docker.ListContainers(true, true, "")
if err != nil {
t.Fatalf("Cannot list containers, %s", err)
}
d("Containers", containers)
if err := docker.RenameContainer(id, "hello_renamed"); err != nil {
t.Fatalf("Cannot rename the container, %s", err)
}
container, err := docker.InspectContainer(id)
if err != nil {
t.Fatalf("Cannot inpsect container, id=%s, %s", id, err)
}
d("Container", container)
if container.Name != "/hello_renamed" {
t.Fatalf("Rename failed, need=%s, but got=%s", "hello_renamed", container.Name)
}
if changes, err := docker.ContainerChanges(id); err != nil {
t.Fatalf("Cannot get container changes, %s", err)
} else {
d("Container Changes", changes)
}
if logs, err := docker.ContainerLogs(id, true, true, false); err != nil {
t.Fatalf("Cannot get logs from the container, %s", err)
} else {
d("Container Logs", logs)
}
if err := docker.RemoveContainer(id, true, false); err != nil {
t.Fatalf("Cannot remove the container, %s", err)
}
}
func TestContainerCtls(t *testing.T) {
fmt.Println("\n\n\n")
containerConf := ContainerConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"python", "app.py"},
Image: "training/webapp",
}
hostConf := HostConfig{
PortBindings: map[string][]PortBinding{
"5000/tcp": []PortBinding{
PortBinding{},
},
},
}
id, err := docker.CreateContainer(containerConf, hostConf, "test_container")
if err != nil {
t.Fatalf("Cannot create the container, %s", err)
}
d("Created Container ID", id)
if err := docker.StartContainer(id); err != nil {
t.Fatalf("Cannot start the container, %s", err)
}
if procs, err := docker.ContainerProcesses(id); err != nil {
t.Fatalf("Cannot get top processes from the container, %s", err)
} else {
d("Processes", procs)
}
if stats, err := docker.ContainerStats(id); err != nil {
t.Fatalf("Cannot get stats from the container, %s", err)
} else {
d("Container Stats", stats)
}
if err := docker.PauseContainer(id); err != nil {
t.Fatalf("Cannot pause the container, %s", err)
}
if err := docker.UnpauseContainer(id); err != nil {
t.Fatalf("Cannot unpause the container, %s", err)
}
//if code, err := docker.WaitContainer(id); err != nil {
//t.Fatalf("Cannot wait on the container, %s", err)
//} else {
//d("Container Return", code)
//}
if err := docker.StopContainer(id); err != nil {
t.Fatalf("Cannot stop the container, %s", err)
}
if err := docker.RestartContainer(id, 5); err != nil {
t.Fatalf("Cannot restart the container, %s", err)
}
if err := docker.KillContainer(id); err != nil {
t.Fatalf("Cannot kill the container, %s", err)
}
if err := docker.RemoveContainer(id, true, false); err != nil {
t.Fatalf("Cannot remove the container, %s", err)
}
}
func TestContainerExec(t *testing.T) {
containerConf := ContainerConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"python", "app.py"},
Image: "training/webapp",
}
hostConf := HostConfig{
PortBindings: map[string][]PortBinding{
"5000/tcp": []PortBinding{
PortBinding{},
},
},
}
id, err := docker.CreateContainer(containerConf, hostConf)
if err != nil {
t.Fatalf("Cannot create the container, %s", err)
}
d("Created Container ID", id)
if err := docker.StartContainer(id); err != nil {
t.Fatalf("Cannot start the container, %s", err)
}
execConfig := ExecConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"ls", "-l", "/"},
}
execId, err := docker.CreateExec(id, execConfig)
if err != nil {
t.Fatalf("Cannot create exec inside container, %s", err)
}
d("Exec Id", execId)
if data, err := docker.StartExec(execId, false, true); err != nil {
t.Fatalf("Cannot start the exec inside container, %s", err)
} else {
d("Exec Results", string(data))
}
if err := docker.RemoveContainer(id, true, false); err != nil {
t.Fatalf("Cannot remove the container, %s", err)
}
}
func TestImages(t *testing.T) {
fmt.Println("\n\n\n")
if images, err := docker.ListImages(false); err != nil {
t.Fatalf("Cannot list all the images, %s", err)
} else {
d("Images", images)
}
if image, err := docker.InspectImage("busybox"); err != nil {
t.Fatalf("Cannot inspect image busybox, %s", err)
} else {
d("Image Detail", image)
}
if err := docker.RemoveImage("busybox", false, false); err != nil {
t.Fatalf("Cannot remove the image, %s", err)
}
if err := docker.PullImage("busybox", "latest"); err != nil {
t.Fatalf("Cannot pull the image, %s", err)
}
}
func d(msg string, o interface{}) {
fmt.Println(msg)
fmt.Printf("%+v\n", o)
}
var docker *DockerClient
func init() {
EnableDebug()
var err error
//docker, err = NewDockerClient("tcp://192.168.51.21:2375", nil)
docker, err = NewSwarmClient("tcp://192.168.51.21:8178", nil)
if err != nil {
fmt.Println(err)
}
}