github.com/openshift/installer@v1.4.17/scripts/openstack/manifest-tests/nova-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_MASTER_ZONE_NAMES = ["masterzone", "masterztwo", "masterzthree"]
    16  
    17  EXPECTED_WORKER_REPLICAS = 1000
    18  EXPECTED_WORKER_ZONE_NAMES = ["zone", "ztwo", "zthree"]
    19  
    20  class NovaAvailabilityZonesMachines(unittest.TestCase):
    21      def setUp(self):
    22          """Parse the Machines into a Python data structure."""
    23          self.machines = []
    24          for machine_path in glob.glob(
    25                  f'{ASSETS_DIR}/openshift/99_openshift-cluster-api_master-machines-*.yaml'
    26          ):
    27              with open(machine_path) as f:
    28                  self.machines.append(yaml.load(f, Loader=yaml.FullLoader))
    29  
    30      def test_zone_names(self):
    31          """Assert that all machines have one valid availability zone."""
    32          for machine in self.machines:
    33              zone = machine["spec"]["providerSpec"]["value"]["availabilityZone"]
    34              self.assertIn(zone, EXPECTED_MASTER_ZONE_NAMES)
    35  
    36      def test_total_instance_number(self):
    37          """Assert that there are as many Machines as required ControlPlane replicas."""
    38          self.assertEqual(len(self.machines), EXPECTED_MASTER_REPLICAS)
    39  
    40      def test_replica_distribution(self):
    41          """Assert that machines are evenly distributed across zones."""
    42          zones = {}
    43          for machine in self.machines:
    44              zone = machine["spec"]["providerSpec"]["value"]["availabilityZone"]
    45              zones[zone] = zones.get(zone, 0) + 1
    46  
    47          setpoint = 0
    48          for replicas in zones.values():
    49              if setpoint == 0:
    50                  setpoint = replicas
    51              else:
    52                  self.assertTrue(-2 < replicas - setpoint < 2)
    53  
    54  
    55  class NovaAvailabilityZonesMachinesets(unittest.TestCase):
    56      def setUp(self):
    57          """Parse the MachineSets into a Python data structure."""
    58          self.machinesets = []
    59          for machineset_path in glob.glob(
    60                  f'{ASSETS_DIR}/openshift/99_openshift-cluster-api_worker-machineset-*.yaml'
    61          ):
    62              with open(machineset_path) as f:
    63                  self.machinesets.append(yaml.load(f, Loader=yaml.FullLoader))
    64  
    65      def test_machineset_zone_name(self):
    66          """Assert that there is exactly one MachineSet per availability zone."""
    67          found = []
    68          for machineset in self.machinesets:
    69              zone = machineset["spec"]["template"]["spec"]["providerSpec"][
    70                  "value"]["availabilityZone"]
    71              self.assertIn(zone, EXPECTED_WORKER_ZONE_NAMES)
    72              self.assertNotIn(zone, found)
    73              found.append(zone)
    74          self.assertEqual(len(self.machinesets), len(EXPECTED_WORKER_ZONE_NAMES))
    75  
    76      def test_total_replica_number(self):
    77          """Assert that replicas spread across the MachineSets add up to the expected number."""
    78          total_found = 0
    79          for machineset in self.machinesets:
    80              total_found += machineset["spec"]["replicas"]
    81          self.assertEqual(total_found, EXPECTED_WORKER_REPLICAS)
    82  
    83      def test_replica_distribution(self):
    84          """Assert that replicas are evenly distributed across machinesets."""
    85          setpoint = 0
    86          for machineset in self.machinesets:
    87              replicas = machineset["spec"]["replicas"]
    88              if setpoint == 0:
    89                  setpoint = replicas
    90              else:
    91                  self.assertTrue(-2 < replicas - setpoint < 2)
    92  
    93  
    94  if __name__ == '__main__':
    95      ASSETS_DIR = sys.argv.pop()
    96      with open(os.environ.get('JUNIT_FILE', '/dev/null'), 'wb') as output:
    97          unittest.main(testRunner=xmlrunner.XMLTestRunner(output=output), failfast=False, buffer=False, catchbreak=False, verbosity=2)