forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_container_exec_start_test.go
170 lines (136 loc) · 4.79 KB
/
api_container_exec_start_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/hex"
"io"
"net"
"strings"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-check/check"
)
// APIContainerExecStartSuite is the test suite for container exec start API.
type APIContainerExecStartSuite struct{}
func init() {
check.Suite(&APIContainerExecStartSuite{})
}
// SetUpTest does common setup in the beginning of each test.
func (suite *APIContainerExecStartSuite) SetUpTest(c *check.C) {
SkipIfFalse(c, environment.IsLinux)
PullImage(c, busyboxImage)
}
func checkEchoSuccess(c *check.C, tty bool, conn net.Conn, br *bufio.Reader, exp string) {
defer conn.Close()
// Allocate a large space incase there is error.
var (
buf bytes.Buffer
err error
)
if !tty {
_, err = stdcopy.StdCopy(&buf, &buf, br)
} else {
_, err = io.Copy(&buf, br)
}
c.Assert(err, check.IsNil)
c.Assert(strings.TrimSpace(buf.String()), check.Equals, exp, check.Commentf("Expected %s, got %s", exp, buf.String()))
}
func (suite *APIContainerExecStartSuite) TestContainerExecWithLongInput(c *check.C) {
cname := "TestContainerExecWithLongInput"
CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)
StartContainerOk(c, cname)
var execID string
// create exec
{
// echo the input
obj := map[string]interface{}{
"Cmd": []string{"cat"},
"AttachStderr": true,
"AttachStdout": true,
"AttachStdin": true,
"Tty": true, // avoid to use docker stdcopy
}
body := request.WithJSONBody(obj)
resp, err := request.Post("/containers/"+cname+"/exec", body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 201)
var got types.ExecCreateResp
c.Assert(request.DecodeBody(&got, resp.Body), check.IsNil)
execID = got.ID
}
// start exec
{
obj := map[string]interface{}{
"Tty": true, // avoid to use docker stdcopy
}
resp, conn, br, err := request.Hijack("/exec/"+execID+"/start", request.WithJSONBody(obj))
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)
defer conn.Close()
// input 256 chars
input := make([]byte, 256)
_, err = rand.Read(input)
c.Assert(err, check.IsNil)
content := hex.EncodeToString(input)
_, err = conn.Write([]byte(content + "\n"))
c.Assert(err, check.IsNil)
reader := bufio.NewReader(br)
got, _, err := reader.ReadLine()
c.Assert(err, check.IsNil)
c.Assert(string(got), check.Equals, content)
}
}
// TestContainerExecStartWithoutUpgrade tests start exec without upgrade which will return 200 OK.
func (suite *APIContainerExecStartSuite) TestContainerExecStartWithoutUpgrade(c *check.C) {
cname := "TestContainerCreateExecStartWithoutUpgrade"
content := "hi pouch"
CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)
StartContainerOk(c, cname)
execid := CreateExecEchoOk(c, cname, content)
obj := map[string]interface{}{}
resp, conn, br, err := request.Hijack("/exec/"+execid+"/start", request.WithJSONBody(obj))
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)
checkEchoSuccess(c, false, conn, br, content)
}
// TestContainerExecStartOk tests start exec.
func (suite *APIContainerExecStartSuite) TestContainerExecStart(c *check.C) {
cname := "TestContainerCreateExecStart"
content := "test"
CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)
StartContainerOk(c, cname)
execid := CreateExecEchoOk(c, cname, content)
resp, conn, reader, err := StartContainerExec(c, execid, false, false)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 101)
checkEchoSuccess(c, false, conn, reader, content)
}
// TestContainerExecStartNotFound tests starting an non-existing execID return error.
func (suite *APIContainerExecStartSuite) TestContainerExecStartNotFound(c *check.C) {
obj := map[string]interface{}{}
body := request.WithJSONBody(obj)
resp, err := request.Post("/exec/TestContainerExecStartNotFound/start", body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 404)
}
// TestContainerExecStartStopped tests start a process in a stopped container return error.
func (suite *APIContainerExecStartSuite) TestContainerExecStartStopped(c *check.C) {
// TODO: missing case
helpwantedForMissingCase(c, "container api exec start stoped case")
}
// TestContainerExecStartPaused tests start a process in a paused container return error.
func (suite *APIContainerExecStartSuite) TestContainerExecStartPaused(c *check.C) {
// TODO: missing case
helpwantedForMissingCase(c, "container api exec start paused case")
}
// TestContainerExecStartDup tests start a process twice return error.
func (suite *APIContainerExecStartSuite) TestContainerExecStartDup(c *check.C) {
// TODO: missing case
helpwantedForMissingCase(c, "container api exec start twice case")
}