-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFactDataTransformer_test.go
More file actions
262 lines (211 loc) · 5.27 KB
/
FactDataTransformer_test.go
File metadata and controls
262 lines (211 loc) · 5.27 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package handler
import (
"fmt"
"testing"
)
func TestFactProcessor_TestMultipleFilters(t *testing.T) {
fact := LagoonFact{
Id: 0,
Environment: 0,
Name: "testKey",
Value: "testvalue",
Source: "",
Description: "",
KeyFact: false,
Type: "",
Category: "",
Service: "",
}
fp1 := FactProcessor{
Fact: fact,
InsightsData: EnvironmentVariable{
Key: "testkey",
Value: "testvalue",
},
hasErrorState: false,
theError: nil,
}
fp1.isOfType("NonExistentType").
setFactField("Name", "friendlyname")
if fp1.Fact.Name == "friendlyname" {
t.Errorf("fp1 should not have been processed")
}
fp2 := FactProcessor{
Fact: fact,
InsightsData: EnvironmentVariable{
Key: "testkey",
Value: "testvalue",
},
hasErrorState: false,
theError: nil,
}
fp2.isOfType("EnvironmentVariable").
setFactField("Name", "friendlyname").setKeyFact(true)
fact = fp2.Fact
if fact.KeyFact != true {
t.Errorf("fp2 should set fact to key")
}
}
// ProcessLagoonFactAgainstRegisteredFilters
func TestFactProcessor_ProcessLagoonFactAgainstRegisteredFilters(t *testing.T) {
fact := LagoonFact{
Id: 0,
Environment: 0,
Name: "testKey",
Value: "testvalue",
Source: "",
Description: "",
KeyFact: false,
Type: "",
Category: "",
Service: "",
}
poppedFactFilters := KeyFactFilters
defer func() { KeyFactFilters = poppedFactFilters }()
KeyFactFilters = []func(filter parserFilter) parserFilter{
func(filter parserFilter) parserFilter {
return filter.
isOfType("EnvironmentVariable").setFactField("Name", "replaced")
},
func(filter parserFilter) parserFilter {
return filter.
isOfType("EnvironmentVariable").setKeyFact(true)
},
}
returnFact, _ := ProcessLagoonFactAgainstRegisteredFilters(fact, EnvironmentVariable{
Key: "testkey",
Value: "testvalue",
})
if returnFact.Name != "replaced" {
t.Errorf("Resulting fact's name should have been replaced by 'replaced'")
}
if !returnFact.KeyFact {
t.Errorf("Resulting fact should be a key fact")
}
}
func TestFactProcessor_RecognisesType(t *testing.T) {
fp := FactProcessor{
Fact: LagoonFact{},
InsightsData: ImageData{},
hasErrorState: false,
theError: nil,
}
fp.isOfType("ImageData")
if fp.hasError() {
t.Errorf("Type should match 'ImageData'")
}
}
func TestFactProcessor_FiltersByField(t *testing.T) {
fp := FactProcessor{
Fact: LagoonFact{},
InsightsData: ImageData{Name: "Testing"},
hasErrorState: false,
theError: nil,
}
fp.isOfType("ImageData").
fieldContains("Name", "Testing")
if fp.hasError() {
t.Errorf("Field 'Name' should contain 'Testing'")
}
}
func TestFactProcessor_SetKeyFact(t *testing.T) {
fp := FactProcessor{
Fact: LagoonFact{},
InsightsData: ImageData{},
hasErrorState: false,
theError: nil,
}
fp.setKeyFact(true)
if fp.Fact.KeyFact != true {
t.Errorf("Should have been set to key fact")
}
}
func TestFactProcessor_SetFactValue(t *testing.T) {
fp := FactProcessor{
Fact: LagoonFact{},
InsightsData: ImageData{},
hasErrorState: false,
theError: nil,
}
fp.setFactField("Name", "namehere")
if fp.Fact.Name != "namehere" {
t.Errorf("Should be hable to set LagoonFact.Name")
}
}
func TestFactProcessor_TestSetFriendlyName(t *testing.T) {
fp1 := FactProcessor{
Fact: LagoonFact{
Id: 0,
Environment: 0,
Name: "testKey",
Value: "testvalue",
Source: "",
Description: "",
KeyFact: false,
Type: "",
Category: "",
Service: "",
},
InsightsData: EnvironmentVariable{
Key: "testkey",
Value: "testvalue",
},
hasErrorState: false,
theError: nil,
}
fp1.isOfType("EnvironmentVariable").
fieldContains("Key", "testkey").
setFactField("Name", "friendlyname")
if fp1.Fact.Name != "friendlyname" {
t.Errorf("Should be able to set LagoonFact.Name")
}
}
func TestFactProcessor_TestExactMatchLookup(t *testing.T) {
genfp := func() FactProcessor {
return FactProcessor{
Fact: LagoonFact{
Id: 0,
Environment: 0,
Name: "testKey",
Value: "testvalue",
Source: "",
Description: "",
KeyFact: false,
Type: "",
Category: "",
Service: "",
},
InsightsData: EnvironmentVariable{
Key: "testkey",
Value: "testvalue",
},
hasErrorState: false,
theError: nil,
}
}
fp1 := genfp()
//First we test regex matching
fp1.isOfType("EnvironmentVariable").
fieldContains("Key", "test").
setFactField("Name", "friendlyname")
if fp1.Fact.Name != "friendlyname" {
t.Errorf("The regex `test` should match `testkey`")
}
fp1 = genfp()
//Now we test a non-exact match
fp1.isOfType("EnvironmentVariable").
fieldContainsExactMatch("Key", "test").
setFactField("Name", "shouldnotbeset")
if fp1.Fact.Name == "shouldnotbeset" {
t.Errorf("The regex `test` should not match `shouldnotbeset`")
}
fp1 = genfp()
//Now we test an exact match
fp1.isOfType("EnvironmentVariable").
fieldContainsExactMatch("Key", "testkey").
setFactField("Name", "shouldbeset")
fmt.Println(fp1.InsightsData)
if fp1.Fact.Name != "shouldbeset" {
t.Errorf("The regex `test` should match `shouldbeset`")
}
}