From 9e3cfe1cd5b16a175d398c7e4a6af59ff0e3f48a Mon Sep 17 00:00:00 2001 From: letty Date: Mon, 9 Apr 2018 16:23:29 +0800 Subject: [PATCH 1/2] test: add daemon log control flag Signed-off-by: letty --- test/daemon/daemon.go | 11 +++++++++-- test/z_cli_daemon_test.go | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/test/daemon/daemon.go b/test/daemon/daemon.go index 2b5fe8cfd..1afa157e2 100644 --- a/test/daemon/daemon.go +++ b/test/daemon/daemon.go @@ -47,6 +47,9 @@ type Config struct { // timeout for starting daemon timeout int64 + + // if Debug=true, dump daemon log when deamon failed to start + Debug bool } // DConfig is the global variable used to pouch daemon test. @@ -56,6 +59,7 @@ func init() { DConfig.Args = make([]string, 0, 1) DConfig.Listen = make([]string, 0, 1) DConfig.timeout = 15 + DConfig.Debug = true } // NewConfig initialize the DConfig with default value. @@ -136,9 +140,12 @@ func (d *Config) StartDaemon() error { }() if util.WaitTimeout(time.Duration(d.timeout)*time.Second, d.IsDaemonUp) == false { - d.DumpLog() + if d.Debug == true { + d.DumpLog() + fmt.Printf("Failed to launch pouchd:%v\n", d.Args) + } + d.KillDaemon() - fmt.Printf("Failed to launch pouchd:%v\n", d.Args) return fmt.Errorf("failed to launch pouchd:%v", d.Args) } diff --git a/test/z_cli_daemon_test.go b/test/z_cli_daemon_test.go index 0b4ee280b..4a857d5b6 100644 --- a/test/z_cli_daemon_test.go +++ b/test/z_cli_daemon_test.go @@ -133,6 +133,10 @@ func (suite *PouchDaemonSuite) TestDaemonConfigFile(c *check.C) { func (suite *PouchDaemonSuite) TestDaemonInvalideArgs(c *check.C) { daemon.DConfig = daemon.NewConfig() daemon.DConfig.Args = append(daemon.DConfig.Args, "--config=xxx") + + // depress debug log + daemon.DConfig.Debug = false + err := daemon.DConfig.StartDaemon() c.Assert(err, check.NotNil) } From effa6ca1ea9dcd91812f61b0de253ade3ef12eda Mon Sep 17 00:00:00 2001 From: letty Date: Mon, 9 Apr 2018 20:14:23 +0800 Subject: [PATCH 2/2] test: refine daemon framework Signed-off-by: letty --- test/daemon/daemon.go | 52 ++++++++++++++------------ test/util_daemon.go | 62 +++++++++++++++++++++++++++++++ test/z_cli_daemon_test.go | 77 +++++++++------------------------------ 3 files changed, 108 insertions(+), 83 deletions(-) create mode 100644 test/util_daemon.go diff --git a/test/daemon/daemon.go b/test/daemon/daemon.go index 1afa157e2..67da8c1cc 100644 --- a/test/daemon/daemon.go +++ b/test/daemon/daemon.go @@ -35,12 +35,12 @@ type Config struct { // pouchd binary location Bin string - Listen []string - HomeDir string - + // The following args are all MUST required, + // in case the new daemon conflicts with existing ones. + Listen string + HomeDir string ContainerdAddr string - - ListenCri string + ListenCri string // pid of pouchd Pid int @@ -52,16 +52,6 @@ type Config struct { Debug bool } -// DConfig is the global variable used to pouch daemon test. -var DConfig Config - -func init() { - DConfig.Args = make([]string, 0, 1) - DConfig.Listen = make([]string, 0, 1) - DConfig.timeout = 15 - DConfig.Debug = true -} - // NewConfig initialize the DConfig with default value. func NewConfig() Config { result := Config{} @@ -70,24 +60,40 @@ func NewConfig() Config { result.LogPath = DaemonLog result.Args = make([]string, 0, 1) - result.Listen = make([]string, 0, 1) - - result.Args = append(result.Args, "--listen="+Listen) - result.Args = append(result.Args, "--home-dir="+HomeDir) - result.Args = append(result.Args, "--containerd="+ContainerdAdd) - result.Args = append(result.Args, "--listen-cri="+ListenCRI) - - result.Listen = append(result.Listen, Listen) + result.Listen = Listen result.HomeDir = HomeDir result.ContainerdAddr = ContainerdAdd result.ListenCri = ListenCRI result.timeout = 15 + result.Debug = true return result } +// NewArgs is used to construct args according to the struct Config and input. +func (d *Config) NewArgs(args ...string) { + // Append all default configuration to d.Args if they exists + // For the rest args in parameter, they must follow the pouchd args usage. + if len(d.Listen) != 0 { + d.Args = append(d.Args, "--listen="+d.Listen) + } + if len(d.HomeDir) != 0 { + d.Args = append(d.Args, "--home-dir="+d.HomeDir) + } + if len(d.ContainerdAddr) != 0 { + d.Args = append(d.Args, "--containerd="+d.ContainerdAddr) + } + if len(d.ListenCri) != 0 { + d.Args = append(d.Args, "--listen-cri="+d.ListenCri) + } + + if len(args) != 0 { + d.Args = append(d.Args, args...) + } +} + // IsDaemonUp checks if the pouchd is launched. func (d *Config) IsDaemonUp() bool { // if pouchd is started with -l option, use the first listen address diff --git a/test/util_daemon.go b/test/util_daemon.go new file mode 100644 index 000000000..83778ada2 --- /dev/null +++ b/test/util_daemon.go @@ -0,0 +1,62 @@ +package main + +import ( + "strings" + + "github.com/alibaba/pouch/test/command" + "github.com/alibaba/pouch/test/daemon" + + "github.com/gotestyourself/gotestyourself/icmd" +) + +// StartDefaultDaemonDebug starts a deamon with default configuration and debug on. +func StartDefaultDaemonDebug(args ...string) (*daemon.Config, error) { + cfg := daemon.NewConfig() + cfg.Debug = true + + cfg.NewArgs(args...) + + return &cfg, cfg.StartDaemon() +} + +// StartDefaultDaemon starts a deamon with all default configuration and debug off. +func StartDefaultDaemon(args ...string) (*daemon.Config, error) { + cfg := daemon.NewConfig() + cfg.Debug = false + + cfg.NewArgs(args...) + + return &cfg, cfg.StartDaemon() +} + +// StartDaemonBareWithArgs starts a deamon with all user specified parameter. +func StartDaemonBareWithArgs(cfg *daemon.Config, args ...string) error { + cfg.NewArgs(args...) + + return cfg.StartDaemon() +} + +// RestartDaemon restart daemon +func RestartDaemon(cfg *daemon.Config) error { + cfg.KillDaemon() + return cfg.StartDaemon() +} + +// RunWithSpecifiedDaemon run pouch command with --host parameter +func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result { + var sock string + + // 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, "=") { + sock = strings.Split(v, "=")[1] + } else { + sock = strings.Fields(v)[1] + } + break + } + } + args := append(append([]string{"--host"}, sock), cmd...) + return command.PouchRun(args...) +} diff --git a/test/z_cli_daemon_test.go b/test/z_cli_daemon_test.go index 4a857d5b6..1e94d654c 100644 --- a/test/z_cli_daemon_test.go +++ b/test/z_cli_daemon_test.go @@ -3,10 +3,8 @@ package main import ( "encoding/json" "fmt" - "os" "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" @@ -29,29 +27,26 @@ func (suite *PouchDaemonSuite) SetUpTest(c *check.C) { // TestDaemonCgroupParent tests daemon with cgroup parent func (suite *PouchDaemonSuite) TestDaemonCgroupParent(c *check.C) { - // Start a test daemon with test args. - daemon.DConfig = daemon.NewConfig() - daemon.DConfig.Args = append(daemon.DConfig.Args, "--cgroup-parent=tmp") - - err := daemon.DConfig.StartDaemon() + dcfg, err := StartDefaultDaemonDebug("--cgroup-parent=tmp") if err != nil { c.Skip("deamon start failed") } + // Must kill it, as we may loose the pid in next call. - defer daemon.DConfig.KillDaemon() + defer dcfg.KillDaemon() cname := "TestDaemonCgroupParent" { result := command.PouchRun("--host", daemon.Listen, "pull", busyboxImage) if result.ExitCode != 0 { - daemon.DConfig.DumpLog() + dcfg.DumpLog() c.Fatalf("pull image failed, err:%v", result) } } { result := command.PouchRun("--host", daemon.Listen, "run", "--name", cname, busyboxImage) if result.ExitCode != 0 { - daemon.DConfig.DumpLog() + dcfg.DumpLog() c.Fatalf("run container failed, err:%v", result) } } @@ -83,102 +78,64 @@ func (suite *PouchDaemonSuite) TestDaemonListenTCP(c *check.C) { } for _, hostDirective := range listeningPorts { - daemon.DConfig = daemon.NewConfig() - addr := fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]) - daemon.DConfig.Listen = append(daemon.DConfig.Listen, addr) - daemon.DConfig.Listen = append(daemon.DConfig.Listen, addr) - daemon.DConfig.Args = append(daemon.DConfig.Args, "--listen="+addr) - - err := daemon.DConfig.StartDaemon() + dcfg, err := StartDefaultDaemonDebug("--listen=" + addr) c.Assert(err, check.IsNil) // verify listen to tcp works command.PouchRun("--host", addr, "version").Assert(c, icmd.Success) - daemon.DConfig.KillDaemon() + dcfg.KillDaemon() } } // TestDaemonConfigFile tests start daemon with configfile works. func (suite *PouchDaemonSuite) TestDaemonConfigFile(c *check.C) { - configFile := "/tmp/pouch.json" - file, err := os.Create(configFile) - c.Assert(err, check.IsNil) - defer file.Close() - defer os.Remove(configFile) - - dcfg := config.Config{ - Debug: true, - } - s, _ := json.Marshal(dcfg) - fmt.Fprintf(file, "%s", s) - file.Sync() - - daemon.DConfig = daemon.NewConfig() - daemon.DConfig.Args = append(daemon.DConfig.Args, "--config-file="+configFile) - - //{ - // err := daemon.DConfig.StartDaemon() - // c.Assert(err, check.IsNil) - //} - - // TODO: verify more - - // Must kill it, as we may loose the pid in next call. - defer daemon.DConfig.KillDaemon() + // TODO } // TestDaemonInvalideArgs tests invalid args in deamon return error func (suite *PouchDaemonSuite) TestDaemonInvalideArgs(c *check.C) { - daemon.DConfig = daemon.NewConfig() - daemon.DConfig.Args = append(daemon.DConfig.Args, "--config=xxx") - - // depress debug log - daemon.DConfig.Debug = false - - err := daemon.DConfig.StartDaemon() + _, err := StartDefaultDaemon("--config=xxx") c.Assert(err, check.NotNil) } // TestDaemonRestart tests daemon restart with running container. func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) { + dcfg, err := StartDefaultDaemonDebug() // Start a test daemon with test args. - daemon.DConfig = daemon.NewConfig() - err := daemon.DConfig.StartDaemon() if err != nil { c.Skip("deamon start failed.") } // Must kill it, as we may loose the pid in next call. - defer daemon.DConfig.KillDaemon() + defer dcfg.KillDaemon() { - result := command.PouchRun("--host", daemon.Listen, "pull", busyboxImage) + result := RunWithSpecifiedDaemon(dcfg, "pull", busyboxImage) if result.ExitCode != 0 { - daemon.DConfig.DumpLog() + dcfg.DumpLog() c.Fatalf("pull image failed, err:%v", result) } } cname := "TestDaemonRestart" { - result := command.PouchRun("--host", daemon.Listen, "run", "--name", cname, + result := RunWithSpecifiedDaemon(dcfg, "run", "--name", cname, "-p", "1234:80", busyboxImage) if result.ExitCode != 0 { - daemon.DConfig.DumpLog() + dcfg.DumpLog() c.Fatalf("run container failed, err:%v", result) } } defer DelContainerForceMultyTime(c, cname) // restart daemon - daemon.DConfig.KillDaemon() - err = daemon.DConfig.StartDaemon() + err = RestartDaemon(dcfg) c.Assert(err, check.IsNil) // test if the container is running. - output := command.PouchRun("inspect", "--host", daemon.Listen, cname).Stdout() + output := RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout() result := &types.ContainerJSON{} if err := json.Unmarshal([]byte(output), result); err != nil { c.Fatalf("failed to decode inspect output: %v", err)