From 5cef5dcc7e535c266f7a2ac1c6374b7f851e6059 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Sat, 30 Apr 2016 13:21:35 -0700 Subject: [PATCH] Add some cluster-level helpers Leader forwarding cannot be fully tested yet, as it needs the cluster service also instantiated. --- system_test/helpers.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/system_test/helpers.go b/system_test/helpers.go index c578a9eb..7a2e84af 100644 --- a/system_test/helpers.go +++ b/system_test/helpers.go @@ -92,6 +92,26 @@ func (n *Node) Join(leader *Node) error { return nil } +// ConfirmRedirect confirms that the node responds with a redirect to the given host. +func (n *Node) ConfirmRedirect(host string) bool { + v, _ := url.Parse("http://" + n.APIAddr + "/db/query") + v.RawQuery = url.Values{"q": []string{`SELECT * FROM foo`}}.Encode() + + resp, err := http.Get(v.String()) + if err != nil { + return false + } + defer resp.Body.Close() + fmt.Println(resp.StatusCode) + if resp.StatusCode != http.StatusMovedPermanently { + return false + } + if resp.Header.Get("location") != host { + return false + } + return true +} + func (n *Node) postExecute(stmt string) (string, error) { resp, err := http.Post("http://"+n.APIAddr+"/db/execute", "application/json", strings.NewReader(stmt)) if err != nil { @@ -140,6 +160,26 @@ func (c Cluster) WaitForNewLeader(old *Node) (*Node, error) { } } +// Followers returns the slide of nodes in the cluster that are followers. +func (c Cluster) Followers() ([]*Node, error) { + n, err := c[0].WaitForLeader() + if err != nil { + return nil, err + } + leader, err := c.FindNodeByRaftAddr(n) + if err != nil { + return nil, err + } + + var followers []*Node + for _, n := range c { + if n != leader { + followers = append(followers, n) + } + } + return followers, nil +} + // RemoveNode removes the given node from the cluster. func (c Cluster) RemoveNode(node *Node) { for i, n := range c {