Debian Codegraph @

octavia_tempest_plugin/tests/act_stdby_scenario/v2/test_active_standby_iptables.py

raw symbols SCIP
1 # Copyright 2019 Rackspace US Inc. All rights reserved.
2 # Copyright 2019 Red Hat Inc. All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import testtools
17
18 from oslo_log import log as logging
19 from tempest import config
20 from tempest.lib.common.utils import data_utils
21 from tempest.lib.common.utils.linux import remote_client
22 from tempest.lib import decorators
23 from tempest.lib import exceptions
24
25 from octavia_tempest_plugin.common import constants as const
26 from octavia_tempest_plugin.tests import test_base
27 from octavia_tempest_plugin.tests import waiters
28
29 CONF = config.CONF
30 LOG = logging.getLogger(__name__)
31
32
33 @testtools.skipUnless(
34 CONF.validation.run_validation,
35 'Active-Standby tests will not work without run_validation enabled.')
36 class ActiveStandbyIptablesScenarioTest(
37 test_base.LoadBalancerBaseTestWithCompute):
38
39 @classmethod
40 def skip_checks(cls):
41 super(ActiveStandbyIptablesScenarioTest, cls).skip_checks()
42
43 if CONF.load_balancer.provider not in ['amphora', 'octavia']:
44 raise cls.skipException("Amphora tests require provider 'amphora' "
45 "or 'octavia' (alias to 'amphora', "
46 "deprecated) set.")
47
48 if CONF.load_balancer.loadbalancer_topology != const.ACTIVE_STANDBY:
49 raise cls.skipException("Configured load balancer topology is not "
50 "%s." % const.ACTIVE_STANDBY)
51
52 cls._get_amphora_ssh_key()
53
54 @classmethod
55 def resource_setup(cls):
56 """Setup resources needed by the tests."""
57 super(ActiveStandbyIptablesScenarioTest, cls).resource_setup()
58
59 lb_name = data_utils.rand_name("lb_member_lb1_actstdby")
60 lb_kwargs = {const.PROVIDER: CONF.load_balancer.provider,
61 const.NAME: lb_name}
62
63 # TODO(rm_work): Make this work with ipv6 and split this test for both
64 ip_version = 4
65 cls._setup_lb_network_kwargs(lb_kwargs, ip_version)
66
67 lb = cls.mem_lb_client.create_loadbalancer(**lb_kwargs)
68 cls.lb_id = lb[const.ID]
69 cls.addClassResourceCleanup(
70 cls.mem_lb_client.cleanup_loadbalancer,
71 cls.lb_id)
72
73 if CONF.validation.connect_method == 'floating':
74 port_id = lb[const.VIP_PORT_ID]
75 result = cls.lb_mem_float_ip_client.create_floatingip(
76 floating_network_id=CONF.network.public_network_id,
77 port_id=port_id)
78 floating_ip = result['floatingip']
79 LOG.info('lb1_floating_ip: %s', floating_ip)
80 cls.addClassResourceCleanup(
81 waiters.wait_for_not_found,
82 cls.lb_mem_float_ip_client.delete_floatingip,
83 cls.lb_mem_float_ip_client.show_floatingip,
84 floatingip_id=floating_ip['id'])
85 cls.lb_vip_address = floating_ip['floating_ip_address']
86 else:
87 cls.lb_vip_address = lb[const.VIP_ADDRESS]
88
89 waiters.wait_for_status(cls.mem_lb_client.show_loadbalancer,
90 cls.lb_id, const.PROVISIONING_STATUS,
91 const.ACTIVE,
92 CONF.load_balancer.lb_build_interval,
93 CONF.load_balancer.lb_build_timeout)
94
95 listener_name = data_utils.rand_name("lb_member_listener1_actstdby")
96 listener_kwargs = {
97 const.NAME: listener_name,
98 const.PROTOCOL: const.HTTP,
99 const.PROTOCOL_PORT: '80',
100 const.LOADBALANCER_ID: cls.lb_id,
101 }
102 listener = cls.mem_listener_client.create_listener(**listener_kwargs)
103 cls.listener_id = listener[const.ID]
104 cls.addClassResourceCleanup(
105 cls.mem_listener_client.cleanup_listener,
106 cls.listener_id,
107 lb_client=cls.mem_lb_client, lb_id=cls.lb_id)
108
109 waiters.wait_for_status(cls.mem_lb_client.show_loadbalancer,
110 cls.lb_id, const.PROVISIONING_STATUS,
111 const.ACTIVE,
112 CONF.load_balancer.build_interval,
113 CONF.load_balancer.build_timeout)
114
115 pool_name = data_utils.rand_name("lb_member_pool1_actstdby")
116 pool_kwargs = {
117 const.NAME: pool_name,
118 const.PROTOCOL: const.HTTP,
119 const.LB_ALGORITHM: cls.lb_algorithm,
120 const.LISTENER_ID: cls.listener_id,
121 }
122 pool = cls.mem_pool_client.create_pool(**pool_kwargs)
123 cls.pool_id = pool[const.ID]
124 cls.addClassResourceCleanup(
125 cls.mem_pool_client.cleanup_pool,
126 cls.pool_id,
127 lb_client=cls.mem_lb_client, lb_id=cls.lb_id)
128
129 waiters.wait_for_status(cls.mem_lb_client.show_loadbalancer,
130 cls.lb_id, const.PROVISIONING_STATUS,
131 const.ACTIVE,
132 CONF.load_balancer.build_interval,
133 CONF.load_balancer.build_timeout)
134
135 # Set up Member 1 for Webserver 1
136 member1_name = data_utils.rand_name("lb_member_member1_actstdby")
137 member1_kwargs = {
138 const.POOL_ID: cls.pool_id,
139 const.NAME: member1_name,
140 const.ADMIN_STATE_UP: True,
141 const.ADDRESS: cls.webserver1_ip,
142 const.PROTOCOL_PORT: 80,
143 }
144 if cls.lb_member_1_subnet:
145 member1_kwargs[const.SUBNET_ID] = cls.lb_member_1_subnet[const.ID]
146
147 member1 = cls.mem_member_client.create_member(
148 **member1_kwargs)
149 cls.addClassResourceCleanup(
150 cls.mem_member_client.cleanup_member,
151 member1[const.ID], pool_id=cls.pool_id,
152 lb_client=cls.mem_lb_client, lb_id=cls.lb_id)
153 waiters.wait_for_status(
154 cls.mem_lb_client.show_loadbalancer, cls.lb_id,
155 const.PROVISIONING_STATUS, const.ACTIVE,
156 CONF.load_balancer.check_interval,
157 CONF.load_balancer.check_timeout)
158
159 # Set up Member 2 for Webserver 2
160 member2_name = data_utils.rand_name("lb_member_member2_actstdby")
161 member2_kwargs = {
162 const.POOL_ID: cls.pool_id,
163 const.NAME: member2_name,
164 const.ADMIN_STATE_UP: True,
165 const.ADDRESS: cls.webserver2_ip,
166 const.PROTOCOL_PORT: 80,
167 }
168 if cls.lb_member_2_subnet:
169 member2_kwargs[const.SUBNET_ID] = cls.lb_member_2_subnet[const.ID]
170
171 member2 = cls.mem_member_client.create_member(
172 **member2_kwargs)
173 cls.addClassResourceCleanup(
174 cls.mem_member_client.cleanup_member,
175 member2[const.ID], pool_id=cls.pool_id,
176 lb_client=cls.mem_lb_client, lb_id=cls.lb_id)
177 waiters.wait_for_status(
178 cls.mem_lb_client.show_loadbalancer, cls.lb_id,
179 const.PROVISIONING_STATUS, const.ACTIVE,
180 CONF.load_balancer.check_interval,
181 CONF.load_balancer.check_timeout)
182
183 @classmethod
184 def _log_vip_traffic(cls, amp, log_prefix):
185 ssh_key = cls._get_amphora_ssh_key()
186 linux_client = remote_client.RemoteClient(
187 amp['lb_network_ip'], CONF.load_balancer.amphora_ssh_user,
188 pkey=ssh_key,
189 **cls.remote_client_args())
190 linux_client.validate_authentication()
191
192 # Allow logging from non-init namespaces
193 # https://lore.kernel.org/patchwork/patch/673714/
194 linux_client.exec_command('echo 1 | sudo tee '
195 '/proc/sys/net/netfilter/nf_log_all_netns')
196
197 linux_client.exec_command('sudo ip netns exec amphora-haproxy '
198 'iptables -I INPUT 1 -d {0} -j LOG '
199 '--log-prefix "{1}"'
200 .format(amp['ha_ip'], log_prefix))
201
202 @classmethod
203 def _has_vip_traffic(cls, ip_address, log_prefix):
204 ssh_key = cls._get_amphora_ssh_key()
205 linux_client = remote_client.RemoteClient(
206 ip_address, CONF.load_balancer.amphora_ssh_user, pkey=ssh_key,
207 **cls.remote_client_args())
208 linux_client.validate_authentication()
209
210 try:
211 linux_client.exec_command('sudo journalctl -t kernel | grep {0}'
212 .format(log_prefix))
213 return True
214 except exceptions.SSHExecCommandFailed:
215 return False
216
217 @classmethod
218 def _get_active_standby_amps(cls, amps, log_prefix):
219 active = None
220 stby = None
221 for amp in amps:
222 if cls._has_vip_traffic(amp['lb_network_ip'], log_prefix):
223 if active:
224 LOG.exception('Failed to determine single active amphora.')
225 raise Exception('More than one amphora is forwarding VIP '
226 'traffic.')
227 active = amp
228 else:
229 stby = amp
230
231 return active, stby
232
233 @classmethod
234 def _get_amphora_ssh_key(cls):
235 key_file = CONF.load_balancer.amphora_ssh_key
236 try:
237 with open(key_file, 'r') as f:
238 return f.read()
239 except IOError:
240 raise Exception("Could not open amphora SSH key file {0}."
241 .format(key_file))
242
243 @testtools.skipIf(CONF.load_balancer.test_with_noop,
244 'Active/Standby tests will not work in noop mode.')
245 @decorators.idempotent_id('deab2b3f-62c7-4a05-9e92-aa45a04773fd')
246 def test_active_standby_vrrp_failover(self):
247 """Tests active/standby VRRP failover
248
249 * Test the load balancer to make sure it is functioning
250 * Identifies the Master and Backup amphora
251 * Deletes the Master amphora
252 * Sends traffic through the load balancer
253 * Validates that the Backup has assumed the Master role
254 """
255
256 # Send some traffic
257 self.check_members_balanced(self.lb_vip_address)
258
259 # Check there are two amphorae associated to the load balancer
260 amps = self.os_admin.amphora_client.list_amphorae(
261 query_params='{loadbalancer_id}={lb_id}'.format(
262 loadbalancer_id=const.LOADBALANCER_ID, lb_id=self.lb_id))
263 self.assertEqual(2, len(amps))
264
265 # Log VIP traffic
266 for amp in amps:
267 self._log_vip_traffic(amp, 'ACTSTBY-1')
268
269 # Send some traffic
270 self.check_members_balanced(self.lb_vip_address)
271
272 # Which amphora is the active?
273 active = self._get_active_standby_amps(amps, 'ACTSTBY-1')[0]
274
275 # Delete active amphora
276 self.os_admin_servers_client.delete_server(active[const.COMPUTE_ID])
277
278 # Wait for the amphora failover to start
279 waiters.wait_for_status(
280 self.mem_lb_client.show_loadbalancer,
281 self.lb_id, const.PROVISIONING_STATUS,
282 const.PENDING_UPDATE, CONF.load_balancer.check_interval,
283 CONF.load_balancer.check_timeout)
284
285 # Send some traffic (checks VRRP failover)
286 self.check_members_balanced(self.lb_vip_address)
287
288 # Wait for the load balancer to return to ACTIVE
289 waiters.wait_for_status(
290 self.mem_lb_client.show_loadbalancer,
291 self.lb_id, const.PROVISIONING_STATUS,
292 const.ACTIVE, CONF.load_balancer.lb_build_interval,
293 CONF.load_balancer.lb_build_timeout)
294
295 # Check again there are two amphorae associated to the load balancer
296 amps = self.os_admin.amphora_client.list_amphorae(
297 query_params='{loadbalancer_id}={lb_id}'.format(
298 loadbalancer_id=const.LOADBALANCER_ID, lb_id=self.lb_id))
299 self.assertEqual(2, len(amps))
300
301 # Log VIP traffic
302 for amp in amps:
303 self._log_vip_traffic(amp, 'ACTSTBY-2')
304
305 # Send some traffic
306 self.check_members_balanced(self.lb_vip_address)
307
308 # Ensure only one amphora is handling VIP traffic
309 self._get_active_standby_amps(amps, 'ACTSTBY-2')