blob: 2b44f8f488a4cd0717c2f1d5b48217d348fb3f55 [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
175func TestModelUpdate(t *testing.T) {
176 expected := `[{"id":1, "message":"Updated"}]`
177
178 got := new(bytes.Buffer)
179 OutputStream = got
180
181 var options ModelOpts
182 options.Update.Args.ModelName = "Slice"
183 options.Update.OutputAs = "json"
184 options.Update.IDArgs.ID = []int32{1}
185 options.Update.SetFields = "name=mockslice1_newname"
186 err := options.Update.Execute([]string{})
187
188 if err != nil {
189 t.Errorf("%s: Received error %v", t.Name(), err)
190 return
191 }
192
193 testutils.AssertJSONEqual(t, got.String(), expected)
194}
195
196func TestModelUpdateUsingFilter(t *testing.T) {
197 expected := `[{"id":1, "message":"Updated"}]`
198
199 got := new(bytes.Buffer)
200 OutputStream = got
201
202 var options ModelOpts
203 options.Update.Args.ModelName = "Slice"
204 options.Update.OutputAs = "json"
205 options.Update.Filter = "id=1"
206 options.Update.SetFields = "name=mockslice1_newname"
207 err := options.Update.Execute([]string{})
208
209 if err != nil {
210 t.Errorf("%s: Received error %v", t.Name(), err)
211 return
212 }
213
214 testutils.AssertJSONEqual(t, got.String(), expected)
215}
216
217func TestModelUpdateNoExist(t *testing.T) {
218 got := new(bytes.Buffer)
219 OutputStream = got
220
221 var options ModelOpts
222 options.Update.Args.ModelName = "Slice"
223 options.Update.OutputAs = "json"
224 options.Update.IDArgs.ID = []int32{77}
225 options.Update.SetFields = "name=mockslice1_newname"
226 err := options.Update.Execute([]string{})
227 testutils.AssertErrorEqual(t, err, "rpc error: code = Unknown desc = Slice matching query does not exist.")
228}
229
230func TestModelUpdateUsingFilterNoExist(t *testing.T) {
231 got := new(bytes.Buffer)
232 OutputStream = got
233
234 var options ModelOpts
235 options.Update.Args.ModelName = "Slice"
236 options.Update.OutputAs = "json"
237 options.Update.Filter = "id=77"
238 options.Update.SetFields = "name=mockslice1_newname"
239 err := options.Update.Execute([]string{})
240
241 testutils.AssertErrorEqual(t, err, "Filter matches no objects")
242}
243
244func TestModelCreate(t *testing.T) {
245 expected := `[{"id":3, "message":"Created"}]`
246
247 got := new(bytes.Buffer)
248 OutputStream = got
249
250 var options ModelOpts
251 options.Create.Args.ModelName = "Slice"
252 options.Create.OutputAs = "json"
253 options.Create.SetFields = "name=mockslice3,site_id=1"
254 err := options.Create.Execute([]string{})
255
256 if err != nil {
257 t.Errorf("%s: Received error %v", t.Name(), err)
258 return
259 }
260
261 testutils.AssertJSONEqual(t, got.String(), expected)
262}
263
264func TestModelDelete(t *testing.T) {
265 expected := `[{"id":1, "message":"Deleted"}]`
266
267 got := new(bytes.Buffer)
268 OutputStream = got
269
270 var options ModelOpts
271 options.Delete.Args.ModelName = "Slice"
272 options.Delete.OutputAs = "json"
273 options.Delete.IDArgs.ID = []int32{1}
274 err := options.Delete.Execute([]string{})
275
276 if err != nil {
277 t.Errorf("%s: Received error %v", t.Name(), err)
278 return
279 }
280
281 testutils.AssertJSONEqual(t, got.String(), expected)
282}
283
284func TestModelDeleteUsingFilter(t *testing.T) {
285 expected := `[{"id":1, "message":"Deleted"}]`
286
287 got := new(bytes.Buffer)
288 OutputStream = got
289
290 var options ModelOpts
291 options.Delete.Args.ModelName = "Slice"
292 options.Delete.OutputAs = "json"
293 options.Delete.Filter = "id=1"
294 err := options.Delete.Execute([]string{})
295
296 if err != nil {
297 t.Errorf("%s: Received error %v", t.Name(), err)
298 return
299 }
300
301 testutils.AssertJSONEqual(t, got.String(), expected)
302}
303
304func TestModelDeleteNoExist(t *testing.T) {
305 expected := `[{"id":77, "message":"Slice matching query does not exist."}]`
306
307 got := new(bytes.Buffer)
308 OutputStream = got
309
310 var options ModelOpts
311 options.Delete.Args.ModelName = "Slice"
312 options.Delete.OutputAs = "json"
313 options.Delete.IDArgs.ID = []int32{77}
314 err := options.Delete.Execute([]string{})
315
316 if err != nil {
317 t.Errorf("%s: Received error %v", t.Name(), err)
318 return
319 }
320
321 testutils.AssertJSONEqual(t, got.String(), expected)
322}
323
324func TestModelDeleteFilterNoExist(t *testing.T) {
325 got := new(bytes.Buffer)
326 OutputStream = got
327
328 var options ModelOpts
329 options.Delete.Args.ModelName = "Slice"
330 options.Delete.OutputAs = "json"
331 options.Delete.Filter = "id=77"
332 err := options.Delete.Execute([]string{})
333 testutils.AssertErrorEqual(t, err, "Filter matches no objects")
334}
335
336func TestModelSync(t *testing.T) {
337 expected := `[{"id":1, "message":"Enacted"}]`
338
339 got := new(bytes.Buffer)
340 OutputStream = got
341
342 var options ModelOpts
343 options.Sync.Args.ModelName = "Slice"
344 options.Sync.OutputAs = "json"
345 options.Sync.IDArgs.ID = []int32{1}
346 options.Sync.SyncTimeout = 5 * time.Second
347 err := options.Sync.Execute([]string{})
348
349 if err != nil {
350 t.Errorf("%s: Received error %v", t.Name(), err)
351 return
352 }
353
354 testutils.AssertJSONEqual(t, got.String(), expected)
355}
356
357func TestModelSyncTimeout(t *testing.T) {
358 expected := `[{"id":2, "message":"context deadline exceeded"}]`
359
360 got := new(bytes.Buffer)
361 OutputStream = got
362
363 var options ModelOpts
364 options.Sync.Args.ModelName = "Slice"
365 options.Sync.OutputAs = "json"
366 options.Sync.IDArgs.ID = []int32{2}
367 options.Sync.SyncTimeout = 5 * time.Second
368 err := options.Sync.Execute([]string{})
369
370 if err != nil {
371 t.Errorf("%s: Received error %v", t.Name(), err)
372 return
373 }
374
375 testutils.AssertJSONEqual(t, got.String(), expected)
376}