-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin-req-data_test.go
268 lines (224 loc) · 8.95 KB
/
plugin-req-data_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
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
263
264
265
266
267
268
package goforjj
import "testing"
func TestNewReqData(t *testing.T) {
t.Log("Expect NewReqData to return a valid request.")
// --- Setting test context ---
// --- Run the test ---
// Creating the request object
ret := NewReqData()
// --- Start testing ---
if ret == nil {
t.Error("Expected NewReqData to return a request Object. Got nil.")
} else if ret.Forj == nil {
t.Error("Expected NewReqData.Forj to be initialized. Got nil.")
} else if ret.Objects == nil {
t.Error("Expected NewReqData.Objects to be initialized. Got nil.")
}
}
func TestAddObjectActions(t *testing.T) {
t.Log("Expect AddObjectActions to add properly data in the request.")
// --- Setting test context ---
req := NewReqData()
const (
test = "test1"
testValue = "test"
obj1 = "obj1"
instance1 = "instance1"
)
if req == nil {
return
}
keys := make(InstanceKeys)
keys[test] = &ValueStruct{value: testValue}
extent := make(InstanceExtentKeys)
creds := make(map[string]string)
// --- Run the test ---
// Check if we can load an instance without 'master' loader. We should not.
req.AddObjectActions(obj1, instance1, keys, extent, creds)
// --- Start testing ---
if req.Objects == nil {
t.Error("Expected req.Objects to be initialized. Got nil.")
} else if v1 := len(req.Objects); v1 != 1 {
t.Errorf("Expected req.Objects to contains only one element. Got %d.", v1)
} else if v2, f1 := req.Objects[obj1]; !f1 {
t.Errorf("Expected req.Objects to contain '%s'. Not found.", obj1)
} else if v3, f2 := v2[instance1]; !f2 {
t.Errorf("Expected '%s' to have '%s'. Not found.", obj1, instance1)
} else if v4, f3 := v3[test]; !f3 {
t.Errorf("Expected '%s/%s' to have '%s'. Not found.", obj1, instance1, test)
} else if v5, ok1 := v4.(*ValueStruct); !ok1 {
t.Errorf("Expected '%s/%s/%s' to have '%s'. Is not.", obj1, instance1, test, "*ValueStruct")
} else if v5.value != testValue {
t.Errorf("Expected '%s/%s/%s=%s' to have '%s'. Got '%s'.", obj1, instance1, test, "*ValueStruct", v5.value, testValue)
}
// --- Setting test context ---
const (
test2 = "test2"
testValue2 = "testValue2"
)
if req == nil {
return
}
keys = make(InstanceKeys)
keys[test] = &ValueStruct{value: testValue}
extent = make(InstanceExtentKeys)
extent[test2] = &ValueStruct{value: testValue2}
// --- Run the test ---
// Check if we can load an instance without 'master' loader. We should not.
req.AddObjectActions(obj1, instance1, keys, extent, creds)
// --- Start testing ---
if req.Objects == nil {
t.Error("Expected req.Objects to be initialized. Got nil.")
} else if v1 := len(req.Objects); v1 != 1 {
t.Errorf("Expected req.Objects to contains only one element. Got %d.", v1)
} else if v2, f1 := req.Objects[obj1]; !f1 {
t.Errorf("Expected req.Objects to contain '%s'. Not found.", obj1)
} else if v3, f2 := v2[instance1]; !f2 {
t.Errorf("Expected '%s' to have '%s'. Not found.", obj1, instance1)
} else if v4, f3 := v3[test]; !f3 {
t.Errorf("Expected '%s/%s' to have '%s'. Not found.", obj1, instance1, test)
} else if v5, ok1 := v4.(*ValueStruct); !ok1 {
t.Errorf("Expected '%s/%s/%s' to have '%s'. Is not.", obj1, instance1, test, "*ValueStruct")
} else if v5.value != testValue {
t.Errorf("Expected '%s/%s/%s=%s' to have '%s'. Got '%s'.", obj1, instance1, test, "*ValueStruct", testValue, v5.value)
} else if v6, f4 := v3["extent"]; !f4 {
t.Errorf("Expected '%s/%s' to have '%s'. Not found.", obj1, instance1, test)
} else if v7, ok2 := v6.(InstanceExtentKeys); !ok2 {
t.Errorf("Expected '%s/%s/%s' to have '%s'. Is not.", obj1, instance1, test, "*ValueStruct")
} else if v8, f5 := v7[test2]; !f5 {
t.Errorf("Expected '%s/%s/extent' to have '%s'. Not found.", obj1, instance1, test2)
} else if v8.value != testValue2 {
t.Errorf("Expected '%s/%s/extent/%s=%s' to have '%s'. Got '%s'.", obj1, instance1, test, "*ValueStruct", testValue2, v5.value)
}
// --- Setting test context ---
const (
credKey = "key"
credValue = "value"
)
if req == nil {
return
}
creds[credKey] = credValue
// --- Run the test ---
// Check if we can load an instance without 'master' loader. We should not.
req.AddObjectActions(obj1, instance1, keys, extent, creds)
// --- Start testing ---
if req.Objects == nil {
t.Error("Expected req.Objects to be initialized. Got nil.")
} else if v1 := len(req.Objects); v1 != 1 {
t.Errorf("Expected req.Objects to contains only one element. Got %d.", v1)
} else if v2, f1 := req.Objects[obj1]; !f1 {
t.Errorf("Expected req.Objects to contain '%s'. Not found.", obj1)
} else if v3, f2 := v2[instance1]; !f2 {
t.Errorf("Expected '%s' to have '%s'. Not found.", obj1, instance1)
} else if v4, f3 := v3[test]; !f3 {
t.Errorf("Expected '%s/%s' to have '%s'. Not found.", obj1, instance1, test)
} else if v5, ok1 := v4.(*ValueStruct); !ok1 {
t.Errorf("Expected '%s/%s/%s' to have '%s'. Is not.", obj1, instance1, test, "*ValueStruct")
} else if v5.value != testValue {
t.Errorf("Expected '%s/%s/%s=%s' to have '%s'. Got '%s'.", obj1, instance1, test, "*ValueStruct", testValue, v5.value)
} else if v6, f4 := v3["extent"]; !f4 {
t.Errorf("Expected '%s/%s' to have '%s'. Not found.", obj1, instance1, test)
} else if v7, ok2 := v6.(InstanceExtentKeys); !ok2 {
t.Errorf("Expected '%s/%s/%s' to have '%s'. Is not.", obj1, instance1, test, "*ValueStruct")
} else if v8, f5 := v7[test2]; !f5 {
t.Errorf("Expected '%s/%s/extent' to have '%s'. Not found.", obj1, instance1, test2)
} else if v8.value != testValue2 {
t.Errorf("Expected '%s/%s/extent/%s=%s' to have '%s'. Got '%s'.", obj1, instance1, test, "*ValueStruct", testValue2, v5.value)
} else if req.Creds == nil {
t.Error("Expected req.Creds to be initialized. Got nil.")
} else if v9, f6 := req.Creds[obj1+"-"+instance1+"-"+credKey]; !f6 {
t.Errorf("Expected req.Creds to contain '%s'. Not found.", obj1+"-"+instance1+"-"+credKey)
} else if v9 != credValue {
t.Errorf("Expected req.Creds[%s] to be '%s'. Got '%s'", obj1+"-"+instance1+"-"+credKey, credValue, v9)
}
}
func TestSetForjFlag(t *testing.T) {
t.Log("Expect SetForjFlag to add properly data in the request.")
// --- Setting test context ---
req := NewReqData()
const (
test = "test1"
testValue = "test"
obj1 = "obj1"
instance1 = "instance1"
)
if req == nil {
return
}
// --- Run the test ---
// Set basic forj value
req.SetForjFlag(test, testValue, false, false)
// --- Start testing ---
if req.Forj == nil {
t.Error("Expected req.Forj to be initialized. Got nil.")
} else if v1 := len(req.Forj); v1 != 1 {
t.Errorf("Expected req.Forj to contains only one element. Got %d.", v1)
} else if v2, f1 := req.Forj[test]; !f1 {
t.Errorf("Expected req.Forj to contain '%s'. Not found.", test)
} else if v2 != testValue {
t.Errorf("Expected '%s' to have '%s'. Got '%s'.", test, testValue, v2)
}
// --- Setting test context ---
const (
test2 = "test2"
testValue2 = "testValue2"
)
if req == nil {
return
}
// --- Run the test ---
// Check we can add a new one but as extent
req.SetForjFlag(test2, testValue2, false, true)
// --- Start testing ---
if req.ForjExtent == nil {
t.Error("Expected req.Forj to be initialized. Got nil.")
} else if v1 := len(req.ForjExtent); v1 != 1 {
t.Errorf("Expected req.Forj to contains only one element. Got %d.", v1)
} else if v2, f1 := req.ForjExtent[test2]; !f1 {
t.Errorf("Expected req.Forj to contain '%s'. Not found.", test2)
} else if v2 != testValue2 {
t.Errorf("Expected '%s' to have '%s'. Got '%s'.", test2, testValue2, v2)
}
// --- Setting test context ---
const ()
req = NewReqData()
if req == nil {
return
}
// --- Run the test ---
// Check if we can set a cred value. The Forj value is kept for compatibility.
req.SetForjFlag(test, testValue, true, false)
// --- Start testing ---
if req.Forj == nil {
t.Error("Expected req.Forj to be initialized. Got nil.")
} else if v1 := len(req.Forj); v1 != 1 {
t.Errorf("Expected req.Forj to contains only one element. Got %d.", v1)
} else if v2, f1 := req.Forj[test]; !f1 { // The Forj value is kept for compatibility.
t.Errorf("Expected req.Forj to contain '%s'. Not found.", test)
} else if v2 != testValue {
t.Errorf("Expected '%s' to have '%s'. Got '%s'.", test, testValue, v2)
} else if v3, f2 := req.Creds[test]; !f2 {
t.Errorf("Expected req.Forj to contain '%s'. Not found.", test)
} else if v3 != testValue {
t.Errorf("Expected '%s' to have '%s'. Got '%s'.", test, testValue, v3)
}
// --- Setting test context ---
const ()
if req == nil {
return
}
// --- Run the test ---
// Check if we can set an extent cred.
req.SetForjFlag(test2, testValue2, true, true)
// --- Start testing ---
if req.ForjExtent != nil {
t.Error("Expected req.Extent to be nil. Got initialized.")
} else if req.Creds == nil {
t.Error("Expected req.Creds to be initialized. Got nil.")
} else if v3, f2 := req.Creds[test]; !f2 {
t.Errorf("Expected req.Forj to contain '%s'. Not found.", test)
} else if v3 != testValue {
t.Errorf("Expected '%s' to have '%s'. Got '%s'.", test, testValue, v3)
}
}