blob: 6d3f948b4326decc1d1b84263ef7ef11f32f867b [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,
Ievgeniia Zadorozhna68d64952024-08-22 18:15:45 +020040 domain=os.environ['OS_PROJECT_DOMAIN_NAME'],
41 endpoint_type=os.environ["OS_ENDPOINT_TYPE"],
42 interface=os.environ["OS_ENDPOINT_TYPE"],
43
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020044 )
45 os_actions = os_client.OSCliActions(openstack_clients)
46 nova_computes = os_actions.list_nova_computes()
47 if len(nova_computes) < 2:
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +010048 raise exceptions.NotEnoughNodes(
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020049 "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 Zadorozhnaa080ec02023-12-05 02:08:52 +010054 raise exceptions.NotEnoughNodes(
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020055 "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 Zadorozhnaa080ec02023-12-05 02:08:52 +010064def 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 Zadorozhna84023022021-12-30 13:00:41 +020097def 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 Zadorozhna90aa8022022-08-11 18:00:23 +0300115
116
117def check_iperf_utility(actual_iperf_utility):
118 valid_values = ["iperf", "iperf3"]
119 if actual_iperf_utility not in valid_values:
Ievgeniia Zadorozhnaa080ec02023-12-05 02:08:52 +0100120 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))