|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +# |
| 14 | + |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +from oslo_config import cfg |
| 18 | +import testscenarios |
| 19 | + |
| 20 | +from ovn_octavia_provider.ovsdb import ovsdb_monitor |
| 21 | +from ovn_octavia_provider.tests.unit import base as base_test |
| 22 | + |
| 23 | + |
| 24 | +OPTS = ('ovn_nb_private_key', 'ovn_nb_certificate', |
| 25 | + 'ovn_nb_ca_cert', 'ovn_sb_private_key', |
| 26 | + 'ovn_sb_certificate', 'ovn_sb_ca_cert') |
| 27 | + |
| 28 | + |
| 29 | +class TestOvsdbMonitor(testscenarios.WithScenarios, |
| 30 | + base_test.TestOvnOctaviaBase): |
| 31 | + |
| 32 | + scenarios = [ |
| 33 | + ('OVN_Northbound', {'schema': 'OVN_Northbound', |
| 34 | + 'private_key': 'ovn_nb_private_key', |
| 35 | + 'certificate': 'ovn_nb_certificate', |
| 36 | + 'ca_cert': 'ovn_nb_ca_cert'}), |
| 37 | + ('OVN_Southbound', {'schema': 'OVN_Southbound', |
| 38 | + 'private_key': 'ovn_sb_private_key', |
| 39 | + 'certificate': 'ovn_sb_certificate', |
| 40 | + 'ca_cert': 'ovn_sb_ca_cert'}) |
| 41 | + ] |
| 42 | + |
| 43 | + def setUp(self): |
| 44 | + super().setUp() |
| 45 | + self._register_opts() |
| 46 | + self.mock_os_path = mock.patch('os.path.exists').start() |
| 47 | + self.mock_stream = mock.patch.object(ovsdb_monitor, 'Stream').start() |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def _register_opts(): |
| 51 | + for opt in OPTS: |
| 52 | + try: |
| 53 | + cfg.CONF.register_opt(cfg.StrOpt(opt), group='ovn') |
| 54 | + except cfg.DuplicateOptError: |
| 55 | + pass |
| 56 | + |
| 57 | + def test_set_ssl(self): |
| 58 | + cfg.CONF.set_override(self.private_key, 'key', group='ovn') |
| 59 | + cfg.CONF.set_override(self.certificate, 'cert', group='ovn') |
| 60 | + cfg.CONF.set_override(self.ca_cert, 'ca-cert', group='ovn') |
| 61 | + self.mock_os_path.return_value = True |
| 62 | + ovsdb_monitor.check_and_set_ssl_files(self.schema) |
| 63 | + self.mock_stream.ssl_set_private_key_file.assert_called_with('key') |
| 64 | + self.mock_stream.ssl_set_certificate_file.assert_called_with('cert') |
| 65 | + self.mock_stream.ssl_set_ca_cert_file.assert_called_with('ca-cert') |
| 66 | + |
| 67 | + def test_no_key_and_certs(self): |
| 68 | + cfg.CONF.set_override(self.private_key, '', group='ovn') |
| 69 | + cfg.CONF.set_override(self.certificate, '', group='ovn') |
| 70 | + cfg.CONF.set_override(self.ca_cert, '', group='ovn') |
| 71 | + self.mock_os_path.return_value = False |
| 72 | + ovsdb_monitor.check_and_set_ssl_files(self.schema) |
| 73 | + self.mock_stream.ssl_set_private_key_file.assert_not_called() |
| 74 | + self.mock_stream.ssl_set_certificate_file.assert_not_called() |
| 75 | + self.mock_stream.ssl_set_ca_cert_file.assert_not_called() |
| 76 | + |
| 77 | + def test_no_nonexisting_files(self): |
| 78 | + cfg.CONF.set_override(self.private_key, 'key', group='ovn') |
| 79 | + cfg.CONF.set_override(self.certificate, 'cert', group='ovn') |
| 80 | + cfg.CONF.set_override(self.ca_cert, 'ca-cert', group='ovn') |
| 81 | + self.mock_os_path.return_value = False |
| 82 | + |
| 83 | + with self.assertLogs(): |
| 84 | + ovsdb_monitor.check_and_set_ssl_files(self.schema) |
| 85 | + |
| 86 | + self.mock_stream.ssl_set_private_key_file.assert_not_called() |
| 87 | + self.mock_stream.ssl_set_certificate_file.assert_not_called() |
| 88 | + self.mock_stream.ssl_set_ca_cert_file.assert_not_called() |
0 commit comments