Skip to content

Commit ec36ce2

Browse files
author
Wei Fu
committed
bugfix: make the PullImage test util work
The post method of pullImage will hijack the connection so that the HTTP header always contains 200. It means that we cannot just check the HTTP status. We should read all the data bytes from server. Signed-off-by: Wei Fu <[email protected]>
1 parent 499f3cd commit ec36ce2

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

test/main_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
"github.com/alibaba/pouch/client"
9-
"github.com/alibaba/pouch/test/command"
109
"github.com/alibaba/pouch/test/environment"
1110
"github.com/go-check/check"
1211
)
@@ -27,8 +26,6 @@ func TestMain(m *testing.M) {
2726
}
2827
apiClient = commonAPIClient.(*client.APIClient)
2928

30-
command.PouchRun("pull", busyboxImage)
31-
3229
os.Exit(m.Run())
3330
}
3431

test/util_api.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package main
22

33
import (
44
"bufio"
5+
"encoding/json"
6+
"fmt"
7+
"io"
58
"net"
69
"net/http"
710
"net/url"
811
"time"
912

1013
"github.com/alibaba/pouch/apis/types"
14+
"github.com/alibaba/pouch/ctrd"
1115
"github.com/alibaba/pouch/test/request"
1216

1317
"github.com/go-check/check"
@@ -261,12 +265,43 @@ func PullImage(c *check.C, image string) {
261265
resp, err := request.Get("/images/" + image + "/json")
262266
c.Assert(err, check.IsNil)
263267

264-
if resp.StatusCode == 404 {
265-
q := url.Values{}
266-
q.Add("fromImage", image)
267-
query := request.WithQuery(q)
268-
resp, err = request.Post("/images/create", query)
269-
c.Assert(err, check.IsNil)
270-
c.Assert(resp.StatusCode, check.Equals, 200)
268+
if resp.StatusCode == http.StatusOK {
269+
resp.Body.Close()
270+
return
271271
}
272+
273+
q := url.Values{}
274+
q.Add("fromImage", image)
275+
resp, err = request.Post("/images/create", request.WithQuery(q))
276+
c.Assert(err, check.IsNil)
277+
c.Assert(resp.StatusCode, check.Equals, 200)
278+
279+
defer resp.Body.Close()
280+
c.Assert(fetchPullStatus(resp.Body), check.IsNil)
281+
}
282+
283+
func fetchPullStatus(r io.ReadCloser) error {
284+
dec := json.NewDecoder(r)
285+
if _, err := dec.Token(); err != nil {
286+
return fmt.Errorf("failed to read the opening token: %v", err)
287+
}
288+
289+
for dec.More() {
290+
var infos []ctrd.ProgressInfo
291+
292+
if err := dec.Decode(&infos); err != nil {
293+
return fmt.Errorf("failed to decode: %v", err)
294+
}
295+
296+
for _, info := range infos {
297+
if info.ErrorMessage != "" {
298+
return fmt.Errorf(info.ErrorMessage)
299+
}
300+
}
301+
}
302+
303+
if _, err := dec.Token(); err != nil {
304+
return fmt.Errorf("failed to read the closing token: %v", err)
305+
}
306+
return nil
272307
}

0 commit comments

Comments
 (0)