|
8 | 8 | from pyats.topology import loader |
9 | 9 | from pyats.datastructures import AttrDict |
10 | 10 |
|
| 11 | +from cryptography import x509 |
| 12 | +from cryptography.hazmat.backends import default_backend |
11 | 13 |
|
12 | 14 | class TestGnmi(unittest.TestCase): |
13 | 15 |
|
@@ -133,6 +135,105 @@ def test_connect_proxy(self): |
133 | 135 | ], |
134 | 136 | } |
135 | 137 |
|
| 138 | + @patch('yang.connector.gnmi.grpc.secure_channel') |
| 139 | + @patch('yang.connector.gnmi.grpc.composite_channel_credentials') |
| 140 | + @patch('yang.connector.gnmi.grpc.metadata_call_credentials') |
| 141 | + @patch('yang.connector.gnmi.grpc.ssl_channel_credentials') |
| 142 | + @patch('yang.connector.gnmi.x509.load_pem_x509_certificate') |
| 143 | + @patch('yang.connector.gnmi.ssl.get_server_certificate') |
| 144 | + def test_connect_with_skip_verify( |
| 145 | + self, mock_get_server_certificate, mock_load_pem_x509_certificate, |
| 146 | + mock_ssl_channel_credentials, mock_metadata_call_credentials, |
| 147 | + mock_composite_channel_credentials, mock_secure_channel, *_ |
| 148 | + ): |
| 149 | + yaml = """ |
| 150 | +devices: |
| 151 | + dummy: |
| 152 | + type: dummy_device |
| 153 | + connections: |
| 154 | + Gnmi: |
| 155 | + class: yang.connector.Gnmi |
| 156 | + protocol: gnmi |
| 157 | + ip : 1.2.3.4 |
| 158 | + port: 830 |
| 159 | + username: admin |
| 160 | + password: admin |
| 161 | + skip_verify: true |
| 162 | +""" |
| 163 | + testbed = loader.load(yaml) |
| 164 | + device = testbed.devices['dummy'] |
| 165 | + device.connect(alias='gnmi', via='Gnmi') |
| 166 | + mock_get_server_certificate.assert_called_once_with(('1.2.3.4', '830')) |
| 167 | + mock_load_pem_x509_certificate.assert_called_once_with( |
| 168 | + mock_get_server_certificate.return_value.encode('utf-8'), |
| 169 | + default_backend() |
| 170 | + ) |
| 171 | + mock_load_pem_x509_certificate.return_value.subject.get_attributes_for_oid.assert_called_once_with( |
| 172 | + (x509.NameOID.COMMON_NAME) |
| 173 | + ) |
| 174 | + mock_ssl_channel_credentials.assert_called_once_with( |
| 175 | + mock_get_server_certificate.return_value.encode('utf-8'), None, None |
| 176 | + ) |
| 177 | + self.assertEqual( |
| 178 | + mock_secure_channel.call_args[0][2][-1], |
| 179 | + ( |
| 180 | + 'grpc.ssl_target_name_override', |
| 181 | + mock_load_pem_x509_certificate.return_value.subject.get_attributes_for_oid.return_value[0].value |
| 182 | + ) |
| 183 | + ) |
| 184 | + |
| 185 | + @patch('yang.connector.gnmi.ssl.get_server_certificate', side_effect=Exception) |
| 186 | + def test_connect_with_skip_verify_get_server_certificate_exception(self, *_): |
| 187 | + yaml = """ |
| 188 | +devices: |
| 189 | + dummy: |
| 190 | + type: dummy_device |
| 191 | + connections: |
| 192 | + Gnmi: |
| 193 | + class: yang.connector.Gnmi |
| 194 | + protocol: gnmi |
| 195 | + ip: 1.2.3.4 |
| 196 | + port: 830 |
| 197 | + username: admin |
| 198 | + password: admin |
| 199 | + skip_verify: true |
| 200 | +""" |
| 201 | + testbed = loader.load(yaml) |
| 202 | + device = testbed.devices['dummy'] |
| 203 | + with self.assertRaises(Exception): |
| 204 | + device.connect(alias='gnmi', via='Gnmi') |
| 205 | + |
| 206 | + @patch('yang.connector.gnmi.ssl.get_server_certificate') |
| 207 | + @patch('yang.connector.gnmi.grpc.insecure_channel') |
| 208 | + @patch('yang.connector.gnmi.x509.load_pem_x509_certificate') |
| 209 | + def test_connect_with_skip_verify_get_attributes_for_oid_exception( |
| 210 | + self, mock_load_pem_x509_certificate, mock_insecure_channel, *_ |
| 211 | + ): |
| 212 | + yaml = """ |
| 213 | +devices: |
| 214 | + dummy: |
| 215 | + type: dummy_device |
| 216 | + connections: |
| 217 | + Gnmi: |
| 218 | + class: yang.connector.Gnmi |
| 219 | + protocol: gnmi |
| 220 | + ip: 1.2.3.4 |
| 221 | + port: 830 |
| 222 | + username: admin |
| 223 | + password: admin |
| 224 | + skip_verify: true |
| 225 | +""" |
| 226 | + mock_load_pem_x509_certificate.return_value.subject.get_attributes_for_oid.side_effect = BaseException |
| 227 | + testbed = loader.load(yaml) |
| 228 | + device = testbed.devices['dummy'] |
| 229 | + device.connect(alias='gnmi', via='Gnmi') |
| 230 | + mock_insecure_channel.assert_called_once_with( |
| 231 | + '1.2.3.4:830', [ |
| 232 | + ('grpc.max_receive_message_length', 1000000000), |
| 233 | + ('grpc.max_send_message_length', 1000000000) |
| 234 | + ] |
| 235 | + ) |
| 236 | + |
136 | 237 | def test_xpath_to_path_elem(self): |
137 | 238 | """Test converting Genie content data to cisco_gnmi format.""" |
138 | 239 | modules, message, origin = xpath_util.xml_path_to_path_elem(self.request) |
|
0 commit comments