-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabout_test.go
201 lines (159 loc) · 7.44 KB
/
about_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
198
199
200
201
package tyrgin
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAboutResponse(t *testing.T) {
aboutResponseString := About(testStatusEndpoints, AboutProtocolHTTP, "test/about.json", "test/version.txt", emptyCustomData)
testAboutResponse := AboutResponse{}
err := json.Unmarshal([]byte(aboutResponseString), &testAboutResponse)
if err != nil {
t.Errorf("Response body is an invalid About format, was: `%s`", aboutResponseString)
}
assertEqualAboutData(t, testAboutResponse, emptyCustomData, defaultServiceID)
}
func TestAboutEmptyAboutData(t *testing.T) {
aboutResponseString := About(testStatusEndpoints, AboutProtocolHTTP, "", "", emptyCustomData)
testAboutResponse := AboutResponse{}
err := json.Unmarshal([]byte(aboutResponseString), &testAboutResponse)
if err != nil {
t.Errorf("Response body is an invalid About format, was: `%s`", aboutResponseString)
}
assertDefaultAboutResponse(t, testAboutResponse)
assertEmptyVersionResponse(t, testAboutResponse)
assert.Len(t, testAboutResponse.Dependencies, 3)
assertEqualAboutDependency(t, testAboutResponse.Dependencies[0], testStatusEndpointA)
assertEqualAboutDependency(t, testAboutResponse.Dependencies[1], testStatusEndpointB)
assertEqualAboutDependency(t, testAboutResponse.Dependencies[2], testStatusEndpointC)
}
func TestAboutFieldMissingAboutData(t *testing.T) {
aboutResponseString := About(testStatusEndpoints, AboutProtocolHTTP, "test/service-id-field-missing.json", "test/version.txt", emptyCustomData)
testAboutResponse := AboutResponse{}
err := json.Unmarshal([]byte(aboutResponseString), &testAboutResponse)
if err != nil {
t.Errorf("Response body is an invalid About format, was: `%s`", aboutResponseString)
}
assertEqualAboutData(t, testAboutResponse, emptyCustomData, "N/A")
}
func TestAboutCustomData(t *testing.T) {
serviceCustomData := make(map[string]interface{})
aboutResponseString := About(testStatusEndpoints, AboutProtocolHTTP, "test/about-custom.json", "test/version.txt", serviceCustomData)
testAboutResponse := AboutResponse{}
err := json.Unmarshal([]byte(aboutResponseString), &testAboutResponse)
if err != nil {
t.Errorf("Response body is an invalid About format, was: `%s`", aboutResponseString)
}
var customData = buildCustomData()
assertEqualAboutData(t, testAboutResponse, customData, defaultServiceID)
}
func TestAboutServiceCustomData(t *testing.T) {
var serviceCustomData map[string]interface{}
serviceCustomData = make(map[string]interface{})
serviceCustomData["some-key"] = "some-value"
aboutResponseString := About(testStatusEndpoints, AboutProtocolHTTP, "test/about.json", "test/version.txt", serviceCustomData)
testAboutResponse := AboutResponse{}
err := json.Unmarshal([]byte(aboutResponseString), &testAboutResponse)
if err != nil {
t.Errorf("Response body is an invalid About format, was: `%s`", aboutResponseString)
}
assertEqualAboutData(t, testAboutResponse, serviceCustomData, defaultServiceID)
}
func TestAboutServiceOverwritesCustomData(t *testing.T) {
var serviceCustomData map[string]interface{}
serviceCustomData = make(map[string]interface{})
serviceCustomData["custom1"] = [2]string{"cool", "bogus"}
serviceCustomData["custom2"] = 111
serviceCustomData["custom3"] = "howdy"
serviceCustom4 := make(map[string]interface{})
serviceCustom4["innerCustom1"] = 1
serviceCustom4["innerCustom2"] = "innerCustom2"
serviceCustomData["custom4"] = serviceCustom4
serviceCustomData["custom5"] = true
aboutResponseString := About(testStatusEndpoints, AboutProtocolHTTP, "test/about-custom.json", "test/version.txt", serviceCustomData)
testAboutResponse := AboutResponse{}
err := json.Unmarshal([]byte(aboutResponseString), &testAboutResponse)
if err != nil {
t.Errorf("Response body is an invalid About format, was: `%s`", aboutResponseString)
}
var customData = buildCustomData()
custom1 := make([]interface{}, 2)
custom1[0] = "cool"
custom1[1] = "bogus"
customData["custom1"] = custom1
customData["custom2"] = float64(111)
customData["custom3"] = "howdy"
custom4 := make(map[string]interface{})
custom4["innerCustom1"] = float64(1)
custom4["innerCustom2"] = "innerCustom2"
customData["custom4"] = custom4
customData["custom5"] = true
assertEqualAboutData(t, testAboutResponse, customData, defaultServiceID)
}
func assertEqualAboutData(t *testing.T, aboutResponse AboutResponse, customData map[string]interface{}, serviceID string) {
assert.Equal(t, aboutResponse.ID, serviceID)
assert.Equal(t, aboutResponse.Name, "Test Service")
assert.Equal(t, aboutResponse.Description, "A test service")
assert.Equal(t, aboutResponse.Owners[0], "Test1 Testerson <[email protected]>")
assert.Equal(t, aboutResponse.Owners[1], "Test2 Testerson <[email protected]>")
assert.Equal(t, aboutResponse.ProjectHome, "https://home.com/hootsuite/test-service")
assert.Equal(t, aboutResponse.ProjectRepo, "https://github.com/hootsuite/test-service")
assert.Equal(t, aboutResponse.LogsLinks[0], "https://logging.com/hootsuite/test-service-1")
assert.Equal(t, aboutResponse.LogsLinks[1], "https://logging.com/hootsuite/test-service-2")
assert.Equal(t, aboutResponse.StatsLinks[0], "https://stats.com/hootsuite/test-service-1")
assert.Equal(t, aboutResponse.StatsLinks[1], "https://stats.com/hootsuite/test-service-2")
assert.Equal(t, aboutResponse.Protocol, "http")
assert.Equal(t, aboutResponse.CustomData, customData)
assert.Len(t, aboutResponse.Dependencies, 3)
assertEqualAboutDependency(t, aboutResponse.Dependencies[0], testStatusEndpointA)
assertEqualAboutDependency(t, aboutResponse.Dependencies[1], testStatusEndpointB)
assertEqualAboutDependency(t, aboutResponse.Dependencies[2], testStatusEndpointC)
}
func assertEqualAboutDependency(t *testing.T, dependency Dependency, statusEndpoint StatusEndpoint) {
assert.Equal(t, statusEndpoint.Name, dependency.Name)
assert.Equal(t, statusEndpoint.IsTraversable, dependency.IsTraversable)
assert.Equal(t, statusEndpoint.Slug, dependency.StatusPath)
assert.Equal(t, statusEndpoint.Type, dependency.Type)
assert.True(t, dependency.StatusDuration > 0)
assert.NotEmpty(t, dependency.Status)
}
func assertDefaultAboutResponse(t *testing.T, response AboutResponse) {
assert.Equal(t, response.Name, "N/A")
assert.Equal(t, response.Description, "N/A")
assert.Equal(t, len(response.Owners), 0)
assert.Equal(t, response.ProjectHome, "N/A")
assert.Equal(t, response.ProjectRepo, "N/A")
assert.Equal(t, response.Protocol, "http")
assert.Equal(t, len(response.LogsLinks), 0)
assert.Equal(t, len(response.StatsLinks), 0)
}
func assertEmptyVersionResponse(t *testing.T, response AboutResponse) {
assert.Equal(t, response.Version, "N/A")
}
func buildCustomData() map[string]interface{} {
/* The default concrete Go types are:
- bool for JSON booleans
- float64 for JSON numbers
- string for JSON strings
- nil for JSON null
Make sure we test each type in our custom JSON */
var data map[string]interface{}
data = make(map[string]interface{})
custom1 := make([]interface{}, 2)
custom1[0] = "custom1-val1"
custom1[1] = "custom1-val2"
data["custom1"] = custom1
data["custom2"] = float64(789)
data["custom3"] = "custom3"
custom4 := make(map[string]interface{})
custom4["innerCustom1"] = float64(1)
custom4["innerCustom2"] = "innerCustom2"
innerCustom3 := make([]interface{}, 2)
innerCustom3[0] = "innerCustom3-val1"
innerCustom3[1] = "innerCustom3-val2"
custom4["innerCustom3"] = innerCustom3
data["custom4"] = custom4
data["custom5"] = true
data["custom6"] = nil
return data
}