|
3 | 3 | from networkapi.plugins import exceptions |
4 | 4 | from networkapi.plugins.Juniper.JUNOS.plugin import JUNOS |
5 | 5 | from mock import patch, MagicMock |
6 | | -from jnpr.junos.exception import LockError |
| 6 | +from jnpr.junos.exception import RPCError, RpcError, LockError, ConfigLoadError, CommitError |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class JunosPluginTest(NetworkApiTestCase): |
@@ -108,14 +108,71 @@ def test_exec_command_fail_to_lock(self, time_sleep, mock_config): |
108 | 108 | test_exec_command_fail_to_lock |
109 | 109 | """ |
110 | 110 |
|
111 | | - time_sleep.return_value = None # mocked to be executed instantly |
112 | | - mock_config.lock.side_effect = LockError('') |
| 111 | + # Mocks |
| 112 | + time_sleep.return_value = None # to be executed instantly |
| 113 | + mock_config.lock.side_effect = LockError('') # Forced exception here |
113 | 114 |
|
| 115 | + # Create plugin |
114 | 116 | plugin = JUNOS(equipment_access=self.mock_equipment_access) |
115 | | - plugin.configuration = mock_config |
| 117 | + plugin.configuration = mock_config # add mock to plugin instance |
| 118 | + |
| 119 | + # Test |
| 120 | + with self.assertRaises(exceptions.APIException): |
| 121 | + plugin.exec_command("any command") |
| 122 | + plugin.configuration.lock.assert_called_with() |
| 123 | + |
| 124 | + @patch('jnpr.junos.utils.config.Config') |
| 125 | + @patch('jnpr.junos.exception.RpcError') |
| 126 | + def test_exec_command_fail_to_load(self, mock_rpc_error, mock_config): |
| 127 | + |
| 128 | + # Mocks |
| 129 | + config_load_error = ConfigLoadError('') |
| 130 | + config_load_error.rpc_error = mock_rpc_error |
| 131 | + mock_config.load.side_effect = config_load_error # Forced exception here |
| 132 | + |
| 133 | + # Create plugin |
| 134 | + plugin = JUNOS(equipment_access=self.mock_equipment_access) |
| 135 | + plugin.configuration = mock_config # add mock to plugin instance |
| 136 | + |
| 137 | + # Test |
| 138 | + with self.assertRaises(exceptions.APIException): |
| 139 | + plugin.exec_command("any command") |
| 140 | + plugin.configuration.load.assert_called_with( |
| 141 | + "any command", format='set', ignore_warning=plugin.ignore_warning_list) |
| 142 | + |
| 143 | + @patch('jnpr.junos.utils.config.Config') |
| 144 | + def test_exec_command_fail_to_commit_check(self, mock_config): |
| 145 | + |
| 146 | + # Mocks |
| 147 | + error = RpcError('') |
| 148 | + mock_config.commit_check.side_effect = error # Forced exception here |
| 149 | + |
| 150 | + # Create plugin |
| 151 | + plugin = JUNOS(equipment_access=self.mock_equipment_access) |
| 152 | + plugin.configuration = mock_config # add mock to plugin instance |
| 153 | + |
| 154 | + # Tests |
| 155 | + with self.assertRaises(exceptions.APIException): |
| 156 | + plugin.exec_command("any command") |
| 157 | + plugin.configuration.commit_check.assert_called_with() |
| 158 | + |
| 159 | + @patch('jnpr.junos.utils.config.Config') |
| 160 | + @patch('jnpr.junos.exception.RpcError') |
| 161 | + def test_exec_command_fail_to_commit_check(self, mock_rpc_error, mock_config): |
| 162 | + |
| 163 | + # Mocks |
| 164 | + error = CommitError('') |
| 165 | + error.rpc_error = mock_rpc_error |
| 166 | + mock_config.commit.side_effect = error # Forced exception here |
| 167 | + |
| 168 | + # Create plugin |
| 169 | + plugin = JUNOS(equipment_access=self.mock_equipment_access) |
| 170 | + plugin.configuration = mock_config # add mock to plugin instance |
116 | 171 |
|
| 172 | + # Tests |
117 | 173 | with self.assertRaises(exceptions.APIException): |
118 | 174 | plugin.exec_command("any command") |
| 175 | + plugin.configuration.commit.assert_called_with() |
119 | 176 |
|
120 | 177 | @patch('jnpr.junos.Device') |
121 | 178 | def test_call_close_success(self, mock_device): |
|
0 commit comments