forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_container_wait_test.go
80 lines (63 loc) · 1.96 KB
/
api_container_wait_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"net/http"
"time"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"
"github.com/go-check/check"
)
// APIContainerWaitSuite is the test suite for container wait API.
type APIContainerWaitSuite struct{}
func init() {
check.Suite(&APIContainerWaitSuite{})
}
// SetUpTest does common setup in the beginning of each test.
func (suite *APIContainerWaitSuite) SetUpTest(c *check.C) {
SkipIfFalse(c, environment.IsLinux)
PullImage(c, busyboxImage)
}
// TestWaitOk tests waiting a stopped container return 200.
func (suite *APIContainerWaitSuite) TestWaitOk(c *check.C) {
cname := "TestWaitOk"
CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)
StartContainerOk(c, cname)
StopContainerOk(c, cname)
resp, err := request.Post("/containers/" + cname + "/wait")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)
}
// TestWaitRunningContainer tests waiting a running container to stop, then returns 200.
func (suite *APIContainerWaitSuite) TestWaitRunningContainer(c *check.C) {
cname := "TestWaitRunningContainer"
CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)
StartContainerOk(c, cname)
var (
err error
resp *http.Response
)
chWait := make(chan struct{})
go func() {
chWait <- struct{}{}
resp, err = request.Post("/containers/" + cname + "/wait")
close(chWait)
}()
<-chWait
time.Sleep(100 * time.Millisecond)
StopContainerOk(c, cname)
select {
case <-chWait:
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)
case <-time.After(2 * time.Second):
c.Errorf("timeout waiting for `pouch wait` API to exit")
}
}
// TestWaitNonExistingContainer tests waiting a non-existing container return 404.
func (suite *APIContainerWaitSuite) TestWaitNonExistingContainer(c *check.C) {
cname := "TestNonExistingContainer"
resp, err := request.Post("/containers/" + cname + "/wait")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 404)
}