@@ -3,12 +3,16 @@ package main
33import (
44 "encoding/json"
55 "fmt"
6+ "os"
7+ "strings"
68
79 "github.com/alibaba/pouch/apis/types"
10+ "github.com/alibaba/pouch/daemon/config"
811 "github.com/alibaba/pouch/test/command"
912 "github.com/alibaba/pouch/test/daemon"
1013 "github.com/alibaba/pouch/test/environment"
1114
15+ "github.com/alibaba/pouch/test/util"
1216 "github.com/go-check/check"
1317 "github.com/gotestyourself/gotestyourself/icmd"
1418)
@@ -72,26 +76,114 @@ func (suite *PouchDaemonSuite) TestDaemonCgroupParent(c *check.C) {
7276func (suite * PouchDaemonSuite ) TestDaemonListenTCP (c * check.C ) {
7377 // Start a test daemon with test args.
7478 listeningPorts := [][]string {
75- {"0.0.0.0" , "0.0.0.0" , "5678 " },
79+ {"0.0.0.0" , "0.0.0.0" , "1236 " },
7680 {"127.0.0.1" , "127.0.0.1" , "1234" },
7781 {"localhost" , "127.0.0.1" , "1235" },
7882 }
7983
8084 for _ , hostDirective := range listeningPorts {
8185 addr := fmt .Sprintf ("tcp://%s:%s" , hostDirective [0 ], hostDirective [2 ])
82- dcfg , err := StartDefaultDaemonDebug ("--listen=" + addr )
86+ dcfg := daemon .NewConfig ()
87+ dcfg .Listen = ""
88+ dcfg .NewArgs ("--listen=" + addr )
89+ err := dcfg .StartDaemon ()
8390 c .Assert (err , check .IsNil )
8491
8592 // verify listen to tcp works
86- command .PouchRun ("--host" , addr , "version" ).Assert (c , icmd .Success )
87-
93+ result := command .PouchRun ("--host" , addr , "version" )
8894 dcfg .KillDaemon ()
95+ result .Assert (c , icmd .Success )
8996 }
9097}
9198
92- // TestDaemonConfigFile tests start daemon with configfile works.
99+ // TestDaemonConfigFile tests start daemon with configure file works.
93100func (suite * PouchDaemonSuite ) TestDaemonConfigFile (c * check.C ) {
94- // TODO
101+ configFile := "/tmp/pouch.json"
102+ file , err := os .Create (configFile )
103+ c .Assert (err , check .IsNil )
104+ defer file .Close ()
105+ defer os .Remove (configFile )
106+
107+ // Unmarshal config.Config, all fields in this struct could be handled in configuration file.
108+ cfg := config.Config {
109+ Debug : true ,
110+ }
111+ s , _ := json .Marshal (cfg )
112+ fmt .Fprintf (file , "%s" , s )
113+ file .Sync ()
114+
115+ // TODO: uncomment this when issue #1003 is fixed.
116+ //dcfg, err := StartDefaultDaemonDebug("--config-file="+configFile)
117+ //{
118+ // err := dcfg.StartDaemon()
119+ // c.Assert(err, check.IsNil)
120+ //}
121+ //
122+ //// TODO: verify more
123+ //
124+ //// Must kill it, as we may loose the pid in next call.
125+ //defer dcfg.KillDaemon()
126+
127+ // config file cowork with parameter, no confilct
128+ }
129+
130+ // TestDaemonConfigFileConfilct tests start daemon with configure file confilicts with parameter.
131+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileConfilct (c * check.C ) {
132+ path := "/tmp/pouch.json"
133+ cfg := struct {
134+ ContainerdPath string `json:"containerd-path"`
135+ }{
136+ ContainerdPath : "abc" ,
137+ }
138+ err := CreateConfigFile (path , cfg )
139+ c .Assert (err , check .IsNil )
140+ defer os .Remove (path )
141+
142+ dcfg , err := StartDefaultDaemon ("--containerd-path" , "def" , "--config-file=" + path )
143+ dcfg .KillDaemon ()
144+ c .Assert (err , check .NotNil )
145+ }
146+
147+ // TestDaemonConfigFileUnknownFlag tests start daemon with unknown flags in configure file.
148+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileUnknownFlag (c * check.C ) {
149+ path := "/tmp/pouch.json"
150+ cfg := struct {
151+ Adsj string `json:"adsj"`
152+ }{
153+ Adsj : "xxx" ,
154+ }
155+ err := CreateConfigFile (path , cfg )
156+ c .Assert (err , check .IsNil )
157+ defer os .Remove (path )
158+
159+ dcfg , err := StartDefaultDaemon ("--debug" , "--config-file=" + path )
160+ c .Assert (err , check .NotNil )
161+ dcfg .KillDaemon ()
162+ }
163+
164+ // TestDaemonConfigFileAndCli tests start daemon with configure file and CLI .
165+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileAndCli (c * check.C ) {
166+ // Check default configure file could work
167+
168+ // TODO: uncomment if issue #1003 is fixed
169+ //path := "/etc/pouch/config.json"
170+ //cfg := struct {
171+ // Labels []string `json:"labels,omitempty"`
172+ //}{
173+ // Labels: []string{"a=b"},
174+ //}
175+ //err := CreateConfigFile(path, cfg)
176+ //c.Assert(err, check.IsNil)
177+ //defer os.Remove(path)
178+ //
179+ //// Do Not specify configure file explicitly, it should work.
180+ //dcfg, err := StartDefaultDaemonDebug()
181+ //c.Assert(err, check.IsNil)
182+ //defer dcfg.KillDaemon()
183+ //
184+ //result := RunWithSpecifiedDaemon(dcfg, "info")
185+ //err = util.PartialEqual(result.Stdout(), "a=b")
186+ //c.Assert(err, check.IsNil)
95187}
96188
97189// TestDaemonInvalideArgs tests invalid args in deamon return error
@@ -120,7 +212,7 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
120212
121213 cname := "TestDaemonRestart"
122214 {
123- result := RunWithSpecifiedDaemon (dcfg , "run" , "--name" , cname ,
215+ result := RunWithSpecifiedDaemon (dcfg , "run" , "-d" , "- -name" , cname ,
124216 "-p" , "1234:80" ,
125217 busyboxImage )
126218 if result .ExitCode != 0 {
@@ -142,3 +234,59 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
142234 }
143235 c .Assert (string (result .State .Status ), check .Equals , "running" )
144236}
237+
238+ // TestDaemonLabel tests start daemon with label works.
239+ func (suite * PouchDaemonSuite ) TestDaemonLabel (c * check.C ) {
240+ dcfg , err := StartDefaultDaemonDebug ("--label" , "a=b" )
241+ // Start a test daemon with test args.
242+ if err != nil {
243+ c .Skip ("deamon start failed." )
244+ }
245+ // Must kill it, as we may loose the pid in next call.
246+ defer dcfg .KillDaemon ()
247+
248+ result := RunWithSpecifiedDaemon (dcfg , "info" )
249+ err = util .PartialEqual (result .Stdout (), "a=b" )
250+ c .Assert (err , check .IsNil )
251+ }
252+
253+ // TestDaemonLabelDup tests start daemon with duplicated label works.
254+ func (suite * PouchDaemonSuite ) TestDaemonLabelDup (c * check.C ) {
255+ dcfg , err := StartDefaultDaemonDebug ("--label" , "a=b" , "--label" , "a=b" )
256+ // Start a test daemon with test args.
257+ if err != nil {
258+ c .Skip ("deamon start failed." )
259+ }
260+ // Must kill it, as we may loose the pid in next call.
261+ defer dcfg .KillDaemon ()
262+
263+ result := RunWithSpecifiedDaemon (dcfg , "info" )
264+ err = util .PartialEqual (result .Stdout (), "a=b" )
265+ c .Assert (err , check .IsNil )
266+
267+ cnt := strings .Count (result .Stdout (), "a=b" )
268+ c .Assert (cnt , check .Equals , 1 )
269+ }
270+
271+ // TestDaemonLabelNeg tests start daemon with wrong label could not work.
272+ func (suite * PouchDaemonSuite ) TestDaemonLabelNeg (c * check.C ) {
273+ _ , err := StartDefaultDaemon ("--label" , "adsf" )
274+ c .Assert (err , check .NotNil )
275+ }
276+
277+ // TestDaemonDefaultRegistry tests set default registry works.
278+ func (suite * PouchDaemonSuite ) TestDaemonDefaultRegistry (c * check.C ) {
279+ dcfg , err := StartDefaultDaemonDebug (
280+ "--default-registry" ,
281+ "reg.docker.alibaba-inc.com" ,
282+ "--default-registry-namespace" ,
283+ "base" )
284+ c .Assert (err , check .IsNil )
285+
286+ // Check pull image with default registry using the registry specified in daemon.
287+ result := RunWithSpecifiedDaemon (dcfg , "pull" , "hello-world" )
288+ err = util .PartialEqual (result .Combined (), "reg.docker.alibaba-inc.com/base/hello-world" )
289+ c .Assert (err , check .IsNil )
290+
291+ defer dcfg .KillDaemon ()
292+ }
0 commit comments