-
Notifications
You must be signed in to change notification settings - Fork 9
/
trafficFlowTests.py
180 lines (146 loc) · 6.22 KB
/
trafficFlowTests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import logging
import task
from pathlib import Path
from ktoolbox import host
import testConfig
import tftbase
from evaluator import Evaluator
from task import Task
from testConfig import ConfigDescriptor
from testSettings import TestSettings
from tftbase import TftResult
from tftbase import TftResults
logger = logging.getLogger("tft." + __name__)
class TrafficFlowTests:
def _configure_namespace(self, cfg_descr: ConfigDescriptor) -> None:
namespace = cfg_descr.get_tft().namespace
logger.info(f"Configuring namespace {namespace}")
cfg_descr.tc.client_tenant.oc(
f"label ns --overwrite {namespace} pod-security.kubernetes.io/enforce=privileged \
pod-security.kubernetes.io/enforce-version=v1.24 \
security.openshift.io/scc.podSecurityLabelSync=false",
die_on_error=True,
)
def _cleanup_previous_testspace(self, cfg_descr: ConfigDescriptor) -> None:
namespace = cfg_descr.get_tft().namespace
client = cfg_descr.tc.client_tenant
logger.info(
f"Cleaning pods, services and multi-networkpolicies with label tft-tests in namespace {namespace}"
)
client.oc("delete pods -l tft-tests", namespace=namespace)
client.oc("delete services -l tft-tests", namespace=namespace)
client.oc(
"delete multi-networkpolicies -l tft-tests",
namespace=namespace,
check_success=client.check_success_delete_ignore_noexist(
"multi-networkpolicies"
),
)
logger.info(
f"Cleaning external containers {task.EXTERNAL_PERF_SERVER} (if present)"
)
host.local.run(
f"podman rm --force --time 10 {task.EXTERNAL_PERF_SERVER}",
log_level_fail=logging.WARN,
)
def _create_log_paths_from_tests(self, test: testConfig.ConfTest) -> Path:
log_file = test.get_output_file()
log_file.parent.mkdir(parents=True, exist_ok=True)
logger.info(f"Logs will be written to {log_file}")
return log_file
def _run_test_case_instance(
self,
cfg_descr: ConfigDescriptor,
instance_index: int,
reverse: bool = False,
) -> TftResult:
connection = cfg_descr.get_connection()
servers: list[task.ServerTask] = []
clients: list[task.ClientTask] = []
monitors: list[Task] = []
ts = TestSettings(
cfg_descr=cfg_descr,
instance_index=instance_index,
reverse=reverse,
)
s, c = connection.test_type_handler.create_server_client(ts)
servers.append(s)
clients.append(c)
for plugin in connection.plugins:
m = plugin.plugin.enable(
ts=ts,
perf_server=servers[-1],
perf_client=clients[-1],
tenant=True,
)
monitors.extend(m)
for t in servers + clients + monitors:
t.initialize()
ts.initialize_clmo_barrier(len(clients) + len(monitors))
for tasks in servers + clients + monitors:
tasks.start_setup()
ts.event_server_alive.wait()
for tasks in servers + clients + monitors:
tasks.start_task()
ts.event_client_finished.wait()
for tasks in servers + clients + monitors:
tasks.finish_task()
for tasks in servers + clients + monitors:
tasks.finish_setup()
tft_result_builder = tftbase.TftResultBuilder()
for tasks in servers + clients + monitors:
tasks.aggregate_output(tft_result_builder)
return tft_result_builder.build()
def _run_test_case(self, cfg_descr: ConfigDescriptor) -> list[TftResult]:
# TODO Allow for multiple connections / instances to run simultaneously
tft_results: list[TftResult] = []
for cfg_descr2 in cfg_descr.describe_all_connections():
connection = cfg_descr2.get_connection()
logger.info(f"Starting {connection.name}")
logger.info(f"Number Of Simultaneous connections {connection.instances}")
for instance_index in range(connection.instances):
tft_results.append(
self._run_test_case_instance(
cfg_descr2,
instance_index=instance_index,
)
)
if connection.test_type_handler.can_run_reverse():
tft_results.append(
self._run_test_case_instance(
cfg_descr2,
instance_index=instance_index,
reverse=True,
)
)
self._cleanup_previous_testspace(cfg_descr2)
return tft_results
def _run_test_cases(self, cfg_descr: ConfigDescriptor) -> TftResults:
tft_results_lst: list[TftResult] = []
for cfg_descr2 in cfg_descr.describe_all_test_cases():
tft_results_lst.extend(self._run_test_case(cfg_descr2))
return TftResults(lst=tuple(tft_results_lst))
def test_run(
self,
cfg_descr: ConfigDescriptor,
evaluator: Evaluator,
) -> None:
test = cfg_descr.get_tft()
self._configure_namespace(cfg_descr)
self._cleanup_previous_testspace(cfg_descr)
logger.info(f"Running test {test.name} for {test.duration} seconds")
tft_results = self._run_test_cases(cfg_descr)
logger.info("Evaluating results of tests")
tft_results = evaluator.eval(tft_results=tft_results)
result_status = tft_results.get_pass_fail_status()
result_status.log()
log_file = self._create_log_paths_from_tests(test)
logger.info(f"Write results to {log_file}")
tft_results.serialize_to_file(log_file)
# For backward compatiblity, still write the "-RESULTS" file. It's
# mostly useless now as it's identical to the main file.
tft_results.serialize_to_file(
log_file.parent / (str(log_file.stem) + "-RESULTS")
)
if not result_status.result:
logger.error(f"Failure detected in {cfg_descr.get_tft().name} results")