-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmon_sd_test.go
177 lines (143 loc) · 3.88 KB
/
cmon_sd_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/severalnines/cmon-proxy/cmon/api"
)
type CmonClientMock struct {
Fail bool
}
func (c CmonClientMock) Authenticate() error {
return nil
}
func (c CmonClientMock) ControllerID() string {
return "00000000-0000-0000-0000-000000000000"
}
func (c CmonClientMock) GetAllClusterInfo(req *api.GetAllClusterInfoRequest) (*api.GetAllClusterInfoResponse, error) {
if c.Fail {
return nil, errors.New("cmon client error")
}
return &api.GetAllClusterInfoResponse{
Clusters: []*api.Cluster{
{
ClusterID: 1,
ClusterName: "cluster1",
ClusterType: "postgresql_single",
Hosts: []*api.Host{
{
Nodetype: "mysql",
IP: "127.0.0.1",
},
{
Nodetype: "mongo",
IP: "127.0.0.1",
Role: "mongos",
},
},
},
},
}, nil
}
func TestHandlerIndexHandlerFail(t *testing.T) {
cmonMock := CmonClientMock{
Fail: true,
}
b := bytes.NewBuffer(nil)
logger := slog.New(slog.NewJSONHandler(b, nil))
s := Service{
cmonClient: cmonMock,
log: logger,
}
mux := s.Handler()
ts := httptest.NewServer(mux)
defer ts.Close()
resp, err := http.Get(ts.URL)
if err != nil {
t.Fatalf("GET failed: %v", err)
}
if resp.StatusCode != http.StatusInternalServerError {
t.Fatalf("GET failed: expected %d, got %d", http.StatusInternalServerError, resp.StatusCode)
}
if resp.Header.Get("Content-Type") != "application/json" {
t.Errorf("GET failed: expected %s, got %s", "application/json", resp.Header.Get("Content-Type"))
}
errMsg := ErrorMessage{}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("read body failed: %v", err)
}
err = json.Unmarshal(body, &errMsg)
if err != nil {
t.Errorf("unmarshal body failed: %v", err)
}
if errMsg.Error != "Error getting cluster info: cmon client error" {
t.Errorf("wrong error message should be 'Error getting cluster info: cmon client error', got %s", errMsg.Error)
}
type log struct {
Time time.Time `json:"time"`
Level string `json:"level"`
Msg string `json:"msg"`
Error string `json:"error"`
}
l := log{}
err = json.Unmarshal(b.Bytes(), &l)
if err != nil {
t.Errorf("failed to unmarshal log: %v", err)
}
if l.Level != "ERROR" {
t.Errorf("expected 'ERROR', got '%s'", l.Level)
}
if l.Msg != "Error getting cluster info" {
t.Errorf("expected 'Error getting cluster info', got '%s'", l.Msg)
}
if l.Error != "cmon client error" {
t.Errorf("expected 'cmon client error', got '%s'", l.Error)
}
}
func TestHandlerIndexHandlerSuccess(t *testing.T) {
cmonMock := CmonClientMock{}
s := Service{
cmonClient: cmonMock,
}
mux := s.Handler()
ts := httptest.NewServer(mux)
defer ts.Close()
resp, err := http.Get(ts.URL)
if err != nil {
t.Fatalf("GET failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("GET failed: expected status OK, got %v", resp.StatusCode)
}
if resp.Header.Get("Content-Type") != "application/json" {
t.Errorf("GET failed: expected Content-Type application/json, got %v", resp.Header.Get("Content-Type"))
}
var clusterTarget []ClusterTarget
err = json.NewDecoder(resp.Body).Decode(&clusterTarget)
if err != nil {
t.Errorf("GET failed: %v", err)
}
expectedClusterTarget := []ClusterTarget{}
expectedClusterTarget = append(expectedClusterTarget, ClusterTarget{
Target: []string{"127.0.0.1:9011", "127.0.0.1:9100", "127.0.0.1:9104", "127.0.0.1:9215"},
Label: map[string]string{
"ClusterID": "1",
"ClusterName": "cluster1",
"ClusterType": "postgresql_single",
"ControllerId": "00000000-0000-0000-0000-000000000000",
"cid": "1",
},
})
if !reflect.DeepEqual(clusterTarget, expectedClusterTarget) {
t.Errorf("GET failed: expected cluster target %v, got %v", expectedClusterTarget, clusterTarget)
}
}