Skip to content

Commit c67f0c8

Browse files
authored
Merge pull request #1087 from Letty5411/0410
test: add tests for label and config file in daemon
2 parents 34c5a06 + 3cae2d1 commit c67f0c8

File tree

3 files changed

+198
-10
lines changed

3 files changed

+198
-10
lines changed

test/daemon/daemon.go

Lines changed: 14 additions & 3 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,14 @@ 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()
151-
fmt.Printf("Failed to launch pouchd:%v\n", d.Args)
155+
156+
fmt.Printf("\nFailed to launch pouchd:%v\n", d.Args)
157+
158+
cmd := "ps aux |grep pouchd"
159+
fmt.Printf("\nList pouchd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
160+
161+
cmd = "ps aux |grep containerd"
162+
fmt.Printf("\nList containerd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
152163
}
153164

154165
d.KillDaemon()
@@ -164,9 +175,9 @@ func (d *Config) DumpLog() {
164175

165176
content, err := ioutil.ReadFile(d.LogPath)
166177
if err != nil {
167-
fmt.Printf("failed to read log, err:%s", err)
178+
fmt.Printf("failed to read log, err: %s\n", err)
168179
}
169-
fmt.Printf("pouch daemon log contents: %s", content)
180+
fmt.Printf("pouch daemon log contents:\n %s\n", content)
170181
}
171182

172183
// 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: 155 additions & 7 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,114 @@ 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
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.
93100
func (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

Comments
 (0)