Skip to content

Commit 56e2f40

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

File tree

3 files changed

+199
-6
lines changed

3 files changed

+199
-6
lines changed

test/daemon/daemon.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/alibaba/pouch/test/command"
1414
"github.com/alibaba/pouch/test/util"
15+
"github.com/gotestyourself/gotestyourself/icmd"
1516
)
1617

1718
// For pouch deamon test, we launched another pouch daemon.
@@ -99,6 +100,9 @@ func (d *Config) IsDaemonUp() bool {
99100
// if pouchd is started with -l option, use the first listen address
100101
for _, v := range d.Args {
101102
if strings.Contains(v, "-l") || strings.Contains(v, "--listen") {
103+
if strings.Contains(v, "--listen-cri") {
104+
continue
105+
}
102106
var sock string
103107
if strings.Contains(v, "=") {
104108
sock = strings.Split(v, "=")[1]
@@ -148,7 +152,17 @@ func (d *Config) StartDaemon() error {
148152
if util.WaitTimeout(time.Duration(d.timeout)*time.Second, d.IsDaemonUp) == false {
149153
if d.Debug == true {
150154
d.DumpLog()
155+
156+
fmt.Printf("\n")
151157
fmt.Printf("Failed to launch pouchd:%v\n", d.Args)
158+
159+
fmt.Printf("\n")
160+
cmd := "ps aux |grep pouchd"
161+
fmt.Printf("List pouchd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
162+
163+
fmt.Printf("\n")
164+
cmd = "ps aux |grep containerd"
165+
fmt.Printf("List containerd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
152166
}
153167

154168
d.KillDaemon()
@@ -164,9 +178,9 @@ func (d *Config) DumpLog() {
164178

165179
content, err := ioutil.ReadFile(d.LogPath)
166180
if err != nil {
167-
fmt.Printf("failed to read log, err:%s", err)
181+
fmt.Printf("failed to read log, err: %s\n", err)
168182
}
169-
fmt.Printf("pouch daemon log contents: %s", content)
183+
fmt.Printf("pouch daemon log contents:\n %s\n", content)
170184
}
171185

172186
// KillDaemon kill pouchd.

test/util_daemon.go

Lines changed: 29 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"
@@ -49,6 +52,9 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result {
4952
// Find the first -l or --listen parameter and use it.
5053
for _, v := range d.Args {
5154
if strings.Contains(v, "-l") || strings.Contains(v, "--listen") {
55+
if strings.Contains(v, "--listen-cri") {
56+
continue
57+
}
5258
if strings.Contains(v, "=") {
5359
sock = strings.Split(v, "=")[1]
5460
} else {
@@ -60,3 +66,26 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result {
6066
args := append(append([]string{"--host"}, sock), cmd...)
6167
return command.PouchRun(args...)
6268
}
69+
70+
// CreateConfigFile create configuration file and marshal cfg.
71+
func CreateConfigFile(path string, cfg interface{}) error {
72+
idx := strings.LastIndex(path, "/")
73+
if _, err := os.Stat(path[0:idx]); os.IsNotExist(err) {
74+
os.Mkdir(path[0:idx], os.ModePerm)
75+
}
76+
77+
file, err := os.Create(path)
78+
if err != nil {
79+
return err
80+
}
81+
82+
s, err := json.Marshal(cfg)
83+
if err != nil {
84+
return err
85+
}
86+
fmt.Fprintf(file, "%s", s)
87+
file.Sync()
88+
89+
defer file.Close()
90+
return nil
91+
}

test/z_cli_daemon_test.go

Lines changed: 154 additions & 4 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
)
@@ -72,26 +76,115 @@ func (suite *PouchDaemonSuite) TestDaemonCgroupParent(c *check.C) {
7276
func (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.
93101
func (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

Comments
 (0)