/
tests.py
940 lines (805 loc) · 36.7 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# Copyright 2020 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Encapsulate OVN testing."""
import logging
import juju
import tenacity
import yaml
import zaza
import zaza.model
import zaza.openstack.charm_tests.test_utils as test_utils
import zaza.openstack.utilities.generic as generic_utils
import zaza.utilities.juju
class BaseCharmOperationTest(test_utils.BaseCharmTest):
"""Base OVN Charm operation tests."""
# override if not possible to determine release pair from charm under test
release_application = None
@classmethod
def setUpClass(cls):
"""Run class setup for OVN charm operation tests."""
super(BaseCharmOperationTest, cls).setUpClass()
cls.services = ['NotImplemented'] # This must be overridden
cls.nrpe_checks = ['NotImplemented'] # This must be overridden
@tenacity.retry(
retry=tenacity.retry_if_result(lambda ret: ret is not None),
# sleep for 2mins to allow 1min cron job to run...
wait=tenacity.wait_fixed(120),
stop=tenacity.stop_after_attempt(2))
def _retry_check_commands_on_units(self, cmds, units):
return generic_utils.check_commands_on_units(cmds, units)
def test_pause_resume(self):
"""Run pause and resume tests.
Pause service and check services are stopped, then resume and check
they are started.
"""
with self.pause_resume(self.services):
logging.info('Testing pause resume (services="{}")'
.format(self.services))
def test_nrpe_configured(self):
"""Confirm that the NRPE service check files are created."""
if not zaza.model.get_relation_id(
self.application_name, 'nrpe',
remote_interface_name='nrpe-external-master'):
self.skipTest('The NRPE charm is not related to the '
'charm under test.')
units = zaza.model.get_units(self.application_name)
cmds = []
for check_name in self.nrpe_checks:
cmds.append(
'egrep -oh /usr/local.* /etc/nagios/nrpe.d/'
'check_{}.cfg'.format(check_name)
)
ret = self._retry_check_commands_on_units(cmds, units)
if ret:
logging.info(ret)
self.assertIsNone(ret, msg=ret)
class CentralCharmOperationTest(BaseCharmOperationTest):
"""OVN Central Charm operation tests."""
@classmethod
def setUpClass(cls):
"""Run class setup for OVN Central charm operation tests."""
super(CentralCharmOperationTest, cls).setUpClass()
cls.services = [
'ovn-northd',
'ovsdb-server',
]
source = zaza.model.get_application_config(
cls.application_name)['source']['value']
logging.info(source)
if 'train' in source:
cls.nrpe_checks = [
'ovn-northd',
'ovn-nb-ovsdb',
'ovn-sb-ovsdb',
]
else:
# Ussuri or later (distro or cloudarchive)
cls.nrpe_checks = [
'ovn-northd',
'ovn-ovsdb-server-sb',
'ovn-ovsdb-server-nb',
]
class ChassisCharmOperationTest(BaseCharmOperationTest):
"""OVN Chassis Charm operation tests."""
release_application = 'ovn-central'
@classmethod
def setUpClass(cls):
"""Run class setup for OVN Chassis charm operation tests."""
super(ChassisCharmOperationTest, cls).setUpClass()
cls.services = [
'ovn-controller',
]
if cls.application_name == 'ovn-chassis':
principal_app_name = 'magpie'
else:
principal_app_name = cls.application_name
source = zaza.model.get_application_config(
principal_app_name)['source']['value']
logging.info(source)
if 'train' in source:
cls.nrpe_checks = [
'ovn-host',
'ovs-vswitchd',
'ovsdb-server',
]
else:
# Ussuri or later (distro or cloudarchive)
cls.nrpe_checks = [
'ovn-controller',
'ovsdb-server',
'ovs-vswitchd',
]
def test_prefer_chassis_as_gw(self):
"""Confirm effect of prefer-chassis-as-gw configuration option."""
expected_key = 'external-ids:ovn-cms-options'
expected_value = 'enable-chassis-as-gw'
with self.config_change(
{}, {'prefer-chassis-as-gw': True},
reset_to_charm_default=True):
for unit in zaza.model.get_units(self.application_name):
self.assertEqual(
zaza.model.run_on_unit(
unit.entity_id,
'ovs-vsctl get open-vswitch . {}'.format(expected_key)
)['Stdout'].rstrip(),
expected_value)
logging.info(
'{}: "{}" set to "{}"'
.format(unit.entity_id, expected_key, expected_value))
logging.info('Config restored, checking things went back to normal')
for unit in zaza.model.get_units(self.application_name):
self.assertEqual(
zaza.model.run_on_unit(
unit.entity_id,
'ovs-vsctl get open-vswitch . '
'external-ids:ovn-cms-options')['Code'],
'1')
logging.info(
'{}: "{}" no longer present'
.format(unit.entity_id, expected_key))
def test_wrong_bridge_config(self):
"""Confirm that ovn-chassis units block with wrong bridge config."""
stored_target_deploy_status = self.test_config.get(
'target_deploy_status', {})
new_target_deploy_status = stored_target_deploy_status.copy()
new_target_deploy_status[self.application_name] = {
'workload-status': 'blocked',
'workload-status-message': 'Wrong format',
}
if 'target_deploy_status' in self.test_config:
self.test_config['target_deploy_status'].update(
new_target_deploy_status)
else:
self.test_config['target_deploy_status'] = new_target_deploy_status
with self.config_change(
self.config_current(
application_name=self.application_name,
keys=['bridge-interface-mappings']),
{'bridge-interface-mappings': 'incorrect'}):
logging.info('Charm went into blocked state as expected, restore '
'configuration')
self.test_config[
'target_deploy_status'] = stored_target_deploy_status
class DPDKTest(test_utils.BaseCharmTest):
"""DPDK-related tests."""
def _openvswitch_switch_dpdk_installed(self):
"""Assert that the openvswitch-switch-dpdk package is installed.
:raises: zaza.model.CommandRunFailed
"""
cmd = 'dpkg-query -s openvswitch-switch-dpdk'
for unit in zaza.model.get_units(self.application_name):
zaza.utilities.juju.remote_run(
unit.name, cmd, model_name=self.model_name, fatal=True)
def _ovs_dpdk_init_configured(self):
"""Assert that DPDK is configured.
:raises: AssertionError, zaza.model.CommandRunFailed
"""
cmd = 'ovs-vsctl get open-vswitch . other_config:dpdk-init'
for unit in zaza.model.get_units(self.application_name):
result = zaza.utilities.juju.remote_run(
unit.name,
cmd,
model_name=self.model_name,
fatal=True).rstrip()
assert result == '"true"', (
'DPDK not configured on {}'.format(unit.name))
def _ovs_dpdk_initialized(self):
"""Assert that OVS successfully initialized DPDK.
:raises: AssertionError, zaza.model.CommandRunFailed
"""
cmd = 'ovs-vsctl get open-vswitch . dpdk_initialized'
for unit in zaza.model.get_units(self.application_name):
result = zaza.utilities.juju.remote_run(
unit.name,
cmd,
model_name=self.model_name,
fatal=True).rstrip()
assert result == 'true', (
'DPDK not initialized on {}'.format(unit.name))
def _ovs_br_ex_port_is_system_interface(self):
"""Assert br-ex bridge is created and has system port in it.
:raises: zaza.model.CommandRunFailed
"""
cmd = ('ip link show dev $(ovs-vsctl --bare --columns name '
'find port external_ids:charm-ovn-chassis=br-ex)')
for unit in zaza.model.get_units(self.application_name):
zaza.utilities.juju.remote_run(
unit.name, cmd, model_name=self.model_name, fatal=True)
def _ovs_br_ex_port_is_dpdk_interface(self):
"""Assert br-ex bridge is created and has DPDK port in it.
:raises: zaza.model.CommandRunFailed
"""
cmd = (
'dpdk-devbind.py --status-dev net '
'| grep ^$(ovs-vsctl --bare --columns options '
'find interface external_ids:charm-ovn-chassis=br-ex '
'|cut -f2 -d=)'
'|grep "drv=vfio-pci unused=$"')
for unit in zaza.model.get_units(self.application_name):
zaza.utilities.juju.remote_run(
unit.name, cmd, model_name=self.model_name, fatal=True)
def _ovs_br_ex_interface_not_in_error(self):
"""Assert br-ex bridge is created and interface is not in error.
:raises: AssertionError, zaza.model.CommandRunFailed
"""
cmd = (
'ovs-vsctl --bare --columns error '
'find interface external_ids:charm-ovn-chassis=br-ex')
for unit in zaza.model.get_units(self.application_name):
result = zaza.utilities.juju.remote_run(
unit.name,
cmd,
model_name=self.model_name,
fatal=True).rstrip()
assert result == '', result
def _dpdk_pre_post_flight_check(self):
"""Assert state of the system before and after enable/disable DPDK."""
with self.assertRaises(
zaza.model.CommandRunFailed,
msg='openvswitch-switch-dpdk unexpectedly installed'):
self._openvswitch_switch_dpdk_installed()
with self.assertRaises(
zaza.model.CommandRunFailed,
msg='OVS unexpectedly configured for DPDK'):
self._ovs_dpdk_init_configured()
with self.assertRaises(
AssertionError,
msg='OVS unexpectedly has DPDK initialized'):
self._ovs_dpdk_initialized()
def test_enable_dpdk(self):
"""Confirm that transitioning to/from DPDK works."""
logging.info('Pre-flight check')
self._dpdk_pre_post_flight_check()
self._ovs_br_ex_port_is_system_interface()
self.enable_hugepages_vfio_on_hvs_in_vms(4)
with self.config_change(
{
'enable-dpdk': False,
'dpdk-driver': '',
},
{
'enable-dpdk': True,
'dpdk-driver': 'vfio-pci',
},
application_name='ovn-chassis'):
logging.info('Checking openvswitch-switch-dpdk is installed')
self._openvswitch_switch_dpdk_installed()
logging.info('Checking DPDK is configured in OVS')
self._ovs_dpdk_init_configured()
logging.info('Checking DPDK is successfully initialized in OVS')
self._ovs_dpdk_initialized()
logging.info('Checking that br-ex configed with DPDK interface...')
self._ovs_br_ex_port_is_dpdk_interface()
logging.info('and is not in error.')
self._ovs_br_ex_interface_not_in_error()
logging.info('Post-flight check')
self._dpdk_pre_post_flight_check()
self.disable_hugepages_vfio_on_hvs_in_vms()
self._ovs_br_ex_port_is_system_interface()
class OVSOVNMigrationTest(test_utils.BaseCharmTest):
"""OVS to OVN migration tests."""
@classmethod
def setUpClass(cls):
"""Run class setup for OVN migration tests."""
super(OVSOVNMigrationTest, cls).setUpClass()
def setUp(self):
"""Perform migration steps prior to validation."""
super(OVSOVNMigrationTest, self).setUp()
# These steps are here due to them having to be executed once and in a
# specific order prior to running any tests. The steps should still
# be idempotent if at all possible as a courtesy to anyone iterating
# on the test code.
try:
if self.one_time_init_done:
logging.debug('Skipping migration steps as they have already '
'run.')
return
except AttributeError:
logging.info('Performing migration steps.')
# as we progress through the steps our target deploy status changes
# store it in the class instance so the individual methods can
# update when appropriate.
self.target_deploy_status = self.test_config.get(
'target_deploy_status', {})
# Stop Neutron agents on hypervisors
self._pause_units('neutron-openvswitch')
try:
self._pause_units('neutron-gateway')
except KeyError:
logging.info(
'No neutron-gateway in deployment, skip pausing it.')
# Add the neutron-api-plugin-ovn subordinate which will make the
# `neutron-api-plugin-ovn` unit appear in the deployment.
#
# NOTE: The OVN drivers will not be activated until we change the
# value for the `manage-neutron-plugin-legacy-mode` config.
self._add_neutron_api_plugin_ovn_subordinate_relation()
# Adjust MTU on overlay networks
#
# Prior to this the end user will already have lowered the MTU on their
# running instances through the use of the `instance-mtu` configuration
# option and manual reconfiguration of instances that do not use DHCP.
#
# We update the value for the MTU on the overlay networks at this point
# in time because:
#
# - Agents are paused and will not actually reconfigure the networks.
#
# - Making changes to non-Geneve networks are prohibited as soon as the
# OVN drivers are activated.
#
# - Get the correct MTU value into the OVN database on first sync.
#
# - This will be particularly important for any instances using
# stateless IPv6 autoconfiguration (SLAAC) as there is currently
# no config knob to feed MTU information into the legacy ML2+OVS
# `radvd` configuration or the native OVN RA.
#
# - Said instances will reconfigure their IPv6 MTU as soon as they
# receive an RA with correct MTU when OVN takes over control.
self._run_migrate_mtu_action()
# Flip `manage-neutron-plugin-legacy-mode` to enable it
#
# NOTE(fnordahl): until we sync/repair the OVN DB this will make the
# `neutron-server` log errors. However we need the neutron unit to be
# unpaused while doing this to have the configuration rendered. The
# configuration is consumed by the `neutron-ovn-db-sync` tool.
self._configure_neutron_api()
# Stop the Neutron server prior to OVN DB sync/repair
self._pause_units('neutron-api')
# Sync the OVN DB
self._run_migrate_ovn_db_action()
# Perform the optional morphing of Neutron DB action
self._run_offline_neutron_morph_db_action()
self._resume_units('neutron-api')
# Run `cleanup` action on neutron-openvswitch units/hypervisors
self._run_cleanup_action('neutron-openvswitch')
# Run `cleanup` action on neutron-gateway units when present
try:
self._run_cleanup_action('neutron-gateway')
except KeyError:
logging.info(
'No neutron-gateway in deployment, skip cleanup of it.')
# Start the OVN controller on hypervisors
#
# NOTE(fnordahl): it is very important to have run cleanup prior to
# starting these, if you don't do that it is almost guaranteed that
# you will program the network to a state of infinite loop.
self._resume_units('ovn-chassis')
try:
self._resume_units('ovn-dedicated-chassis')
except KeyError:
logging.info(
'No ovn-dedicated-chassis in deployment, skip resume.')
# And we should be off to the races
self.one_time_init_done = True
def _add_neutron_api_plugin_ovn_subordinate_relation(self):
"""Add relation between neutron-api and neutron-api-plugin-ovn."""
try:
logging.info('Adding relation neutron-api-plugin-ovn '
'-> neutron-api')
zaza.model.add_relation(
'neutron-api-plugin-ovn', 'neutron-plugin',
'neutron-api:neutron-plugin-api-subordinate')
zaza.model.wait_for_agent_status()
# NOTE(lourot): usually in this scenario, the test bundle has been
# originally deployed with a non-related neutron-api-plugin-ovn
# subordinate application, and thus Zaza has been taught to expect
# initially no unit from this application. We are now relating it
# to a principal neutron-api application with one unit. Thus we now
# need to make sure we wait for one unit from this subordinate
# before proceeding:
target_deploy_status = self.test_config.get('target_deploy_status',
{})
try:
target_deploy_status['neutron-api-plugin-ovn'][
'num-expected-units'] = 1
except KeyError:
# num-expected-units wasn't set to 0, no expectation to be
# fixed, let's move on.
pass
zaza.model.wait_for_application_states(
states=target_deploy_status)
except juju.errors.JujuAPIError:
# we were not able to add the relation, let's make sure it's
# because it's already there
assert (zaza.model.get_relation_id(
'neutron-api-plugin-ovn', 'neutron-api',
remote_interface_name='neutron-plugin-api-subordinate')
is not None), 'Unable to add relation required for test'
logging.info('--> On the other hand, did not need to add the '
'relation as it was already there.')
def _configure_neutron_api(self):
"""Set configuration option `manage-neutron-plugin-legacy-mode`."""
logging.info('Configuring `manage-neutron-plugin-legacy-mode` for '
'neutron-api...')
n_api_config = {
'manage-neutron-plugin-legacy-mode': False,
}
with self.config_change(
n_api_config, n_api_config, 'neutron-api'):
logging.info('done')
def _run_offline_neutron_morph_db_action(self):
"""Run offline-neutron-morph-db action."""
logging.info('Running the optional `offline-neutron-morph-db` action '
'on neutron-api-plugin-ovn/leader')
generic_utils.assertActionRanOK(
zaza.model.run_action_on_leader(
'neutron-api-plugin-ovn',
'offline-neutron-morph-db',
action_params={
'i-really-mean-it': True},
raise_on_failure=True,
)
)
def _run_migrate_ovn_db_action(self):
"""Run migrate-ovn-db action."""
logging.info('Running `migrate-ovn-db` action on '
'neutron-api-plugin-ovn/leader')
generic_utils.assertActionRanOK(
zaza.model.run_action_on_leader(
'neutron-api-plugin-ovn',
'migrate-ovn-db',
action_params={
'i-really-mean-it': True},
raise_on_failure=True,
)
)
# Charm readiness is no guarantee for API being ready to serve requests.
# https://bugs.launchpad.net/charm-neutron-api/+bug/1854518
@tenacity.retry(wait=tenacity.wait_exponential(min=5, max=60),
reraise=True, stop=tenacity.stop_after_attempt(3))
def _run_migrate_mtu_action(self):
"""Run migrate-mtu action with retry.
The action is idempotent.
Due to LP: #1854518 and the point in time of the test life cycle we run
this action the probability for the Neutron API not being available
for the script to do its job is high, thus we retry.
"""
logging.info('Running `migrate-mtu` action on '
'neutron-api-plugin-ovn/leader')
generic_utils.assertActionRanOK(
zaza.model.run_action_on_leader(
'neutron-api-plugin-ovn',
'migrate-mtu',
action_params={
'i-really-mean-it': True},
raise_on_failure=True,
)
)
def _pause_units(self, application):
"""Pause units of application.
:param application: Name of application
:type application: str
"""
logging.info('Pausing {} units'.format(application))
zaza.model.run_action_on_units(
[unit.entity_id
for unit in zaza.model.get_units(application)],
'pause',
raise_on_failure=True,
)
self.target_deploy_status.update(
{
application: {
'workload-status': 'maintenance',
'workload-status-message': 'Paused',
},
},
)
def _run_cleanup_action(self, application):
"""Run cleanup action on application units.
:param application: Name of application
:type application: str
"""
logging.info('Running `cleanup` action on {} units.'
.format(application))
zaza.model.run_action_on_units(
[unit.entity_id
for unit in zaza.model.get_units(application)],
'cleanup',
action_params={
'i-really-mean-it': True},
raise_on_failure=True,
)
def _resume_units(self, application):
"""Resume units of application.
:param application: Name of application
:type application: str
"""
logging.info('Resuming {} units'.format(application))
zaza.model.run_action_on_units(
[unit.entity_id
for unit in zaza.model.get_units(application)],
'resume',
raise_on_failure=True,
)
self.target_deploy_status.pop(application)
def test_ovs_ovn_migration(self):
"""Test migration of existing Neutron ML2+OVS deployment to OVN.
The test should be run after deployment and validation of a legacy
deployment combined with subsequent run of a network connectivity test
on instances created prior to the migration.
"""
# The setUp method of this test class will perform the migration steps.
# The tests.yaml is programmed to do further validation after the
# migration.
# Reset the n-gw and n-ovs instance-mtu configuration option so it does
# not influence how further tests are executed.
reset_config_keys = ['instance-mtu']
for app in ('neutron-gateway', 'neutron-openvswitch'):
try:
zaza.model.reset_application_config(app, reset_config_keys)
logging.info('Reset configuration to default on "{}" for "{}"'
.format(app, reset_config_keys))
except KeyError:
pass
zaza.model.wait_for_agent_status()
zaza.model.block_until_all_units_idle()
zaza.model.wait_for_application_states(
states=self.target_deploy_status)
class OVNChassisDeferredRestartTest(test_utils.BaseDeferredRestartTest):
"""Deferred restart tests."""
@classmethod
def setUpClass(cls):
"""Run setup for deferred restart tests."""
super().setUpClass(application_name='ovn-chassis')
def run_tests(self):
"""Run deferred restart tests."""
# Trigger a config change which triggers a deferred hook.
self.run_charm_change_hook_test('configure_ovs')
# Trigger a package change which requires a restart
self.run_package_change_test(
'openvswitch-switch',
'openvswitch-switch')
def get_new_config(self):
"""Return the config key and new value to trigger a hook execution.
:returns: Config key and new value
:rtype: (str, bool)
"""
app_config = zaza.model.get_application_config(self.application_name)
return 'enable-sriov', str(not app_config['enable-sriov']['value'])
class OVNDedicatedChassisDeferredRestartTest(
test_utils.BaseDeferredRestartTest):
"""Deferred restart tests."""
@classmethod
def setUpClass(cls):
"""Run setup for deferred restart tests."""
super().setUpClass(application_name='ovn-dedicated-chassis')
def run_tests(self):
"""Run deferred restart tests."""
# Trigger a config change which triggers a deferred hook.
self.run_charm_change_hook_test('configure_ovs')
# Trigger a package change which requires a restart
self.run_package_change_test(
'openvswitch-switch',
'openvswitch-switch')
def get_new_config(self):
"""Return the config key and new value to trigger a hook execution.
:returns: Config key and new value
:rtype: (str, bool)
"""
app_config = zaza.model.get_application_config(self.application_name)
new_value = str(not app_config['disable-mlockall'].get('value', False))
return 'disable-mlockall', new_value
class OVNCentralDeferredRestartTest(
test_utils.BaseDeferredRestartTest):
"""Deferred restart tests for OVN Central."""
@classmethod
def setUpClass(cls):
"""Run setup for deferred restart tests."""
super().setUpClass(application_name='ovn-central')
def run_tests(self):
"""Run deferred restart tests."""
# Charm does not defer hooks so that test is not included.
# Trigger a package change which requires a restart
self.run_package_change_test(
'ovn-central',
'ovn-central')
class OVNCentralDownscaleTests(test_utils.BaseCharmTest):
"""Tests for cluster-status and cluster-kick actions."""
SB_CMD = "ovn-appctl -t /var/run/ovn/ovnsb_db.ctl {}"
NB_CMD = "ovn-appctl -t /var/run/ovn/ovnnb_db.ctl {}"
def _cluster_status_action(self):
"""Return Southbound and Northbound cluster status.
This function returns data as reported by "cluster-status" action
parsed into two dictionaries in the following order:
"Southbound status", "Northbound status"
"""
yaml_load_err = "Status of '{}' could not be loaded as yaml:\n{}"
status_raw = zaza.model.run_action_on_leader("ovn-central",
"cluster-status")
status_data = status_raw.data["results"]
# Verify expected items in the action result
self.assertIn("ovnnb", status_data)
self.assertIn("ovnsb", status_data)
try:
nb_status = yaml.safe_load(status_data["ovnnb"])
except yaml.YAMLError:
self.fail(yaml_load_err.format("northbound-cluster",
status_data["ovnnb"]))
try:
sb_status = yaml.safe_load(status_data["ovnsb"])
except yaml.YAMLError:
self.fail(yaml_load_err.format("southbound-cluster",
status_data["ovnsb"]))
return sb_status, nb_status
@staticmethod
def _add_unit(number_of_units=1):
"""Add specified number of units to ovn-central application.
This function also waits until the application reaches active state.
"""
zaza.model.add_unit(
"ovn-central",
count=number_of_units,
wait_appear=True
)
zaza.model.wait_for_application_states()
@staticmethod
def _remove_unit(unit_name):
"""Remove specified unit from ovn-central application.
This function also waits until the application reaches active state
again.
"""
zaza.model.destroy_unit("ovn-central", unit_name)
zaza.model.block_until_all_units_idle()
zaza.model.wait_for_application_states()
def _assert_servers_cleanly_removed(self, sb_id, nb_id):
"""Assert that specified members were removed from cluster.
This checks that they are no longer listed in cluster_status
and that there are no missing server.
:param sb_id: ID of a Southbound server that should no longer be
present
:type sb_id: str
:param nb_id: ID of a Northbound server that should no longer be
present
:type nb_id: str
"""
sb_status, nb_status = self._cluster_status_action()
self.assertNotIn(sb_id, sb_status["unit_map"])
self.assertNotIn("UNKNOWN", sb_status["unit_map"])
self.assertNotIn(nb_id, nb_status["unit_map"])
self.assertNotIn("UNKNOWN", nb_status["unit_map"])
def _get_server_ids(self, unit_name):
"""Return SB and NB server id belonging to the servers on the unit.
:return: Southbound and Northbound IDs (in this order)
:rtype: str, str
"""
missing_unit_err = ("Failed to find Server IDs. Unit {} is already"
" missing from the {} cluster status")
sb_status, nb_status = self._cluster_status_action()
for unit, server_id in sb_status["unit_map"].items():
if unit_name == unit:
sb_id = server_id
break
else:
self.fail(missing_unit_err.format(unit_name, "Southbound"))
for unit, server_id in nb_status["unit_map"].items():
if unit_name == unit:
nb_id = server_id
break
else:
self.fail(missing_unit_err.format(unit_name, "Northbound"))
return sb_id, nb_id
def _get_unit_hosting_ovn(self, leader):
"""Return ID of a unit with at least one OVN server leader/follower.
:param leader: If `True`, this method returns ID of a unit that host
at least one leader. Otherwise, the ID of a unit hosting at least
one follower will be returned.
:type leader: bool
:return: ID of a unit hosting OVN leader/follower (based on the
`leader` param)
:rtype: str
"""
# It's sufficient to parse only one of the cluster statuses To
# determine if unit holds at least one leader or one follower.
cluster_status, _ = self._cluster_status_action()
leader_id = cluster_status["leader"]
if leader_id == "self":
leader_id = cluster_status["server_id"][:4]
for unit_id, server_id in cluster_status["unit_map"].items():
if (server_id == leader_id) == leader:
return unit_id
else:
self.fail("Test failed to locate unit that hosts OVN leader.")
def test_cluster_status(self):
"""Test that cluster-status action returns expected results."""
application = zaza.model.get_application("ovn-central")
sb_status, nb_status = self._cluster_status_action()
# Verify that cluster status includes "unit_map" field with correct
# type
for status in (nb_status, sb_status):
self.assertIn("unit_map", status)
self.assertIsInstance(status["unit_map"], dict)
# Verify that units and their Server IDs are properly paired
expected_mapping = {}
for unit in application.units:
unit_name = unit.entity_id
nb_status_cmd = self.NB_CMD.format("cluster/status OVN_Northbound")
sb_status_cmd = self.SB_CMD.format("cluster/status OVN_Southbound")
nb_cluster_status = zaza.model.run_on_unit(unit_name,
nb_status_cmd)
sb_cluster_status = zaza.model.run_on_unit(unit_name,
sb_status_cmd)
nb_id = nb_cluster_status["Stdout"].splitlines()[0]
sb_id = sb_cluster_status["Stdout"].splitlines()[0]
expected_mapping[unit_name] = {"sb_id": sb_id, "nb_id": nb_id}
for unit_name, unit_data in expected_mapping.items():
sb_id = unit_data["sb_id"]
nb_id = unit_data["nb_id"]
self.assertEqual(sb_status["unit_map"][unit_name], sb_id)
self.assertEqual(nb_status["unit_map"][unit_name], nb_id)
def test_cluster_kick(self):
"""Test forcefully removing a member of an ovn cluster.
If unit fails to remove itself gracefully from the
Southbound/Northbound OVN clusters, it can be kicked using
"cluster-kick" action. This test simulates such scenario by removing
contents of "/var/run/ovn/*" to mess with OVN communication before
removal of the unit which prevents the unit from gracefully leaving
the OVN cluster.
"""
logging.info("Add a ovn-central unit to be kicked")
self._add_unit()
application = zaza.model.get_application("ovn-central")
removed_unit = application.units[-1].entity_id
removed_sb_id, removed_nb_id = self._get_server_ids(removed_unit)
logging.info("Killing OVN services on %s unit" % removed_unit)
zaza.model.run_on_unit(removed_unit, "rm -rf /var/run/ovn/*")
logging.info("Removing unit %s", removed_unit)
self._remove_unit(removed_unit)
# Verify that Server IDs of the removed unit are no longer associated
# with the units ID and show in "UNKNOWN" instead
sb_status, nb_status = self._cluster_status_action()
self.assertNotIn(removed_sb_id, sb_status["unit_map"])
self.assertIn(removed_sb_id, sb_status["unit_map"]["UNKNOWN"])
self.assertNotIn(removed_nb_id, nb_status["unit_map"])
self.assertIn(removed_nb_id, nb_status["unit_map"]["UNKNOWN"])
logging.info("Requesting kick of removed servers (Southbound ID: %s, "
"Northbound ID: %s) from OVN clusters",
removed_sb_id,
removed_nb_id)
action_params = {"sb-server-id": removed_sb_id,
"nb-server-id": removed_nb_id,
"i-really-mean-it": True}
zaza.model.run_action_on_leader("ovn-central",
"cluster-kick",
action_params=action_params)
# Verify that Server IDs of the removed unit are completely removed
# from the cluster status
self._assert_servers_cleanly_removed(removed_sb_id, removed_nb_id)
def test_cluster_downscale(self):
"""Test unit's graceful departure from OVN cluster.
When ovn-central unit is removed. It should automatically leave from
OVN clusters (Northbound and Southbound) as well.
"""
logging.info("Adding units needed for downscaling test.")
self._add_unit(2)
# Remove unit hosting at least one follower
non_leader_unit = self._get_unit_hosting_ovn(leader=False)
logging.info(
"Removing unit (%s) that hosts OVN follower server.",
non_leader_unit
)
non_leader_sb, non_leader_nb = self._get_server_ids(non_leader_unit)
self._remove_unit(non_leader_unit)
self._assert_servers_cleanly_removed(non_leader_sb, non_leader_nb)
# Remove unit hosting at least one leader
leader_unit = self._get_unit_hosting_ovn(leader=True)
logging.info(
"Removing unit (%s) that hosts OVN leader server.",
leader_unit
)
leader_sb, leader_nb = self._get_server_ids(leader_unit)
self._remove_unit(leader_unit)
self._assert_servers_cleanly_removed(leader_sb, leader_nb)