Debian Codegraph @

octavia_tempest_plugin/tests/api/v2/test_amphora.py

raw symbols SCIP
1 # Copyright 2019 Rackspace US Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import testtools
16 import time
17 from uuid import UUID
18
19 from dateutil import parser
20 from tempest import config
21 from tempest.lib.common.utils import data_utils
22 from tempest.lib import decorators
23
24 from octavia_tempest_plugin.common import constants as const
25 from octavia_tempest_plugin.tests import test_base
26 from octavia_tempest_plugin.tests import waiters
27
28 CONF = config.CONF
29
30
31 class AmphoraAPITest(test_base.LoadBalancerBaseTest):
32 """Test the amphora object API."""
33
34 @classmethod
35 def skip_checks(cls):
36 super(AmphoraAPITest, cls).skip_checks()
37 if CONF.load_balancer.provider not in const.AMPHORA_PROVIDERS:
38 raise cls.skipException('Amphora tests only run with the amphora '
39 'provider enabled.')
40
41 @classmethod
42 def resource_setup(cls):
43 """Setup resources needed by the tests."""
44 super(AmphoraAPITest, cls).resource_setup()
45
46 lb_name = data_utils.rand_name("lb_member_lb1-amphora-api")
47 lb_kwargs = {const.PROVIDER: CONF.load_balancer.provider,
48 const.NAME: lb_name}
49
50 cls._setup_lb_network_kwargs(lb_kwargs)
51
52 lb = cls.mem_lb_client.create_loadbalancer(**lb_kwargs)
53 cls.lb_id = lb[const.ID]
54 cls.addClassResourceCleanup(
55 cls.mem_lb_client.cleanup_loadbalancer,
56 cls.lb_id, cascade=True)
57
58 waiters.wait_for_status(cls.mem_lb_client.show_loadbalancer,
59 cls.lb_id, const.PROVISIONING_STATUS,
60 const.ACTIVE,
61 CONF.load_balancer.lb_build_interval,
62 CONF.load_balancer.lb_build_timeout)
63
64 def _expected_amp_count(self, amp_list):
65 self.assertNotEmpty(amp_list)
66 if amp_list[0][const.ROLE] in (const.ROLE_MASTER, const.ROLE_BACKUP):
67 return 2
68 return 1
69
70 @decorators.idempotent_id('a0e9ff99-2c4f-45d5-81c9-78d3107c236f')
71 def test_amphora_list_and_show(self):
72 """Tests amphora show API.
73
74 * Show amphora details.
75 * Validate the show reflects the requested values.
76 * Validates that other accounts cannot see the amphora.
77 """
78 lb_name = data_utils.rand_name("lb_member_lb2_amphora-list")
79 lb = self.mem_lb_client.create_loadbalancer(
80 name=lb_name, provider=CONF.load_balancer.provider,
81 vip_network_id=self.lb_member_vip_net[const.ID])
82 lb_id = lb[const.ID]
83 self.addCleanup(self.mem_lb_client.cleanup_loadbalancer, lb_id)
84
85 waiters.wait_for_status(self.mem_lb_client.show_loadbalancer,
86 lb_id,
87 const.PROVISIONING_STATUS,
88 const.ACTIVE,
89 CONF.load_balancer.lb_build_interval,
90 CONF.load_balancer.lb_build_timeout)
91
92 # Test RBAC for list amphorae
93 expected_allowed = ['os_admin', 'os_roles_lb_admin']
94 self.check_list_RBAC_enforcement(
95 'AmphoraClient', 'list_amphorae', expected_allowed)
96
97 # Get an actual list of the amphorae
98 amphorae = self.lb_admin_amphora_client.list_amphorae()
99
100 # There should be AT LEAST 2, there may be more depending on the
101 # configured topology
102 self.assertGreaterEqual(
103 len(amphorae), 2 * self._expected_amp_count(amphorae))
104
105 # Test filtering by loadbalancer_id
106 amphorae = self.lb_admin_amphora_client.list_amphorae(
107 query_params='{loadbalancer_id}={lb_id}'.format(
108 loadbalancer_id=const.LOADBALANCER_ID, lb_id=self.lb_id))
109 self.assertEqual(self._expected_amp_count(amphorae), len(amphorae))
110 self.assertEqual(self.lb_id, amphorae[0][const.LOADBALANCER_ID])
111
112 # Test RBAC for show amphora
113 if expected_allowed:
114 self.check_show_RBAC_enforcement(
115 'AmphoraClient', 'show_amphora', expected_allowed,
116 amphora_id=amphorae[0][const.ID])
117
118 show_amphora_response_fields = const.SHOW_AMPHORA_RESPONSE_FIELDS
119 if self.lb_admin_amphora_client.is_version_supported(
120 self.api_version, '2.1'):
121 show_amphora_response_fields.append('created_at')
122 show_amphora_response_fields.append('updated_at')
123 show_amphora_response_fields.append('image_id')
124
125 for amp in amphorae:
126
127 # Make sure all of the fields exist on the amp list records
128 for field in show_amphora_response_fields:
129 self.assertIn(field, amp)
130
131 # Verify a few of the fields are the right type
132 if self.lb_admin_amphora_client.is_version_supported(
133 self.api_version, '2.1'):
134 parser.parse(amp[const.CREATED_AT])
135 parser.parse(amp[const.UPDATED_AT])
136
137 UUID(amp[const.ID])
138 UUID(amp[const.HA_PORT_ID])
139 UUID(amp[const.LOADBALANCER_ID])
140 UUID(amp[const.COMPUTE_ID])
141 UUID(amp[const.VRRP_PORT_ID])
142 self.assertEqual(amp[const.STATUS], const.STATUS_ALLOCATED)
143 self.assertIn(amp[const.ROLE], const.AMPHORA_ROLES)
144
145 # Test that all of the fields from the amp list match those
146 # from a show for the LB we created.
147 amp_obj = self.lb_admin_amphora_client.show_amphora(
148 amphora_id=amp[const.ID])
149 for field in show_amphora_response_fields:
150 self.assertEqual(amp[field], amp_obj[field])
151
152 @decorators.idempotent_id('b7fc231b-dcfa-47a5-99f3-ec5ddcc48f30')
153 def test_amphora_update(self):
154 """Tests the amphora agent configuration update API
155
156 * Tests that users without the loadbalancer admin role cannot
157 update an amphora.
158 * Update the amphora.
159 """
160
161 # We have to do this here as the api_version and clients are not
162 # setup in time to use a decorator or the skip_checks mixin
163 if not self.lb_admin_amphora_client.is_version_supported(
164 self.api_version, '2.7'):
165 raise self.skipException('Amphora update is only available on '
166 'Octavia API version 2.7 or newer.')
167
168 amphorae = self.lb_admin_amphora_client.list_amphorae(
169 query_params='{loadbalancer_id}={lb_id}'.format(
170 loadbalancer_id=const.LOADBALANCER_ID, lb_id=self.lb_id))
171 amphora_1 = amphorae[0]
172
173 # Test RBAC for update an amphora
174 expected_allowed = ['os_admin', 'os_roles_lb_admin']
175 self.check_update_RBAC_enforcement(
176 'AmphoraClient', 'update_amphora_config', expected_allowed,
177 None, None, amphora_1[const.ID])
178
179 self.lb_admin_amphora_client.update_amphora_config(amphora_1[const.ID])
180
181 # TODO(johnsom) Assert that an amphora config setting updated
182 # when we have a setting to check.
183
184 amp = self.lb_admin_amphora_client.show_amphora(amphora_1[const.ID])
185
186 self.assertEqual(const.STATUS_ALLOCATED, amp[const.STATUS])
187
188 @decorators.idempotent_id('fb772680-b2ba-4fc3-989b-95ad8492ccaf')
189 def test_amphora_failover(self):
190 """Tests the amphora failover API.
191
192 * Validates that non-admin accounts cannot failover amphora
193 * Fails over an amphora
194 * Validates that a new amphora is built
195 """
196 amphorae = self.lb_admin_amphora_client.list_amphorae(
197 query_params='{loadbalancer_id}={lb_id}'.format(
198 loadbalancer_id=const.LOADBALANCER_ID, lb_id=self.lb_id))
199 amphora_1 = amphorae[0]
200
201 # Test RBAC for failover an amphora
202 expected_allowed = ['os_admin', 'os_roles_lb_admin']
203 self.check_update_RBAC_enforcement(
204 'AmphoraClient', 'amphora_failover', expected_allowed,
205 None, None, amphora_1[const.ID])
206
207 self.lb_admin_amphora_client.amphora_failover(amphora_1[const.ID])
208
209 waiters.wait_for_status(self.mem_lb_client.show_loadbalancer,
210 self.lb_id, const.PROVISIONING_STATUS,
211 const.ACTIVE,
212 CONF.load_balancer.lb_build_interval,
213 CONF.load_balancer.lb_build_timeout)
214
215 after_amphorae = self.lb_admin_amphora_client.list_amphorae(
216 query_params='{loadbalancer_id}={lb_id}'.format(
217 loadbalancer_id=const.LOADBALANCER_ID, lb_id=self.lb_id))
218
219 for new_amp in after_amphorae:
220 self.assertNotEqual(amphora_1[const.ID], new_amp[const.ID])
221
222 @testtools.skipIf(CONF.load_balancer.test_with_noop,
223 'Log offload tests will not work in noop mode.')
224 @testtools.skipUnless(
225 CONF.loadbalancer_feature_enabled.log_offload_enabled,
226 'Skipping log offload tests because tempest configuration '
227 '[loadbalancer-feature-enabled] log_offload_enabled is False.')
228 @decorators.idempotent_id('4e3c6fcb-5f83-4da1-8296-56f209eae30d')
229 def test_admin_log(self):
230 """Tests admin log offloading
231
232 * Create a listener
233 * Validates the listener config log message is present
234 """
235 listener_name = data_utils.rand_name("lb_member_listener1_admin_log")
236 protocol_port = '8124'
237 listener_kwargs = {
238 const.NAME: listener_name,
239 const.PROTOCOL: const.HTTP,
240 const.PROTOCOL_PORT: protocol_port,
241 const.LOADBALANCER_ID: self.lb_id,
242 }
243 listener = self.mem_listener_client.create_listener(**listener_kwargs)
244 listener_id = listener[const.ID]
245 self.addCleanup(
246 self.mem_listener_client.cleanup_listener,
247 listener_id,
248 lb_client=self.mem_lb_client, lb_id=self.lb_id)
249
250 waiters.wait_for_status(self.mem_lb_client.show_loadbalancer,
251 self.lb_id, const.PROVISIONING_STATUS,
252 const.ACTIVE,
253 CONF.load_balancer.build_interval,
254 CONF.load_balancer.build_timeout)
255
256 # We need to give the log subsystem time to commit the log
257 time.sleep(CONF.load_balancer.check_interval)
258
259 # Check for an amphora agent API call and code log entry
260 # One is logged via the gunicorn logging and the other via
261 # oslo.logging.
262 agent_found = False
263 client_found = False
264 with open(CONF.load_balancer.amphora_admin_log_file) as f:
265 for line in f:
266 if 'Octavia HaProxy Rest Client' in line:
267 client_found = True
268 if ' amphora-agent: ' in line:
269 agent_found = True
270 if client_found and agent_found:
271 break
272
273 self.assertTrue(
274 client_found,
275 'Octavia user agent string was not found in: {0}'.format(
276 CONF.load_balancer.amphora_admin_log_file))
277
278 self.assertTrue(
279 agent_found, 'Amphora agent string was not found in: {0}'.format(
280 CONF.load_balancer.amphora_admin_log_file))