-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_diag_test.go
197 lines (166 loc) · 5.94 KB
/
cluster_diag_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gocb
import (
"encoding/json"
"time"
"github.com/stretchr/testify/mock"
"github.com/couchbase/gocbcore/v10"
)
func (suite *UnitTestSuite) diagnosticsCluster(runFn func(args mock.Arguments), args ...interface{}) *Cluster {
diagnosticsProviderCoreProvider := new(mockDiagnosticsProviderCoreProvider)
call := diagnosticsProviderCoreProvider.
On("Diagnostics", mock.AnythingOfType("gocbcore.DiagnosticsOptions")).
Return(args...)
if runFn != nil {
call.Run(runFn)
}
diagnosticsProvider := &diagnosticsProviderCore{
provider: diagnosticsProviderCoreProvider,
}
cli := new(mockConnectionManager)
cli.On("getDiagnosticsProvider", mock.AnythingOfType("string")).Return(diagnosticsProvider, nil)
cli.On("MarkOpBeginning").Return()
cli.On("MarkOpCompleted").Return()
c := &Cluster{
timeoutsConfig: TimeoutsConfig{
KVTimeout: 1000 * time.Second,
AnalyticsTimeout: 1000 * time.Second,
QueryTimeout: 1000 * time.Second,
SearchTimeout: 1000 * time.Second,
},
connectionManager: cli,
}
return c
}
func (suite *UnitTestSuite) TestDiagnostics() {
layout := "2006-01-02T15:04:05.000Z"
date1, err := time.Parse(layout, "2014-11-12T11:45:26.371Z")
if err != nil {
suite.T().Fatalf("Failed to parse date: %v", err)
}
date2, err := time.Parse(layout, "2017-11-12T11:45:26.371Z")
if err != nil {
suite.T().Fatalf("Failed to parse date: %v", err)
}
info := &gocbcore.DiagnosticInfo{
ConfigRev: 1,
MemdConns: []gocbcore.MemdConnInfo{
{
LastActivity: date1,
LocalAddr: "10.112.191.101",
RemoteAddr: "10.112.191.102",
Scope: "bucket",
State: gocbcore.EndpointStateConnected,
ID: "0xc000094120",
},
{
LastActivity: date2,
LocalAddr: "",
RemoteAddr: "",
ID: "",
State: gocbcore.EndpointStateDisconnected,
},
},
}
c := suite.diagnosticsCluster(nil, info, nil)
report, err := c.Diagnostics(nil)
if err != nil {
suite.T().Fatalf("Expected error to be nil but was %v", err)
}
if report.ID == "" {
suite.T().Fatalf("Report ID should have been not empty")
}
services, ok := report.Services["kv"]
if !ok {
suite.T().Fatalf("Report missing kv service")
}
if len(services) != len(info.MemdConns) {
suite.T().Fatalf("Expected Services length to be %d but was %d", len(info.MemdConns), len(report.Services))
}
for i, service := range services {
if service.Type != ServiceTypeKeyValue {
suite.T().Fatalf("Expected service to be KeyValueService but was %d", service.Type)
}
expected := info.MemdConns[i]
if service.Remote != expected.RemoteAddr {
suite.T().Fatalf("Expected service Remote to be %s but was %s", expected.RemoteAddr, service.Remote)
}
if service.Local != expected.LocalAddr {
suite.T().Fatalf("Expected service Local to be %s but was %s", expected.LocalAddr, service.Local)
}
if service.LastActivity != expected.LastActivity {
suite.T().Fatalf("Expected service LastActivity to be %s but was %s", expected.LastActivity, service.LastActivity)
}
if service.Namespace != expected.Scope {
suite.T().Fatalf("Expected service Scope to be %s but was %s", expected.Scope, service.Namespace)
}
if service.ID != expected.ID {
suite.T().Fatalf("Expected service ID to be %s but was %s", expected.ID, service.ID)
}
if service.State != EndpointState(expected.State) {
suite.T().Fatalf("Expected service state to be %s but was %s", endpointStateToString(EndpointState(expected.State)),
endpointStateToString(service.State))
}
}
marshaled, err := json.Marshal(report)
if err != nil {
suite.T().Fatalf("Failed to Marshal report: %v", err)
}
var jsonReport jsonDiagnosticReport
err = json.Unmarshal(marshaled, &jsonReport)
if err != nil {
suite.T().Fatalf("Failed to Unmarshal report: %v", err)
}
if jsonReport.ID != report.ID {
suite.T().Fatalf("Expected json report ID to be %s but was %s", report.ID, jsonReport.ID)
}
if jsonReport.Version != 2 {
suite.T().Fatalf("Expected json report Version to be 1 but was %d", jsonReport.Version)
}
if jsonReport.SDK != Identifier() {
suite.T().Fatalf("Expected json report SDK to be %s but was %s", Identifier(), jsonReport.SDK)
}
if len(jsonReport.Services) != 1 {
suite.T().Fatalf("Expected json report Services to be of length 1 but was %d", len(jsonReport.Services))
}
jsonServices, ok := jsonReport.Services["kv"]
if !ok {
suite.T().Fatalf("Expected json report services to contain kv but didn't")
}
if len(services) != len(jsonServices) {
suite.T().Fatalf("Expected json report Services length to be %d but was %d", len(report.Services), len(services))
}
for i, service := range jsonServices {
expected := services[i]
if service.Remote != expected.Remote {
suite.T().Fatalf("Expected service Remote to be %s but was %s", expected.Remote, service.Remote)
}
if service.Local != expected.Local {
suite.T().Fatalf("Expected service Local to be %s but was %s", expected.Local, service.Local)
}
if service.LastActivityUs == 0 {
suite.T().Fatalf("Expected service LastActivityUs to be non zero but was %d", service.LastActivityUs)
}
if service.Namespace != expected.Namespace {
suite.T().Fatalf("Expected service Scope to be %s but was %s", expected.Namespace, service.Namespace)
}
if service.ID != expected.ID {
suite.T().Fatalf("Expected service Scope to be %s but was %s", expected.ID, service.ID)
}
if service.State != endpointStateToString(expected.State) {
suite.T().Fatalf("Expected service state to be %s but was %s", endpointStateToString(expected.State),
service.State)
}
}
}
func (suite *UnitTestSuite) TestDiagnosticsWithID() {
c := suite.diagnosticsCluster(nil, &gocbcore.DiagnosticInfo{
ConfigRev: 1,
}, nil)
report, err := c.Diagnostics(&DiagnosticsOptions{ReportID: "myreportid"})
if err != nil {
suite.T().Fatalf("Expected error to be nil but was %v", err)
}
if report.ID != "myreportid" {
suite.T().Fatalf("Report ID should have been myreportid but was %s", report.ID)
}
}