forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
z_cli_daemon_test.go
508 lines (437 loc) · 14.8 KB
/
z_cli_daemon_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/daemon"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/util"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
// PouchDaemonSuite is the test suite fo daemon.
type PouchDaemonSuite struct{}
func init() {
check.Suite(&PouchDaemonSuite{})
}
// SetUpTest does common setup in the beginning of each test.
func (suite *PouchDaemonSuite) SetUpTest(c *check.C) {
SkipIfFalse(c, environment.IsLinux)
}
// TestDaemonCgroupParent tests daemon with cgroup parent
func (suite *PouchDaemonSuite) TestDaemonCgroupParent(c *check.C) {
dcfg, err := StartDefaultDaemonDebug("--cgroup-parent=tmp")
if err != nil {
c.Skip("deamon start failed")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()
cname := "TestDaemonCgroupParent"
{
result := RunWithSpecifiedDaemon(dcfg, "pull", busyboxImage)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("pull image failed, err:%v", result)
}
}
{
result := RunWithSpecifiedDaemon(dcfg, "run",
"-d", "--name", cname, busyboxImage, "top")
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("run container failed, err:%v", result)
}
}
defer RunWithSpecifiedDaemon(dcfg, "rm", "-f", cname)
// test if the value is in inspect result
output := RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
//// test if cgroup has the right parent path
//path := fmt.Sprintf("/sys/fs/cgroup/memory/tmp/%s", result.ID)
//_, err = os.Stat(path)
//if err != nil {
// daemon.DConfig.DumpLog()
// c.Fatalf("get cgroup path failed, err:%s", err)
//}
}
// TestDaemonListenTCP tests daemon listen with tcp address.
func (suite *PouchDaemonSuite) TestDaemonListenTCP(c *check.C) {
// Start a test daemon with test args.
listeningPorts := [][]string{
{"0.0.0.0", "0.0.0.0", "1236"},
{"127.0.0.1", "127.0.0.1", "1234"},
{"localhost", "127.0.0.1", "1235"},
}
for _, hostDirective := range listeningPorts {
addr := fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2])
dcfg := daemon.NewConfig()
dcfg.Listen = ""
dcfg.NewArgs("--listen=" + addr)
dcfg.Debug = true
err := dcfg.StartDaemon()
c.Assert(err, check.IsNil)
// verify listen to tcp works
result := command.PouchRun("--host", addr, "version")
dcfg.KillDaemon()
result.Assert(c, icmd.Success)
}
}
//// TestDaemonConfigFile tests start daemon with configure file works.
//func (suite *PouchDaemonSuite) TestDaemonConfigFile(c *check.C) {
// path := "/tmp/pouch.json"
//
// // Unmarshal config.Config, all fields in this struct could be handled in configuration file.
// cfg := config.Config{
// Debug: true,
// }
// err := CreateConfigFile(path, cfg)
// c.Assert(err, check.IsNil)
// defer os.Remove(path)
//
// dcfg, err := StartDefaultDaemonDebug("--config-file=" + path)
// {
// err := dcfg.StartDaemon()
// c.Assert(err, check.IsNil)
// }
//
// // Must kill it, as we may loose the pid in next call.
// defer dcfg.KillDaemon()
//}
// TestDaemonConfigFileConflict tests start daemon with configure file conflicts with parameter.
func (suite *PouchDaemonSuite) TestDaemonConfigFileConflict(c *check.C) {
path := "/tmp/pouch.json"
cfg := struct {
ContainerdPath string `json:"containerd-path"`
}{
ContainerdPath: "abc",
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)
dcfg, err := StartDefaultDaemon("--containerd-path", "def", "--config-file="+path)
dcfg.KillDaemon()
c.Assert(err, check.NotNil)
}
// TestDaemonNestObjectConflict tests start daemon with configure file contains nest objects conflicts with parameter.
func (suite *PouchDaemonSuite) TestDaemonNestObjectConflict(c *check.C) {
path := "/tmp/pouch_nest.json"
type TLSConfig struct {
CA string `json:"tlscacert,omitempty"`
Cert string `json:"tlscert,omitempty"`
Key string `json:"tlskey,omitempty"`
VerifyRemote bool `json:"tlsverify"`
ManagerWhiteList string `json:"manager-whitelist"`
}
cfg := struct {
TLS TLSConfig
}{
TLS: TLSConfig{
CA: "ca",
Cert: "cert",
Key: "key",
},
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)
dcfg, err := StartDefaultDaemon("--tlscacert", "ca", "--config-file="+path)
dcfg.KillDaemon()
c.Assert(err, check.NotNil)
}
// TestDaemonSliceFlagNotConflict tests start daemon with configure file contains slice flag will not conflicts with parameter.
func (suite *PouchDaemonSuite) TestDaemonSliceFlagNotConflict(c *check.C) {
path := "/tmp/pouch_slice.json"
cfg := struct {
Labels []string `json:"label"`
}{
Labels: []string{"a=a", "b=b"},
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)
dcfg, err := StartDefaultDaemon("--label", "c=d", "--config-file="+path)
dcfg.KillDaemon()
c.Assert(err, check.IsNil)
}
// TestDaemonConfigFileUnknownFlag tests start daemon with unknown flags in configure file.
func (suite *PouchDaemonSuite) TestDaemonConfigFileUnknownFlag(c *check.C) {
path := "/tmp/pouch.json"
cfg := struct {
Adsj string `json:"adsj"`
}{
Adsj: "xxx",
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)
dcfg, err := StartDefaultDaemon("--debug", "--config-file="+path)
c.Assert(err, check.NotNil)
dcfg.KillDaemon()
}
// TestDaemonConfigFileAndCli tests start daemon with configure file and CLI .
func (suite *PouchDaemonSuite) TestDaemonConfigFileAndCli(c *check.C) {
// Check default configure file could work
path := "/etc/pouch/config.json"
cfg := struct {
Labels []string `json:"label,omitempty"`
}{
Labels: []string{"a=b"},
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)
// Do Not specify configure file explicitly, it should work.
dcfg, err := StartDefaultDaemonDebug()
c.Assert(err, check.IsNil)
defer dcfg.KillDaemon()
result := RunWithSpecifiedDaemon(dcfg, "info")
err = util.PartialEqual(result.Stdout(), "a=b")
c.Assert(err, check.IsNil)
}
// TestDaemonInvalideArgs tests invalid args in deamon return error
func (suite *PouchDaemonSuite) TestDaemonInvalideArgs(c *check.C) {
_, err := StartDefaultDaemon("--config=xxx")
c.Assert(err, check.NotNil)
}
// TestDaemonRestart tests daemon restart with running container.
func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
dcfg, err := StartDefaultDaemonDebug()
// Start a test daemon with test args.
if err != nil {
c.Skip("daemon start failed.")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()
{
result := RunWithSpecifiedDaemon(dcfg, "pull", busyboxImage)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("pull image failed, err:%v", result)
}
}
cname := "TestDaemonRestart"
{
result := RunWithSpecifiedDaemon(dcfg, "run", "-d", "--name", cname,
"-p", "1234:80",
busyboxImage, "top")
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("run container failed, err:%v", result)
}
}
defer RunWithSpecifiedDaemon(dcfg, "rm", "-f", cname)
// restart daemon
err = RestartDaemon(dcfg)
c.Assert(err, check.IsNil)
// test if the container is running.
output := RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Fatalf("failed to decode inspect output: %v", err)
}
c.Assert(string(result[0].State.Status), check.Equals, "running")
}
// TestDaemonRestartWithPausedContainer tests daemon with paused container.
func (suite *PouchDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
dcfg, err := StartDefaultDaemonDebug()
//Start a test daemon with test args.
if err != nil {
c.Skip("daemon start failed")
}
defer dcfg.KillDaemon()
{
result := RunWithSpecifiedDaemon(dcfg, "pull", busyboxImage)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("pull image failed, err: %v", result)
}
}
cname := "TestDaemonRestartWithPausedContainer"
{
result := RunWithSpecifiedDaemon(dcfg, "run", "-d", "--name", cname,
"-p", "5678:80", busyboxImage, "top")
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("run container failed, err: %v", result)
}
// pause the container
result = RunWithSpecifiedDaemon(dcfg, "pause", cname)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("pause container failed, err: %v", result)
}
}
defer RunWithSpecifiedDaemon(dcfg, "rm", "-f", cname)
// restart daemon
err = RestartDaemon(dcfg)
c.Assert(err, check.IsNil)
// test if the container is paused.
output := RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout()
data := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &data); err != nil {
c.Fatalf("failed to decode inspect output: %v", err)
}
c.Assert(string(data[0].State.Status), check.Equals, "paused")
// unpause the container
result := RunWithSpecifiedDaemon(dcfg, "unpause", cname)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("unpause container failed, err: %v", result)
}
//test if the container is running
output = RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout()
if err := json.Unmarshal([]byte(output), &data); err != nil {
c.Fatalf("failed to decode inspect output: %v", err)
}
c.Assert(string(data[0].State.Status), check.Equals, "running")
}
// TestDaemonLabel tests start daemon with label works.
func (suite *PouchDaemonSuite) TestDaemonLabel(c *check.C) {
dcfg, err := StartDefaultDaemonDebug("--label", "a=b")
// Start a test daemon with test args.
if err != nil {
c.Skip("deamon start failed.")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()
result := RunWithSpecifiedDaemon(dcfg, "info")
err = util.PartialEqual(result.Stdout(), "a=b")
c.Assert(err, check.IsNil)
}
// TestDaemonLabelDup tests start daemon with duplicated label works.
func (suite *PouchDaemonSuite) TestDaemonLabelDup(c *check.C) {
dcfg, err := StartDefaultDaemonDebug("--label", "a=b", "--label", "a=b")
// Start a test daemon with test args.
if err != nil {
c.Skip("deamon start failed.")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()
result := RunWithSpecifiedDaemon(dcfg, "info")
err = util.PartialEqual(result.Stdout(), "a=b")
c.Assert(err, check.IsNil)
cnt := strings.Count(result.Stdout(), "a=b")
c.Assert(cnt, check.Equals, 1)
}
// TestDaemonLabelNeg tests start daemon with wrong label could not work.
func (suite *PouchDaemonSuite) TestDaemonLabelNeg(c *check.C) {
_, err := StartDefaultDaemon("--label", "adsf")
c.Assert(err, check.NotNil)
}
// TestDaemonDefaultRegistry tests set default registry works.
func (suite *PouchDaemonSuite) TestDaemonDefaultRegistry(c *check.C) {
dcfg, err := StartDefaultDaemonDebug(
"--default-registry",
"reg.docker.alibaba-inc.com",
"--default-registry-namespace",
"base")
c.Assert(err, check.IsNil)
// Check pull image with default registry using the registry specified in daemon.
result := RunWithSpecifiedDaemon(dcfg, "pull", "hello-world")
err = util.PartialEqual(result.Combined(), "reg.docker.alibaba-inc.com/base/hello-world")
c.Assert(err, check.IsNil)
defer dcfg.KillDaemon()
}
// TestDaemonTlsVerify tests start daemon with TLS verification enabled.
func (suite *PouchDaemonSuite) TestDaemonTlsVerify(c *check.C) {
SkipIfFalse(c, IsTLSExist)
dcfg := daemon.NewConfig()
dcfg.Listen = ""
dcfg.NewArgs("--listen=" + testDaemonHTTPSAddr)
dcfg.Args = append(dcfg.Args,
"--tlsverify",
"--tlscacert="+serverCa,
"--tlscert="+serverCert,
"--tlskey="+serverKey)
dcfg.Debug = false
// Skip error check, because the function to check daemon up using CLI without TLS info.
dcfg.StartDaemon()
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()
// Use TLS could success
result := RunWithSpecifiedDaemon(&dcfg,
"--tlscacert="+clientCa,
"--tlscert="+clientCert,
"--tlskey="+clientKey, "version")
result.Assert(c, icmd.Success)
// Do not use TLS should fail
result = RunWithSpecifiedDaemon(&dcfg, "version")
c.Assert(result.ExitCode, check.Equals, 1)
err := util.PartialEqual(result.Stderr(), "malformed HTTP response")
c.Assert(err, check.IsNil)
{
// Use wrong CA should fail
result := RunWithSpecifiedDaemon(&dcfg,
"--tlscacert="+clientWrongCa,
"--tlscert="+clientCert,
"--tlskey="+clientKey, "version")
c.Assert(result.ExitCode, check.Equals, 1)
err := util.PartialEqual(result.Stderr(), "failed to append certificates")
c.Assert(err, check.IsNil)
}
}
// TestDaemonStartOverOneTimes tests start daemon over one times should fail.
func (suite *PouchDaemonSuite) TestDaemonStartOverOneTimes(c *check.C) {
dcfg1 := daemon.NewConfig()
dcfg1.Listen = ""
addr1 := "unix:///var/run/pouchtest1.sock"
dcfg1.NewArgs("--listen=" + addr1)
err := dcfg1.StartDaemon()
c.Assert(err, check.IsNil)
// verify listen to tcp works
command.PouchRun("--host", addr1, "version").Assert(c, icmd.Success)
defer dcfg1.KillDaemon()
// test second daemon with same pidfile should start fail
dcfg2 := daemon.NewConfig()
dcfg2.Listen = ""
addr2 := "unix:///var/run/pouchtest2.sock"
dcfg2.NewArgs("--listen=" + addr2)
err = dcfg2.StartDaemon()
c.Assert(err, check.NotNil)
}
// TestDaemonWithMultiRuntimes tests start daemon with multiple runtimes
func (suite *PouchDaemonSuite) TestDaemonWithMultiRuntimes(c *check.C) {
dcfg1, err := StartDefaultDaemonDebug(
"--add-runtime", "foo=bar")
c.Assert(err, check.IsNil)
dcfg1.KillDaemon()
// should fail if runtime name equal
dcfg2, err := StartDefaultDaemonDebug(
"--add-runtime", "runa=runa",
"--add-runtime", "runa=runa")
c.Assert(err, check.NotNil)
dcfg2.KillDaemon()
}
// TestUpdateDaemonWithLabels tests update daemon online with labels updated
func (suite *PouchDaemonSuite) TestUpdateDaemonWithLabels(c *check.C) {
cfg := daemon.NewConfig()
err := cfg.StartDaemon()
c.Assert(err, check.IsNil)
defer cfg.KillDaemon()
RunWithSpecifiedDaemon(&cfg, "updatedaemon", "--label", "aaa=bbb").Assert(c, icmd.Success)
ret := RunWithSpecifiedDaemon(&cfg, "info")
ret.Assert(c, icmd.Success)
updated := strings.Contains(ret.Stdout(), "aaa=bbb")
c.Assert(updated, check.Equals, true)
}
// TestUpdateDaemonWithLabels tests update daemon offline
func (suite *PouchDaemonSuite) TestUpdateDaemonOffline(c *check.C) {
path := "/tmp/pouchconfig.json"
fd, err := os.Create(path)
c.Assert(err, check.IsNil)
fd.Close()
defer os.Remove(path)
cfg := daemon.NewConfig()
err = cfg.StartDaemon()
c.Assert(err, check.IsNil)
defer cfg.KillDaemon()
RunWithSpecifiedDaemon(&cfg, "updatedaemon", "--config-file", path, "--offline=true").Assert(c, icmd.Success)
ret := RunWithSpecifiedDaemon(&cfg, "info")
ret.Assert(c, icmd.Success)
}