diff --git a/test/api_container_pause_test.go b/test/api_container_pause_test.go index 96d8ae403..b47009503 100644 --- a/test/api_container_pause_test.go +++ b/test/api_container_pause_test.go @@ -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" @@ -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) } diff --git a/test/api_image_delete_test.go b/test/api_image_delete_test.go index 02690c1a8..289eb7fe0 100644 --- a/test/api_image_delete_test.go +++ b/test/api_image_delete_test.go @@ -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" diff --git a/test/api_image_load_test.go b/test/api_image_load_test.go deleted file mode 100644 index 1c1f18d3e..000000000 --- a/test/api_image_load_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "github.com/alibaba/pouch/test/environment" - - "github.com/go-check/check" -) - -// APIImageLoadSuite is the test suite for image load API. -type APIImageLoadSuite struct{} - -func init() { - check.Suite(&APIImageLoadSuite{}) -} - -// SetUpTest does common setup in the beginning of each test. -func (suite *APIImageLoadSuite) SetUpTest(c *check.C) { - SkipIfFalse(c, environment.IsLinux) -} - -// TODO(fuwei): We cannot upload the oci.v1 format tar into repo because it will -// increase our repo size. The test will be done with "pouch save" functionality. diff --git a/test/api_image_save_and_load_test.go b/test/api_image_save_and_load_test.go new file mode 100644 index 000000000..e98cda14b --- /dev/null +++ b/test/api_image_save_and_load_test.go @@ -0,0 +1,92 @@ +package main + +import ( + "io" + "io/ioutil" + "net/url" + "os" + "path/filepath" + + "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() + + dir, err := ioutil.TempDir("", "TestImageSaveLoadOk") + if err != nil { + c.Errorf("failed to create a new temporary directory: %v", err) + } + defer os.RemoveAll(dir) + + tmpFile := filepath.Join(dir, "helloworld.tar") + f, err := os.Create(tmpFile) + if err != nil { + c.Errorf("failed to create file: %v", err) + } + + if _, err := io.Copy(f, resp.Body); err != nil { + c.Errorf("failed to save data to file: %v", err) + } + + data, err := os.Open(tmpFile) + if err != nil { + c.Errorf("failed to load file's data: %v", err) + } + + loadImageName := "load-helloworld" + q = url.Values{} + q.Set("name", loadImageName) + + query = request.WithQuery(q) + reader := request.WithRawData(data) + 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) +} diff --git a/test/cli_save_and_load_test.go b/test/cli_save_and_load_test.go index 33936aaf6..c2e2698ee 100644 --- a/test/cli_save_and_load_test.go +++ b/test/cli_save_and_load_test.go @@ -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{} @@ -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{} diff --git a/test/request/request.go b/test/request/request.go index e8c8372b1..477251459 100644 --- a/test/request/request.go +++ b/test/request/request.go @@ -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 {