Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 1 | import logging |
Ievgeniia Zadorozhna | 97dfde4 | 2022-06-17 20:05:09 +0300 | [diff] [blame] | 2 | import os |
| 3 | import sys |
| 4 | import yaml |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 5 | |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 6 | from kubernetes import client as kclient, config as kconfig |
| 7 | |
| 8 | from utils import exceptions |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 9 | from utils import os_client |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 10 | from utils import k8s_client |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 11 | |
| 12 | logger = logging.getLogger(__name__) |
| 13 | |
| 14 | |
| 15 | def compile_pairs(nodes): |
| 16 | result = {} |
Ievgeniia Zadorozhna | 670e7ff | 2023-03-29 00:22:22 +0300 | [diff] [blame] | 17 | if len(nodes) % 2 != 0: |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 18 | nodes.pop(1) |
| 19 | pairs = list(zip(*[iter(nodes)] * 2)) |
| 20 | for pair in pairs: |
Ievgeniia Zadorozhna | 670e7ff | 2023-03-29 00:22:22 +0300 | [diff] [blame] | 21 | result[pair[0] + '<>' + pair[1]] = pair |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 22 | return result |
| 23 | |
| 24 | |
| 25 | def get_pairs(): |
| 26 | config = get_configuration() |
| 27 | cmp_hosts = config.get('CMP_HOSTS') or [] |
| 28 | skipped_nodes = config.get('skipped_nodes') or [] |
| 29 | if skipped_nodes: |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 30 | sys.stdout.write(("\nNotice: {} node(s) will be skipped for vm2vm " |
| 31 | "test.\n".format(", ".join(skipped_nodes)))) |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 32 | logger.info("Skipping nodes {}".format(",".join(skipped_nodes))) |
| 33 | if not cmp_hosts: |
| 34 | openstack_clients = os_client.OfficialClientManager( |
| 35 | username=os.environ['OS_USERNAME'], |
| 36 | password=os.environ['OS_PASSWORD'], |
Ievgeniia Zadorozhna | f22827b | 2022-07-20 13:30:32 +0300 | [diff] [blame] | 37 | project_name=os.environ['OS_PROJECT_NAME'], |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 38 | auth_url=os.environ['OS_AUTH_URL'], |
| 39 | cert=False, |
Ievgeniia Zadorozhna | 68d6495 | 2024-08-22 18:15:45 +0200 | [diff] [blame^] | 40 | domain=os.environ['OS_PROJECT_DOMAIN_NAME'], |
| 41 | endpoint_type=os.environ["OS_ENDPOINT_TYPE"], |
| 42 | interface=os.environ["OS_ENDPOINT_TYPE"], |
| 43 | |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 44 | ) |
| 45 | os_actions = os_client.OSCliActions(openstack_clients) |
| 46 | nova_computes = os_actions.list_nova_computes() |
| 47 | if len(nova_computes) < 2: |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 48 | raise exceptions.NotEnoughNodes( |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 49 | "At least 2 compute hosts are needed for VM2VM test, " |
| 50 | "now: {}.".format(len(nova_computes))) |
| 51 | cmp_hosts = [n.host_name for n in nova_computes |
| 52 | if n.host_name not in skipped_nodes] |
| 53 | if len(cmp_hosts) < 2: |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 54 | raise exceptions.NotEnoughNodes( |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 55 | "At least 2 compute hosts are needed for VM2VM test. " |
| 56 | "Cannot create a pair from {}. Please check skip list, at " |
| 57 | "least 2 computes should be tested.".format(cmp_hosts)) |
| 58 | logger.info("CMP_HOSTS option is not set, using host pair from " |
| 59 | "Nova compute list. Pair generated: {}".format(cmp_hosts)) |
| 60 | |
| 61 | return compile_pairs(cmp_hosts) |
| 62 | |
| 63 | |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 64 | def get_hw_pairs(): |
| 65 | # get the K8S config, check whether the HW nodes list is set |
| 66 | config = get_configuration() |
| 67 | logger.info("Getting the K8S config path from the global_config.yaml file.") |
| 68 | k8s_config_path = config.get("mos_kubeconfig_path", "") |
| 69 | hw_nodes_list = config.get("hw_nodes_list", []) |
| 70 | |
| 71 | # if the specific HW nodes list is not set in the config, get from K8S |
| 72 | hw_nodes = None |
| 73 | if not hw_nodes_list: |
| 74 | # fetch only compute nodes |
| 75 | label_selector = "openstack-compute-node=enabled," \ |
| 76 | "openvswitch=enabled" |
| 77 | k8s_api = k8s_client.K8SClientManager(k8s_config_path=k8s_config_path) |
| 78 | k8s_actions = k8s_client.K8SCliActions(k8s_api.k8s_v1) |
| 79 | hw_nodes_list = k8s_actions.list_nodes_names( |
| 80 | label_selector=label_selector) |
| 81 | |
| 82 | # remove some skipped nodes if any |
| 83 | skipped_nodes = config.get('skipped_nodes', []) |
| 84 | if skipped_nodes: |
| 85 | print(f"Notice: {', '.join(skipped_nodes)} node(s) will be skipped for" |
| 86 | f" hw2hw test.\n") |
| 87 | hw_nodes = [node for node in hw_nodes_list |
| 88 | if node not in skipped_nodes] |
| 89 | if len(hw_nodes) < 2: |
| 90 | raise exceptions.NotEnoughNodes( |
| 91 | f"At least 2 HW nodes are required to run hw2hw test. Cannot " |
| 92 | f"create a pair from {hw_nodes}. Check whether the cluster has at" |
| 93 | f" least 2 compute nodes, or the nodes are not in the skip list.") |
| 94 | return compile_pairs(hw_nodes) |
| 95 | |
| 96 | |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 97 | def get_configuration(): |
| 98 | """function returns configuration for environment |
| 99 | and for test if it's specified""" |
| 100 | |
| 101 | global_config_file = os.path.join( |
| 102 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 103 | with open(global_config_file, 'r') as file: |
| 104 | global_config = yaml.load(file, Loader=yaml.SafeLoader) |
| 105 | for param in list(global_config.keys()): |
| 106 | if param in list(os.environ.keys()): |
| 107 | if ',' in os.environ[param]: |
| 108 | global_config[param] = [] |
| 109 | for item in os.environ[param].split(','): |
| 110 | global_config[param].append(item) |
| 111 | else: |
| 112 | global_config[param] = os.environ[param] |
| 113 | |
| 114 | return global_config |
Ievgeniia Zadorozhna | 90aa802 | 2022-08-11 18:00:23 +0300 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def check_iperf_utility(actual_iperf_utility): |
| 118 | valid_values = ["iperf", "iperf3"] |
| 119 | if actual_iperf_utility not in valid_values: |
Ievgeniia Zadorozhna | a080ec0 | 2023-12-05 02:08:52 +0100 | [diff] [blame] | 120 | raise exceptions.InvalidConfigException( |
| 121 | "The iperf utility for multiple threads test case is not correct. " |
| 122 | "Valid value is one of {}. Actual value is {}. Please set the " |
| 123 | "correct value in global_config.yaml:" |
| 124 | "multiple_threads_iperf_utility".format( |
| 125 | valid_values, actual_iperf_utility)) |