diff --git a/systest/group-delete/group_delete_test.go b/systest/group-delete/group_delete_test.go index de0ce3dc96f..205b9388fee 100644 --- a/systest/group-delete/group_delete_test.go +++ b/systest/group-delete/group_delete_test.go @@ -40,7 +40,16 @@ import ( func NodesSetup(t *testing.T, c *dgo.Dgraph) { ctx := context.Background() - require.NoError(t, c.Alter(ctx, &api.Operation{DropAll: true})) + // Retry DropAll to make sure the nodes is up and running. + var err error + for i := 0; i < 3; i ++ { + if err =c.Alter(ctx, &api.Operation{DropAll: true}); err != nil { + time.Sleep(5*time.Second) + continue + } + break + } + require.NoError(t, err, "error while dropping all the data") schema, err := ioutil.ReadFile(`../data/goldendata.schema`) require.NoError(t, err) @@ -157,7 +166,10 @@ func TestNodes(t *testing.T) { t.Errorf("moving tablets failed") } - resp, err := http.Get("http://" + testutil.SockAddrZeroHttp + "/removeNode?group=3&id=3") + groupNodes, err := testutil.GetNodesInGroup("3") + require.NoError(t, err) + resp, err := http.Get("http://" + testutil.SockAddrZeroHttp + "/removeNode?group=3&id=" + + groupNodes[0]) require.NoError(t, err) require.NoError(t, getError(resp.Body)) @@ -191,7 +203,10 @@ func TestNodes(t *testing.T) { t.Errorf("moving tablets failed") } - resp, err = http.Get("http://" + testutil.SockAddrZeroHttp + "/removeNode?group=2&id=2") + groupNodes, err = testutil.GetNodesInGroup("2") + require.NoError(t, err) + resp, err = http.Get("http://" + testutil.SockAddrZeroHttp + "/removeNode?group=2&id=" + + groupNodes[0]) require.NoError(t, err) require.NoError(t, getError(resp.Body)) diff --git a/testutil/zero.go b/testutil/zero.go index e07b33b92c4..43d69740822 100644 --- a/testutil/zero.go +++ b/testutil/zero.go @@ -31,17 +31,19 @@ import ( "google.golang.org/grpc" ) +type Member struct { + Addr string `json:"addr"` + GroupID int `json:"groupId"` + ID string `json:"id"` + LastUpdate string `json:"lastUpdate"` + Leader bool `json:"leader"` +} + // StateResponse represents the structure of the JSON object returned by calling // the /state endpoint in zero. type StateResponse struct { Groups map[string]struct { - Members map[string]struct { - Addr string `json:"addr"` - GroupID int `json:"groupId"` - ID string `json:"id"` - LastUpdate string `json:"lastUpdate"` - Leader bool `json:"leader"` - } `json:"members"` + Members map[string]Member `json:"members"` Tablets map[string]struct { GroupID int `json:"groupId"` Predicate string `json:"predicate"` @@ -79,22 +81,27 @@ func GetState() (*StateResponse, error) { } // GetClientToGroup returns a dgraph client connected to an alpha in the given group. -func GetClientToGroup(groupID string) (*dgo.Dgraph, error) { +func GetClientToGroup(gid string) (*dgo.Dgraph, error) { state, err := GetState() if err != nil { return nil, err } - group, ok := state.Groups[groupID] + group, ok := state.Groups[gid] if !ok { - return nil, errors.Errorf("group %s does not exist", groupID) + return nil, errors.Errorf("group %s does not exist", gid) } if len(group.Members) == 0 { - return nil, errors.Errorf("the group %s has no members", groupID) + return nil, errors.Errorf("the group %s has no members", gid) } - member := group.Members["1"] + // Select the first member found in the iteration. + var member Member + for _, m := range group.Members { + member = m + break + } parts := strings.Split(member.Addr, ":") if len(parts) != 2 { return nil, errors.Errorf("the member has an invalid address: %v", member.Addr) @@ -114,3 +121,25 @@ func GetClientToGroup(groupID string) (*dgo.Dgraph, error) { } return dgo.NewDgraphClient(api.NewDgraphClient(conn)), nil } + +func GetNodesInGroup(gid string) ([]string, error) { + state, err := GetState() + if err != nil { + return nil, err + } + + group, ok := state.Groups[gid] + if !ok { + return nil, errors.Errorf("group %s does not exist", gid) + } + + if len(group.Members) == 0 { + return nil, errors.Errorf("the group %s has no members", gid) + } + + nodes := make([]string, 0) + for id := range group.Members { + nodes = append(nodes, id) + } + return nodes, nil +}