-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: letty <[email protected]>
- Loading branch information
Showing
5 changed files
with
461 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.