Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refine daemon framework #1078

Merged
merged 2 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions test/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,21 @@ 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

// timeout for starting daemon
timeout int64
}

// 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
// if Debug=true, dump daemon log when deamon failed to start
Debug bool
}

// NewConfig initialize the DConfig with default value.
Expand All @@ -66,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
Expand Down Expand Up @@ -136,9 +146,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)
}

Expand Down
62 changes: 62 additions & 0 deletions test/util_daemon.go
Original file line number Diff line number Diff line change
@@ -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...)
}
73 changes: 17 additions & 56 deletions test/z_cli_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -83,98 +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")
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)
Expand Down