From 61ee3e382ca466c8ea9ec8ceb67d888a10ffe27f Mon Sep 17 00:00:00 2001 From: Aravinda VK Date: Fri, 11 Aug 2017 12:41:14 +0530 Subject: [PATCH] restclient: Modified e2e tests to use restclient Updates: #265 Signed-off-by: Aravinda VK --- e2e/peer_ops_test.go | 49 ++++++++-------------------------------- e2e/restart_test.go | 41 ++++++++++++--------------------- e2e/utils_test.go | 5 ++++ e2e/volume_ops_test.go | 25 +++++--------------- pkg/api/resp.go | 3 +++ pkg/restclient/volume.go | 7 ++++++ 6 files changed, 44 insertions(+), 86 deletions(-) diff --git a/e2e/peer_ops_test.go b/e2e/peer_ops_test.go index 25b8392e..69d2011f 100644 --- a/e2e/peer_ops_test.go +++ b/e2e/peer_ops_test.go @@ -1,18 +1,10 @@ package e2e import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "strings" "testing" "time" - "github.com/gluster/glusterd2/pkg/api" "github.com/stretchr/testify/require" - - "github.com/gluster/glusterd2/peer" ) func TestAddRemovePeer(t *testing.T) { @@ -36,48 +28,25 @@ func TestAddRemovePeer(t *testing.T) { defer g3.EraseWorkdir() r.True(g3.IsRunning()) - // add peer: ask g1 to add g2 as peer - peerAddReq := api.PeerAddReq{ - Addresses: []string{g2.PeerAddress}, - } - reqBody, err := json.Marshal(peerAddReq) - r.Nil(err) + client := initRestclient(g1.ClientAddress) - resp, err := http.Post("http://"+g1.ClientAddress+"/v1/peers", "application/json", strings.NewReader(string(reqBody))) - r.Nil(err) - defer resp.Body.Close() - r.Equal(http.StatusCreated, resp.StatusCode) + _, err2 := client.PeerProbe(g2.PeerAddress) + r.Nil(err2) time.Sleep(5 * time.Second) // add peer: ask g1 to add g3 as peer - reqBody = strings.NewReader(fmt.Sprintf(`{"addresses": ["%s"]}`, g3.PeerAddress)) - resp, err = http.Post("http://"+g1.ClientAddress+"/v1/peers", "application/json", reqBody) - r.Nil(err) - defer resp.Body.Close() - r.Equal(http.StatusCreated, resp.StatusCode) + _, err3 := client.PeerProbe(g3.PeerAddress) + r.Nil(err3) time.Sleep(5 * time.Second) // list and check you have 3 peers in cluster - resp, err = http.Get("http://" + g1.ClientAddress + "/v1/peers") - r.Nil(err) - defer resp.Body.Close() - r.Equal(http.StatusOK, resp.StatusCode) - - data, err := ioutil.ReadAll(resp.Body) - r.Nil(err) - var peers []peer.Peer - err = json.Unmarshal(data, &peers) - r.Nil(err) + peers, err4 := client.Peers() + r.Nil(err4) r.Len(peers, 3) // remove peer: ask g1 to remove g2 as peer - delURL := fmt.Sprintf("http://%s/v1/peers/%s", g1.ClientAddress, g2.PeerID()) - req, err := http.NewRequest("DELETE", delURL, nil) - r.Nil(err) - resp, err = http.DefaultClient.Do(req) - r.Nil(err) - defer resp.Body.Close() - r.Equal(http.StatusNoContent, resp.StatusCode) + err5 := client.PeerDetachByID(g2.PeerID()) + r.Nil(err5) } diff --git a/e2e/restart_test.go b/e2e/restart_test.go index 610d8c1a..2a711b4b 100644 --- a/e2e/restart_test.go +++ b/e2e/restart_test.go @@ -1,14 +1,11 @@ package e2e import ( - "encoding/json" - "fmt" "io/ioutil" - "net/http" "os" - "strings" "testing" + "github.com/gluster/glusterd2/pkg/api" "github.com/stretchr/testify/require" ) @@ -20,23 +17,21 @@ func TestRestart(t *testing.T) { r.Nil(err) r.True(gd.IsRunning()) - reqT := ` -{ - "name": "vol1", - "bricks": ["%s:%s"], - "force": true -} - ` dir, err := ioutil.TempDir("", "") r.Nil(err) defer os.RemoveAll(dir) - req := fmt.Sprintf(reqT, gd.PeerAddress, dir) + client := initRestclient(gd.ClientAddress) - resp, err := http.Post(fmt.Sprintf("http://%s/v1/volumes", gd.ClientAddress), "application/json", strings.NewReader(req)) - r.Nil(err) - defer resp.Body.Close() - r.Equal(http.StatusCreated, resp.StatusCode) + createReq := api.VolCreateReq{ + Name: "vol1", + Bricks: []string{ + gd.ClientAddress + ":" + dir, + }, + Force: true, + } + _, errVolCreate := client.VolumeCreate(createReq) + r.Nil(errVolCreate) r.Len(getVols(gd, r), 1) @@ -51,17 +46,9 @@ func TestRestart(t *testing.T) { r.Nil(gd.Stop()) } -func getVols(gd *gdProcess, r *require.Assertions) map[string]string { - resp, err := http.Get(fmt.Sprintf("http://%s/v1/volumes", gd.ClientAddress)) +func getVols(gd *gdProcess, r *require.Assertions) api.VolList { + client := initRestclient(gd.ClientAddress) + vols, err := client.Volumes() r.Nil(err) - r.Equal(http.StatusOK, resp.StatusCode) - defer resp.Body.Close() - - vols := make(map[string]string) - data, err := ioutil.ReadAll(resp.Body) - r.Nil(err) - err = json.Unmarshal(data, &vols) - r.Nil(err) - return vols } diff --git a/e2e/utils_test.go b/e2e/utils_test.go index d2e62d9a..522d1abe 100644 --- a/e2e/utils_test.go +++ b/e2e/utils_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/gluster/glusterd2/pkg/api" + "github.com/gluster/glusterd2/pkg/restclient" "gopkg.in/yaml.v2" ) @@ -181,3 +182,7 @@ func teardownCluster(gds []*gdProcess) error { } return nil } + +func initRestclient(clientAddress string) *restclient.Client { + return restclient.New("http://"+clientAddress, "", "") +} diff --git a/e2e/volume_ops_test.go b/e2e/volume_ops_test.go index a7eb617f..6829269f 100644 --- a/e2e/volume_ops_test.go +++ b/e2e/volume_ops_test.go @@ -1,12 +1,8 @@ package e2e import ( - "encoding/json" - "fmt" "io/ioutil" - "net/http" "os" - "strings" "testing" "github.com/gluster/glusterd2/pkg/api" @@ -30,6 +26,8 @@ func TestVolumeCreateDelete(t *testing.T) { brickPaths = append(brickPaths, brickPath) } + client := initRestclient(gds[0].ClientAddress) + // create 2x2 dist-rep volume volname := "testvol" createReq := api.VolCreateReq{ @@ -42,21 +40,10 @@ func TestVolumeCreateDelete(t *testing.T) { gds[1].PeerAddress + ":" + brickPaths[3]}, Force: true, } - reqBody, err := json.Marshal(createReq) - r.Nil(err) - - volCreateURL := fmt.Sprintf("http://%s/v1/volumes", gds[0].ClientAddress) - resp, err := http.Post(volCreateURL, "application/json", strings.NewReader(string(reqBody))) - r.Nil(err) - defer resp.Body.Close() - r.Equal(resp.StatusCode, 201) + _, errVolCreate := client.VolumeCreate(createReq) + r.Nil(errVolCreate) // delete volume - volDelURL := fmt.Sprintf("http://%s/v1/volumes/%s", gds[0].ClientAddress, volname) - delReq, err := http.NewRequest("DELETE", volDelURL, nil) - r.Nil(err) - resp, err = http.DefaultClient.Do(delReq) - r.Nil(err) - defer resp.Body.Close() - r.Equal(resp.StatusCode, 200) + errVolDel := client.VolumeDelete(volname) + r.Nil(errVolDel) } diff --git a/pkg/api/resp.go b/pkg/api/resp.go index 02067db3..9cc19705 100644 --- a/pkg/api/resp.go +++ b/pkg/api/resp.go @@ -47,3 +47,6 @@ type Volinfo struct { Bricks []Brickinfo Auth VolAuth // TODO: should not be returned to client } + +// VolList respresents volumes list +type VolList map[string]string diff --git a/pkg/restclient/volume.go b/pkg/restclient/volume.go index 023abba1..68070fff 100644 --- a/pkg/restclient/volume.go +++ b/pkg/restclient/volume.go @@ -14,6 +14,13 @@ func (c *Client) VolumeCreate(req api.VolCreateReq) (api.Volinfo, error) { return vol, err } +// Volumes returns list of all volumes +func (c *Client) Volumes() (api.VolList, error) { + var vols api.VolList + err := c.get("/v1/volumes", nil, http.StatusOK, &vols) + return vols, err +} + // VolumeStart starts a Gluster Volume func (c *Client) VolumeStart(volname string) error { url := fmt.Sprintf("/v1/volumes/%s/start", volname)