blob: 155c03f55fea52278acf6f838f4e29912176670d [file] [log] [blame]
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02001import logging
Ievgeniia Zadorozhna97dfde42022-06-17 20:05:09 +03002import os
3import sys
4import yaml
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02005
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +01006from kubernetes import client as kclient, config as kconfig
7
8from utils import exceptions
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02009from utils import os_client
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +010010from utils import k8s_client
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020011
12logger = logging.getLogger(__name__)
13
14
15def compile_pairs(nodes):
16 result = {}
Ievgeniia Zadorozhna670e7ff2023-03-29 00:22:22 +030017 if len(nodes) % 2 != 0:
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020018 nodes.pop(1)
19 pairs = list(zip(*[iter(nodes)] * 2))
20 for pair in pairs:
Ievgeniia Zadorozhna670e7ff2023-03-29 00:22:22 +030021 result[pair[0] + '<>' + pair[1]] = pair
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020022 return result
23
24
25def 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 Zadorozhnaa080ec02023-12-05 02:08:52 +010030 sys.stdout.write(("\nNotice: {} node(s) will be skipped for vm2vm "
31 "test.\n".format(", ".join(skipped_nodes))))
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020032 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 Zadorozhnaf22827b2022-07-20 13:30:32 +030037 project_name=os.environ['OS_PROJECT_NAME'],
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020038 auth_url=os.environ['OS_AUTH_URL'],
39 cert=False,
40 domain=os.environ['OS_PROJECT_DOMAIN_NAME']
41 )
42 os_actions = os_client.OSCliActions(openstack_clients)
43 nova_computes = os_actions.list_nova_computes()
44 if len(nova_computes) < 2:
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +010045 raise exceptions.NotEnoughNodes(
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020046 "At least 2 compute hosts are needed for VM2VM test, "
47 "now: {}.".format(len(nova_computes)))
48 cmp_hosts = [n.host_name for n in nova_computes
49 if n.host_name not in skipped_nodes]
50 if len(cmp_hosts) < 2:
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +010051 raise exceptions.NotEnoughNodes(
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020052 "At least 2 compute hosts are needed for VM2VM test. "
53 "Cannot create a pair from {}. Please check skip list, at "
54 "least 2 computes should be tested.".format(cmp_hosts))
55 logger.info("CMP_HOSTS option is not set, using host pair from "
56 "Nova compute list. Pair generated: {}".format(cmp_hosts))
57
58 return compile_pairs(cmp_hosts)
59
60
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +010061def get_hw_pairs():
62 # get the K8S config, check whether the HW nodes list is set
63 config = get_configuration()
64 logger.info("Getting the K8S config path from the global_config.yaml file.")
65 k8s_config_path = config.get("mos_kubeconfig_path", "")
66 hw_nodes_list = config.get("hw_nodes_list", [])
67
68 # if the specific HW nodes list is not set in the config, get from K8S
69 hw_nodes = None
70 if not hw_nodes_list:
71 # fetch only compute nodes
72 label_selector = "openstack-compute-node=enabled," \
73 "openvswitch=enabled"
74 k8s_api = k8s_client.K8SClientManager(k8s_config_path=k8s_config_path)
75 k8s_actions = k8s_client.K8SCliActions(k8s_api.k8s_v1)
76 hw_nodes_list = k8s_actions.list_nodes_names(
77 label_selector=label_selector)
78
79 # remove some skipped nodes if any
80 skipped_nodes = config.get('skipped_nodes', [])
81 if skipped_nodes:
82 print(f"Notice: {', '.join(skipped_nodes)} node(s) will be skipped for"
83 f" hw2hw test.\n")
84 hw_nodes = [node for node in hw_nodes_list
85 if node not in skipped_nodes]
86 if len(hw_nodes) < 2:
87 raise exceptions.NotEnoughNodes(
88 f"At least 2 HW nodes are required to run hw2hw test. Cannot "
89 f"create a pair from {hw_nodes}. Check whether the cluster has at"
90 f" least 2 compute nodes, or the nodes are not in the skip list.")
91 return compile_pairs(hw_nodes)
92
93
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020094def get_configuration():
95 """function returns configuration for environment
96 and for test if it's specified"""
97
98 global_config_file = os.path.join(
99 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
100 with open(global_config_file, 'r') as file:
101 global_config = yaml.load(file, Loader=yaml.SafeLoader)
102 for param in list(global_config.keys()):
103 if param in list(os.environ.keys()):
104 if ',' in os.environ[param]:
105 global_config[param] = []
106 for item in os.environ[param].split(','):
107 global_config[param].append(item)
108 else:
109 global_config[param] = os.environ[param]
110
111 return global_config
Ievgeniia Zadorozhna90aa8022022-08-11 18:00:23 +0300112
113
114def check_iperf_utility(actual_iperf_utility):
115 valid_values = ["iperf", "iperf3"]
116 if actual_iperf_utility not in valid_values:
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +0100117 raise exceptions.InvalidConfigException(
118 "The iperf utility for multiple threads test case is not correct. "
119 "Valid value is one of {}. Actual value is {}. Please set the "
120 "correct value in global_config.yaml:"
121 "multiple_threads_iperf_utility".format(
122 valid_values, actual_iperf_utility))