diff --git a/test/daemon/daemon.go b/test/daemon/daemon.go index 67da8c1cc..51876f677 100644 --- a/test/daemon/daemon.go +++ b/test/daemon/daemon.go @@ -12,6 +12,7 @@ import ( "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/util" + "github.com/gotestyourself/gotestyourself/icmd" ) // For pouch deamon test, we launched another pouch daemon. @@ -99,6 +100,9 @@ func (d *Config) IsDaemonUp() bool { // if pouchd is started with -l option, use the first listen address for _, v := range d.Args { if strings.Contains(v, "-l") || strings.Contains(v, "--listen") { + if strings.Contains(v, "--listen-cri") { + continue + } var sock string if strings.Contains(v, "=") { sock = strings.Split(v, "=")[1] @@ -148,7 +152,14 @@ func (d *Config) StartDaemon() error { if util.WaitTimeout(time.Duration(d.timeout)*time.Second, d.IsDaemonUp) == false { if d.Debug == true { d.DumpLog() - fmt.Printf("Failed to launch pouchd:%v\n", d.Args) + + fmt.Printf("\nFailed to launch pouchd:%v\n", d.Args) + + cmd := "ps aux |grep pouchd" + fmt.Printf("\nList pouchd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined()) + + cmd = "ps aux |grep containerd" + fmt.Printf("\nList containerd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined()) } d.KillDaemon() @@ -164,9 +175,9 @@ func (d *Config) DumpLog() { content, err := ioutil.ReadFile(d.LogPath) if err != nil { - fmt.Printf("failed to read log, err:%s", err) + fmt.Printf("failed to read log, err: %s\n", err) } - fmt.Printf("pouch daemon log contents: %s", content) + fmt.Printf("pouch daemon log contents:\n %s\n", content) } // KillDaemon kill pouchd. diff --git a/test/util_daemon.go b/test/util_daemon.go index 83778ada2..a29fe37ff 100644 --- a/test/util_daemon.go +++ b/test/util_daemon.go @@ -1,6 +1,9 @@ package main import ( + "encoding/json" + "fmt" + "os" "strings" "github.com/alibaba/pouch/test/command" @@ -49,6 +52,9 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result { // Find the first -l or --listen parameter and use it. for _, v := range d.Args { if strings.Contains(v, "-l") || strings.Contains(v, "--listen") { + if strings.Contains(v, "--listen-cri") { + continue + } if strings.Contains(v, "=") { sock = strings.Split(v, "=")[1] } else { @@ -60,3 +66,26 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result { args := append(append([]string{"--host"}, sock), cmd...) return command.PouchRun(args...) } + +// CreateConfigFile create configuration file and marshal cfg. +func CreateConfigFile(path string, cfg interface{}) error { + idx := strings.LastIndex(path, "/") + if _, err := os.Stat(path[0:idx]); os.IsNotExist(err) { + os.Mkdir(path[0:idx], os.ModePerm) + } + + file, err := os.Create(path) + if err != nil { + return err + } + + s, err := json.Marshal(cfg) + if err != nil { + return err + } + fmt.Fprintf(file, "%s", s) + file.Sync() + + defer file.Close() + return nil +} diff --git a/test/z_cli_daemon_test.go b/test/z_cli_daemon_test.go index 1e94d654c..aa8941d72 100644 --- a/test/z_cli_daemon_test.go +++ b/test/z_cli_daemon_test.go @@ -3,12 +3,16 @@ package main import ( "encoding/json" "fmt" + "os" + "strings" "github.com/alibaba/pouch/apis/types" + "github.com/alibaba/pouch/daemon/config" "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/daemon" "github.com/alibaba/pouch/test/environment" + "github.com/alibaba/pouch/test/util" "github.com/go-check/check" "github.com/gotestyourself/gotestyourself/icmd" ) @@ -72,26 +76,114 @@ func (suite *PouchDaemonSuite) TestDaemonCgroupParent(c *check.C) { func (suite *PouchDaemonSuite) TestDaemonListenTCP(c *check.C) { // Start a test daemon with test args. listeningPorts := [][]string{ - {"0.0.0.0", "0.0.0.0", "5678"}, + {"0.0.0.0", "0.0.0.0", "1236"}, {"127.0.0.1", "127.0.0.1", "1234"}, {"localhost", "127.0.0.1", "1235"}, } for _, hostDirective := range listeningPorts { addr := fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]) - dcfg, err := StartDefaultDaemonDebug("--listen=" + addr) + dcfg := daemon.NewConfig() + dcfg.Listen = "" + dcfg.NewArgs("--listen=" + addr) + err := dcfg.StartDaemon() c.Assert(err, check.IsNil) // verify listen to tcp works - command.PouchRun("--host", addr, "version").Assert(c, icmd.Success) - + result := command.PouchRun("--host", addr, "version") dcfg.KillDaemon() + result.Assert(c, icmd.Success) } } -// TestDaemonConfigFile tests start daemon with configfile works. +// TestDaemonConfigFile tests start daemon with configure file works. func (suite *PouchDaemonSuite) TestDaemonConfigFile(c *check.C) { - // TODO + configFile := "/tmp/pouch.json" + file, err := os.Create(configFile) + c.Assert(err, check.IsNil) + defer file.Close() + defer os.Remove(configFile) + + // Unmarshal config.Config, all fields in this struct could be handled in configuration file. + cfg := config.Config{ + Debug: true, + } + s, _ := json.Marshal(cfg) + fmt.Fprintf(file, "%s", s) + file.Sync() + + // TODO: uncomment this when issue #1003 is fixed. + //dcfg, err := StartDefaultDaemonDebug("--config-file="+configFile) + //{ + // err := dcfg.StartDaemon() + // c.Assert(err, check.IsNil) + //} + // + //// TODO: verify more + // + //// Must kill it, as we may loose the pid in next call. + //defer dcfg.KillDaemon() + + // config file cowork with parameter, no confilct +} + +// TestDaemonConfigFileConfilct tests start daemon with configure file confilicts with parameter. +func (suite *PouchDaemonSuite) TestDaemonConfigFileConfilct(c *check.C) { + path := "/tmp/pouch.json" + cfg := struct { + ContainerdPath string `json:"containerd-path"` + }{ + ContainerdPath: "abc", + } + err := CreateConfigFile(path, cfg) + c.Assert(err, check.IsNil) + defer os.Remove(path) + + dcfg, err := StartDefaultDaemon("--containerd-path", "def", "--config-file="+path) + dcfg.KillDaemon() + c.Assert(err, check.NotNil) +} + +// TestDaemonConfigFileUnknownFlag tests start daemon with unknown flags in configure file. +func (suite *PouchDaemonSuite) TestDaemonConfigFileUnknownFlag(c *check.C) { + path := "/tmp/pouch.json" + cfg := struct { + Adsj string `json:"adsj"` + }{ + Adsj: "xxx", + } + err := CreateConfigFile(path, cfg) + c.Assert(err, check.IsNil) + defer os.Remove(path) + + dcfg, err := StartDefaultDaemon("--debug", "--config-file="+path) + c.Assert(err, check.NotNil) + dcfg.KillDaemon() +} + +// TestDaemonConfigFileAndCli tests start daemon with configure file and CLI . +func (suite *PouchDaemonSuite) TestDaemonConfigFileAndCli(c *check.C) { + // Check default configure file could work + + // TODO: uncomment if issue #1003 is fixed + //path := "/etc/pouch/config.json" + //cfg := struct { + // Labels []string `json:"labels,omitempty"` + //}{ + // Labels: []string{"a=b"}, + //} + //err := CreateConfigFile(path, cfg) + //c.Assert(err, check.IsNil) + //defer os.Remove(path) + // + //// Do Not specify configure file explicitly, it should work. + //dcfg, err := StartDefaultDaemonDebug() + //c.Assert(err, check.IsNil) + //defer dcfg.KillDaemon() + // + //result := RunWithSpecifiedDaemon(dcfg, "info") + //err = util.PartialEqual(result.Stdout(), "a=b") + //c.Assert(err, check.IsNil) } // TestDaemonInvalideArgs tests invalid args in deamon return error @@ -120,7 +212,7 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) { cname := "TestDaemonRestart" { - result := RunWithSpecifiedDaemon(dcfg, "run", "--name", cname, + result := RunWithSpecifiedDaemon(dcfg, "run", "-d", "--name", cname, "-p", "1234:80", busyboxImage) if result.ExitCode != 0 { @@ -142,3 +234,59 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) { } c.Assert(string(result.State.Status), check.Equals, "running") } + +// TestDaemonLabel tests start daemon with label works. +func (suite *PouchDaemonSuite) TestDaemonLabel(c *check.C) { + dcfg, err := StartDefaultDaemonDebug("--label", "a=b") + // Start a test daemon with test args. + if err != nil { + c.Skip("deamon start failed.") + } + // Must kill it, as we may loose the pid in next call. + defer dcfg.KillDaemon() + + result := RunWithSpecifiedDaemon(dcfg, "info") + err = util.PartialEqual(result.Stdout(), "a=b") + c.Assert(err, check.IsNil) +} + +// TestDaemonLabelDup tests start daemon with duplicated label works. +func (suite *PouchDaemonSuite) TestDaemonLabelDup(c *check.C) { + dcfg, err := StartDefaultDaemonDebug("--label", "a=b", "--label", "a=b") + // Start a test daemon with test args. + if err != nil { + c.Skip("deamon start failed.") + } + // Must kill it, as we may loose the pid in next call. + defer dcfg.KillDaemon() + + result := RunWithSpecifiedDaemon(dcfg, "info") + err = util.PartialEqual(result.Stdout(), "a=b") + c.Assert(err, check.IsNil) + + cnt := strings.Count(result.Stdout(), "a=b") + c.Assert(cnt, check.Equals, 1) +} + +// TestDaemonLabelNeg tests start daemon with wrong label could not work. +func (suite *PouchDaemonSuite) TestDaemonLabelNeg(c *check.C) { + _, err := StartDefaultDaemon("--label", "adsf") + c.Assert(err, check.NotNil) +} + +// TestDaemonDefaultRegistry tests set default registry works. +func (suite *PouchDaemonSuite) TestDaemonDefaultRegistry(c *check.C) { + dcfg, err := StartDefaultDaemonDebug( + "--default-registry", + "reg.docker.alibaba-inc.com", + "--default-registry-namespace", + "base") + c.Assert(err, check.IsNil) + + // Check pull image with default registry using the registry specified in daemon. + result := RunWithSpecifiedDaemon(dcfg, "pull", "hello-world") + err = util.PartialEqual(result.Combined(), "reg.docker.alibaba-inc.com/base/hello-world") + c.Assert(err, check.IsNil) + + defer dcfg.KillDaemon() +}