Skip to content

Commit 06ed8dc

Browse files
committed
test: add deamon related function
Signed-off-by: letty <[email protected]>
1 parent 1564d7a commit 06ed8dc

File tree

7 files changed

+385
-221
lines changed

7 files changed

+385
-221
lines changed

test/daemon/daemon.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package daemon
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/alibaba/pouch/test/command"
8+
"github.com/alibaba/pouch/test/util"
9+
"github.com/gotestyourself/gotestyourself/icmd"
10+
)
11+
12+
const (
13+
DaemonLog = "/tmp/pouch.log"
14+
PouchdBin = "pouchd"
15+
)
16+
17+
type DaemonConfig struct {
18+
Log string
19+
Args []string
20+
Bin string
21+
Listen []string
22+
}
23+
24+
var DConfig DaemonConfig
25+
26+
func init() {
27+
DConfig = NewDaemonConfig()
28+
}
29+
30+
func NewDaemonConfig() DaemonConfig{
31+
result := DaemonConfig{}
32+
result.Bin = PouchdBin
33+
result.Log = DaemonLog
34+
35+
return result
36+
}
37+
38+
// IsDaemonUp checks if the pouchd is launched.
39+
func (d *DaemonConfig) IsDaemonUp() bool{
40+
if len(d.Listen) != 0 {
41+
return command.PouchRun("-h",d.Listen[0],"version").ExitCode == 0
42+
}
43+
44+
result := command.PouchRun("version")
45+
fmt.Printf("result=%v",result)
46+
47+
return result.ExitCode == 0
48+
}
49+
50+
// StartDaemon starts pouchd
51+
func (d *DaemonConfig)StartDaemon() error {
52+
cmd := icmd.Command(d.Bin, d.Args...)
53+
result := icmd.StartCmd(cmd)
54+
55+
if util.WaitTimeout(time.Second * 5, d.IsDaemonUp) == false {
56+
d.KillDaemon()
57+
return fmt.Errorf("faile to launch pouchd within 5s, log:%v",result)
58+
}
59+
60+
return nil
61+
}
62+
63+
// KillDaemon kill pouchd.
64+
func (d *DaemonConfig) KillDaemon() error {
65+
if d.IsDaemonUp() == false {
66+
return nil
67+
}
68+
69+
result := icmd.RunCommand("pkill", "--signal", "9", d.Bin)
70+
if result.ExitCode != 0 {
71+
return fmt.Errorf("kill pouchd failed, err:%s", result.Stderr())
72+
}
73+
74+
for _,v := range d.Listen {
75+
icmd.RunCommand("rm","-f",v)
76+
}
77+
return nil
78+
}
79+
80+
81+

test/environment/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ func IsAliKernel() bool {
3333
return true
3434
}
3535
return false
36-
}
36+
}

