Skip to content

Commit

Permalink
Merge pull request #1087 from Letty5411/0410
Browse files Browse the repository at this point in the history
test: add tests for label and config file in daemon
  • Loading branch information
allencloud authored Apr 11, 2018
2 parents 34c5a06 + 3cae2d1 commit c67f0c8
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 10 deletions.
17 changes: 14 additions & 3 deletions test/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"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.
Expand Down Expand Up @@ -99,6 +100,9 @@ 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") {
if strings.Contains(v, "--listen-cri") {
continue
}
var sock string
if strings.Contains(v, "=") {
sock = strings.Split(v, "=")[1]
Expand Down Expand Up @@ -148,7 +152,14 @@ func (d *Config) StartDaemon() error {
if util.WaitTimeout(time.Duration(d.timeout)*time.Second, d.IsDaemonUp) == false {
if d.Debug == true {
d.DumpLog()
fmt.Printf("Failed to launch pouchd:%v\n", d.Args)

fmt.Printf("\nFailed to launch pouchd:%v\n", d.Args)

cmd := "ps aux |grep pouchd"
fmt.Printf("\nList pouchd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())

cmd = "ps aux |grep containerd"
fmt.Printf("\nList containerd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
}

d.KillDaemon()
Expand All @@ -164,9 +175,9 @@ func (d *Config) DumpLog() {

content, err := ioutil.ReadFile(d.LogPath)
if err != nil {
fmt.Printf("failed to read log, err:%s", err)
fmt.Printf("failed to read log, err: %s\n", err)
}
fmt.Printf("pouch daemon log contents: %s", content)
fmt.Printf("pouch daemon log contents:\n %s\n", content)
}

// KillDaemon kill pouchd.
Expand Down
29 changes: 29 additions & 0 deletions test/util_daemon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

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

"github.com/alibaba/pouch/test/command"
Expand Down Expand Up @@ -49,6 +52,9 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result {
// 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 {
Expand All @@ -60,3 +66,26 @@ func RunWithSpecifiedDaemon(d *daemon.Config, cmd ...string) *icmd.Result {
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
}
162 changes: 155 additions & 7 deletions test/z_cli_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package main
import (
"encoding/json"
"fmt"
"os"
"strings"

"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"

"github.com/alibaba/pouch/test/util"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
Expand Down Expand Up @@ -72,26 +76,114 @@ func (suite *PouchDaemonSuite) TestDaemonCgroupParent(c *check.C) {
func (suite *PouchDaemonSuite) TestDaemonListenTCP(c *check.C) {
// Start a test daemon with test args.
listeningPorts := [][]string{
{"0.0.0.0", "0.0.0.0", "5678"},
{"0.0.0.0", "0.0.0.0", "1236"},
{"127.0.0.1", "127.0.0.1", "1234"},
{"localhost", "127.0.0.1", "1235"},
}

for _, hostDirective := range listeningPorts {
addr := fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2])
dcfg, err := StartDefaultDaemonDebug("--listen=" + addr)
dcfg := daemon.NewConfig()
dcfg.Listen = ""
dcfg.NewArgs("--listen=" + addr)
err := dcfg.StartDaemon()
c.Assert(err, check.IsNil)

// verify listen to tcp works
command.PouchRun("--host", addr, "version").Assert(c, icmd.Success)

result := command.PouchRun("--host", addr, "version")
dcfg.KillDaemon()
result.Assert(c, icmd.Success)
}
}

// TestDaemonConfigFile tests start daemon with configfile works.
// TestDaemonConfigFile tests start daemon with configure file works.
func (suite *PouchDaemonSuite) TestDaemonConfigFile(c *check.C) {
// TODO
configFile := "/tmp/pouch.json"
file, err := os.Create(configFile)
c.Assert(err, check.IsNil)
defer file.Close()
defer os.Remove(configFile)

// Unmarshal config.Config, all fields in this struct could be handled in configuration file.
cfg := config.Config{
Debug: true,
}
s, _ := json.Marshal(cfg)
fmt.Fprintf(file, "%s", s)
file.Sync()

// TODO: uncomment this when issue #1003 is fixed.
//dcfg, err := StartDefaultDaemonDebug("--config-file="+configFile)
//{
// err := dcfg.StartDaemon()
// c.Assert(err, check.IsNil)
//}
//
//// TODO: verify more
//
//// Must kill it, as we may loose the pid in next call.
//defer dcfg.KillDaemon()

// config file cowork with parameter, no confilct
}

// TestDaemonConfigFileConfilct tests start daemon with configure file confilicts with parameter.
func (suite *PouchDaemonSuite) TestDaemonConfigFileConfilct(c *check.C) {
path := "/tmp/pouch.json"
cfg := struct {
ContainerdPath string `json:"containerd-path"`
}{
ContainerdPath: "abc",
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)

dcfg, err := StartDefaultDaemon("--containerd-path", "def", "--config-file="+path)
dcfg.KillDaemon()
c.Assert(err, check.NotNil)
}

// TestDaemonConfigFileUnknownFlag tests start daemon with unknown flags in configure file.
func (suite *PouchDaemonSuite) TestDaemonConfigFileUnknownFlag(c *check.C) {
path := "/tmp/pouch.json"
cfg := struct {
Adsj string `json:"adsj"`
}{
Adsj: "xxx",
}
err := CreateConfigFile(path, cfg)
c.Assert(err, check.IsNil)
defer os.Remove(path)

dcfg, err := StartDefaultDaemon("--debug", "--config-file="+path)
c.Assert(err, check.NotNil)
dcfg.KillDaemon()
}

// TestDaemonConfigFileAndCli tests start daemon with configure file and CLI .
func (suite *PouchDaemonSuite) TestDaemonConfigFileAndCli(c *check.C) {
// Check default configure file could work

// TODO: uncomment if issue #1003 is fixed
//path := "/etc/pouch/config.json"
//cfg := struct {
// Labels []string `json:"labels,omitempty"`
//}{
// Labels: []string{"a=b"},
//}
//err := CreateConfigFile(path, cfg)
//c.Assert(err, check.IsNil)
//defer os.Remove(path)
//
//// Do Not specify configure file explicitly, it should work.
//dcfg, err := StartDefaultDaemonDebug()
//c.Assert(err, check.IsNil)
//defer dcfg.KillDaemon()
//
//result := RunWithSpecifiedDaemon(dcfg, "info")
//err = util.PartialEqual(result.Stdout(), "a=b")
//c.Assert(err, check.IsNil)
}

// TestDaemonInvalideArgs tests invalid args in deamon return error
Expand Down Expand Up @@ -120,7 +212,7 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {

cname := "TestDaemonRestart"
{
result := RunWithSpecifiedDaemon(dcfg, "run", "--name", cname,
result := RunWithSpecifiedDaemon(dcfg, "run", "-d", "--name", cname,
"-p", "1234:80",
busyboxImage)
if result.ExitCode != 0 {
Expand All @@ -142,3 +234,59 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
}
c.Assert(string(result.State.Status), check.Equals, "running")
}

// TestDaemonLabel tests start daemon with label works.
func (suite *PouchDaemonSuite) TestDaemonLabel(c *check.C) {
dcfg, err := StartDefaultDaemonDebug("--label", "a=b")
// Start a test daemon with test args.
if err != nil {
c.Skip("deamon start failed.")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()

result := RunWithSpecifiedDaemon(dcfg, "info")
err = util.PartialEqual(result.Stdout(), "a=b")
c.Assert(err, check.IsNil)
}

// TestDaemonLabelDup tests start daemon with duplicated label works.
func (suite *PouchDaemonSuite) TestDaemonLabelDup(c *check.C) {
dcfg, err := StartDefaultDaemonDebug("--label", "a=b", "--label", "a=b")
// Start a test daemon with test args.
if err != nil {
c.Skip("deamon start failed.")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()

result := RunWithSpecifiedDaemon(dcfg, "info")
err = util.PartialEqual(result.Stdout(), "a=b")
c.Assert(err, check.IsNil)

cnt := strings.Count(result.Stdout(), "a=b")
c.Assert(cnt, check.Equals, 1)
}

// TestDaemonLabelNeg tests start daemon with wrong label could not work.
func (suite *PouchDaemonSuite) TestDaemonLabelNeg(c *check.C) {
_, err := StartDefaultDaemon("--label", "adsf")
c.Assert(err, check.NotNil)
}

// TestDaemonDefaultRegistry tests set default registry works.
func (suite *PouchDaemonSuite) TestDaemonDefaultRegistry(c *check.C) {
dcfg, err := StartDefaultDaemonDebug(
"--default-registry",
"reg.docker.alibaba-inc.com",
"--default-registry-namespace",
"base")
c.Assert(err, check.IsNil)

// Check pull image with default registry using the registry specified in daemon.
result := RunWithSpecifiedDaemon(dcfg, "pull", "hello-world")
err = util.PartialEqual(result.Combined(), "reg.docker.alibaba-inc.com/base/hello-world")
c.Assert(err, check.IsNil)

defer dcfg.KillDaemon()
}

0 comments on commit c67f0c8

Please sign in to comment.