This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathpilosa_test.go
81 lines (69 loc) · 1.74 KB
/
pilosa_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package test_test
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"github.com/featurebasedb/featurebase/v3/disco"
"github.com/featurebasedb/featurebase/v3/test"
)
func TestNewCluster(t *testing.T) {
numNodes := 3
cluster := test.MustRunCluster(t, numNodes)
defer cluster.Close()
primary := getPrimary(cluster.Nodes[0])
for i := 1; i < numNodes; i++ {
if coordi := getPrimary(cluster.Nodes[i]); coordi != primary {
t.Fatalf("node %d does not have the same primary as node 0. '%v' and '%v' respectively", i, coordi, primary)
}
}
req, err := http.NewRequest(
"GET",
cluster.Nodes[0].URL()+"/status",
strings.NewReader(""),
)
if err != nil {
t.Fatalf("creating http request: %v", err)
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("sending request: %v", err)
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
body := struct {
State string
Nodes []struct {
Scheme string
Host string
Port int
}
}{}
err = dec.Decode(&body)
if err != nil {
t.Fatalf("decoding status response: %v", err)
}
bytes, err := json.MarshalIndent(body, "", " ")
if err != nil {
t.Fatalf("encoding: %v", err)
}
if len(body.Nodes) != 3 {
t.Fatalf("wrong number of nodes in status: %s", bytes)
}
if body.State != string(disco.ClusterStateNormal) {
t.Fatalf("cluster state should be %s but is %s", disco.ClusterStateNormal, body.State)
}
}
func getPrimary(m *test.Command) string {
hosts := m.API.Hosts(context.Background())
for _, host := range hosts {
if host.IsPrimary {
return host.ID
}
}
panic("no primary in cluster")
}