-
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 <letty.ll@alibaba-inc.com>
- Loading branch information
Showing
4 changed files
with
371 additions
and
8 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
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
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,272 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/alibaba/pouch/test/command" | ||
"github.com/alibaba/pouch/test/environment" | ||
|
||
"github.com/go-check/check" | ||
"github.com/gotestyourself/gotestyourself/icmd" | ||
) | ||
|
||
// PouchRichContainerSuite is the test suite fo rich container related CLI. | ||
type PouchRichContainerSuite struct{} | ||
|
||
func init() { | ||
check.Suite(&PouchRichContainerSuite{}) | ||
} | ||
|
||
var centosImage string | ||
|
||
// SetUpSuite does common setup in the beginning of each test suite. | ||
func (suite *PouchRichContainerSuite) SetUpSuite(c *check.C) { | ||
SkipIfFalse(c, environment.IsLinux) | ||
SkipIfFalse(c, environment.IsRuncVersionSupportRichContianer) | ||
|
||
command.PouchRun("pull", busyboxImage).Assert(c, icmd.Success) | ||
|
||
// Use image from AliYun on AliOS. | ||
if environment.IsAliKernel() { | ||
centosImage = "reg.docker.alibaba-inc.com/alibase/alios7u2:latest" | ||
} else { | ||
centosImage = "registry.hub.docker.com/library/centos:latest" | ||
} | ||
command.PouchRun("pull", centosImage).Assert(c, icmd.Success) | ||
} | ||
|
||
// TearDownSuite does common cleanup in the end of each test suite. | ||
func (suite *PouchRichContainerSuite) TearDownSuite(c *check.C) { | ||
command.PouchRun("rmi", centosImage).Assert(c, icmd.Success) | ||
} | ||
|
||
// isFileExistsInImage checks if the file exists in given image. | ||
func isFileExistsInImage(image string, file string, cname string) (bool, error) { | ||
if image == "" || file == "" || cname == "" { | ||
return false, errors.New("input args is nil") | ||
} | ||
|
||
// check the existence of /sbin/init in image | ||
expect := icmd.Expected{ | ||
ExitCode: 0, | ||
Out: "Access", | ||
} | ||
err := command.PouchRun("run", "--name", cname, image, "stat", file).Compare(expect) | ||
defer command.PouchRun("rm", "-f", cname) | ||
|
||
return err == nil, nil | ||
} | ||
|
||
// checkPidofProcessIsOne checks the process of pid 1 is expected. | ||
func checkPidofProcessIsOne(cname string, p string) bool { | ||
// Check the number 1 process is dumb-init | ||
expect := icmd.Expected{ | ||
ExitCode: 0, | ||
Out: "1", | ||
} | ||
|
||
err := command.PouchRun("exec", cname, "ps", "-ef", "|grep", p, "|awk '{print $1}'").Compare(expect) | ||
if err != nil { | ||
fmt.Printf("err=%s\n", err) | ||
} | ||
return err == nil | ||
} | ||
|
||
// checkPPid checks the ppid of process is expected. | ||
func checkPPid(cname string, p string, ppid string) bool { | ||
// Check the number 1 process is dumb-init | ||
expect := icmd.Expected{ | ||
ExitCode: 0, | ||
Out: ppid, | ||
} | ||
|
||
err := command.PouchRun("exec", cname, "sh", "-c", "ps", "-ef", "|grep", p, "|awk '{print $3}'").Compare(expect) | ||
if err != nil { | ||
fmt.Printf("err=%s\n", err) | ||
} | ||
|
||
return err == nil | ||
} | ||
|
||
// checkInitScriptWorks | ||
func checkInitScriptWorks(c *check.C, cname string, image string, richmode string) { | ||
// TODO: Use bash script to get the stdin may have error, need to find a better way. | ||
|
||
//// Check run shell script works | ||
//script := "/tmp/" + cname + ".sh" | ||
//os.Remove(script) | ||
//defer os.Remove(script) | ||
// | ||
//if _, err := os.Create(script); err != nil { | ||
// c.Fatal("Fail to create %s file", script) | ||
//} | ||
//if err := os.Chmod(script, 0777); err != nil { | ||
// c.Fatal("Fail to chmode %s file", script) | ||
//} | ||
// | ||
//cmd := "echo '#!/bin/bash' >>" + script | ||
//// shebang is reuqired | ||
//icmd.RunCommand("sh", "-c", cmd).Assert(c, icmd.Success) | ||
//// Get the stdin | ||
//cmd = "echo 'echo $(cat) > /tmp/checkInitScriptWorks' >>" + script | ||
//icmd.RunCommand("sh", "-c", cmd).Assert(c, icmd.Success) | ||
// | ||
//fmt.Println(icmd.RunCommand("cat", script).Combined()) | ||
// | ||
//command.PouchRun("run", "-d", "--privileged", "-v", "/tmp:/tmp", "--rich", "--rich-mode", richmode, "--initscript", | ||
// script, "--name", cname, image, "sleep 10000").Assert(c, icmd.Success) | ||
//output := command.PouchRun("inspect", cname).Stdout() | ||
//result := &types.ContainerJSON{} | ||
//if err := json.Unmarshal([]byte(output), result); err != nil { | ||
// c.Errorf("failed to decode inspect output: %v", err) | ||
//} | ||
// | ||
//stdinOut := icmd.RunCommand("cat", "/tmp/checkInitScriptWorks").Stdout() | ||
//fmt.Printf("out=%s", stdinOut) | ||
//fmt.Printf("script = %s", icmd.RunCommand("cat", script).Stdout()) | ||
//// check the stdin get from runc includes container ID,PID etc. | ||
//c.Assert(strings.Contains(stdinOut, result.ID), check.Equals, true) | ||
//c.Assert(strings.Contains(stdinOut, string(result.State.Pid)), check.Equals, true) | ||
//c.Assert(strings.Contains(stdinOut, "ociVersion"), check.Equals, true) | ||
//c.Assert(strings.Contains(stdinOut, "status"), check.Equals, true) | ||
//c.Assert(strings.Contains(stdinOut, "bundle"), check.Equals, true) | ||
// | ||
//command.PouchRun("rm", "-f", cname) | ||
} | ||
|
||
// TestRichContainerDumbInitWorks check the dumb-init works. | ||
func (suite *PouchRichContainerSuite) TestRichContainerDumbInitWorks(c *check.C) { | ||
SkipIfFalse(c, environment.IsDumbInitExist) | ||
pc, _, _, _ := runtime.Caller(0) | ||
tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".") | ||
var funcname string | ||
for i := range tmpname { | ||
funcname = tmpname[i] | ||
} | ||
|
||
defer command.PouchRun("rm", "-f", funcname) | ||
|
||
{ | ||
command.PouchRun("run", "-d", "--rich", "--rich-mode", "dumb-init", "--name", funcname, busyboxImage, "sleep", "1000").Assert(c, icmd.Success) | ||
|
||
c.Assert(checkPidofProcessIsOne(funcname, "dumb-init"), check.Equals, true) | ||
|
||
// stop and start could work well. | ||
command.PouchRun("stop", funcname).Assert(c, icmd.Success) | ||
command.PouchRun("start", funcname).Assert(c, icmd.Success) | ||
c.Assert(checkPidofProcessIsOne(funcname, "dumb-init"), check.Equals, true) | ||
|
||
// pause and unpause | ||
command.PouchRun("pause", funcname).Assert(c, icmd.Success) | ||
command.PouchRun("unpause", funcname).Assert(c, icmd.Success) | ||
c.Assert(checkPidofProcessIsOne(funcname, "dumb-init"), check.Equals, true) | ||
|
||
command.PouchRun("rm", "-f", funcname) | ||
} | ||
|
||
//{ | ||
// checkInitScriptWorks(c, funcname, busyboxImage, "dumb-init") | ||
//} | ||
|
||
} | ||
|
||
// TestRichContainerWrongArgs check the wrong args of rich container. | ||
func (suite *PouchRichContainerSuite) TestRichContainerDumbInitWrongArgs(c *check.C) { | ||
SkipIfFalse(c, environment.IsDumbInitExist) | ||
|
||
// TODO | ||
// Don't add '--rich' when use other rich container related options should fail. | ||
|
||
} | ||
|
||
// TestRichContainerSbinInitWorks check the initd works. | ||
func (suite *PouchRichContainerSuite) TestRichContainerInitdWorks(c *check.C) { | ||
pc, _, _, _ := runtime.Caller(0) | ||
tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".") | ||
var funcname string | ||
for i := range tmpname { | ||
funcname = tmpname[i] | ||
} | ||
|
||
ok, _ := isFileExistsInImage(centosImage, "/sbin/init", "checkinit") | ||
if !ok { | ||
c.Skip("/sbin/init doesn't exist in test image") | ||
} | ||
|
||
defer command.PouchRun("rm", "-f", funcname) | ||
|
||
{ | ||
// --privileged is MUST required | ||
command.PouchRun("run", "-d", "--privileged", "--rich", "--rich-mode", "sbin-init", "--name", funcname, centosImage, "sleep 10000").Assert(c, icmd.Success) | ||
|
||
c.Assert(checkPidofProcessIsOne(funcname, "/sbin/init"), check.Equals, true) | ||
c.Assert(checkPPid(funcname, "sleep", "1"), check.Equals, true) | ||
|
||
// stop and start could work well. | ||
command.PouchRun("stop", funcname).Assert(c, icmd.Success) | ||
command.PouchRun("start", funcname).Assert(c, icmd.Success) | ||
c.Assert(checkPidofProcessIsOne(funcname, "/sbin/init"), check.Equals, true) | ||
c.Assert(checkPPid(funcname, "sleep", "1"), check.Equals, true) | ||
|
||
// pause and unpause | ||
command.PouchRun("pause", funcname).Assert(c, icmd.Success) | ||
command.PouchRun("unpause", funcname).Assert(c, icmd.Success) | ||
c.Assert(checkPidofProcessIsOne(funcname, "/sbin/init"), check.Equals, true) | ||
c.Assert(checkPPid(funcname, "sleep", "1"), check.Equals, true) | ||
|
||
command.PouchRun("rm", "-f", funcname) | ||
} | ||
|
||
//{ | ||
// checkInitScriptWorks(c, funcname, centosImage, "sbin-init") | ||
// | ||
//} | ||
} | ||
|
||
// TestRichContainerSystemdWorks check the systemd works. | ||
func (suite *PouchRichContainerSuite) TestRichContainerSystemdWorks(c *check.C) { | ||
pc, _, _, _ := runtime.Caller(0) | ||
tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".") | ||
var funcname string | ||
for i := range tmpname { | ||
funcname = tmpname[i] | ||
} | ||
|
||
ok, _ := isFileExistsInImage(centosImage, "/usr/lib/systemd/systemd", "checksysd") | ||
if !ok { | ||
c.Skip("/usr/lib/systemd/systemd doesn't exist in test image") | ||
} | ||
|
||
defer command.PouchRun("rm", "-f", funcname) | ||
|
||
{ | ||
command.PouchRun("run", "-d", "--privileged", "--rich", "--rich-mode", "systemd", "--name", funcname, centosImage, "echo test").Assert(c, icmd.Success) | ||
|
||
time.Sleep(1000000) | ||
|
||
c.Assert(checkPidofProcessIsOne(funcname, "/usr/lib/systemd/systemd"), check.Equals, true) | ||
c.Assert(checkPPid(funcname, "sleep", "1"), check.Equals, true) | ||
|
||
// stop and start could work well. | ||
command.PouchRun("stop", funcname).Assert(c, icmd.Success) | ||
command.PouchRun("start", funcname).Assert(c, icmd.Success) | ||
c.Assert(checkPidofProcessIsOne(funcname, "dumb-init"), check.Equals, true) | ||
c.Assert(checkPPid(funcname, "sleep", "1"), check.Equals, true) | ||
|
||
// pause and unpause | ||
command.PouchRun("pause", funcname).Assert(c, icmd.Success) | ||
command.PouchRun("unpause", funcname).Assert(c, icmd.Success) | ||
c.Assert(checkPidofProcessIsOne(funcname, "dumb-init"), check.Equals, true) | ||
c.Assert(checkPPid(funcname, "sleep", "1"), check.Equals, true) | ||
|
||
command.PouchRun("rm", "-f", funcname) | ||
} | ||
|
||
//{ | ||
// checkInitScriptWorks(c, funcname, centosImage, "systemd") | ||
//} | ||
} |
Oops, something went wrong.