Skip to content

Commit 9a9a113

Browse files
committed
test: add tests for label and config file in daemon
Signed-off-by: letty <[email protected]>
1 parent 8d90aba commit 9a9a113

File tree

2 files changed

+151
-2
lines changed

2 files changed

+151
-2
lines changed

test/util_daemon.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
47
"strings"
58

69
"github.com/alibaba/pouch/test/command"
@@ -60,3 +63,26 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result {
6063
args := append(append([]string{"--host"}, sock), cmd...)
6164
return command.PouchRun(args...)
6265
}
66+
67+
// CreateConfigFile create configuration file and marshal cfg.
68+
func CreateConfigFile(path string, cfg interface{}) error {
69+
idx := strings.LastIndex(path, "/")
70+
if _, err := os.Stat(path[0:idx]); os.IsNotExist(err) {
71+
os.Mkdir(path[0:idx], os.ModePerm)
72+
}
73+
74+
file, err := os.Create(path)
75+
if err != nil {
76+
return err
77+
}
78+
79+
s, err := json.Marshal(cfg)
80+
if err != nil {
81+
return err
82+
}
83+
fmt.Fprintf(file, "%s", s)
84+
file.Sync()
85+
86+
defer file.Close()
87+
return nil
88+
}

test/z_cli_daemon_test.go

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package main
33
import (
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.
9397
func (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

Comments
 (0)