|
| 1 | +import os |
| 2 | +import traceback |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +from click.testing import CliRunner |
| 6 | + |
| 7 | +import config.main as config |
| 8 | +import show.main as show |
| 9 | +from utilities_common.db import Db |
| 10 | + |
| 11 | +ERROR_MSG = ''' |
| 12 | +Error: ip address or mask is not valid. |
| 13 | +''' |
| 14 | + |
| 15 | +class TestConfigIP(object): |
| 16 | + @classmethod |
| 17 | + def setup_class(cls): |
| 18 | + os.environ['UTILITIES_UNIT_TESTING'] = "1" |
| 19 | + print("SETUP") |
| 20 | + |
| 21 | + ''' Tests for IPv4 ''' |
| 22 | + |
| 23 | + def test_add_del_interface_valid_ipv4(self): |
| 24 | + db = Db() |
| 25 | + runner = CliRunner() |
| 26 | + obj = {'config_db':db.cfgdb} |
| 27 | + |
| 28 | + # config int ip add Ethernet64 10.10.10.1/24 |
| 29 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet64", "10.10.10.1/24"], obj=obj) |
| 30 | + print(result.exit_code, result.output) |
| 31 | + assert result.exit_code == 0 |
| 32 | + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') |
| 33 | + |
| 34 | + # config int ip remove Ethernet64 10.10.10.1/24 |
| 35 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Ethernet64", "10.10.10.1/24"], obj=obj) |
| 36 | + print(result.exit_code, result.output) |
| 37 | + assert result.exit_code != 0 |
| 38 | + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') |
| 39 | + |
| 40 | + def test_add_interface_invalid_ipv4(self): |
| 41 | + db = Db() |
| 42 | + runner = CliRunner() |
| 43 | + obj = {'config_db':db.cfgdb} |
| 44 | + |
| 45 | + # config int ip add Ethernet64 10000.10.10.1/24 |
| 46 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet64", "10000.10.10.1/24"], obj=obj) |
| 47 | + print(result.exit_code, result.output) |
| 48 | + assert result.exit_code != 0 |
| 49 | + assert ERROR_MSG in result.output |
| 50 | + |
| 51 | + def test_add_interface_ipv4_invalid_mask(self): |
| 52 | + db = Db() |
| 53 | + runner = CliRunner() |
| 54 | + obj = {'config_db':db.cfgdb} |
| 55 | + |
| 56 | + # config int ip add Ethernet64 10.10.10.1/37 |
| 57 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet64", "10.10.10.1/37"], obj=obj) |
| 58 | + print(result.exit_code, result.output) |
| 59 | + assert result.exit_code != 0 |
| 60 | + assert ERROR_MSG in result.output |
| 61 | + |
| 62 | + def test_add_del_interface_ipv4_with_leading_zeros(self): |
| 63 | + db = Db() |
| 64 | + runner = CliRunner() |
| 65 | + obj = {'config_db':db.cfgdb} |
| 66 | + |
| 67 | + # config int ip add Ethernet68 10.10.10.002/24 |
| 68 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet68", "10.10.10.002/24"], obj=obj) |
| 69 | + print(result.exit_code, result.output) |
| 70 | + assert result.exit_code == 0 |
| 71 | + assert ('Ethernet68', '10.10.10.2/24') in db.cfgdb.get_table('INTERFACE') |
| 72 | + |
| 73 | + # config int ip remove Ethernet68 10.10.10.002/24 |
| 74 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Ethernet68", "10.10.10.002/24"], obj=obj) |
| 75 | + print(result.exit_code, result.output) |
| 76 | + assert result.exit_code != 0 |
| 77 | + assert ('Ethernet68', '10.10.10.2/24') not in db.cfgdb.get_table('INTERFACE') |
| 78 | + |
| 79 | + ''' Tests for IPv6 ''' |
| 80 | + |
| 81 | + def test_add_del_interface_valid_ipv6(self): |
| 82 | + db = Db() |
| 83 | + runner = CliRunner() |
| 84 | + obj = {'config_db':db.cfgdb} |
| 85 | + |
| 86 | + # config int ip add Ethernet72 2001:1db8:11a3:19d7:1f34:8a2e:17a0:765d/34 |
| 87 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet72", "2001:1db8:11a3:19d7:1f34:8a2e:17a0:765d/34"], obj=obj) |
| 88 | + print(result.exit_code, result.output) |
| 89 | + assert result.exit_code == 0 |
| 90 | + assert ('Ethernet72', '2001:1db8:11a3:19d7:1f34:8a2e:17a0:765d/34') in db.cfgdb.get_table('INTERFACE') |
| 91 | + |
| 92 | + # config int ip remove Ethernet72 2001:1db8:11a3:19d7:1f34:8a2e:17a0:765d/34 |
| 93 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Ethernet72", "2001:1db8:11a3:19d7:1f34:8a2e:17a0:765d/34"], obj=obj) |
| 94 | + print(result.exit_code, result.output) |
| 95 | + assert result.exit_code != 0 |
| 96 | + assert ('Ethernet72', '2001:1db8:11a3:19d7:1f34:8a2e:17a0:765d/34') not in db.cfgdb.get_table('INTERFACE') |
| 97 | + |
| 98 | + def test_add_interface_invalid_ipv6(self): |
| 99 | + db = Db() |
| 100 | + runner = CliRunner() |
| 101 | + obj = {'config_db':db.cfgdb} |
| 102 | + |
| 103 | + # config int ip add Ethernet72 20001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/34 |
| 104 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet72", "20001:0db8:11a3:19d7:1f34:8a2e:17a0:765d/34"], obj=obj) |
| 105 | + print(result.exit_code, result.output) |
| 106 | + assert result.exit_code != 0 |
| 107 | + assert ERROR_MSG in result.output |
| 108 | + |
| 109 | + def test_add_interface_ipv6_invalid_mask(self): |
| 110 | + db = Db() |
| 111 | + runner = CliRunner() |
| 112 | + obj = {'config_db':db.cfgdb} |
| 113 | + |
| 114 | + # config int ip add Ethernet72 2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/200 |
| 115 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet72", "2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/200"], obj=obj) |
| 116 | + print(result.exit_code, result.output) |
| 117 | + assert result.exit_code != 0 |
| 118 | + assert ERROR_MSG in result.output |
| 119 | + |
| 120 | + def test_add_del_interface_ipv6_with_leading_zeros(self): |
| 121 | + db = Db() |
| 122 | + runner = CliRunner() |
| 123 | + obj = {'config_db':db.cfgdb} |
| 124 | + |
| 125 | + # config int ip del Ethernet68 2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/34 |
| 126 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet68", "2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/34"], obj=obj) |
| 127 | + print(result.exit_code, result.output) |
| 128 | + assert result.exit_code == 0 |
| 129 | + assert ('Ethernet68', '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d/34') in db.cfgdb.get_table('INTERFACE') |
| 130 | + |
| 131 | + # config int ip remove Ethernet68 2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/34 |
| 132 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Ethernet68", "2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d/34"], obj=obj) |
| 133 | + print(result.exit_code, result.output) |
| 134 | + assert result.exit_code != 0 |
| 135 | + assert ('Ethernet68', '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d/34') not in db.cfgdb.get_table('INTERFACE') |
| 136 | + |
| 137 | + def test_add_del_interface_shortened_ipv6_with_leading_zeros(self): |
| 138 | + db = Db() |
| 139 | + runner = CliRunner() |
| 140 | + obj = {'config_db':db.cfgdb} |
| 141 | + |
| 142 | + # config int ip del Ethernet68 3000::001/64 |
| 143 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Ethernet68", "3000::001/64"], obj=obj) |
| 144 | + print(result.exit_code, result.output) |
| 145 | + assert result.exit_code == 0 |
| 146 | + assert ('Ethernet68', '3000::1/64') in db.cfgdb.get_table('INTERFACE') |
| 147 | + |
| 148 | + # config int ip remove Ethernet68 3000::001/64 |
| 149 | + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Ethernet68", "3000::001/64"], obj=obj) |
| 150 | + print(result.exit_code, result.output) |
| 151 | + assert result.exit_code != 0 |
| 152 | + assert ('Ethernet68', '3000::1/64') not in db.cfgdb.get_table('INTERFACE') |
| 153 | + |
| 154 | + @classmethod |
| 155 | + def teardown_class(cls): |
| 156 | + os.environ['UTILITIES_UNIT_TESTING'] = "0" |
| 157 | + print("TEARDOWN") |
0 commit comments