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