1
0
Fork 0

Joiner unit tests

master
Philip O'Toole 10 months ago
parent 002973f787
commit a96a5fa571

@ -1,195 +1,163 @@
package cluster package cluster
import ( import (
"encoding/json" "net"
"io"
"net/http"
"net/http/httptest"
"testing" "testing"
"time" "time"
"github.com/rqlite/rqlite/cluster/servicetest"
"google.golang.org/protobuf/proto"
) )
const numAttempts int = 3 const numAttempts int = 3
const attemptInterval = 1 * time.Second const attemptInterval = 1 * time.Second
func Test_SingleJoinOK(t *testing.T) { XXXXXX THIS TEST NEEDS TO BE UPDATED func Test_SingleJoinOK(t *testing.T) {
var body map[string]interface{} srv := servicetest.NewService()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv.Handler = func(conn net.Conn) {
if r.Method != "POST" { var p []byte
t.Fatalf("Client did not use POST") var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
} }
w.WriteHeader(http.StatusOK) if c.Type != Command_COMMAND_TYPE_JOIN {
t.Fatalf("unexpected command type: %d", c.Type)
if r.Header["Content-Type"][0] != "application/json" {
t.Fatalf("incorrect Content-Type set")
} }
jr := c.GetJoinRequest()
b, err := io.ReadAll(r.Body) if jr == nil {
if err != nil { t.Fatal("join request is nil")
w.WriteHeader(http.StatusBadRequest)
return
} }
if exp, got := "id0", jr.Id; exp != got {
if err := json.Unmarshal(b, &body); err != nil { t.Fatalf("unexpected id, got %s, exp: %s", got, exp)
w.WriteHeader(http.StatusBadRequest) }
return if exp, got := "1.2.3.4", jr.Address; exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
} }
}))
defer ts.Close()
joiner := NewJoiner("127.0.0.1", numAttempts, attemptInterval, nil) resp := &CommandJoinResponse{}
p, err = proto.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
}
srv.Start()
defer srv.Close()
// Ensure joining with protocol prefix works. c := NewClient(&simpleDialer{}, 0)
j, err := joiner.Do([]string{ts.URL}, "id0", "127.0.0.1:9090", false) joiner := NewJoiner(c, numAttempts, attemptInterval)
addr, err := joiner.Do([]string{srv.Addr()}, "id0", "1.2.3.4", true)
if err != nil { if err != nil {
t.Fatalf("failed to join a single node: %s", err.Error()) t.Fatal(err)
} }
if j != ts.URL+"/join" { if exp, got := srv.Addr(), addr; exp != got {
t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", j, ts.URL) t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
} }
}
if got, exp := body["id"].(string), "id0"; got != exp { func Test_SingleJoinZeroAttempts(t *testing.T) {
t.Fatalf("wrong node ID supplied, exp %s, got %s", exp, got) srv := servicetest.NewService()
srv.Handler = func(conn net.Conn) {
t.Fatalf("handler should not have been called")
} }
if got, exp := body["addr"].(string), "127.0.0.1:9090"; got != exp { srv.Start()
t.Fatalf("wrong address supplied, exp %s, got %s", exp, got) defer srv.Close()
}
if got, exp := body["voter"].(bool), false; got != exp { c := NewClient(&simpleDialer{}, 0)
t.Fatalf("wrong voter state supplied, exp %v, got %v", exp, got) joiner := NewJoiner(c, 0, attemptInterval)
_, err := joiner.Do([]string{srv.Addr()}, "id0", "1.2.3.4", true)
if err != ErrJoinFailed {
t.Fatalf("Incorrect error returned when zero attempts specified")
} }
}
// Ensure joining without protocol prefix works. func Test_SingleJoinFail(t *testing.T) {
j, err = joiner.Do([]string{ts.Listener.Addr().String()}, "id0", "127.0.0.1:9090", false) srv := servicetest.NewService()
if err != nil { srv.Handler = func(conn net.Conn) {
t.Fatalf("failed to join a single node: %s", err.Error()) var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
resp := &CommandJoinResponse{
Error: "bad request",
}
p, err = proto.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
} }
if j != ts.URL+"/join" { srv.Start()
t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", j, ts.URL) defer srv.Close()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, numAttempts, attemptInterval)
_, err := joiner.Do([]string{srv.Addr()}, "id0", "1.2.3.4", true)
if err == nil {
t.Fatalf("expected error when joining bad node")
} }
}
if got, exp := body["id"].(string), "id0"; got != exp { func Test_DoubleJoinOKSecondNode(t *testing.T) {
t.Fatalf("wrong node ID supplied, exp %s, got %s", exp, got) srv1 := servicetest.NewService()
srv1.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
resp := &CommandJoinResponse{
Error: "bad request",
}
p, err = proto.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
} }
if got, exp := body["addr"].(string), "127.0.0.1:9090"; got != exp { srv1.Start()
t.Fatalf("wrong address supplied, exp %s, got %s", exp, got) defer srv1.Close()
srv2 := servicetest.NewService()
srv2.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
if c.Type != Command_COMMAND_TYPE_JOIN {
t.Fatalf("unexpected command type: %d", c.Type)
}
resp := &CommandJoinResponse{}
p, err = proto.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
} }
if got, exp := body["voter"].(bool), false; got != exp { srv2.Start()
t.Fatalf("wrong voter state supplied, exp %v, got %v", exp, got) defer srv2.Close()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, numAttempts, attemptInterval)
addr, err := joiner.Do([]string{srv1.Addr(), srv2.Addr()}, "id0", "1.2.3.4", true)
if err != nil {
t.Fatal(err)
}
if exp, got := srv2.Addr(), addr; exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
} }
} }
// func Test_SingleJoinZeroAttempts(t *testing.T) {
// ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// t.Fatalf("handler should not have been called")
// }))
// joiner := NewJoiner("127.0.0.1", 0, attemptInterval, nil)
// _, err := joiner.Do([]string{ts.URL}, "id0", "127.0.0.1:9090", false)
// if err != ErrJoinFailed {
// t.Fatalf("Incorrect error returned when zero attempts specified")
// }
// }
// func Test_SingleJoinFail(t *testing.T) {
// ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusBadRequest)
// }))
// defer ts.Close()
// joiner := NewJoiner("", 0, attemptInterval, nil)
// _, err := joiner.Do([]string{ts.URL}, "id0", "127.0.0.1:9090", true)
// if err == nil {
// t.Fatalf("expected error when joining bad node")
// }
// }
// func Test_DoubleJoinOK(t *testing.T) {
// ts1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// }))
// defer ts1.Close()
// ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// }))
// defer ts2.Close()
// joiner := NewJoiner("127.0.0.1", numAttempts, attemptInterval, nil)
// // Ensure joining with protocol prefix works.
// j, err := joiner.Do([]string{ts1.URL, ts2.URL}, "id0", "127.0.0.1:9090", true)
// if err != nil {
// t.Fatalf("failed to join a single node: %s", err.Error())
// }
// if j != ts1.URL+"/join" {
// t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", j, ts1.URL)
// }
// // Ensure joining without protocol prefix works.
// j, err = joiner.Do([]string{ts1.Listener.Addr().String(), ts2.Listener.Addr().String()}, "id0", "127.0.0.1:9090", true)
// if err != nil {
// t.Fatalf("failed to join a single node: %s", err.Error())
// }
// if j != ts1.URL+"/join" {
// t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", j, ts1.URL)
// }
// }
// func Test_DoubleJoinOKSecondNode(t *testing.T) {
// ts1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusBadRequest)
// }))
// defer ts1.Close()
// ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// }))
// defer ts2.Close()
// joiner := NewJoiner("", numAttempts, attemptInterval, nil)
// // Ensure joining with protocol prefix works.
// j, err := joiner.Do([]string{ts1.URL, ts2.URL}, "id0", "127.0.0.1:9090", true)
// if err != nil {
// t.Fatalf("failed to join a single node: %s", err.Error())
// }
// if j != ts2.URL+"/join" {
// t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", j, ts2.URL)
// }
// // Ensure joining without protocol prefix works.
// j, err = joiner.Do([]string{ts1.Listener.Addr().String(), ts2.Listener.Addr().String()}, "id0", "127.0.0.1:9090", true)
// if err != nil {
// t.Fatalf("failed to join a single node: %s", err.Error())
// }
// if j != ts2.URL+"/join" {
// t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", j, ts2.URL)
// }
// }
// func Test_DoubleJoinOKSecondNodeRedirect(t *testing.T) {
// ts1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// }))
// defer ts1.Close()
// redirectAddr := fmt.Sprintf("%s%s", ts1.URL, "/join")
// ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// http.Redirect(w, r, redirectAddr, http.StatusMovedPermanently)
// }))
// defer ts2.Close()
// joiner := NewJoiner("127.0.0.1", numAttempts, attemptInterval, nil)
// // Ensure joining with protocol prefix works.
// j, err := joiner.Do([]string{ts2.URL}, "id0", "127.0.0.1:9090", true)
// if err != nil {
// t.Fatalf("failed to join a single node: %s", err.Error())
// }
// if j != redirectAddr {
// t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", redirectAddr, j)
// }
// // Ensure joining without protocol prefix works.
// j, err = joiner.Do([]string{ts2.Listener.Addr().String()}, "id0", "127.0.0.1:9090", true)
// if err != nil {
// t.Fatalf("failed to join a single node: %s", err.Error())
// }
// if j != redirectAddr {
// t.Fatalf("node joined using wrong endpoint, exp: %s, got: %s", redirectAddr, j)
// }
// }

Loading…
Cancel
Save