|
14 | 14 | from unittest import mock
|
15 | 15 |
|
16 | 16 | from keystoneauth1 import exceptions as ks_exceptions
|
| 17 | +from octavia_lib.api.drivers import exceptions as driver_exceptions |
17 | 18 | from oslo_config import cfg
|
18 | 19 | from oslo_config import fixture as oslo_fixture
|
19 | 20 | from oslotest import base
|
@@ -127,3 +128,70 @@ def test_singleton_exception(self):
|
127 | 128 | c3 = clients.NeutronAuth()
|
128 | 129 | self.assertIs(c2, c3)
|
129 | 130 | self.assertEqual(os_sdk._mock_call_count, 2)
|
| 131 | + |
| 132 | + @mock.patch.object(clients, 'KeystoneSession') |
| 133 | + def test_get_client(self, mock_ks): |
| 134 | + clients.get_neutron_client() |
| 135 | + self.mock_client.assert_called_once_with( |
| 136 | + session=mock_ks().session) |
| 137 | + |
| 138 | + @mock.patch.object(clients, 'NeutronAuth', side_effect=[RuntimeError]) |
| 139 | + def test_get_client_error(self, mock_ks): |
| 140 | + msg = self.assertRaises( |
| 141 | + driver_exceptions.DriverError, |
| 142 | + clients.get_neutron_client) |
| 143 | + self.assertEqual("An unknown driver error occurred.", str(msg)) |
| 144 | + |
| 145 | + |
| 146 | +class TestOctaviaAuth(base.BaseTestCase): |
| 147 | + def setUp(self): |
| 148 | + super().setUp() |
| 149 | + config.register_opts() |
| 150 | + self.mock_client = mock.patch( |
| 151 | + 'openstack.connection.Connection').start() |
| 152 | + clients.Singleton._instances = {} |
| 153 | + |
| 154 | + @mock.patch.object(clients, 'KeystoneSession') |
| 155 | + @mock.patch('openstack.connection.Connection') |
| 156 | + def test_init(self, mock_conn, mock_ks): |
| 157 | + clients.OctaviaAuth() |
| 158 | + mock_conn.assert_called_once_with( |
| 159 | + session=mock_ks().session, |
| 160 | + region_name=mock.ANY |
| 161 | + ) |
| 162 | + |
| 163 | + def test_singleton(self): |
| 164 | + c1 = clients.OctaviaAuth() |
| 165 | + c2 = clients.OctaviaAuth() |
| 166 | + self.assertIs(c1, c2) |
| 167 | + |
| 168 | + def test_singleton_exception(self): |
| 169 | + mock_client = mock.Mock() |
| 170 | + mock_client.lbaas_proxy = 'foo' |
| 171 | + with mock.patch( |
| 172 | + 'openstack.connection.Connection', |
| 173 | + side_effect=[RuntimeError, |
| 174 | + mock_client, mock_client]) as os_sdk: |
| 175 | + self.assertRaises( |
| 176 | + RuntimeError, |
| 177 | + clients.OctaviaAuth) |
| 178 | + c2 = clients.OctaviaAuth() |
| 179 | + c3 = clients.OctaviaAuth() |
| 180 | + self.assertIs(c2, c3) |
| 181 | + self.assertEqual(os_sdk._mock_call_count, 2) |
| 182 | + |
| 183 | + @mock.patch.object(clients, 'KeystoneSession') |
| 184 | + @mock.patch('openstack.connection.Connection') |
| 185 | + def test_get_client(self, mock_conn, mock_ks): |
| 186 | + clients.get_octavia_client() |
| 187 | + mock_conn.assert_called_once_with( |
| 188 | + session=mock_ks().session, |
| 189 | + region_name=mock.ANY |
| 190 | + ) |
| 191 | + |
| 192 | + @mock.patch.object(clients, 'OctaviaAuth', side_effect=[RuntimeError]) |
| 193 | + def test_get_client_error(self, mock_ks): |
| 194 | + msg = self.assertRaises( |
| 195 | + driver_exceptions.DriverError, |
| 196 | + clients.get_octavia_client) |
| 197 | + self.assertEqual("An unknown driver error occurred.", str(msg)) |
0 commit comments