Skip to content

Commit

Permalink
test: add api test for image load and save
Browse files Browse the repository at this point in the history
Signed-off-by: xiechengsheng <[email protected]>
  • Loading branch information
xiechengsheng committed Jul 19, 2018
1 parent f251319 commit 863e968
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 29 deletions.
25 changes: 24 additions & 1 deletion test/api_container_pause_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"

Expand Down Expand Up @@ -32,12 +33,34 @@ func (suite *APIContainerPauseSuite) TestPauseUnpauseOk(c *check.C) {
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 204)

// TODO: Add state check
// add state check
resp, err = request.Get("/containers/" + cname + "/json")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)

got := types.ContainerJSON{}
err = request.DecodeBody(&got, resp.Body)
c.Assert(err, check.IsNil)
defer resp.Body.Close()

c.Assert(string(got.State.Status), check.Equals, "paused")

resp, err = request.Post("/containers/" + cname + "/unpause")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 204)

// add state check
resp, err = request.Get("/containers/" + cname + "/json")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)

got = types.ContainerJSON{}
err = request.DecodeBody(&got, resp.Body)
c.Assert(err, check.IsNil)
defer resp.Body.Close()

c.Assert(string(got.State.Status), check.Equals, "running")

DelContainerForceMultyTime(c, cname)
}

Expand Down
12 changes: 12 additions & 0 deletions test/api_image_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func (suite *APIImageDeleteSuite) TestDeleteNonExisting(c *check.C) {
CheckRespStatus(c, resp, 404)
}

// TestDeleteImageOk tests deleting an image is ok.
func (suite *APIImageDeleteSuite) TestDeleteImageOk(c *check.C) {
PullImage(c, helloworldImage)
resp, err := request.Delete("/images/" + helloworldImage)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 204)

resp, err = request.Get("/images/" + helloworldImage + "/json")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 404)
}

// TestDeleteUsingImage tests deleting an image in use by running container will fail.
func (suite *APIImageDeleteSuite) TestDeleteUsingImage(c *check.C) {
cname := "TestDeleteUsingImage"
Expand Down
22 changes: 0 additions & 22 deletions test/api_image_load_test.go

This file was deleted.

68 changes: 68 additions & 0 deletions test/api_image_save_and_load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"net/url"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"

"github.com/go-check/check"
)

// APIImageSaveLoadSuite is the test suite for image save and load API.
type APIImageSaveLoadSuite struct{}

func init() {
check.Suite(&APIImageSaveLoadSuite{})
}

// SetUpTest does common setup in the beginning of each test.
func (suite *APIImageSaveLoadSuite) SetUpTest(c *check.C) {
SkipIfFalse(c, environment.IsLinux)

PullImage(c, helloworldImage)
}

// TestImageSaveLoadOk tests saving and loading images are OK.
func (suite *APIImageSaveLoadSuite) TestImageSaveLoadOk(c *check.C) {
before, err := request.Get("/images/" + helloworldImage + "/json")
c.Assert(err, check.IsNil)
CheckRespStatus(c, before, 200)
gotBefore := types.ImageInfo{}
err = request.DecodeBody(&gotBefore, before.Body)
c.Assert(err, check.IsNil)

q := url.Values{}
q.Set("name", helloworldImage)
query := request.WithQuery(q)
resp, err := request.Get("/images/save", query)
c.Assert(err, check.IsNil)
defer resp.Body.Close()

loadImageName := "load-helloworld"
q = url.Values{}
q.Set("name", loadImageName)
q.Set("quiet", "0")

query = request.WithQuery(q)
reader := request.WithRawData(resp.Body)
header := request.WithHeader("Content-Type", "application/x-tar")

resp, err = request.Post("/images/load", query, reader, header)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)

after, err := request.Get("/images/" + loadImageName + ":" + environment.HelloworldTag + "/json")
c.Assert(err, check.IsNil)
CheckRespStatus(c, after, 200)
defer request.Delete("/images/" + loadImageName + ":" + environment.HelloworldTag)

gotAfter := types.ImageInfo{}
err = request.DecodeBody(&gotAfter, after.Body)
c.Assert(err, check.IsNil)

c.Assert(gotBefore.ID, check.Equals, gotAfter.ID)
c.Assert(gotBefore.CreatedAt, check.Equals, gotAfter.CreatedAt)
c.Assert(gotBefore.Size, check.Equals, gotAfter.Size)
}
13 changes: 7 additions & 6 deletions test/cli_save_and_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func (suite *PouchSaveLoadSuite) SetUpSuite(c *check.C) {

// TestSaveLoadWorks tests "pouch save" and "pouch load" work.
func (suite *PouchSaveLoadSuite) TestSaveLoadWorks(c *check.C) {
res := command.PouchRun("pull", busyboxImage)
res := command.PouchRun("pull", helloworldImage)
res.Assert(c, icmd.Success)

res = command.PouchRun("image", "inspect", busyboxImage)
res = command.PouchRun("image", "inspect", helloworldImage)
res.Assert(c, icmd.Success)

before := []types.ImageInfo{}
Expand All @@ -46,14 +46,15 @@ func (suite *PouchSaveLoadSuite) TestSaveLoadWorks(c *check.C) {
}
defer os.RemoveAll(dir)

res = command.PouchRun("save", "-o", filepath.Join(dir, "busybox.tar"), busyboxImage)
res = command.PouchRun("save", "-o", filepath.Join(dir, "helloworld.tar"), helloworldImage)
res.Assert(c, icmd.Success)

loadImageName := "load-busybox"
res = command.PouchRun("load", "-i", filepath.Join(dir, "busybox.tar"), loadImageName)
loadImageName := "load-helloworld"
res = command.PouchRun("load", "-i", filepath.Join(dir, "helloworld.tar"), loadImageName)
res.Assert(c, icmd.Success)
defer command.PouchRun("rmi", loadImageName+":"+environment.HelloworldTag)

res = command.PouchRun("image", "inspect", loadImageName+":"+environment.BusyboxTag)
res = command.PouchRun("image", "inspect", loadImageName+":"+environment.HelloworldTag)
res.Assert(c, icmd.Success)

after := []types.ImageInfo{}
Expand Down
8 changes: 8 additions & 0 deletions test/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func WithQuery(query url.Values) Option {
}
}

// WithRawData sets the input data with raw data
func WithRawData(data io.ReadCloser) Option {
return func(r *http.Request) error {
r.Body = data
return nil
}
}

// WithJSONBody encodes the input data to JSON and sets it to the body in http.Request
func WithJSONBody(obj interface{}) Option {
return func(r *http.Request) error {
Expand Down

0 comments on commit 863e968

Please sign in to comment.