Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "encoding/hex" |
| 20 | "encoding/json" |
| 21 | "github.com/google/uuid" |
| 22 | "github.com/opencord/voltha-go/protos/voltha" |
| 23 | "reflect" |
| 24 | "strconv" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | /* |
| 29 | |
| 30 | 1. Add device |
| 31 | 2. Do parallel updates of that device |
| 32 | 3. Do parallel gets of that device |
| 33 | 4. Remove device |
| 34 | |
| 35 | */ |
| 36 | |
| 37 | var ( |
| 38 | //pctTargetDeviceId string |
| 39 | target *voltha.Device |
| 40 | ) |
| 41 | |
| 42 | func Test_ConcurrentProxy_1_1_Add_NewDevice(t *testing.T) { |
| 43 | devIdBin, _ := uuid.New().MarshalBinary() |
| 44 | devId = "0001" + hex.EncodeToString(devIdBin)[:12] |
| 45 | |
| 46 | preAddExecuted := false |
| 47 | postAddExecuted := false |
| 48 | |
| 49 | modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted) |
| 50 | modelTestConfig.RootProxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted) |
| 51 | |
| 52 | device.Id = devId |
| 53 | if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil { |
| 54 | t.Error("Failed to add device") |
| 55 | } else { |
| 56 | t.Logf("Added device : %+v", added) |
| 57 | } |
| 58 | |
| 59 | if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 60 | t.Error("Failed to find added device") |
| 61 | } else { |
| 62 | djson, _ := json.Marshal(d) |
| 63 | t.Logf("Found device: %s", string(djson)) |
| 64 | } |
| 65 | |
| 66 | //if !preAddExecuted { |
| 67 | // t.Error("PRE_ADD callback was not executed") |
| 68 | //} |
| 69 | //if !postAddExecuted { |
| 70 | // t.Error("POST_ADD callback was not executed") |
| 71 | //} |
| 72 | } |
| 73 | |
| 74 | func Test_ConcurrentProxy_1_Add_ExistingDevice(t *testing.T) { |
| 75 | t.Parallel() |
| 76 | |
| 77 | device.Id = devId |
| 78 | if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil { |
| 79 | t.Logf("Successfully detected that the device already exists: %s", devId) |
| 80 | } else { |
| 81 | t.Errorf("A new device should not have been created : %+v", added) |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | func Test_ConcurrentProxy_Get_AllDevices(t *testing.T) { |
| 87 | t.Parallel() |
| 88 | |
| 89 | devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "") |
| 90 | |
| 91 | if len(devices.([]interface{})) == 0 { |
| 92 | t.Error("there are no available devices to retrieve") |
| 93 | } else { |
| 94 | t.Logf("retrieved all devices: %+v", devices) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func Test_ConcurrentProxy_Get_Update_DeviceAdminState(t *testing.T) { |
| 99 | t.Parallel() |
| 100 | if retrieved := modelTestConfig.RootProxy.Get("/devices/"+devId, 1, false, ""); retrieved == nil { |
| 101 | t.Error("Failed to get device") |
| 102 | } else { |
| 103 | retrieved.(*voltha.Device).AdminState = voltha.AdminState_DISABLED |
| 104 | |
| 105 | preUpdateExecuted := false |
| 106 | postUpdateExecuted := false |
| 107 | |
| 108 | modelTestConfig.RootProxy.RegisterCallback( |
| 109 | PRE_UPDATE, |
| 110 | commonCallback, |
| 111 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 112 | ) |
| 113 | modelTestConfig.RootProxy.RegisterCallback( |
| 114 | POST_UPDATE, |
| 115 | commonCallback, |
| 116 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 117 | ) |
| 118 | |
| 119 | if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+devId, retrieved, false, ""); afterUpdate == nil { |
| 120 | t.Error("Failed to update device") |
| 121 | } else { |
| 122 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 123 | } |
| 124 | if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 125 | t.Error("Failed to find updated device (root proxy)") |
| 126 | } else { |
| 127 | djson, _ := json.Marshal(d) |
| 128 | t.Logf("Found device (root proxy): %s", string(djson)) |
| 129 | } |
| 130 | |
| 131 | //if !preUpdateExecuted { |
| 132 | // t.Error("PRE_UPDATE callback was not executed") |
| 133 | //} |
| 134 | //if !postUpdateExecuted { |
| 135 | // t.Error("POST_UPDATE callback was not executed") |
| 136 | //} |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func Test_ConcurrentProxy_Get_SingleDevice(t *testing.T) { |
| 141 | //t.Parallel() |
| 142 | |
| 143 | if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 144 | t.Errorf("Failed to find device : %s", devId) |
| 145 | } else { |
| 146 | djson, _ := json.Marshal(d) |
| 147 | t.Logf("Found device: %s", string(djson)) |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | func Test_ConcurrentProxy_Get_SingleDeviceFlows(t *testing.T) { |
| 153 | t.Parallel() |
| 154 | |
| 155 | if d := modelTestConfig.RootProxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 156 | t.Errorf("Failed to find device : %s", devId) |
| 157 | } else { |
| 158 | djson, _ := json.Marshal(d) |
| 159 | t.Logf("Found device: %s", string(djson)) |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | |
| 164 | func Test_ConcurrentProxy_Get_SingleDevicePorts(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | if d := modelTestConfig.RootProxy.Get("/devices/"+devId+"/ports", 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 167 | t.Errorf("Failed to find device : %s", devId) |
| 168 | } else { |
| 169 | djson, _ := json.Marshal(d) |
| 170 | t.Logf("Found device: %s", string(djson)) |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | func Test_ConcurrentProxy_Get_Update_DeviceFirmware(t *testing.T) { |
| 176 | t.Parallel() |
| 177 | |
| 178 | if retrieved := modelTestConfig.RootProxy.Get("/devices/"+devId, 1, false, ""); retrieved == nil { |
| 179 | t.Error("Failed to get device") |
| 180 | } else { |
| 181 | var fwVersion int |
| 182 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 183 | fwVersion = 0 |
| 184 | } else { |
| 185 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
| 186 | fwVersion += 1 |
| 187 | } |
| 188 | |
| 189 | preUpdateExecuted := false |
| 190 | postUpdateExecuted := false |
| 191 | |
| 192 | modelTestConfig.RootProxy.RegisterCallback( |
| 193 | PRE_UPDATE, |
| 194 | commonCallback, |
| 195 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 196 | ) |
| 197 | modelTestConfig.RootProxy.RegisterCallback( |
| 198 | POST_UPDATE, |
| 199 | commonCallback, |
| 200 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 201 | ) |
| 202 | |
| 203 | retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion) |
| 204 | |
| 205 | if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+devId, retrieved, false, ""); afterUpdate == nil { |
| 206 | t.Error("Failed to update device") |
| 207 | } else { |
| 208 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 209 | } |
| 210 | if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 211 | t.Error("Failed to find updated device (root proxy)") |
| 212 | } else { |
| 213 | djson, _ := json.Marshal(d) |
| 214 | t.Logf("Found device (root proxy): %s", string(djson)) |
| 215 | } |
| 216 | |
| 217 | //if !preUpdateExecuted { |
| 218 | // t.Error("PRE_UPDATE callback was not executed") |
| 219 | //} |
| 220 | //if !postUpdateExecuted { |
| 221 | // t.Error("POST_UPDATE callback was not executed") |
| 222 | //} |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | //func Test_ConcurrentProxy_Get_Update_DeviceFlows(t *testing.T) { |
| 227 | // t.Parallel() |
| 228 | // |
| 229 | // // Get a device proxy and update a specific port |
| 230 | // //devflowsProxy := modelTestConfig.Root.GetProxy("/devices/"+devId+"/flows", false) |
| 231 | // flows := modelTestConfig.RootProxy.Get("/", 0, false, "") |
| 232 | // flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244 |
| 233 | // |
| 234 | // preUpdateExecuted := false |
| 235 | // postUpdateExecuted := false |
| 236 | // |
| 237 | // modelTestConfig.RootProxy.RegisterCallback( |
| 238 | // PRE_UPDATE, |
| 239 | // commonCallback, |
| 240 | // "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted, |
| 241 | // ) |
| 242 | // modelTestConfig.RootProxy.RegisterCallback( |
| 243 | // POST_UPDATE, |
| 244 | // commonCallback, |
| 245 | // "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted, |
| 246 | // ) |
| 247 | // |
| 248 | // kvFlows := modelTestConfig.RootProxy.Get("/devices/"+devId+"/flows", 0, false, "") |
| 249 | // |
| 250 | // if reflect.DeepEqual(flows, kvFlows) { |
| 251 | // t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows) |
| 252 | // } |
| 253 | // |
| 254 | // if updated := modelTestConfig.RootProxy.Update("/devices/"+devId+"/flows", flows.([]interface{})[0], false, ""); updated == nil { |
| 255 | // t.Error("Failed to update flow") |
| 256 | // } else { |
| 257 | // t.Logf("Updated flows : %+v", updated) |
| 258 | // } |
| 259 | // |
| 260 | // if d := modelTestConfig.RootProxy.Get("/devices/"+devId+"/flows", 0, false, ""); d == nil { |
| 261 | // t.Error("Failed to find updated flows (flows proxy)") |
| 262 | // } else { |
| 263 | // djson, _ := json.Marshal(d) |
| 264 | // t.Logf("Found flows (flows proxy): %s", string(djson)) |
| 265 | // } |
| 266 | // |
| 267 | // if d := modelTestConfig.RootProxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 268 | // t.Error("Failed to find updated flows (root proxy)") |
| 269 | // } else { |
| 270 | // djson, _ := json.Marshal(d) |
| 271 | // t.Logf("Found flows (root proxy): %s", string(djson)) |
| 272 | // } |
| 273 | // |
| 274 | // //if !preUpdateExecuted { |
| 275 | // // t.Error("PRE_UPDATE callback was not executed") |
| 276 | // //} |
| 277 | // //if !postUpdateExecuted { |
| 278 | // // t.Error("POST_UPDATE callback was not executed") |
| 279 | // //} |
| 280 | //} |
| 281 | |
| 282 | //func Test_ConcurrentProxy_4_1_Remove_Device(t *testing.T) { |
| 283 | // preRemoveExecuted := false |
| 284 | // postRemoveExecuted := false |
| 285 | // |
| 286 | // modelTestConfig.RootProxy.RegisterCallback( |
| 287 | // PRE_REMOVE, |
| 288 | // commonCallback, |
| 289 | // "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted, |
| 290 | // ) |
| 291 | // modelTestConfig.RootProxy.RegisterCallback( |
| 292 | // POST_REMOVE, |
| 293 | // commonCallback, |
| 294 | // "POST_REMOVE instructions (root proxy)", &postRemoveExecuted, |
| 295 | // ) |
| 296 | // |
| 297 | // if removed := modelTestConfig.RootProxy.Remove("/devices/"+devId, ""); removed == nil { |
| 298 | // t.Error("Failed to remove device") |
| 299 | // } else { |
| 300 | // t.Logf("Removed device : %+v", removed) |
| 301 | // } |
| 302 | // if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
| 303 | // djson, _ := json.Marshal(d) |
| 304 | // t.Errorf("Device was not removed - %s", djson) |
| 305 | // } else { |
| 306 | // t.Logf("Device was removed: %s", devId) |
| 307 | // } |
| 308 | // |
| 309 | // if !preRemoveExecuted { |
| 310 | // t.Error("PRE_UPDATE callback was not executed") |
| 311 | // } |
| 312 | // if !postRemoveExecuted { |
| 313 | // t.Error("POST_UPDATE callback was not executed") |
| 314 | // } |
| 315 | //} |
| 316 | |
| 317 | //func Benchmark_ConcurrentProxy_UpdateFirmware(b *testing.B) { |
| 318 | // //var target *voltha.Device |
| 319 | // if target == nil { |
| 320 | // devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "") |
| 321 | // if len(devices.([]interface{})) == 0 { |
| 322 | // b.Error("there are no available devices to retrieve") |
| 323 | // } else { |
| 324 | // // Save the target device id for later tests |
| 325 | // target = devices.([]interface{})[0].(*voltha.Device) |
| 326 | // //b.Logf("retrieved all devices: %+v", devices) |
| 327 | // } |
| 328 | // } |
| 329 | // |
| 330 | // for n := 0; n < b.N; n++ { |
| 331 | // var fwVersion int |
| 332 | // |
| 333 | // if target.FirmwareVersion == "n/a" { |
| 334 | // fwVersion = 0 |
| 335 | // } else { |
| 336 | // fwVersion, _ = strconv.Atoi(target.FirmwareVersion) |
| 337 | // fwVersion += 1 |
| 338 | // } |
| 339 | // |
| 340 | // target.FirmwareVersion = strconv.Itoa(fwVersion) |
| 341 | // |
| 342 | // if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+target.Id, target, false, |
| 343 | // ""); afterUpdate == nil { |
| 344 | // b.Error("Failed to update device") |
| 345 | // } else { |
| 346 | // if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 347 | // b.Errorf("Failed to find device : %s", devId) |
| 348 | // } else { |
| 349 | // //djson, _ := json.Marshal(d) |
| 350 | // //b.Logf("Checking updated device device: %s", string(djson)) |
| 351 | // } |
| 352 | // } |
| 353 | // } |
| 354 | // |
| 355 | //} |
| 356 | // |
| 357 | //func Benchmark_ConcurrentProxy_GetDevice(b *testing.B) { |
| 358 | // if target == nil { |
| 359 | // //var target *voltha.Device |
| 360 | // devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "") |
| 361 | // if len(devices.([]interface{})) == 0 { |
| 362 | // b.Error("there are no available devices to retrieve") |
| 363 | // } else { |
| 364 | // // Save the target device id for later tests |
| 365 | // target = devices.([]interface{})[0].(*voltha.Device) |
| 366 | // //b.Logf("retrieved all devices: %+v", devices) |
| 367 | // } |
| 368 | // } |
| 369 | // |
| 370 | // for n := 0; n < b.N; n++ { |
| 371 | // if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 372 | // b.Errorf("Failed to find device : %s", devId) |
| 373 | // } else { |
| 374 | // //djson, _ := json.Marshal(d) |
| 375 | // //b.Logf("Found device: %s", string(djson)) |
| 376 | // } |
| 377 | // } |
| 378 | // |
| 379 | //} |
| 380 | |
| 381 | func Benchmark_ConcurrentProxy_UpdateFirmware(b *testing.B) { |
| 382 | //GetProfiling().Reset() |
| 383 | defer GetProfiling().Report() |
| 384 | //b.SetParallelism(100) |
| 385 | if target == nil { |
| 386 | devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "") |
| 387 | if len(devices.([]interface{})) == 0 { |
| 388 | b.Error("there are no available devices to retrieve") |
| 389 | } else { |
| 390 | // Save the target device id for later tests |
| 391 | target = devices.([]interface{})[0].(*voltha.Device) |
| 392 | //b.Logf("retrieved all devices: %+v", devices) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | b.RunParallel(func(pb *testing.PB) { |
| 397 | for pb.Next() { |
| 398 | var fwVersion int |
| 399 | |
| 400 | if target.FirmwareVersion == "n/a" { |
| 401 | fwVersion = 0 |
| 402 | } else { |
| 403 | fwVersion, _ = strconv.Atoi(target.FirmwareVersion) |
| 404 | fwVersion += 1 |
| 405 | } |
| 406 | |
| 407 | target.FirmwareVersion = strconv.Itoa(fwVersion) |
| 408 | |
| 409 | if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+target.Id, target, false, |
| 410 | ""); afterUpdate == nil { |
| 411 | b.Error("Failed to update device") |
| 412 | } else { |
| 413 | if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 414 | b.Errorf("Failed to find device : %s", devId) |
| 415 | } else if d.(*voltha.Device).FirmwareVersion != target.FirmwareVersion { |
| 416 | b.Errorf("Firmware was not uptaded - expected: %s, actual: %s", |
| 417 | target.FirmwareVersion, |
| 418 | d.(*voltha.Device).FirmwareVersion, |
| 419 | ) |
| 420 | } else { |
| 421 | b.Logf("Firmware is now : %s", d.(*voltha.Device).FirmwareVersion) |
| 422 | //djson, _ := json.Marshal(d) |
| 423 | //b.Logf("Checking updated device device: %s", string(djson)) |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | }) |
| 428 | } |
| 429 | |
| 430 | func Benchmark_ConcurrentProxy_GetDevice(b *testing.B) { |
| 431 | //GetProfiling().Reset() |
| 432 | defer GetProfiling().Report() |
| 433 | //b.SetParallelism(5) |
| 434 | if target == nil { |
| 435 | //var target *voltha.Device |
| 436 | devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "") |
| 437 | if len(devices.([]interface{})) == 0 { |
| 438 | b.Error("there are no available devices to retrieve") |
| 439 | } else { |
| 440 | // Save the target device id for later tests |
| 441 | target = devices.([]interface{})[0].(*voltha.Device) |
| 442 | //b.Logf("retrieved all devices: %+v", devices) |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | b.RunParallel(func(pb *testing.PB) { |
| 447 | for pb.Next() { |
| 448 | if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 449 | b.Errorf("Failed to find device : %s", devId) |
| 450 | } else { |
| 451 | //djson, _ := json.Marshal(d) |
| 452 | //b.Logf("Found device: %s", string(djson)) |
| 453 | } |
| 454 | } |
| 455 | }) |
| 456 | } |