mirror of
https://github.com/gluster/glusterd2.git
synced 2026-02-05 12:45:38 +01:00
restclient: Modified e2e tests to use restclient
Updates: #265 Signed-off-by: Aravinda VK <mail@aravindavk.in>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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, "", "")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user