| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from unittest import mock |
| 17 |
import urllib |
| 18 |
|
| 19 |
from tenacity import RetryError |
| 20 |
from tenacity import wait_fixed |
| 21 |
|
| 22 |
from config_tempest import constants as C |
| 23 |
from config_tempest.services.octavia import LoadBalancerService |
| 24 |
from config_tempest.tests.base import BaseServiceTest |
| 25 |
|
| 26 |
|
| 27 |
class TestOctaviaService(BaseServiceTest): |
| 28 |
def setUp(self): |
| 29 |
super(TestOctaviaService, self).setUp() |
| 30 |
self.conf = self._get_conf("v2", "v3") |
| 31 |
self.clients = self._get_clients(self.conf) |
| 32 |
self.Service = LoadBalancerService("ServiceName", |
| 33 |
"ServiceType", |
| 34 |
self.FAKE_URL + "v2.0/", |
| 35 |
self.FAKE_TOKEN, |
| 36 |
disable_ssl_validation=False) |
| 37 |
self.Service.client = self.FakeServiceClient( |
| 38 |
services={"services": [{"name": "octavia", "enabled": True}]} |
| 39 |
) |
| 40 |
self.conf.set("identity", "region", "regionOne") |
| 41 |
self._fake_service_do_get_method(self.FAKE_LBAAS_PROVIDERS) |
| 42 |
|
| 43 |
def test_list_drivers(self): |
| 44 |
expected_resp = [ |
| 45 |
"amphora:The Octavia Amphora driver.", |
| 46 |
"octavia:Deprecated alias of the Octavia driver.", |
| 47 |
] |
| 48 |
providers = self.Service.list_drivers() |
| 49 |
self.assertEqual(len(providers), len(expected_resp)) |
| 50 |
|
| 51 |
@mock.patch("config_tempest.services.services.Services.is_service") |
| 52 |
def test_octavia_service_post_configuration(self, mock_is_service): |
| 53 |
mock_is_service.return_value = True |
| 54 |
self.Service.post_configuration(self.conf, mock_is_service) |
| 55 |
self.assertEqual(self.conf.get("load_balancer", "member_role"), |
| 56 |
"member") |
| 57 |
self.assertEqual(self.conf.get("load_balancer", "region"), |
| 58 |
"regionOne") |
| 59 |
self.assertEqual(self.conf.get("load_balancer", |
| 60 |
"enabled_provider_drivers"), |
| 61 |
("amphora:The Octavia Amphora driver.," |
| 62 |
"octavia:Deprecated alias of the Octavia driver."), |
| 63 |
) |
| 64 |
|
| 65 |
@mock.patch("urllib.request.urlopen") |
| 66 |
@mock.patch("os.path.exists") |
| 67 |
def test_octavia__download_file(self, |
| 68 |
mock_path_exists, |
| 69 |
mock_urlopen): |
| 70 |
mock_url = mock.Mock() |
| 71 |
mock_destination = mock.Mock() |
| 72 |
mock_data = mock.Mock(name='Fake data') |
| 73 |
|
| 74 |
|
| 75 |
mock_path_exists.return_value = True |
| 76 |
self.Service._download_file(mock_url, mock_destination) |
| 77 |
mock_urlopen.assert_not_called() |
| 78 |
|
| 79 |
|
| 80 |
mock_path_exists.return_value = False |
| 81 |
|
| 82 |
mock_response = mock.MagicMock() |
| 83 |
mock_response.read.return_value = mock_data |
| 84 |
mock_urlopen.return_value = mock_response |
| 85 |
|
| 86 |
mock_open = mock.mock_open() |
| 87 |
with mock.patch("builtins.open", mock_open): |
| 88 |
self.Service._download_file(mock_url, mock_destination) |
| 89 |
mock_urlopen.assert_called_once_with(mock_url) |
| 90 |
mock_open.assert_called_once_with(mock_destination, "wb") |
| 91 |
handle = mock_open() |
| 92 |
handle.write.assert_called_once_with(mock_data) |
| 93 |
|
| 94 |
mock_urlopen.reset_mock() |
| 95 |
|
| 96 |
|
| 97 |
mock_path_exists.return_value = False |
| 98 |
|
| 99 |
mock_response = mock.MagicMock() |
| 100 |
mock_response.read.return_value = mock_data |
| 101 |
mock_urlopen.side_effect = [ |
| 102 |
urllib.error.URLError(reason="reason1"), |
| 103 |
urllib.error.URLError(reason="reason2"), |
| 104 |
mock_response] |
| 105 |
|
| 106 |
mock_open = mock.mock_open() |
| 107 |
with mock.patch("builtins.open", mock_open): |
| 108 |
|
| 109 |
with mock.patch.object(self.Service._download_file.retry, |
| 110 |
"wait", wait_fixed(0)): |
| 111 |
self.Service._download_file(mock_url, mock_destination) |
| 112 |
mock_urlopen.assert_called_with(mock_url) |
| 113 |
mock_open.assert_called_once_with(mock_destination, "wb") |
| 114 |
handle = mock_open() |
| 115 |
handle.write.assert_called_once_with(mock_data) |
| 116 |
|
| 117 |
|
| 118 |
mock_path_exists.return_value = False |
| 119 |
|
| 120 |
mock_urlopen.side_effect = [ |
| 121 |
urllib.error.URLError(reason="reason1"), |
| 122 |
urllib.error.URLError(reason="reason2"), |
| 123 |
urllib.error.URLError(reason="reason3"), |
| 124 |
urllib.error.URLError(reason="reason4")] |
| 125 |
|
| 126 |
mock_open = mock.mock_open() |
| 127 |
with mock.patch("builtins.open", mock_open): |
| 128 |
|
| 129 |
with mock.patch.object(self.Service._download_file.retry, |
| 130 |
"wait", wait_fixed(0)): |
| 131 |
self.assertRaises(RetryError, |
| 132 |
self.Service._download_file, |
| 133 |
mock_url, |
| 134 |
mock_destination) |
| 135 |
mock_urlopen.assert_called_with(mock_url) |
| 136 |
mock_open.assert_not_called() |
| 137 |
|
| 138 |
@mock.patch("config_tempest.services.octavia.LoadBalancerService." |
| 139 |
"_download_file") |
| 140 |
@mock.patch("os.chmod") |
| 141 |
def test_octavia_get_test_server_application(self, |
| 142 |
mock_chmod, |
| 143 |
mock_download_file): |
| 144 |
|
| 145 |
self.Service.get_test_server_application(self.conf) |
| 146 |
mock_download_file.assert_not_called() |
| 147 |
|
| 148 |
|
| 149 |
location = "dummy://location" |
| 150 |
self.conf.set("load_balancer", "test_server_remote_url", |
| 151 |
location) |
| 152 |
self.Service.get_test_server_application(self.conf) |
| 153 |
mock_download_file.assert_called_once_with( |
| 154 |
location, C.DEFAULT_OCTAVIA_TEST_SERVER_FILE) |
| 155 |
mock_chmod.assert_called_once_with( |
| 156 |
C.DEFAULT_OCTAVIA_TEST_SERVER_FILE, 0o755) |
| 157 |
|
| 158 |
self.assertEqual(self.conf.get("load_balancer", "test_server_path"), |
| 159 |
C.DEFAULT_OCTAVIA_TEST_SERVER_FILE) |