Skip to content

Commit

Permalink
test: refine daemon framework
Browse files Browse the repository at this point in the history
Signed-off-by: letty <[email protected]>
  • Loading branch information
Letty5411 committed Apr 9, 2018
1 parent 9e3cfe1 commit effa6ca
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 83 deletions.
52 changes: 29 additions & 23 deletions test/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand All @@ -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
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...)
}
77 changes: 17 additions & 60 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,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)
Expand Down

0 comments on commit effa6ca

Please sign in to comment.