Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: make the PullImage test util work #1386

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -27,8 +26,6 @@ func TestMain(m *testing.M) {
}
apiClient = commonAPIClient.(*client.APIClient)

command.PouchRun("pull", busyboxImage)

os.Exit(m.Run())
}

Expand Down
49 changes: 42 additions & 7 deletions test/util_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}