From ec36ce2894d6336018486e9a825442a2f957a8ca Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Wed, 23 May 2018 15:22:25 +0800 Subject: [PATCH] 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 --- test/main_test.go | 3 --- test/util_api.go | 49 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/test/main_test.go b/test/main_test.go index f819c8cae..584901660 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/alibaba/pouch/client" - "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/environment" "github.com/go-check/check" ) @@ -27,8 +26,6 @@ func TestMain(m *testing.M) { } apiClient = commonAPIClient.(*client.APIClient) - command.PouchRun("pull", busyboxImage) - os.Exit(m.Run()) } diff --git a/test/util_api.go b/test/util_api.go index 99a2237d6..7fa9053fd 100644 --- a/test/util_api.go +++ b/test/util_api.go @@ -2,12 +2,16 @@ package main import ( "bufio" + "encoding/json" + "fmt" + "io" "net" "net/http" "net/url" "time" "github.com/alibaba/pouch/apis/types" + "github.com/alibaba/pouch/ctrd" "github.com/alibaba/pouch/test/request" "github.com/go-check/check" @@ -261,12 +265,43 @@ func PullImage(c *check.C, image string) { resp, err := request.Get("/images/" + image + "/json") c.Assert(err, check.IsNil) - if resp.StatusCode == 404 { - q := url.Values{} - q.Add("fromImage", image) - query := request.WithQuery(q) - resp, err = request.Post("/images/create", query) - c.Assert(err, check.IsNil) - c.Assert(resp.StatusCode, check.Equals, 200) + if resp.StatusCode == http.StatusOK { + resp.Body.Close() + return } + + q := url.Values{} + q.Add("fromImage", image) + resp, err = request.Post("/images/create", request.WithQuery(q)) + c.Assert(err, check.IsNil) + c.Assert(resp.StatusCode, check.Equals, 200) + + defer resp.Body.Close() + c.Assert(fetchPullStatus(resp.Body), check.IsNil) +} + +func fetchPullStatus(r io.ReadCloser) error { + dec := json.NewDecoder(r) + if _, err := dec.Token(); err != nil { + return fmt.Errorf("failed to read the opening token: %v", err) + } + + for dec.More() { + var infos []ctrd.ProgressInfo + + if err := dec.Decode(&infos); err != nil { + return fmt.Errorf("failed to decode: %v", err) + } + + for _, info := range infos { + if info.ErrorMessage != "" { + return fmt.Errorf(info.ErrorMessage) + } + } + } + + if _, err := dec.Token(); err != nil { + return fmt.Errorf("failed to read the closing token: %v", err) + } + return nil }