|
| 1 | +import subprocess |
| 2 | +import unittest |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import network |
| 6 | + |
| 7 | + |
| 8 | +# This test checks the various potential JSON output values that the underlying |
| 9 | +# `ip` command may return. |
| 10 | +class InspectInterfaceTest(unittest.TestCase): |
| 11 | + |
| 12 | + @mock.patch.object(subprocess, 'check_output') |
| 13 | + def test_treats_empty_response_as_inactive_interface(self, mock_cmd): |
| 14 | + mock_cmd.return_value = '' |
| 15 | + self.assertEqual( |
| 16 | + network.InterfaceStatus(False, None, None), |
| 17 | + network.inspect_interface('eth0'), |
| 18 | + ) |
| 19 | + |
| 20 | + @mock.patch.object(subprocess, 'check_output') |
| 21 | + def test_treats_empty_array_as_inactive_interface(self, mock_cmd): |
| 22 | + mock_cmd.return_value = '[]' |
| 23 | + self.assertEqual( |
| 24 | + network.InterfaceStatus(False, None, None), |
| 25 | + network.inspect_interface('eth0'), |
| 26 | + ) |
| 27 | + |
| 28 | + @mock.patch.object(subprocess, 'check_output') |
| 29 | + def test_treats_emtpy_object_as_inactive_interface(self, mock_cmd): |
| 30 | + mock_cmd.return_value = '[{}]' |
| 31 | + self.assertEqual( |
| 32 | + network.InterfaceStatus(False, None, None), |
| 33 | + network.inspect_interface('eth0'), |
| 34 | + ) |
| 35 | + |
| 36 | + @mock.patch.object(subprocess, 'check_output') |
| 37 | + def test_disregards_command_failure(self, mock_cmd): |
| 38 | + mock_cmd.side_effect = mock.Mock( |
| 39 | + side_effect=subprocess.CalledProcessError(returncode=1, cmd='ip')) |
| 40 | + self.assertEqual( |
| 41 | + network.InterfaceStatus(False, None, None), |
| 42 | + network.inspect_interface('eth0'), |
| 43 | + ) |
| 44 | + |
| 45 | + @mock.patch.object(subprocess, 'check_output') |
| 46 | + def test_parses_operstate_down_as_not_connected(self, mock_cmd): |
| 47 | + mock_cmd.return_value = """ |
| 48 | + [{"operstate":"DOWN"}] |
| 49 | + """ |
| 50 | + self.assertEqual( |
| 51 | + network.InterfaceStatus(False, None, None), |
| 52 | + network.inspect_interface('eth0'), |
| 53 | + ) |
| 54 | + |
| 55 | + @mock.patch.object(subprocess, 'check_output') |
| 56 | + def test_parses_operstate_up_as_connected(self, mock_cmd): |
| 57 | + mock_cmd.return_value = """ |
| 58 | + [{"operstate":"UP"}] |
| 59 | + """ |
| 60 | + self.assertEqual( |
| 61 | + network.InterfaceStatus(True, None, None), |
| 62 | + network.inspect_interface('eth0'), |
| 63 | + ) |
| 64 | + |
| 65 | + @mock.patch.object(subprocess, 'check_output') |
| 66 | + def test_parses_mac_address(self, mock_cmd): |
| 67 | + mock_cmd.return_value = """ |
| 68 | + [{"address":"00-b0-d0-63-c2-26"}] |
| 69 | + """ |
| 70 | + self.assertEqual( |
| 71 | + network.InterfaceStatus(False, None, '00-b0-d0-63-c2-26'), |
| 72 | + network.inspect_interface('eth0'), |
| 73 | + ) |
| 74 | + |
| 75 | + @mock.patch.object(subprocess, 'check_output') |
| 76 | + def test_normalizes_mac_address_to_use_dashes(self, mock_cmd): |
| 77 | + mock_cmd.return_value = """ |
| 78 | + [{"address":"00:b0:d0:63:c2:26"}] |
| 79 | + """ |
| 80 | + self.assertEqual( |
| 81 | + network.InterfaceStatus(False, None, '00-b0-d0-63-c2-26'), |
| 82 | + network.inspect_interface('eth0'), |
| 83 | + ) |
| 84 | + |
| 85 | + @mock.patch.object(subprocess, 'check_output') |
| 86 | + def test_parses_ip_address(self, mock_cmd): |
| 87 | + mock_cmd.return_value = """ |
| 88 | + [{"addr_info":[{"family":"inet","local":"192.168.2.5"}]}] |
| 89 | + """ |
| 90 | + self.assertEqual( |
| 91 | + network.InterfaceStatus(False, '192.168.2.5', None), |
| 92 | + network.inspect_interface('eth0'), |
| 93 | + ) |
| 94 | + |
| 95 | + @mock.patch.object(subprocess, 'check_output') |
| 96 | + def test_disregards_other_families_such_as_ipv6(self, mock_cmd): |
| 97 | + mock_cmd.return_value = """ |
| 98 | + [{"addr_info":[{"family":"inet6","local":"::ffff:c0a8:205"}]}] |
| 99 | + """ |
| 100 | + self.assertEqual( |
| 101 | + network.InterfaceStatus(False, None, None), |
| 102 | + network.inspect_interface('eth0'), |
| 103 | + ) |
| 104 | + |
| 105 | + @mock.patch.object(subprocess, 'check_output') |
| 106 | + def test_parses_all_data(self, mock_cmd): |
| 107 | + mock_cmd.return_value = """ |
| 108 | + [{ |
| 109 | + "operstate":"UP", |
| 110 | + "address":"00-b0-d0-63-c2-26", |
| 111 | + "addr_info":[{"family":"inet","local":"192.168.2.5"}] |
| 112 | + }] |
| 113 | + """ |
| 114 | + self.assertEqual( |
| 115 | + network.InterfaceStatus(True, '192.168.2.5', '00-b0-d0-63-c2-26'), |
| 116 | + network.inspect_interface('eth0'), |
| 117 | + ) |
| 118 | + |
| 119 | + @mock.patch.object(subprocess, 'check_output') |
| 120 | + def test_disregards_invalid_json(self, mock_cmd): |
| 121 | + mock_cmd.return_value = '[{"address' |
| 122 | + self.assertEqual( |
| 123 | + network.InterfaceStatus(False, None, None), |
| 124 | + network.inspect_interface('eth0'), |
| 125 | + ) |
0 commit comments