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