Skip to content

Commit

Permalink
test: add deamon related function
Browse files Browse the repository at this point in the history
Signed-off-by: letty <[email protected]>
  • Loading branch information
Letty5411 committed Mar 13, 2018
1 parent 1564d7a commit d049d59
Show file tree
Hide file tree
Showing 5 changed files with 461 additions and 220 deletions.
142 changes: 142 additions & 0 deletions test/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package daemon

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"time"

"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.
const (
DaemonLog = "/tmp/test/pouch/pouch.log"
PouchdBin = "pouchd"
HomeDir = "/tmp/test/pouch"
Listen = "unix:///tmp/test/pouch/pouchd.sock"
)

// Config is the configuration of pouch daemon.
type Config struct {
Log string

// Daemon startup arguments.
Args []string

// pouchd binary location
Bin string

Listen []string
HomeDir string

// pid of pouchd
Pid int
}

// DConfig is the global variable used to pouch daemon test.
var DConfig Config

// NewConfig initialize the DConfig with default value.
func NewConfig() Config {
result := Config{}

result.Bin = PouchdBin
result.Log = DaemonLog

result.Args = make([]string, 0, 1)
result.Args = append(result.Args, "--listen="+Listen)
result.Args = append(result.Args, "--home-dir="+HomeDir)

result.Listen = make([]string, 0, 1)
result.Listen = append(result.Listen, Listen)

result.HomeDir = HomeDir

return result
}

// IsDaemonUp checks if the pouchd is launched.
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") {
var sock string
if strings.Contains(v, "=") {
sock = strings.Split(v, "=")[1]
} else {
sock = strings.Fields(v)[1]
}
return command.PouchRun("--host", sock, "version").ExitCode == 0
}
}

return command.PouchRun("version").ExitCode == 0
}

// StartDaemon starts pouchd
func (d *Config) StartDaemon() error {
cmd := exec.Command(d.Bin, d.Args...)

outfile, err := os.Create(d.Log)
if err != nil {
return fmt.Errorf("failed to create log file %s, err %s", d.Log, err)
}
defer outfile.Close()

mwriter := io.MultiWriter(outfile)
cmd.Stderr = mwriter
cmd.Stdout = mwriter

err = cmd.Start()
if err != nil {
return fmt.Errorf("failed to start cmd %v, err %s", cmd, err)
}

// record the pid
d.Pid = cmd.Process.Pid

if util.WaitTimeout(time.Second*5, d.IsDaemonUp) == false {
content, err := ioutil.ReadFile(d.Log)
if err != nil {
return fmt.Errorf("read log file failed, err:%s", err)
}
fmt.Printf("pouch daemon log contents: %s", content)

d.KillDaemon()
return fmt.Errorf("failed to launch pouchd within 5s")
}

return nil
}

// KillDaemon kill pouchd.
func (d *Config) KillDaemon() error {
fmt.Printf("kill pouchd %d", d.Pid)

if d.IsDaemonUp() == false {
return nil
}

var result *icmd.Result
if d.Pid != 0 {
result = icmd.RunCommand("kill", "-9", strconv.Itoa(d.Pid))
fmt.Printf("resutl=%v", result)
if result.ExitCode != 0 {
return fmt.Errorf("kill pouchd failed, err:%s", result.Stderr())
}

if _, err := os.Stat(d.HomeDir); err == nil {
os.RemoveAll(d.HomeDir)
}

}
return nil
}
35 changes: 35 additions & 0 deletions test/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package util

import (
"fmt"
"time"
)

// WaitTimeout wait at most timeout nanoseconds,
// until the conditon become true or timeout reached.
func WaitTimeout(timeout time.Duration, condition func() bool) bool {
ch := make(chan bool, 1)

ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()

done := make(chan bool)
go func() {
time.Sleep(timeout)
done <- true
}()

for {
select {
case <-ch:
return true
case <-done:
fmt.Printf("condition failed to return true within %f seconds.\n", timeout.Seconds())
return false
case <-ticker.C:
if condition() == true {
ch <- true
}
}
}
}
Loading

0 comments on commit d049d59

Please sign in to comment.