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

test: add api tests for some commands #1747

Merged
merged 1 commit into from
Aug 1, 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
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.

92 changes: 92 additions & 0 deletions test/api_image_save_and_load_test.go
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use the tmpfile to store the intermediate result? It seems that it is tricky to use one response body as other request body. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will done soon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that save the body data into tmpFile not the var

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fuweid I don't think we should create a new file to store the response data stream, because this is a API test, not client test. In API test I think we handle the http response data is enough. And both save and load commands handle the data stream to tar file(or tar file to data stream) in client side, not the http response side. And in other API test, such as api_image_inspect_test and api_container_inspect_test, we just use the HTTP response data to check the correctness of this API function. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client test includes the api test, I think. cli wrap the client to provide better UEX. for this cases, the api test is same to the cli test. However, you connect two http connections, which is pretty hacking in testing. So I want to save the data in the local and send it again.


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)
}
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