github.com/openshift/installer@v1.4.17/scripts/openstack/manifest-tests/cinder-availability-zones/test_machines.py (about) 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import unittest 5 import xmlrunner 6 7 import os 8 import sys 9 import glob 10 import yaml 11 12 ASSETS_DIR = "" 13 14 EXPECTED_MASTER_REPLICAS = 10 15 EXPECTED_WORKER_REPLICAS = 1000 16 17 EXPECTED_MASTER_ZONE_NAMES = ["MasterAZ1", "MasterAZ2", "MasterAZ3"] 18 EXPECTED_WORKER_ZONE_NAMES = ["ComputeAZ1", "ComputeAZ2", "ComputeAZ3"] 19 20 EXPECTED_VOLUME_ZONE_NAMES = ["VolumeAZ1", "VolumeAZ2", "VolumeAZ3"] 21 22 23 class CinderAvailabilityZonesMachines(unittest.TestCase): 24 def setUp(self): 25 """Parse the Machines into a Python data structure.""" 26 self.machines = [] 27 for machine_path in glob.glob( 28 f'{ASSETS_DIR}/openshift/99_openshift-cluster-api_master-machines-*.yaml' 29 ): 30 with open(machine_path) as f: 31 self.machines.append(yaml.load(f, Loader=yaml.FullLoader)) 32 33 def test_zone_names(self): 34 """Assert that all machines have one valid compute az that matches volume az.""" 35 for machine in self.machines: 36 master_zone = machine["spec"]["providerSpec"]["value"]["availabilityZone"] 37 volume_zone = machine["spec"]["providerSpec"]["value"]["rootVolume"]["availabilityZone"] 38 self.assertIn(master_zone, EXPECTED_MASTER_ZONE_NAMES) 39 self.assertIn(volume_zone, EXPECTED_VOLUME_ZONE_NAMES) 40 self.assertEqual(master_zone[-3:], volume_zone[-3:]) 41 42 def test_total_instance_number(self): 43 """Assert that there are as many Machines as required ControlPlane replicas.""" 44 self.assertEqual(len(self.machines), EXPECTED_MASTER_REPLICAS) 45 46 def test_replica_distribution(self): 47 """Assert that machines are evenly distributed across volume azs.""" 48 zones = {} 49 for machine in self.machines: 50 volume_zone = machine["spec"]["providerSpec"]["value"]["rootVolume"]["availabilityZone"] 51 zones[volume_zone] = zones.get(volume_zone, 0) + 1 52 53 setpoint = 0 54 for replicas in zones.values(): 55 if setpoint == 0: 56 setpoint = replicas 57 else: 58 self.assertTrue(-2 < replicas - setpoint < 2) 59 60 61 class CinderAvailabilityZonesMachinesets(unittest.TestCase): 62 def setUp(self): 63 """Parse the MachineSets into a Python data structure.""" 64 self.machinesets = [] 65 for machineset_path in glob.glob( 66 f'{ASSETS_DIR}/openshift/99_openshift-cluster-api_worker-machineset-*.yaml' 67 ): 68 with open(machineset_path) as f: 69 self.machinesets.append(yaml.load(f, Loader=yaml.FullLoader)) 70 71 def test_machineset_zone_name(self): 72 """Assert that there is exactly one MachineSet per volume availability zone.""" 73 found = [] 74 for machineset in self.machinesets: 75 master_zone = machineset["spec"]["template"]["spec"]["providerSpec"]["value"]["availabilityZone"] 76 volume_zone = machineset["spec"]["template"]["spec"]["providerSpec"]["value"]["rootVolume"]["availabilityZone"] 77 self.assertIn(volume_zone, EXPECTED_VOLUME_ZONE_NAMES) 78 self.assertNotIn(volume_zone, found) 79 self.assertEqual(master_zone[-3:], volume_zone[-3:]) 80 found.append(volume_zone) 81 self.assertEqual(len(self.machinesets), len(EXPECTED_VOLUME_ZONE_NAMES)) 82 83 def test_total_replica_number(self): 84 """Assert that replicas spread across the MachineSets add up to the expected number.""" 85 total_found = 0 86 for machineset in self.machinesets: 87 total_found += machineset["spec"]["replicas"] 88 self.assertEqual(total_found, EXPECTED_WORKER_REPLICAS) 89 90 def test_replica_distribution(self): 91 """Assert that replicas are evenly distributed across machinesets.""" 92 setpoint = 0 93 for machineset in self.machinesets: 94 replicas = machineset["spec"]["replicas"] 95 if setpoint == 0: 96 setpoint = replicas 97 else: 98 self.assertTrue(-2 < replicas - setpoint < 2) 99 100 101 if __name__ == '__main__': 102 ASSETS_DIR = sys.argv.pop() 103 with open(os.environ.get('JUNIT_FILE', '/dev/null'), 'wb') as output: 104 unittest.main(testRunner=xmlrunner.XMLTestRunner(output=output), failfast=False, buffer=False, catchbreak=False, verbosity=2)