@@ -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)
@@ -89,9 +93,89 @@ func (suite *PouchDaemonSuite) TestDaemonListenTCP(c *check.C) {
8993 }
9094}
9195
92- // TestDaemonConfigFile tests start daemon with configfile works.
96+ // TestDaemonConfigFile tests start daemon with configure file works.
9397func (suite * PouchDaemonSuite ) TestDaemonConfigFile (c * check.C ) {
94- // TODO
98+ configFile := "/tmp/pouch.json"
99+ file , err := os .Create (configFile )
100+ c .Assert (err , check .IsNil )
101+ defer file .Close ()
102+ defer os .Remove (configFile )
103+
104+ // Unmarshal config.Config, all fields in this struct could be handled in configuration file.
105+ cfg := config.Config {
106+ Debug : true ,
107+ }
108+ s , _ := json .Marshal (cfg )
109+ fmt .Fprintf (file , "%s" , s )
110+ file .Sync ()
111+
112+ // TODO: uncomment this when issue #1003 is fixed.
113+ //dcfg, err := StartDefaultDaemonDebug("--config-file="+configFile)
114+ //{
115+ // err := dcfg.StartDaemon()
116+ // c.Assert(err, check.IsNil)
117+ //}
118+ //
119+ //// TODO: verify more
120+ //
121+ //// Must kill it, as we may loose the pid in next call.
122+ //defer dcfg.KillDaemon()
123+
124+ // config file cowork with parameter, no confilct
125+ }
126+
127+ // TestDaemonConfigFileConfilct tests start daemon with configure file confilicts with parameter.
128+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileConfilct (c * check.C ) {
129+ path := "/tmp/pouch.json"
130+ cfg := struct {
131+ Debug bool `json:"debug,omitempty"`
132+ }{
133+ Debug : false ,
134+ }
135+ CreateConfigFile (path , cfg )
136+ defer os .Remove (path )
137+
138+ _ , err := StartDefaultDaemon ("--debug" , "--config-file=" + path )
139+ c .Assert (err , check .NotNil )
140+ util .PartialEqual (fmt .Sprintf ("%s" , err ), "conflict" )
141+ }
142+
143+ // TestDaemonConfigFileUnknownFlag tests start daemon with unknow flags in configure file.
144+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileUnknownFlag (c * check.C ) {
145+ path := "/tmp/pouch.json"
146+ cfg := struct {
147+ Adsj bool `json:"adsj,omitempty"`
148+ }{
149+ Adsj : false ,
150+ }
151+ CreateConfigFile (path , cfg )
152+ defer os .Remove (path )
153+
154+ _ , err := StartDefaultDaemon ("--debug" , "--config-file=" + path )
155+ c .Assert (err , check .NotNil )
156+ util .PartialEqual (fmt .Sprintf ("%s" , err ), "unknow" )
157+ }
158+
159+ // TestDaemonConfigFileAndCli tests start daemon with configure file and CLI .
160+ func (suite * PouchDaemonSuite ) TestDaemonConfigFileAndCli (c * check.C ) {
161+ // Check default configure file could work
162+ path := "/etc/pouch/config.json"
163+ cfg := struct {
164+ Labels []string `json:"labels,omitempty"`
165+ }{
166+ Labels : []string {"a=b" },
167+ }
168+ CreateConfigFile (path , cfg )
169+ defer os .Remove (path )
170+
171+ // Do Not specify configure file explicitly, it should work.
172+ dcfg , err := StartDefaultDaemon ()
173+ c .Assert (err , check .IsNil )
174+ defer dcfg .KillDaemon ()
175+
176+ result := RunWithSpecifiedDaemon (dcfg , "info" )
177+ err = util .PartialEqual (result .Stdout (), "a=b" )
178+ c .Assert (err , check .IsNil )
95179}
96180
97181// TestDaemonInvalideArgs tests invalid args in deamon return error
@@ -142,3 +226,42 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
142226 }
143227 c .Assert (string (result .State .Status ), check .Equals , "running" )
144228}
229+
230+ // TestDaemonLabel tests start daemon with label works.
231+ func (suite * PouchDaemonSuite ) TestDaemonLabel (c * check.C ) {
232+ dcfg , err := StartDefaultDaemonDebug ("--label a=b" )
233+ // Start a test daemon with test args.
234+ if err != nil {
235+ c .Skip ("deamon start failed." )
236+ }
237+ // Must kill it, as we may loose the pid in next call.
238+ defer dcfg .KillDaemon ()
239+
240+ result := RunWithSpecifiedDaemon (dcfg , "info" )
241+ err = util .PartialEqual (result .Stdout (), "a=b" )
242+ c .Assert (err , check .IsNil )
243+ }
244+
245+ // TestDaemonLabelDup tests start daemon with duplicated label works.
246+ func (suite * PouchDaemonSuite ) TestDaemonLabelDup (c * check.C ) {
247+ dcfg , err := StartDefaultDaemonDebug ("--label a=b --label a=b" )
248+ // Start a test daemon with test args.
249+ if err != nil {
250+ c .Skip ("deamon start failed." )
251+ }
252+ // Must kill it, as we may loose the pid in next call.
253+ defer dcfg .KillDaemon ()
254+
255+ result := RunWithSpecifiedDaemon (dcfg , "info" )
256+ err = util .PartialEqual (result .Stdout (), "a=b" )
257+ c .Assert (err , check .IsNil )
258+
259+ cnt := strings .Count (result .Stdout (), "a=b" )
260+ c .Assert (cnt , check .Equals , 1 )
261+ }
262+
263+ // TestDaemonLabelNeg tests start daemon with wrong label could not work.
264+ func (suite * PouchDaemonSuite ) TestDaemonLabelNeg (c * check.C ) {
265+ _ , err := StartDefaultDaemon ("--label adsf" )
266+ c .Assert (err , check .NotNil )
267+ }
0 commit comments