blob: c4f6c475ffcc1745f009aee9ee797fc428ad34e0 [file] [log] [blame]
Scott Bakerc328cf12019-05-28 16:03:12 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
20 "bytes"
21 "github.com/opencord/cordctl/testutils"
22 "testing"
23 "time"
24)
25
26func TestModelList(t *testing.T) {
27 // use `python -m json.tool` to pretty-print json
28 expected := `[
29 {
30 "controller_kind": "",
31 "controller_replica_count": 0,
32 "creator_id": 0,
33 "default_flavor_id": 0,
34 "default_image_id": 0,
35 "default_isolation": "",
36 "default_node_id": 0,
37 "description": "",
38 "enabled": false,
39 "exposed_ports": "",
40 "id": 1,
41 "max_instances": 0,
42 "mount_data_sets": "",
43 "name": "mockslice1",
44 "network": "",
45 "principal_id": 0,
46 "service_id": 0,
47 "site_id": 1,
48 "trust_domain_id": 0
49 },
50 {
51 "controller_kind": "",
52 "controller_replica_count": 0,
53 "creator_id": 0,
54 "default_flavor_id": 0,
55 "default_image_id": 0,
56 "default_isolation": "",
57 "default_node_id": 0,
58 "description": "",
59 "enabled": false,
60 "exposed_ports": "",
61 "id": 2,
62 "max_instances": 0,
63 "mount_data_sets": "",
64 "name": "mockslice2",
65 "network": "",
66 "principal_id": 0,
67 "service_id": 0,
68 "site_id": 1,
69 "trust_domain_id": 0
70 }
71 ]`
72
73 got := new(bytes.Buffer)
74 OutputStream = got
75
76 var options ModelOpts
77 options.List.Args.ModelName = "Slice"
78 options.List.OutputAs = "json"
79 err := options.List.Execute([]string{})
80
81 if err != nil {
82 t.Errorf("%s: Received error %v", t.Name(), err)
83 return
84 }
85
86 testutils.AssertJSONEqual(t, got.String(), expected)
87}
88
89func TestModelListFilterID(t *testing.T) {
90 // use `python -m json.tool` to pretty-print json
91 expected := `[
92 {
93 "controller_kind": "",
94 "controller_replica_count": 0,
95 "creator_id": 0,
96 "default_flavor_id": 0,
97 "default_image_id": 0,
98 "default_isolation": "",
99 "default_node_id": 0,
100 "description": "",
101 "enabled": false,
102 "exposed_ports": "",
103 "id": 1,
104 "max_instances": 0,
105 "mount_data_sets": "",
106 "name": "mockslice1",
107 "network": "",
108 "principal_id": 0,
109 "service_id": 0,
110 "site_id": 1,
111 "trust_domain_id": 0
112 }
113 ]`
114
115 got := new(bytes.Buffer)
116 OutputStream = got
117
118 var options ModelOpts
119 options.List.Args.ModelName = "Slice"
120 options.List.OutputAs = "json"
121 options.List.Filter = "id=1"
122 err := options.List.Execute([]string{})
123
124 if err != nil {
125 t.Errorf("%s: Received error %v", t.Name(), err)
126 return
127 }
128
129 testutils.AssertJSONEqual(t, got.String(), expected)
130}
131
132func TestModelListFilterName(t *testing.T) {
133 // use `python -m json.tool` to pretty-print json
134 expected := `[
135 {
136 "controller_kind": "",
137 "controller_replica_count": 0,
138 "creator_id": 0,
139 "default_flavor_id": 0,
140 "default_image_id": 0,
141 "default_isolation": "",
142 "default_node_id": 0,
143 "description": "",
144 "enabled": false,
145 "exposed_ports": "",
146 "id": 2,
147 "max_instances": 0,
148 "mount_data_sets": "",
149 "name": "mockslice2",
150 "network": "",
151 "principal_id": 0,
152 "service_id": 0,
153 "site_id": 1,
154 "trust_domain_id": 0
155 }
156 ]`
157
158 got := new(bytes.Buffer)
159 OutputStream = got
160
161 var options ModelOpts
162 options.List.Args.ModelName = "Slice"
163 options.List.OutputAs = "json"
164 options.List.Filter = "name=mockslice2"
165 err := options.List.Execute([]string{})
166
167 if err != nil {
168 t.Errorf("%s: Received error %v", t.Name(), err)
169 return
170 }
171
172 testutils.AssertJSONEqual(t, got.String(), expected)
173}
174
Scott Baker1dd06672019-06-14 15:40:56 -0700175func TestModelListDirty(t *testing.T) {
176 // use `python -m json.tool` to pretty-print json
177 expected := `[
178 {
179 "controller_kind": "",
180 "controller_replica_count": 0,
181 "creator_id": 0,
182 "default_flavor_id": 0,
183 "default_image_id": 0,
184 "default_isolation": "",
185 "default_node_id": 0,
186 "description": "",
187 "enabled": false,
188 "exposed_ports": "",
189 "id": 2,
190 "max_instances": 0,
191 "mount_data_sets": "",
192 "name": "mockslice2",
193 "network": "",
194 "principal_id": 0,
195 "service_id": 0,
196 "site_id": 1,
197 "trust_domain_id": 0
198 }
199 ]`
200
201 got := new(bytes.Buffer)
202 OutputStream = got
203
204 var options ModelOpts
205 options.List.Args.ModelName = "Slice"
206 options.List.OutputAs = "json"
207 options.List.State = "dirty"
208 err := options.List.Execute([]string{})
209
210 if err != nil {
211 t.Errorf("%s: Received error %v", t.Name(), err)
212 return
213 }
214
215 testutils.AssertJSONEqual(t, got.String(), expected)
216}
217
Scott Bakerc328cf12019-05-28 16:03:12 -0700218func TestModelUpdate(t *testing.T) {
219 expected := `[{"id":1, "message":"Updated"}]`
220
221 got := new(bytes.Buffer)
222 OutputStream = got
223
224 var options ModelOpts
225 options.Update.Args.ModelName = "Slice"
226 options.Update.OutputAs = "json"
227 options.Update.IDArgs.ID = []int32{1}
228 options.Update.SetFields = "name=mockslice1_newname"
229 err := options.Update.Execute([]string{})
230
231 if err != nil {
232 t.Errorf("%s: Received error %v", t.Name(), err)
233 return
234 }
235
236 testutils.AssertJSONEqual(t, got.String(), expected)
237}
238
239func TestModelUpdateUsingFilter(t *testing.T) {
240 expected := `[{"id":1, "message":"Updated"}]`
241
242 got := new(bytes.Buffer)
243 OutputStream = got
244
245 var options ModelOpts
246 options.Update.Args.ModelName = "Slice"
247 options.Update.OutputAs = "json"
248 options.Update.Filter = "id=1"
249 options.Update.SetFields = "name=mockslice1_newname"
250 err := options.Update.Execute([]string{})
251
252 if err != nil {
253 t.Errorf("%s: Received error %v", t.Name(), err)
254 return
255 }
256
257 testutils.AssertJSONEqual(t, got.String(), expected)
258}
259
260func TestModelUpdateNoExist(t *testing.T) {
261 got := new(bytes.Buffer)
262 OutputStream = got
263
264 var options ModelOpts
265 options.Update.Args.ModelName = "Slice"
266 options.Update.OutputAs = "json"
267 options.Update.IDArgs.ID = []int32{77}
268 options.Update.SetFields = "name=mockslice1_newname"
269 err := options.Update.Execute([]string{})
270 testutils.AssertErrorEqual(t, err, "rpc error: code = Unknown desc = Slice matching query does not exist.")
271}
272
273func TestModelUpdateUsingFilterNoExist(t *testing.T) {
274 got := new(bytes.Buffer)
275 OutputStream = got
276
277 var options ModelOpts
278 options.Update.Args.ModelName = "Slice"
279 options.Update.OutputAs = "json"
280 options.Update.Filter = "id=77"
281 options.Update.SetFields = "name=mockslice1_newname"
282 err := options.Update.Execute([]string{})
283
284 testutils.AssertErrorEqual(t, err, "Filter matches no objects")
285}
286
287func TestModelCreate(t *testing.T) {
288 expected := `[{"id":3, "message":"Created"}]`
289
290 got := new(bytes.Buffer)
291 OutputStream = got
292
293 var options ModelOpts
294 options.Create.Args.ModelName = "Slice"
295 options.Create.OutputAs = "json"
296 options.Create.SetFields = "name=mockslice3,site_id=1"
297 err := options.Create.Execute([]string{})
298
299 if err != nil {
300 t.Errorf("%s: Received error %v", t.Name(), err)
301 return
302 }
303
304 testutils.AssertJSONEqual(t, got.String(), expected)
305}
306
307func TestModelDelete(t *testing.T) {
308 expected := `[{"id":1, "message":"Deleted"}]`
309
310 got := new(bytes.Buffer)
311 OutputStream = got
312
313 var options ModelOpts
314 options.Delete.Args.ModelName = "Slice"
315 options.Delete.OutputAs = "json"
316 options.Delete.IDArgs.ID = []int32{1}
317 err := options.Delete.Execute([]string{})
318
319 if err != nil {
320 t.Errorf("%s: Received error %v", t.Name(), err)
321 return
322 }
323
324 testutils.AssertJSONEqual(t, got.String(), expected)
325}
326
327func TestModelDeleteUsingFilter(t *testing.T) {
328 expected := `[{"id":1, "message":"Deleted"}]`
329
330 got := new(bytes.Buffer)
331 OutputStream = got
332
333 var options ModelOpts
334 options.Delete.Args.ModelName = "Slice"
335 options.Delete.OutputAs = "json"
336 options.Delete.Filter = "id=1"
337 err := options.Delete.Execute([]string{})
338
339 if err != nil {
340 t.Errorf("%s: Received error %v", t.Name(), err)
341 return
342 }
343
344 testutils.AssertJSONEqual(t, got.String(), expected)
345}
346
347func TestModelDeleteNoExist(t *testing.T) {
348 expected := `[{"id":77, "message":"Slice matching query does not exist."}]`
349
350 got := new(bytes.Buffer)
351 OutputStream = got
352
353 var options ModelOpts
354 options.Delete.Args.ModelName = "Slice"
355 options.Delete.OutputAs = "json"
356 options.Delete.IDArgs.ID = []int32{77}
357 err := options.Delete.Execute([]string{})
358
359 if err != nil {
360 t.Errorf("%s: Received error %v", t.Name(), err)
361 return
362 }
363
364 testutils.AssertJSONEqual(t, got.String(), expected)
365}
366
367func TestModelDeleteFilterNoExist(t *testing.T) {
368 got := new(bytes.Buffer)
369 OutputStream = got
370
371 var options ModelOpts
372 options.Delete.Args.ModelName = "Slice"
373 options.Delete.OutputAs = "json"
374 options.Delete.Filter = "id=77"
375 err := options.Delete.Execute([]string{})
376 testutils.AssertErrorEqual(t, err, "Filter matches no objects")
377}
378
379func TestModelSync(t *testing.T) {
380 expected := `[{"id":1, "message":"Enacted"}]`
381
382 got := new(bytes.Buffer)
383 OutputStream = got
384
385 var options ModelOpts
386 options.Sync.Args.ModelName = "Slice"
387 options.Sync.OutputAs = "json"
388 options.Sync.IDArgs.ID = []int32{1}
389 options.Sync.SyncTimeout = 5 * time.Second
390 err := options.Sync.Execute([]string{})
391
392 if err != nil {
393 t.Errorf("%s: Received error %v", t.Name(), err)
394 return
395 }
396
397 testutils.AssertJSONEqual(t, got.String(), expected)
398}
399
400func TestModelSyncTimeout(t *testing.T) {
401 expected := `[{"id":2, "message":"context deadline exceeded"}]`
402
403 got := new(bytes.Buffer)
404 OutputStream = got
405
406 var options ModelOpts
407 options.Sync.Args.ModelName = "Slice"
408 options.Sync.OutputAs = "json"
409 options.Sync.IDArgs.ID = []int32{2}
410 options.Sync.SyncTimeout = 5 * time.Second
411 err := options.Sync.Execute([]string{})
412
413 if err != nil {
414 t.Errorf("%s: Received error %v", t.Name(), err)
415 return
416 }
417
418 testutils.AssertJSONEqual(t, got.String(), expected)
419}
Scott Baker1dd06672019-06-14 15:40:56 -0700420
421func TestModelSetDirty(t *testing.T) {
422 expected := `[{"id":1, "message":"Dirtied"}]`
423
424 got := new(bytes.Buffer)
425 OutputStream = got
426
427 var options ModelOpts
428 options.SetDirty.Args.ModelName = "Slice"
429 options.SetDirty.OutputAs = "json"
430 options.SetDirty.IDArgs.ID = []int32{1}
431 err := options.SetDirty.Execute([]string{})
432
433 if err != nil {
434 t.Errorf("%s: Received error %v", t.Name(), err)
435 return
436 }
437
438 testutils.AssertJSONEqual(t, got.String(), expected)
439}