|
6 | 6 | import jsonpatch
|
7 | 7 | import sys
|
8 | 8 | import unittest
|
| 9 | +import ipaddress |
9 | 10 | from unittest import mock
|
10 | 11 |
|
11 | 12 | import click
|
|
42 | 43 | Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`.
|
43 | 44 | """
|
44 | 45 |
|
| 46 | +load_mgmt_config_command_ipv4_only_output="""\ |
| 47 | +Running command: /usr/local/bin/sonic-cfggen -M device_desc.xml --write-to-db |
| 48 | +parse dummy device_desc.xml |
| 49 | +change hostname to dummy |
| 50 | +Running command: ifconfig eth0 10.0.0.100 netmask 255.255.255.0 |
| 51 | +Running command: ip route add default via 10.0.0.1 dev eth0 table default |
| 52 | +Running command: ip rule add from 10.0.0.100 table default |
| 53 | +Running command: [ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid |
| 54 | +Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`. |
| 55 | +""" |
| 56 | + |
| 57 | +load_mgmt_config_command_ipv6_only_output="""\ |
| 58 | +Running command: /usr/local/bin/sonic-cfggen -M device_desc.xml --write-to-db |
| 59 | +parse dummy device_desc.xml |
| 60 | +change hostname to dummy |
| 61 | +Running command: ifconfig eth0 add fc00:1::32/64 |
| 62 | +Running command: ip -6 route add default via fc00:1::1 dev eth0 table default |
| 63 | +Running command: ip -6 rule add from fc00:1::32 table default |
| 64 | +Running command: [ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid |
| 65 | +Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`. |
| 66 | +""" |
| 67 | + |
| 68 | +load_mgmt_config_command_ipv4_ipv6_output="""\ |
| 69 | +Running command: /usr/local/bin/sonic-cfggen -M device_desc.xml --write-to-db |
| 70 | +parse dummy device_desc.xml |
| 71 | +change hostname to dummy |
| 72 | +Running command: ifconfig eth0 10.0.0.100 netmask 255.255.255.0 |
| 73 | +Running command: ip route add default via 10.0.0.1 dev eth0 table default |
| 74 | +Running command: ip rule add from 10.0.0.100 table default |
| 75 | +Running command: ifconfig eth0 add fc00:1::32/64 |
| 76 | +Running command: ip -6 route add default via fc00:1::1 dev eth0 table default |
| 77 | +Running command: ip -6 rule add from fc00:1::32 table default |
| 78 | +Running command: [ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid |
| 79 | +Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`. |
| 80 | +""" |
45 | 81 |
|
46 | 82 | RELOAD_CONFIG_DB_OUTPUT = """\
|
47 | 83 | Running command: rm -rf /tmp/dropstat-*
|
@@ -1356,3 +1392,97 @@ def validate_list_checkpoints_optional_parameter(self, param_args, expected_call
|
1356 | 1392 | self.assertTrue(expected_output in result.output)
|
1357 | 1393 | mock_generic_updater.list_checkpoints.assert_called_once()
|
1358 | 1394 | mock_generic_updater.list_checkpoints.assert_has_calls([expected_call])
|
| 1395 | + |
| 1396 | + |
| 1397 | +class TestConfigLoadMgmtConfig(object): |
| 1398 | + @classmethod |
| 1399 | + def setup_class(cls): |
| 1400 | + os.environ['UTILITIES_UNIT_TESTING'] = "1" |
| 1401 | + print("SETUP") |
| 1402 | + |
| 1403 | + from .mock_tables import mock_single_asic |
| 1404 | + importlib.reload(mock_single_asic) |
| 1405 | + |
| 1406 | + import config.main |
| 1407 | + importlib.reload(config.main) |
| 1408 | + |
| 1409 | + def test_config_load_mgmt_config_ipv4_only(self, get_cmd_module, setup_single_broadcom_asic): |
| 1410 | + device_desc_result = { |
| 1411 | + 'DEVICE_METADATA': { |
| 1412 | + 'localhost': { |
| 1413 | + 'hostname': 'dummy' |
| 1414 | + } |
| 1415 | + }, |
| 1416 | + 'MGMT_INTERFACE': { |
| 1417 | + ('eth0', '10.0.0.100/24') : { |
| 1418 | + 'gwaddr': ipaddress.ip_address(u'10.0.0.1') |
| 1419 | + } |
| 1420 | + } |
| 1421 | + } |
| 1422 | + self.check_output(get_cmd_module, device_desc_result, load_mgmt_config_command_ipv4_only_output, 5) |
| 1423 | + |
| 1424 | + def test_config_load_mgmt_config_ipv6_only(self, get_cmd_module, setup_single_broadcom_asic): |
| 1425 | + device_desc_result = { |
| 1426 | + 'DEVICE_METADATA': { |
| 1427 | + 'localhost': { |
| 1428 | + 'hostname': 'dummy' |
| 1429 | + } |
| 1430 | + }, |
| 1431 | + 'MGMT_INTERFACE': { |
| 1432 | + ('eth0', 'FC00:1::32/64') : { |
| 1433 | + 'gwaddr': ipaddress.ip_address(u'fc00:1::1') |
| 1434 | + } |
| 1435 | + } |
| 1436 | + } |
| 1437 | + self.check_output(get_cmd_module, device_desc_result, load_mgmt_config_command_ipv6_only_output, 5) |
| 1438 | + |
| 1439 | + def test_config_load_mgmt_config_ipv4_ipv6(self, get_cmd_module, setup_single_broadcom_asic): |
| 1440 | + device_desc_result = { |
| 1441 | + 'DEVICE_METADATA': { |
| 1442 | + 'localhost': { |
| 1443 | + 'hostname': 'dummy' |
| 1444 | + } |
| 1445 | + }, |
| 1446 | + 'MGMT_INTERFACE': { |
| 1447 | + ('eth0', '10.0.0.100/24') : { |
| 1448 | + 'gwaddr': ipaddress.ip_address(u'10.0.0.1') |
| 1449 | + }, |
| 1450 | + ('eth0', 'FC00:1::32/64') : { |
| 1451 | + 'gwaddr': ipaddress.ip_address(u'fc00:1::1') |
| 1452 | + } |
| 1453 | + } |
| 1454 | + } |
| 1455 | + self.check_output(get_cmd_module, device_desc_result, load_mgmt_config_command_ipv4_ipv6_output, 8) |
| 1456 | + |
| 1457 | + def check_output(self, get_cmd_module, parse_device_desc_xml_result, expected_output, expected_command_call_count): |
| 1458 | + def parse_device_desc_xml_side_effect(filename): |
| 1459 | + print("parse dummy device_desc.xml") |
| 1460 | + return parse_device_desc_xml_result |
| 1461 | + def change_hostname_side_effect(hostname): |
| 1462 | + print("change hostname to {}".format(hostname)) |
| 1463 | + with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command: |
| 1464 | + with mock.patch('config.main.parse_device_desc_xml', mock.MagicMock(side_effect=parse_device_desc_xml_side_effect)): |
| 1465 | + with mock.patch('config.main._change_hostname', mock.MagicMock(side_effect=change_hostname_side_effect)): |
| 1466 | + (config, show) = get_cmd_module |
| 1467 | + runner = CliRunner() |
| 1468 | + with runner.isolated_filesystem(): |
| 1469 | + with open('device_desc.xml', 'w') as f: |
| 1470 | + f.write('dummy') |
| 1471 | + result = runner.invoke(config.config.commands["load_mgmt_config"], ["-y", "device_desc.xml"]) |
| 1472 | + print(result.exit_code) |
| 1473 | + print(result.output) |
| 1474 | + traceback.print_tb(result.exc_info[2]) |
| 1475 | + assert result.exit_code == 0 |
| 1476 | + assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == expected_output |
| 1477 | + assert mock_run_command.call_count == expected_command_call_count |
| 1478 | + |
| 1479 | + @classmethod |
| 1480 | + def teardown_class(cls): |
| 1481 | + print("TEARDOWN") |
| 1482 | + os.environ['UTILITIES_UNIT_TESTING'] = "0" |
| 1483 | + |
| 1484 | + # change back to single asic config |
| 1485 | + from .mock_tables import dbconnector |
| 1486 | + from .mock_tables import mock_single_asic |
| 1487 | + importlib.reload(mock_single_asic) |
| 1488 | + dbconnector.load_namespace_config() |
0 commit comments