|
1 | 1 | import os
|
2 | 2 | import sys
|
| 3 | +import mock |
3 | 4 | from imp import load_source
|
4 | 5 |
|
5 | 6 | from mock import Mock, MagicMock, patch
|
|
40 | 41 | CHASSIS_ASIC_PCI_ADDRESS_FIELD = 'asic_pci_address'
|
41 | 42 | CHASSIS_ASIC_ID_IN_MODULE_FIELD = 'asic_id_in_module'
|
42 | 43 |
|
| 44 | +CHASSIS_MODULE_REBOOT_TIMESTAMP_FIELD = 'timestamp' |
| 45 | +CHASSIS_MODULE_REBOOT_REBOOT_FIELD = 'reboot' |
| 46 | +PLATFORM_ENV_CONF_FILE = "/usr/share/sonic/platform/platform_env.conf" |
| 47 | + |
43 | 48 | def setup_function():
|
44 | 49 | ModuleUpdater.log_notice = MagicMock()
|
45 | 50 | ModuleUpdater.log_warning = MagicMock()
|
@@ -357,6 +362,125 @@ def test_midplane_presence_modules():
|
357 | 362 | fvs = midplane_table.get(name)
|
358 | 363 | assert fvs == None
|
359 | 364 |
|
| 365 | +builtin_open = open # save the unpatched version |
| 366 | +def mock_open(*args, **kwargs): |
| 367 | + if args[0] == PLATFORM_ENV_CONF_FILE: |
| 368 | + return mock.mock_open(read_data="dummy=1\nlinecard_reboot_timeout=240\n")(*args, **kwargs) |
| 369 | + # unpatched version for every other path |
| 370 | + return builtin_open(*args, **kwargs) |
| 371 | + |
| 372 | +@patch("builtins.open", mock_open) |
| 373 | +@patch('os.path.isfile', MagicMock(return_value=True)) |
| 374 | +def test_midplane_presence_modules_linecard_reboot(): |
| 375 | + chassis = MockChassis() |
| 376 | + |
| 377 | + #Supervisor |
| 378 | + index = 0 |
| 379 | + name = "SUPERVISOR0" |
| 380 | + desc = "Supervisor card" |
| 381 | + slot = 16 |
| 382 | + serial = "RP1000101" |
| 383 | + module_type = ModuleBase.MODULE_TYPE_SUPERVISOR |
| 384 | + supervisor = MockModule(index, name, desc, module_type, slot, serial) |
| 385 | + supervisor.set_midplane_ip() |
| 386 | + chassis.module_list.append(supervisor) |
| 387 | + |
| 388 | + #Linecard |
| 389 | + index = 1 |
| 390 | + name = "LINE-CARD0" |
| 391 | + desc = "36 port 400G card" |
| 392 | + slot = 1 |
| 393 | + serial = "LC1000101" |
| 394 | + module_type = ModuleBase.MODULE_TYPE_LINE |
| 395 | + module = MockModule(index, name, desc, module_type, slot, serial) |
| 396 | + module.set_midplane_ip() |
| 397 | + chassis.module_list.append(module) |
| 398 | + |
| 399 | + #Fabric-card |
| 400 | + index = 1 |
| 401 | + name = "FABRIC-CARD0" |
| 402 | + desc = "Switch fabric card" |
| 403 | + slot = 17 |
| 404 | + serial = "FC1000101" |
| 405 | + module_type = ModuleBase.MODULE_TYPE_FABRIC |
| 406 | + fabric = MockModule(index, name, desc, module_type, slot, serial) |
| 407 | + chassis.module_list.append(fabric) |
| 408 | + |
| 409 | + #Run on supervisor |
| 410 | + module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot, |
| 411 | + module.supervisor_slot) |
| 412 | + module_updater.supervisor_slot = supervisor.get_slot() |
| 413 | + module_updater.my_slot = supervisor.get_slot() |
| 414 | + module_updater.modules_num_update() |
| 415 | + module_updater.module_db_update() |
| 416 | + module_updater.check_midplane_reachability() |
| 417 | + |
| 418 | + midplane_table = module_updater.midplane_table |
| 419 | + #Check only one entry in database |
| 420 | + assert 1 == midplane_table.size() |
| 421 | + |
| 422 | + #Check fields in database |
| 423 | + name = "LINE-CARD0" |
| 424 | + fvs = midplane_table.get(name) |
| 425 | + assert fvs != None |
| 426 | + if isinstance(fvs, list): |
| 427 | + fvs = dict(fvs[-1]) |
| 428 | + assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD] |
| 429 | + assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD] |
| 430 | + |
| 431 | + #Set access of line-card to Up (midplane connectivity is down initially) |
| 432 | + module.set_midplane_reachable(True) |
| 433 | + module_updater.check_midplane_reachability() |
| 434 | + fvs = midplane_table.get(name) |
| 435 | + assert fvs != None |
| 436 | + if isinstance(fvs, list): |
| 437 | + fvs = dict(fvs[-1]) |
| 438 | + assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD] |
| 439 | + assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD] |
| 440 | + |
| 441 | + |
| 442 | + #Set access of line-card to Down (to mock midplane connectivity state change) |
| 443 | + module.set_midplane_reachable(False) |
| 444 | + # set expected reboot of linecard |
| 445 | + module_reboot_table = module_updater.module_reboot_table |
| 446 | + linecard_fvs = swsscommon.FieldValuePairs([("reboot", "expected")]) |
| 447 | + module_reboot_table.set(name,linecard_fvs) |
| 448 | + module_updater.check_midplane_reachability() |
| 449 | + fvs = midplane_table.get(name) |
| 450 | + assert fvs != None |
| 451 | + if isinstance(fvs, list): |
| 452 | + fvs = dict(fvs[-1]) |
| 453 | + assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD] |
| 454 | + assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD] |
| 455 | + |
| 456 | + #Set access of line-card to up on time (to mock midplane connectivity state change) |
| 457 | + module.set_midplane_reachable(True) |
| 458 | + module_updater.check_midplane_reachability() |
| 459 | + fvs = midplane_table.get(name) |
| 460 | + assert fvs != None |
| 461 | + if isinstance(fvs, list): |
| 462 | + fvs = dict(fvs[-1]) |
| 463 | + assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD] |
| 464 | + assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD] |
| 465 | + |
| 466 | + # test linecard reboot midplane connectivity restored timeout |
| 467 | + # Set access of line-card to Down (to mock midplane connectivity state change) |
| 468 | + module.set_midplane_reachable(False) |
| 469 | + linecard_fvs = swsscommon.FieldValuePairs([("reboot", "expected")]) |
| 470 | + module_reboot_table.set(name,linecard_fvs) |
| 471 | + module_updater.check_midplane_reachability() |
| 472 | + time_now= time.time() - module_updater.linecard_reboot_timeout |
| 473 | + linecard_fvs = swsscommon.FieldValuePairs([(CHASSIS_MODULE_REBOOT_TIMESTAMP_FIELD, str(time_now))]) |
| 474 | + module_reboot_table.set(name,linecard_fvs) |
| 475 | + module_updater.check_midplane_reachability() |
| 476 | + fvs = midplane_table.get(name) |
| 477 | + assert fvs != None |
| 478 | + if isinstance(fvs, list): |
| 479 | + fvs = dict(fvs[-1]) |
| 480 | + assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD] |
| 481 | + assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD] |
| 482 | + assert module_updater.linecard_reboot_timeout == 240 |
| 483 | + |
360 | 484 | def test_midplane_presence_supervisor():
|
361 | 485 | chassis = MockChassis()
|
362 | 486 |
|
|
0 commit comments