forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_wait_test.go
112 lines (93 loc) · 3.48 KB
/
cli_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"time"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
// PouchWaitSuite is the test suite for wait CLI.
type PouchWaitSuite struct{}
func init() {
check.Suite(&PouchWaitSuite{})
}
// SetUpSuite does common setup in the beginning of each test suite.
func (suite *PouchWaitSuite) SetUpSuite(c *check.C) {
SkipIfFalse(c, environment.IsLinux)
environment.PruneAllContainers(apiClient)
PullImage(c, busyboxImage)
}
// TearDownTest does cleanup work in the end of each test.
func (suite *PouchWaitSuite) TearDownTest(c *check.C) {
}
// TestWaitNonBlockedExitZero is to verify the correctness of waiting a non-blocking container with 0 exit code
func (suite *PouchWaitSuite) TestWaitNonBlockedExitZero(c *check.C) {
name := "TestWaitNonBlockedExitZero"
command.PouchRun("run", "-d", "--name", name, busyboxImage, "sh", "-c", "true").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
res := command.PouchRun("wait", name)
res.Assert(c, icmd.Success)
output := res.Stdout()
c.Assert(output, check.Equals, fmt.Sprintf("%s\n", "0"))
}
// TestWaitBlockedExitZero is to verify the correctness of waiting a blocking container with 0 exit code
func (suite *PouchWaitSuite) TestWaitBlockedExitZero(c *check.C) {
name := "TestWaitBlockedExitZero"
command.PouchRun("run", "-d", "--name", name, busyboxImage, "/bin/sh", "-c", "trap 'exit 0' TERM; "+
"while true; do usleep 10; done").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
var output string
chWait := make(chan struct{})
go func() {
chWait <- struct{}{}
res := command.PouchRun("wait", name)
res.Assert(c, icmd.Success)
output = res.Stdout()
close(chWait)
}()
<-chWait
time.Sleep(100 * time.Millisecond)
command.PouchRun("stop", name).Assert(c, icmd.Success)
select {
case <-chWait:
c.Assert(output, check.Equals, fmt.Sprintf("%s\n", "0"))
case <-time.After(2 * time.Second):
c.Errorf("timeout waiting for `pouch wait` to exit")
}
}
// TestWaitNonBlockedExitRandom is to verify the correctness of waiting a non-blocking container with random exit code
func (suite *PouchWaitSuite) TestWaitNonBlockedExitRandom(c *check.C) {
name := "TestWaitNonBlockedExitRandom"
command.PouchRun("run", "-d", "--name", name, busyboxImage, "sh", "-c", "exit 99").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
res := command.PouchRun("wait", name)
res.Assert(c, icmd.Success)
output := res.Stdout()
c.Assert(output, check.Equals, fmt.Sprintf("%s\n", "99"))
}
// TestWaitBlockedExitRandom is to verify the correctness of waiting a blocking container with random exit code
func (suite *PouchWaitSuite) TestWaitBlockedExitRandom(c *check.C) {
name := "TestWaitBlockedExitRandom"
command.PouchRun("run", "-d", "--name", name, busyboxImage, "/bin/sh", "-c", "trap 'exit 99' TERM; "+
"while true; do usleep 10; done").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
var output string
chWait := make(chan struct{})
go func() {
chWait <- struct{}{}
res := command.PouchRun("wait", name)
res.Assert(c, icmd.Success)
output = res.Stdout()
close(chWait)
}()
<-chWait
time.Sleep(100 * time.Millisecond)
command.PouchRun("stop", name).Assert(c, icmd.Success)
select {
case <-chWait:
c.Assert(output, check.Equals, fmt.Sprintf("%s\n", "99"))
case <-time.After(2 * time.Second):
c.Errorf("timeout waiting for `pouch wait` to exit")
}
}