test/main_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ func TestMain(m *testing.M) {
3333
func Test(t *testing.T) {
3434
check.TestingT(t)
3535
}
36+
37+
func init() {
38+
39+
}

test/util/util.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// WaitTimeout wait until the conditon become true or timeout.
9+
func WaitTimeout(timeout time.Duration, condition func () bool) bool {
10+
ch := make(chan bool,1)
11+
12+
ticker := time.NewTicker(500*time.Millisecond)
13+
defer ticker.Stop()
14+
15+
done := make(chan bool)
16+
go func() {
17+
time.Sleep(timeout)
18+
done <- true
19+
}()
20+
21+
for {
22+
select {
23+
case <-ch:
24+
return true
25+
case <-done:
26+
fmt.Errorf("timeout")
27+
return false
28+
case <-ticker.C:
29+
fmt.Printf("call condition")
30+
ret := condition()
31+
if ret == true {
32+
ch <- true
33+
}
34+
}
35+
}
36+
return false
37+
}
38+

test/util_api.go

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"net"
6+
"net/http"
7+
"net/url"
8+
9+
"github.com/alibaba/pouch/apis/types"
10+
"github.com/alibaba/pouch/test/request"
11+
12+
"github.com/go-check/check"
13+
)
14+
15+
// CheckRespStatus checks the http.Response.Status is equal to status.
16+
func CheckRespStatus(c *check.C, resp *http.Response, status int) {
17+
if resp.StatusCode != status {
18+
got := types.Error{}
19+
_ = request.DecodeBody(&got, resp.Body)
20+
c.Assert(resp.StatusCode, check.Equals, status, check.Commentf("Error:%s", got.Message))
21+
}
22+
}
23+
24+
// CreateBusyboxContainerOk creates a busybox container and asserts success.
25+
func CreateBusyboxContainerOk(c *check.C, cname string, cmd ...string) {
26+
// If not specified, CMD executed in container is "top".
27+
if len(cmd) == 0 {
28+
cmd = []string{"top"}
29+
}
30+
31+
resp, err := CreateBusyboxContainer(c, cname, cmd...)
32+
c.Assert(err, check.IsNil)
33+
CheckRespStatus(c, resp, 201)
34+
}
35+
36+
// CreateBusyboxContainer creates a basic container using busybox image.
37+
func CreateBusyboxContainer(c *check.C, cname string, cmd ...string) (*http.Response, error) {
38+
q := url.Values{}
39+
q.Add("name", cname)
40+
41+
obj := map[string]interface{}{
42+
"Image": busyboxImage,
43+
"Cmd": cmd,
44+
"HostConfig": map[string]interface{}{},
45+
}
46+
47+
path := "/containers/create"
48+
query := request.WithQuery(q)
49+
body := request.WithJSONBody(obj)
50+
return request.Post(path, query, body)
51+
}
52+
53+
// StartContainerOk starts the container and asserts success.
54+
func StartContainerOk(c *check.C, cname string) {
55+
resp, err := StartContainer(c, cname)
56+
c.Assert(err, check.IsNil)
57+
58+
CheckRespStatus(c, resp, 204)
59+
}
60+
61+
// StartContainer starts the container.
62+
func StartContainer(c *check.C, cname string) (*http.Response, error) {
63+
return request.Post("/containers/" + cname + "/start")
64+
}
65+
66+
// DelContainerForceOk forcely deletes the container and asserts success.
67+
func DelContainerForceOk(c *check.C, cname string) {
68+
resp, err := DelContainerForce(c, cname)
69+
c.Assert(err, check.IsNil)
70+
71+
CheckRespStatus(c, resp, 204)
72+
}
73+
74+
// DelContainerForce forcely deletes the container.
75+
func DelContainerForce(c *check.C, cname string) (*http.Response, error) {
76+
q := url.Values{}
77+
q.Add("force", "true")
78+
return request.Delete("/containers/"+cname, request.WithQuery(q))
79+
}
80+
81+
// StopContainerOk stops the container and asserts success..
82+
func StopContainerOk(c *check.C, cname string) {
83+
resp, err := StopContainer(c, cname)
84+
c.Assert(err, check.IsNil)
85+
86+
CheckRespStatus(c, resp, 204)
87+
}
88+
89+
// StopContainer stops the container.
90+
func StopContainer(c *check.C, cname string) (*http.Response, error) {
91+
return request.Post("/containers/" + cname + "/stop")
92+
}
93+
94+
// PauseContainerOk pauses the container and asserts success..
95+
func PauseContainerOk(c *check.C, cname string) {
96+
resp, err := PauseContainer(c, cname)
97+
c.Assert(err, check.IsNil)
98+
99+
CheckRespStatus(c, resp, 204)
100+
}
101+
102+
// PauseContainer pauses the container.
103+
func PauseContainer(c *check.C, cname string) (*http.Response, error) {
104+
return request.Post("/containers/" + cname + "/pause")
105+
}
106+
107+
// UnpauseContainerOk unpauses the container and asserts success..
108+
func UnpauseContainerOk(c *check.C, cname string) {
109+
resp, err := UnpauseContainer(c, cname)
110+
c.Assert(err, check.IsNil)
111+
112+
CheckRespStatus(c, resp, 204)
113+
}
114+
115+
// UnpauseContainer unpauses the container.
116+
func UnpauseContainer(c *check.C, cname string) (*http.Response, error) {
117+
return request.Post("/containers/" + cname + "/unpause")
118+
}
119+
120+
121+
// IsContainerCreated returns true is container's state is created.
122+
func IsContainerCreated(c *check.C, cname string) (bool, error) {
123+
return isContainerStateEqual(c, cname, "created")
124+
}
125+
126+
// IsContainerRunning returns true is container's state is running.
127+
func IsContainerRunning(c *check.C, cname string) (bool, error) {
128+
return isContainerStateEqual(c, cname, "running")
129+
}
130+
131+
func isContainerStateEqual(c *check.C, cname string, status string) (bool, error) {
132+
resp, err := request.Get("/containers/" + cname + "/json")
133+
c.Assert(err, check.IsNil)
134+
CheckRespStatus(c, resp, 200)
135+
136+
defer resp.Body.Close()
137+
got := types.ContainerJSON{}
138+
err = request.DecodeBody(&got, resp.Body)
139+
c.Assert(err, check.IsNil)
140+
141+
if got.State == nil {
142+
return false, nil
143+
}
144+
145+
return string(got.State.Status) == status, nil
146+
}
147+
148+
// CreateExecEchoOk exec process's environment with "echo" CMD.
149+
func CreateExecEchoOk(c *check.C, cname string) string {
150+
// NOTICE:
151+
// All files in the obj is needed, or start a new process may hang.
152+
obj := map[string]interface{}{
153+
"Cmd": []string{"echo", "test"},
154+
"Detach": true,
155+
"AttachStderr": true,
156+
"AttachStdout": true,
157+
"AttachStdin": true,
158+
"Privileged": false,
159+
"User": "",
160+
}
161+
body := request.WithJSONBody(obj)
162+
163+
resp, err := request.Post("/containers/"+cname+"/exec", body)
164+
c.Assert(err, check.IsNil)
165+
CheckRespStatus(c, resp, 201)
166+
167+
var got types.ExecCreateResp
168+
request.DecodeBody(&got, resp.Body)
169+
return got.ID
170+
}
171+
172+
// StartContainerExec starts executing a process in the container.
173+
func StartContainerExec(c *check.C, execid string, tty bool, detach bool) (*http.Response, net.Conn, *bufio.Reader, error) {
174+
obj := map[string]interface{}{
175+
"Detach": detach,
176+
"Tty": tty,
177+
}
178+
179+
return request.Hijack("/exec/"+execid+"/start",
180+
request.WithHeader("Connection", "Upgrade"),
181+
request.WithHeader("Upgrade", "tcp"),
182+
request.WithJSONBody(obj))
183+
}
184+
185+
// CreateVolume creates a volume in pouchd.
186+
func CreateVolume(c *check.C, name, driver string) error {
187+
obj := map[string]interface{}{
188+
"Driver": driver,
189+
"Name": name,
190+
}
191+
path := "/volumes/create"
192+
body := request.WithJSONBody(obj)
193+
194+
resp, err := request.Post(path, body)
195+
defer resp.Body.Close()
196+
197+
c.Assert(err, check.IsNil)
198+
CheckRespStatus(c, resp, 201)
199+
200+
return err
201+
}
202+
203+
// RemoveVolume removes a volume in pouchd.
204+
func RemoveVolume(c *check.C, name string) error {
205+
path := "/volumes/" + name
206+
resp, err := request.Delete(path)
207+
defer resp.Body.Close()
208+
209+
c.Assert(err, check.IsNil)
210+
CheckRespStatus(c, resp, 204)
211+
212+
return err
213+
}
214+
215+
216+
// DelNetworkOk deletes the network and asserts success.
217+
func DelNetworkOk(c *check.C, cname string) {
218+
resp, err := DelNetwork(c, cname)
219+
c.Assert(err, check.IsNil)
220+
221+
CheckRespStatus(c, resp, 204)
222+
}
223+
224+
// DelNetwork deletes the network.
225+
func DelNetwork(c *check.C, cname string) (*http.Response, error) {
226+
return request.Delete("/networks/" + cname)
227+
}

0 commit comments

Comments
 (0)