1package functionaltests
2
3import (
4	cluster "github.com/couchbase/indexing/secondary/tests/framework/clusterutility"
5	"testing"
6)
7
8func addNode(hostname, role string, t *testing.T) {
9	serverAddr := clusterconfig.KVAddress
10	username := clusterconfig.Username
11	password := clusterconfig.Password
12
13	if err := cluster.AddNode(serverAddr, username, password, hostname, role); err != nil {
14		t.Fatalf(err.Error())
15	}
16}
17
18func removeNode(hostname string, t *testing.T) {
19	serverAddr := clusterconfig.KVAddress
20	username := clusterconfig.Username
21	password := clusterconfig.Password
22
23	if err := cluster.RemoveNode(serverAddr, username, password, hostname); err != nil {
24		t.Fatalf(err.Error())
25	}
26}
27
28func failoverNode(hostname string, t *testing.T) {
29	serverAddr := clusterconfig.KVAddress
30	username := clusterconfig.Username
31	password := clusterconfig.Password
32
33	if err := cluster.FailoverNode(serverAddr, username, password, hostname); err != nil {
34		t.Fatalf(err.Error())
35	}
36}
37
38func rebalance(t *testing.T) {
39	serverAddr := clusterconfig.KVAddress
40	username := clusterconfig.Username
41	password := clusterconfig.Password
42
43	if err := cluster.Rebalance(serverAddr, username, password); err != nil {
44		t.Fatalf(err.Error())
45	}
46}
47
48// resetCluster will drop the nodes: Nodes[1], Nodes[2], Nodes[3] from
49// the cluster and adds back Nodes[1] ("index") into the cluster. We assume
50// that Nodes[0] is present in always present in the cluster
51func resetCluster(t *testing.T) {
52	serverAddr := clusterconfig.KVAddress
53	username := clusterconfig.Username
54	password := clusterconfig.Password
55
56	dropNodes := []string{clusterconfig.Nodes[1], clusterconfig.Nodes[2], clusterconfig.Nodes[3]}
57	keepNodes := make(map[string]string)
58	keepNodes[clusterconfig.Nodes[1]] = "index"
59
60	if err := cluster.ResetCluster(serverAddr, username, password, dropNodes, keepNodes); err != nil {
61		t.Fatalf(err.Error())
62	}
63
64	// Verify that the cluster was reset successfully
65	status := getClusterStatus()
66	if len(status) != 2 || !isNodeIndex(status, clusterconfig.Nodes[1]) {
67		t.Fatalf("Unable to resest the cluster, current cluster state: %v", status)
68	}
69}
70
71func getClusterStatus() map[string][]string {
72	serverAddr := clusterconfig.KVAddress
73	username := clusterconfig.Username
74	password := clusterconfig.Password
75
76	return cluster.GetClusterStatus(serverAddr, username, password)
77}
78
79func isNodeIndex(status map[string][]string, hostname string) bool {
80	return cluster.IsNodeIndex(status, hostname)
81}
82
83func validateServers(nodes []string) error {
84	serverAddr := clusterconfig.KVAddress
85	username := clusterconfig.Username
86	password := clusterconfig.Password
87
88	return cluster.ValidateServers(serverAddr, username, password, nodes)
89}
90