Skip to content

Commit

Permalink
using config file to run daemon test
Browse files Browse the repository at this point in the history
*background*: some env may depend on some
specific config options.

Signed-off-by: zhuangqh <zhuangqhc@gmail.com>
  • Loading branch information
zhuangqh committed Jul 15, 2019
1 parent 83d6ef4 commit 9571e39
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 388 deletions.
2 changes: 1 addition & 1 deletion test/cli_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (suite *PouchNetworkSuite) TestNetworkDisconnect(c *check.C) {
// and 'network disconnect' after restart daemon.
func (suite *PouchNetworkSuite) TestNetworkConnectWithRestart(c *check.C) {
// start the test pouch daemon
dcfg, err := StartDefaultDaemonDebug()
dcfg, err := StartDefaultDaemon(nil)
if err != nil {
c.Skip("daemon start failed.")
}
Expand Down
32 changes: 24 additions & 8 deletions test/cli_snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *PouchSnapshotterSuite) TearDownTest(c *check.C) {

// TestNotSetSnapshotter tests default snapshotter to run pouchd
func (suite *PouchSnapshotterSuite) TestNotSetSnapshotter(c *check.C) {
dcfg, err := StartDefaultDaemon()
dcfg, err := StartDefaultDaemon(nil)
c.Assert(err, check.IsNil)

defer dcfg.KillDaemon()
Expand All @@ -50,7 +50,9 @@ func (suite *PouchSnapshotterSuite) TestNotSetSnapshotter(c *check.C) {

// TestSetDefaultSnapshotter tests set default snapshotter driver to run pouchd
func (suite *PouchSnapshotterSuite) TestSetDefaultSnapshotter(c *check.C) {
dcfg, err := StartDefaultDaemon("--snapshotter", "overlayfs")
dcfg, err := StartDefaultDaemon(map[string]interface{}{
"snapshotter": "overlayfs",
})
c.Assert(err, check.IsNil)

defer dcfg.KillDaemon()
Expand All @@ -67,7 +69,9 @@ func (suite *PouchSnapshotterSuite) TestSetDefaultSnapshotter(c *check.C) {

// TestOldSnapshotterNotClean tests old snapshotter driver not clean and then set a new one
func (suite *PouchSnapshotterSuite) TestOldSnapshotterNotClean(c *check.C) {
dcfg, err := StartDefaultDaemon("--snapshotter", "overlayfs")
dcfg, err := StartDefaultDaemon(map[string]interface{}{
"snapshotter": "overlayfs",
})
c.Assert(err, check.IsNil)

fileSystemInfo, err := mount.Lookup(dcfg.HomeDir)
Expand All @@ -83,12 +87,16 @@ func (suite *PouchSnapshotterSuite) TestOldSnapshotterNotClean(c *check.C) {
dcfg.KillDaemon()
time.Sleep(10 * time.Second)

_, err = StartDefaultDaemon("--snapshotter", "btrfs")
_, err = StartDefaultDaemon(map[string]interface{}{
"snapshotter": "btrfs",
})
fmt.Printf("start pouchd failed:%s", err.Error())
c.Assert(err, check.NotNil)

// clean image
dcfg, err = StartDefaultDaemon("--snapshotter", "overlayfs")
dcfg, err = StartDefaultDaemon(map[string]interface{}{
"snapshotter": "overlayfs",
})
c.Assert(err, check.IsNil)

result = RunWithSpecifiedDaemon(dcfg, "rmi", busyboxImage)
Expand All @@ -98,7 +106,10 @@ func (suite *PouchSnapshotterSuite) TestOldSnapshotterNotClean(c *check.C) {

// TestAllowMultiSnapshotter tests pouchd with two snapshotter
func (suite *PouchSnapshotterSuite) TestAllowMultiSnapshotter(c *check.C) {
dcfg, err := StartDefaultDaemon("--snapshotter", "overlayfs", "--allow-multi-snapshotter")
dcfg, err := StartDefaultDaemon(map[string]interface{}{
"snapshotter": "overlayfs",
"allow-multi-snapshotter": true,
})
c.Assert(err, check.IsNil)

fileSystemInfo, err := mount.Lookup(dcfg.HomeDir)
Expand All @@ -114,14 +125,19 @@ func (suite *PouchSnapshotterSuite) TestAllowMultiSnapshotter(c *check.C) {
dcfg.KillDaemon()
time.Sleep(10 * time.Second)

dcfg, err = StartDefaultDaemon("--snapshotter", "btrfs", "--allow-multi-snapshotter")
dcfg, err = StartDefaultDaemon(map[string]interface{}{
"snapshotter": "btrfs",
"allow-multi-snapshotter": true,
})
c.Assert(err, check.IsNil)

dcfg.KillDaemon()
time.Sleep(10 * time.Second)

// clean image
dcfg, err = StartDefaultDaemon("--snapshotter", "overlayfs")
dcfg, err = StartDefaultDaemon(map[string]interface{}{
"snapshotter": "overlayfs",
})
c.Assert(err, check.IsNil)

result = RunWithSpecifiedDaemon(dcfg, "rmi", busyboxImage)
Expand Down
154 changes: 102 additions & 52 deletions test/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package daemon

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
Expand All @@ -18,12 +20,14 @@ import (

// For pouch daemon test, we launched another pouch daemon.
const (
DaemonLog = "/tmp/pouchd.log"
PouchdBin = "pouchd"
HomeDir = "/tmp/test/pouch"
Listen = "unix:///tmp/test/pouch/pouchd.sock"
ContainerdAdd = "/tmp/test/pouch/containerd.sock"
Pidfile = "/tmp/test/pouch/pouch.pid"
DaemonLog = "/tmp/pouchd.log"
PouchdBin = "pouchd"
HomeDir = "/tmp/test/pouch"
Listen = "unix:///tmp/test/pouch/pouchd.sock"
StreamServerPort = "10020"
ContainerdAdd = "/tmp/test/pouch/containerd.sock"
Pidfile = "/tmp/test/pouch/pouch.pid"
ConfigFile = "/tmp/test-config.json"
)

// Config is the configuration of pouch daemon.
Expand All @@ -34,15 +38,19 @@ type Config struct {
// Daemon startup arguments.
Args []string

// Daemon startup config.
Cfg map[string]interface{}

// pouchd binary location
Bin string

// The following args are all MUST required,
// in case the new daemon conflicts with existing ones.
Listen string
HomeDir string
ContainerdAddr string
Pidfile string
Listen string
StreamServerPort string
HomeDir string
ContainerdAddr string
Pidfile string

// pid of pouchd
Pid int
Expand All @@ -64,6 +72,7 @@ func NewConfig() Config {
result.Args = make([]string, 0, 1)

result.Listen = Listen
result.StreamServerPort = StreamServerPort
result.HomeDir = HomeDir
result.ContainerdAddr = ContainerdAdd
result.Pidfile = Pidfile
Expand All @@ -74,55 +83,17 @@ func NewConfig() Config {
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.Pidfile) != 0 {
d.Args = append(d.Args, "--pidfile="+d.Pidfile)
}

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
var sock string

for _, v := range d.Args {
if strings.Contains(v, "-l") || strings.Contains(v, "--listen") {
if strings.Contains(v, "--listen-cri") {
continue
}
if strings.Contains(v, "=") {
sock = strings.Split(v, "=")[1]
break
} else {
sock = strings.Fields(v)[1]
break
if v, ok := d.Cfg["listen"].([]string); ok {
for _, host := range v {
if strings.HasPrefix(host, "unix") {
sock = host
}
}
}

for _, v := range d.Args {
if strings.Contains(v, "--tlsverify") {
// TODO: need to verify server with TLS
return true
}
}

if len(sock) != 0 {
return command.PouchRun("--host", sock, "version").ExitCode == 0
}
Expand All @@ -132,9 +103,36 @@ func (d *Config) IsDaemonUp() bool {

// StartDaemon starts pouchd
func (d *Config) StartDaemon() error {
d.Args = append(d.Args, "--config-file="+ConfigFile)
cmd := exec.Command(d.Bin, d.Args...)

// set default config
if d.Cfg == nil {
d.Cfg = make(map[string]interface{})
}
if _, ok := d.Cfg["listen"]; !ok {
d.Cfg["listen"] = []string{d.Listen}
}
if _, ok := d.Cfg["home-dir"]; !ok {
d.Cfg["home-dir"] = d.HomeDir
}
if _, ok := d.Cfg["containerd"]; !ok {
d.Cfg["containerd"] = d.ContainerdAddr
}
if _, ok := d.Cfg["pidfile"]; !ok {
d.Cfg["pidfile"] = d.Pidfile
}
if _, ok := d.Cfg["cri-config"]; !ok {
d.Cfg["cri-config"] = map[string]string{
"stream-server-port": d.StreamServerPort,
}
}

var err error
if err := CreateConfigFile(ConfigFile, d.Cfg); err != nil {
return err
}

d.LogFile, err = os.Create(d.LogPath)
if err != nil {
return fmt.Errorf("failed to create log file %s, err %s", d.LogPath, err)
Expand Down Expand Up @@ -211,3 +209,55 @@ func (d *Config) KillDaemon() {
d.LogFile.Close()
}
}

// CreateConfigFile create configuration file and marshal cfg.
// Merge the default config to it if the default config exists.
func CreateConfigFile(path string, cfg interface{}) error {
defaultCfg := make(map[string]interface{})
defaultCfgPath := "/etc/pouch/config.json"
if _, err := os.Stat(defaultCfgPath); !os.IsNotExist(err) {
byt, err := ioutil.ReadFile(defaultCfgPath)
if err != nil {
return fmt.Errorf("failed to read default config: %v", err)
}

if err := json.Unmarshal(byt, &defaultCfg); err != nil {
return fmt.Errorf("failed to unmarshal default config: %v", err)
}
}

cfgMap := make(map[string]interface{})
byt, err := json.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to marshal config: %v", err)
}
if err := json.Unmarshal(byt, &cfgMap); err != nil {
return fmt.Errorf("failed to unmarshal config: %v", err)
}

if cfgMap == nil {
cfgMap = make(map[string]interface{})
}

// merge
for k, v := range defaultCfg {
if _, ok := cfgMap[k]; !ok {
cfgMap[k] = v
}
}

s, err := json.Marshal(cfgMap)
if err != nil {
return err
}

if err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}

if err = ioutil.WriteFile(path, s, 0644); err != nil {
return err
}

return nil
}
62 changes: 9 additions & 53 deletions test/util_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package main

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/command"
Expand Down Expand Up @@ -43,22 +40,13 @@ func GetRootDir() (string, error) {
return got.PouchRootDir, nil
}

// StartDefaultDaemonDebug starts a daemon 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 daemon with all default configuration and debug off.
func StartDefaultDaemon(args ...string) (*daemon.Config, error) {
func StartDefaultDaemon(configMap map[string]interface{}, args ...string) (*daemon.Config, error) {
cfg := daemon.NewConfig()
cfg.Debug = false
cfg.Debug = true

cfg.NewArgs(args...)
cfg.Cfg = configMap
cfg.Args = append(cfg.Args, args...)

return &cfg, cfg.StartDaemon()
}
Expand All @@ -72,44 +60,12 @@ func RestartDaemon(cfg *daemon.Config) error {
// 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, "--listen-cri") {
continue
}
if strings.Contains(v, "=") {
sock = strings.Split(v, "=")[1]
} else {
sock = strings.Fields(v)[1]
}
break
if v, ok := d.Cfg["listen"].([]string); ok {
if len(v) > 0 {
sock = v[0]
}
}
args := append(append([]string{"--host"}, sock), cmd...)
return command.PouchRun(args...)
}

// CreateConfigFile create configuration file and marshal cfg.
func CreateConfigFile(path string, cfg interface{}) error {
idx := strings.LastIndex(path, "/")
if _, err := os.Stat(path[0:idx]); os.IsNotExist(err) {
os.Mkdir(path[0:idx], os.ModePerm)
}

file, err := os.Create(path)
if err != nil {
return err
}

s, err := json.Marshal(cfg)
if err != nil {
return err
}
fmt.Fprintf(file, "%s", s)
file.Sync()

defer file.Close()
return nil
args := append([]string{"--host", sock}, cmd...)
return command.PouchRun(args...)
}
Loading

0 comments on commit 9571e39

Please sign in to comment.