@@ -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,115 @@ 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
8693 command .PouchRun ("--host" , addr , "version" ).Assert (c , icmd .Success )
8794
8895 dcfg .KillDaemon ()
96+ defer dcfg .KillDaemon ()
8997 }
9098}
9199
92- // TestDaemonConfigFile tests start daemon with configfile works.
100+ // TestDaemonConfigFile tests start daemon with configure file works.
93101func (suite * PouchDaemonSuite ) TestDaemonConfigFile (c * check.C ) {
94- // TODO
102+ configFile := "/tmp/pouch.json"
103+ file , err := os .Create (configFile )
104+ c .Assert (err , check .IsNil )
105+ defer file .Close ()
106+ defer os .Remove (configFile )
107+
108+ // Unmarshal config.Config, all fields in this struct could be handled in configuration file.
109+ cfg := config.Config {
110+ Debug : true ,
111+ }
112+ s , _ := json .Marshal (cfg )
113+ fmt .Fprintf (file , "%s" , s )
114+ file .Sync ()
115+
116+ // TODO: uncomment this when issue #1003 is fixed.
117+ //dcfg, err := StartDefaultDaemonDebug("--config-file="+configFile)
118+ //{
119+ // err := dcfg.StartDaemon()
120+ // c.Assert(err, check.IsNil)
121+ //}
122+ //
123+ //// TODO: verify more
124+ //
125+ //// Must kill it, as we may loose the pid in next call.
126+ //defer dcfg.KillDaemon()
127+
128+ // config file cowork with parameter, no confilct
129+ }
130+
131+ // TestDaemonConfigFileConfilct tests start daemon with configure file confilicts with parameter.
132+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileConfilct (c * check.C ) {
133+ path := "/tmp/pouch.json"
134+ cfg := struct {
135+ ContainerdPath string `json:"containerd-path"`
136+ }{
137+ ContainerdPath : "abc" ,
138+ }
139+ err := CreateConfigFile (path , cfg )
140+ c .Assert (err , check .IsNil )
141+ defer os .Remove (path )
142+
143+ dcfg , err := StartDefaultDaemon ("--containerd-path" , "def" , "--config-file=" + path )
144+ dcfg .KillDaemon ()
145+ c .Assert (err , check .NotNil )
146+ }
147+
148+ // TestDaemonConfigFileUnknownFlag tests start daemon with unknow flags in configure file.
149+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileUnknownFlag (c * check.C ) {
150+ path := "/tmp/pouch.json"
151+ cfg := struct {
152+ Adsj string `json:"adsj"`
153+ }{
154+ Adsj : "xxx" ,
155+ }
156+ err := CreateConfigFile (path , cfg )
157+ c .Assert (err , check .IsNil )
158+ defer os .Remove (path )
159+
160+ dcfg , err := StartDefaultDaemon ("--debug" , "--config-file=" + path )
161+ c .Assert (err , check .NotNil )
162+ dcfg .KillDaemon ()
163+ }
164+
165+ // TestDaemonConfigFileAndCli tests start daemon with configure file and CLI .
166+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileAndCli (c * check.C ) {
167+ // Check default configure file could work
168+
169+ // TODO: uncomment if issue #1003 is fixed
170+ //path := "/etc/pouch/config.json"
171+ //cfg := struct {
172+ // Labels []string `json:"labels,omitempty"`
173+ //}{
174+ // Labels: []string{"a=b"},
175+ //}
176+ //err := CreateConfigFile(path, cfg)
177+ //c.Assert(err, check.IsNil)
178+ //defer os.Remove(path)
179+ //
180+ //// Do Not specify configure file explicitly, it should work.
181+ //dcfg, err := StartDefaultDaemonDebug()
182+ //c.Assert(err, check.IsNil)
183+ //defer dcfg.KillDaemon()
184+ //
185+ //result := RunWithSpecifiedDaemon(dcfg, "info")
186+ //err = util.PartialEqual(result.Stdout(), "a=b")
187+ //c.Assert(err, check.IsNil)
95188}
96189
97190// TestDaemonInvalideArgs tests invalid args in deamon return error
@@ -142,3 +235,60 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
142235 }
143236 c .Assert (string (result .State .Status ), check .Equals , "running" )
144237}
238+
239+ // TestDaemonLabel tests start daemon with label works.
240+ func (suite * PouchDaemonSuite ) TestDaemonLabel (c * check.C ) {
241+ dcfg , err := StartDefaultDaemonDebug ("--label" , "a=b" )
242+ // Start a test daemon with test args.
243+ if err != nil {
244+ c .Skip ("deamon start failed." )
245+ }
246+ // Must kill it, as we may loose the pid in next call.
247+ defer dcfg .KillDaemon ()
248+
249+ result := RunWithSpecifiedDaemon (dcfg , "info" )
250+ err = util .PartialEqual (result .Stdout (), "a=b" )
251+ c .Assert (err , check .IsNil )
252+ }
253+
254+ // TestDaemonLabelDup tests start daemon with duplicated label works.
255+ func (suite * PouchDaemonSuite ) TestDaemonLabelDup (c * check.C ) {
256+ dcfg , err := StartDefaultDaemonDebug ("--label" , "a=b" , "--label" , "a=b" )
257+ // Start a test daemon with test args.
258+ if err != nil {
259+ c .Skip ("deamon start failed." )
260+ }
261+ // Must kill it, as we may loose the pid in next call.
262+ defer dcfg .KillDaemon ()
263+
264+ result := RunWithSpecifiedDaemon (dcfg , "info" )
265+ err = util .PartialEqual (result .Stdout (), "a=b" )
266+ c .Assert (err , check .IsNil )
267+
268+ cnt := strings .Count (result .Stdout (), "a=b" )
269+ c .Assert (cnt , check .Equals , 1 )
270+ }
271+
272+ // TestDaemonLabelNeg tests start daemon with wrong label could not work.
273+ func (suite * PouchDaemonSuite ) TestDaemonLabelNeg (c * check.C ) {
274+ _ , err := StartDefaultDaemon ("--label" , "adsf" )
275+ c .Assert (err , check .NotNil )
276+ }
277+
278+ // TestDaemonDefaultRegistry tests set default registry works.
279+ func (suite * PouchDaemonSuite ) TestDaemonDefaultRegistry (c * check.C ) {
280+ dcfg , err := StartDefaultDaemonDebug (
281+ "--default-registry" ,
282+ "reg.docker.alibaba-inc.com" ,
283+ "--default-registry-namespace" ,
284+ "base" )
285+ c .Assert (err , check .IsNil )
286+
287+ // Check pull image with default registry using the registry specified in daemon.
288+ RunWithSpecifiedDaemon (dcfg , "pull" , "hello-world" ).Assert (c , icmd .Success )
289+ result := RunWithSpecifiedDaemon (dcfg , "images" )
290+ err = util .PartialEqual (result .Stdout (), "reg.docker.alibaba-inc.com/base/hello-world" )
291+ c .Assert (err , check .IsNil )
292+
293+ defer dcfg .KillDaemon ()
294+ }
0 commit comments