-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmisc.py
1442 lines (1222 loc) · 53.9 KB
/
misc.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import configparser
import glob
import html
import json
import os
import re
import sys
import warnings
import yaml
import xml.etree.ElementTree as ET
try:
from systemd import journal # type: ignore [import]
except ModuleNotFoundError:
# Match executiion as `pytest` or `python -m pytest`
if "pytest" in sys.argv[0] or "pytest" in sys.argv:
warnings.warn(
"ModuleNotFoundError: systemd.journal module is missing", UserWarning
)
else:
raise
import nmci
def __getattr__(attr):
return getattr(_module, attr)
class _Misc:
TEST_NAME_VALID_CHAR_SET = "-a-zA-Z0-9_.+=/"
NMCI_JOURNAL_MESSAGE_ID = "c93c2505-a0c5-4e26-9053-7c889d59a3de"
def test_name_normalize(self, test_name):
"""
Normalize testname, raise if invalid characters are contained.
:param test_name: name of the test
:type test_name: str
:raises ValueError: when name is invalid
:return: normalized name of the test
:rtype: str
"""
test_name0 = test_name
m = re.match("^[^_]*NetworkManager[^_]*_[^_]*[0-9]*_(.*)$", test_name)
if m:
test_name = m.group(1)
if test_name and test_name[0] == "@":
test_name = test_name[1:]
if not re.match("^[" + self.TEST_NAME_VALID_CHAR_SET + "]+$", test_name):
raise ValueError(f"Invalid test name {test_name0}")
return test_name
def test_get_feature_files(self, feature="*"):
"""
Get list of feature files,
:param feature: feature name, will interpret '*', defaults to "*"
:type feature: str, optional
:return: list of filenames
:rtype: list of str
"""
feature_dir = ""
if feature[0] != "/":
feature_dir = nmci.util.base_dir("features", "scenarios")
if not feature.endswith(".feature"):
feature = feature + ".feature"
feature_path = os.path.join(feature_dir, feature)
return glob.glob(feature_path)
_re_tag = re.compile("^\\s*@([" + TEST_NAME_VALID_CHAR_SET + "]+)($|\\s+(.*))$")
_re_sce = re.compile(r"^\s*Scenario: +")
def _test_load_tags_from_file(self, filename, test_name=None):
"""
Loads tags from feature file.
:param filename: feature file name
:type filename: str
:param test_name: tag name of the test, defaults to None
:type test_name: str, optional
:yield: list of tags
:rtype: list of str
"""
with open(filename, "rb") as f:
i_line = 0
tags = []
for cur_line in f:
i_line += 1
cur_line = cur_line.decode("utf-8", errors="strict")
s = cur_line
tag_added = False
while True:
m = self._re_tag.match(s)
if not m:
if tag_added:
if s and s[0] != "#":
# We found tags on the same line, but now there is some garbage(??)
raise Exception(
f"Invalid tag at {filename}:{i_line}: followed by garbage and not a # comment"
)
break
tags.append(m.group(1))
s = m.group(3)
tag_added = True
if s is None:
break
scenario_line = not tag_added and self._re_sce.match(cur_line)
if not tags:
# We are in between tags and have no tags yet. Proceed to next line.
if scenario_line:
# Hm? A "Scenario:" but not tags? That's wrong.
raise Exception(
f"Unexpected scenario without any tags at {filename}:{i_line}"
)
continue
if not scenario_line:
if tag_added:
# Good, we just found a tag on the current line.
continue
if re.match("^\\s*(#.*)?$", cur_line):
# an empty or comment line between tags is also fine.
continue
if re.match("^Feature:", cur_line):
# These were the tags for the feature. We ignore them.
tags = []
continue
raise Exception(
f"Invalid feature file {filename}:{i_line}: all tags are expected in consecutive lines"
)
if test_name is None or test_name in tags:
yield tags
tags = []
if tags:
raise Exception(
f"Invalid feature file {filename}:{i_line}: contains tags without a Scenario"
)
def test_load_tags_from_file(self, filename, test_name=None):
"""
Load tags from feature file.
We memoize the result of the parsing. Feel free to
:code:`delattr(self, "_test_load_tags_from_file_cache")` to
prune the cache.
:param filename: feature file name
:type filename: str
:param test_name: tag name of the test, defaults to None
:type test_name: str, optional
:return: list of test tags
:rtype: list of list of str
"""
k = (filename, test_name)
if hasattr(self, "_test_load_tags_from_file_cache"):
l = self._test_load_tags_from_file_cache.get(k, None)
if l is not None:
return l
else:
self._test_load_tags_from_file_cache = {}
if test_name is None:
l = list(self._test_load_tags_from_file(filename))
else:
l = self.test_load_tags_from_file(filename)
l = [tags for tags in l if test_name in tags]
self._test_load_tags_from_file_cache[k] = l
return l
def test_load_tags_from_features(
self,
feature=None,
test_name=None,
feature_file=None,
):
"""
Load test tags from feature.
:param feature: name of the featue, defaults to None
:type feature: str, optional
:param test_name: tag name of the test, defaults to None
:type test_name: str, optional
:param feature_file: filename of the feature file, defaults to None
:type feature_file: str, optional
:return: list of tags of the tests
:rtype: list of list of str
"""
if feature_file is not None:
assert feature is None
feature_files = [feature_file]
else:
if feature is None:
feature = "*"
feature_files = self.test_get_feature_files(feature=feature)
test_tags = []
for filename in feature_files:
for tags in self.test_load_tags_from_file(filename, test_name):
test_tags.append(tags)
return test_tags
def get_mapper_obj(self):
"""
Loads mapper as python object (dict). Yaml loader is significantly slower
than json. Since this is called multiple times per test, we convert yaml
to json cache. Json cache is not used if it does not exists or yaml file has
newer modification timestamp.
:return: mapper
:rtype: dict
"""
if not os.path.isfile("mapper.json") or (
os.path.getmtime("mapper.json") < os.path.getmtime("mapper.yaml")
):
with open("mapper.yaml", "r") as m_yaml:
mapper = yaml.load(m_yaml, Loader=yaml.SafeLoader)
with open("mapper.json", "w") as m_json:
json.dump(mapper, m_json)
return mapper
with open("mapper.json", "r") as m_file:
return json.load(m_file)
def get_mapper_tests(self, mapper, feature="*", testmapper="*"):
"""
Get all tests from mapper. Possible to filter specific feature or testmapper.
:param mapper: mapper object
:type mapper: dict
:param feature: name of the feature, defaults to "*"
:type feature: str, optional
:param testmapper: name of the testmapper, defaults to "*"
:type testmapper: str, optional
:return: list of the tests with attributes
:rtype: list of dict
"""
all_values = ["*", "all"]
if testmapper in all_values:
testmappers = [x for x in mapper["testmapper"]]
else:
testmappers = [testmapper]
def flatten_test(test):
testname = list(test.keys())[0]
test = test[testname]
test["testname"] = testname
if testname.startswith("gsm_hub"):
test["feature"] = "gsm"
return test
mapper_tests = [
flatten_test(x) for tm in testmappers for x in mapper["testmapper"][tm]
]
return [
test
for test in mapper_tests
if (
"feature" in test
and (test["feature"] == feature or feature in all_values)
)
or ("feature" not in test and feature in all_values)
]
def nm_version_parse(self, version):
"""
Parses the version string from :code:`/sbin/NetworkManager -V` and detects a version
array and a stream string.
In particular, the stream is whether this is a package from upstream or from
dist-git (fedora/fedpkg or rhel/rhpkg).
Since a package build for e.g. rhel-8.3 always has the suffix .el8, we cannot
use that reliably to detect the stream. Well, we can, but all el8 packages
that are not actually "rhel-8" stream, must have a unique version tag.
Like for example copr builds of upstream have.
:param version: version
:type version: str
:raises ValueError: when unable to parse version
:return: tuple of stream and version as list of ints
:rtype: tuple of string and list of int
"""
m = re.match(r"^(.*)\.((el|fc)([0-9]+)(_([0-9]+))?)$", version)
if m:
if m.group(3) == "el":
d = "rhel"
else:
d = "fedora"
pkg_suffix = {
"full": m.group(2),
"dist": d,
"dist_1": m.group(4),
"dist_2": m.group(6),
}
version_base = m.group(1)
else:
pkg_suffix = None
version_base = version
m = re.match(
r"^([0-9]+\.[0-9]+\.[0-9]+([-.][0-9]+(\.[0-9]+)*)?)([-.].+)??$",
version_base,
)
if m:
v = [int(x) for x in m.group(1).replace("-", ".").split(".")]
if len(v) < 4:
stream = "unknown"
elif v[3] > 1000:
stream = "upstream"
elif pkg_suffix:
stream = f"{pkg_suffix['dist']}-{pkg_suffix['dist_1']}"
if pkg_suffix["dist_2"]:
stream = f"{stream}-{pkg_suffix['dist_2']}"
else:
stream = "unknown"
return (stream, v)
raise ValueError('cannot parse version "%s"' % (version))
def test_version_tag_parse(self, version_tag, tag_candidate):
"""
Parse version tag into operator and version, given that prefix is preparsed.
:param version_tag: version tag
:type version_tag: str
:param tag_candidate: prefix of the version tag (e.g. :code:`'ver'` or :code:`'ver/rhel/8'`)
:type tag_candidate: str
:return: tuple of operator and version as list of int
:rtype: tuple of str and list of int
"""
version_tag0 = version_tag
if not version_tag.startswith(tag_candidate):
raise ValueError(
f'tag "{version_tag0}" does not start with "{tag_candidate}"'
)
version_tag = version_tag[len(tag_candidate) :]
if version_tag == "-" or version_tag == "+":
# as a special case, we support plain @ver+/@ver- tags. They
# make sense to always enabled/disable a test per stream.
# For example, if a certain test should never run with a RHEL
# package, use "@ver/rhel-"
return (version_tag, [])
if version_tag.startswith("+=") or version_tag.startswith("-="):
op = version_tag[0:2]
ver = version_tag[2:]
elif version_tag.startswith("+") or version_tag.startswith("-"):
op = version_tag[0:1]
ver = version_tag[1:]
else:
raise ValueError(
f'tag "{version_tag0}" does not have a suitable "+-" part for "{tag_candidate}"'
)
if not re.match("^[0-9.]+$", ver):
raise ValueError(
f'tag "{version_tag0}" does not have a suitable version number for "{tag_candidate}"'
)
ver_arr = [int(x) for x in ver.split(".")]
return (op, ver_arr)
def test_version_tag_parse_ver(self, version_tag):
"""
This parses tags in the form @ver$STREAM$OP$VERSION where
- $STREAM is for example "", "/upstream", "/fedora", "/fedora/33", "/rhel", "/rhel/8". It matches
the stream returned by nm_version_parse().
- $OP is the comparison operator ("-", "-=", "+", "+=")
- $VERSION is the version number to compare. Corresponds the version returned by :code:`nm_version_parse()`.
:param version_tag: version tag
:type version_tag: str
:raises ValueError: if version tag is invalid
:return: tuple of stream and operator and version
:rtype: tuple of string and string and list of ints
"""
if not version_tag.startswith("ver"):
raise ValueError(f'version tag "{version_tag}" does not start with "ver""')
v = version_tag[len("ver") :]
stream = []
while True:
m = re.match("^/([^/\\-+=]+)", v)
if not m:
break
stream.append(m.group(1))
v = v[(1 + len(m.group(1))) :]
op, version = self.test_version_tag_parse(
version_tag, "/".join(("ver", *stream))
)
return (stream, op, version)
def test_version_tag_filter_for_stream(self, tags_ver, nm_stream):
"""
This function now returns a list of (op,version) tuple, but only selecting
those that have a matching stream... that means, if the nm_stream is
"rhel-8-10", then:
- if exist, it will return all tags_ver with stream :code:`["rhel", "8", "10"]`
- if exist, it will return all tags_ver with stream :code:`["rhel", "8"]`
- if exist, it will return all tags_ver with stream :code:`["rhel"]`
- if exist, it will return all tags_ver with stream :code:`[]`
With this scheme, you can have a default version tag that always matches (:code:`@ver+=1.39`),
but you can override it for rhel (:code:`@ver/rhel+=x`) or even for rhel-8 only (:code:`@ver/rhel/8+=x`)
:param tags_ver: list of version tags parsed by :code:`test_version_tag_parse_ver()` (it contains a 3-tuple of (stream, op, version)
:type tags_ver: tuple of string and string and list of ints
:param nm_stream: the stream detected by :code:`nm_version_parse()`, for example :code:`rhel-8-10` or :code:`fedora-33`
:type nm_stream: str
:return: filtered versions with matching stream
:rtype: list of tuple of string and list of int
"""
if not tags_ver:
return tags_ver
nm_streams = nm_stream.split("-")
assert all(s for s in nm_streams)
while True:
if any((nm_streams == t[0] for t in tags_ver)):
return [(t[1], t[2]) for t in tags_ver if nm_streams == t[0]]
if not nm_streams:
# nothing found. Return empty.
return []
nm_streams = nm_streams[:-1]
def nm_version_detect(self, use_cached=True):
"""
Get parsed NetworkManager version. If environment variable :code:`NM_VERSION`
is set, it is used instead of getting version by :code:`NetworkManager -v`.
:param use_cached: whether to use already processed version, defaults to True
:type use_cached: bool, optional
:return: parsed version returned by :code:`nm_version_parse()`
:rtype: tuple of string and list of int
"""
if use_cached and hasattr(self, "_nm_version_detect_cached"):
return self._nm_version_detect_cached
# gather current system info (versions, pkg vs. build)
if "NM_VERSION" in os.environ:
current_version_str = os.environ["NM_VERSION"]
elif os.path.isfile("/tmp/nm_version_override"):
with open("/tmp/nm_version_override") as f:
current_version_str = f.read()
else:
current_version_str = nmci.process.run_stdout(
["NetworkManager", "-V"], embed_combine_tag=nmci.embed.NO_EMBED
)
v = self.nm_version_parse(current_version_str)
self._nm_version_detect_cached = v
return v
def distro_detect(self, use_cached=True, release_file_content=None):
"""
Get distribution name and version.
:param use_cached: whether to use already processed version, defaults to True
:type use_cached: bool, optional
:param release_file_content: content of /etc/redhat-release, if already in memory, defaults to None
:type release_file_content: str, optional
:return: distribution name and numerical version
:rtype: tuple of string and list of int
"""
if use_cached and hasattr(self, "_distro_detect_cached"):
return self._distro_detect_cached
if release_file_content is None:
release_file_content = nmci.util.file_get_content_simple(
"/etc/redhat-release"
)
release_file_content = release_file_content.strip("\n")
distro_version = [
int(x)
# get number right after "release " and split it by "."
for x in release_file_content.split("release ")[-1].split(" ")[0].split(".")
]
if "fedora" in release_file_content.lower():
distro_flavor = "fedora"
else:
distro_flavor = "rhel"
if len(distro_version) == 1:
# CentOS stream only gives "CentOS Stream release 8". Hack a minor version
# number
distro_version.append(99)
v = (distro_flavor, distro_version)
self._distro_detect_cached = v
return v
def ver_param_to_str(self, nm_stream, nm_version, distro_flavor, distro_version):
"""
String represenation of parsed NetworkManager and distro version
:param nm_stream: NetworkManager stream
:type nm_stream: str
:param nm_version: NetworkManager version
:type nm_version: list of int
:param distro_flavor: distribution flavor name
:type distro_flavor: str
:param distro_version: distribution version
:type distro_version: list of int
:return: formatted arguments, versions are dot separated integers :code:`nm_stream:nm_version (distro_flavor:distro_version)`
:rtype: str
"""
nm_version = ".".join([str(c) for c in nm_version])
distro_version = ".".join([str(c) for c in distro_version])
return f"{nm_stream}:{nm_version} ({distro_flavor}:{distro_version})"
def test_tags_match_version(self, test_tags, nm_version_info, distro_version_info):
"""
Check if tags match the versions of NetworkManager and distro
:param test_tags: all tags for given test
:type test_tags: list of string
:param nm_version_info: parsed NetworkManager version :code:`(stream, version)`
:type nm_version_info: tuple of string and list of int
:param distro_version_info: parsed distro version :code:`(flavor, version)`
:type distro_version_info: tuple of string and list of int
:return: test_tags if they match versions, None otherwise
:rtype: list of string
"""
(nm_stream, nm_version) = nm_version_info
(distro_flavor, distro_version) = distro_version_info
nm_stream_base = nm_stream.split("-")[0]
tags_ver = []
tags_rhelver = []
tags_fedoraver = []
run = True
has_any = False
for tag in test_tags:
has_any = True
if tag.startswith("ver"):
tags_ver.append(self.test_version_tag_parse_ver(tag))
elif tag.startswith("rhelver"):
tags_rhelver.append(self.test_version_tag_parse(tag, "rhelver"))
elif tag.startswith("fedoraver"):
tags_fedoraver.append(self.test_version_tag_parse(tag, "fedoraver"))
elif tag == "rhel_pkg":
# "@rhel_pkg" (and "@fedora_pkg") have some overlap with
# "@ver/rhel+"
#
# - if the test already specifies some @ver$OP$VERSION, then
# it's similar to "@ver- @ver/rhel$OP$VERSION" (for all @ver tags)
# - if the test does not specify other @ver tags, then it's similar
# to "@ver- @ver/rhel+".
#
# These tags are still useful aliases. Also note that they take
# into account distro_flavor, while @ver/rhel does not take it
# into account. If you rebuild a rhel package on Fedora, then
# @ver/rhel would not care that you are on Fedora, while @rhel_pkg
# would.
if not (distro_flavor == "rhel" and nm_stream_base == "rhel"):
run = False
elif tag == "not_with_rhel_pkg":
if distro_flavor == "rhel" and nm_stream_base == "rhel":
run = False
elif tag == "fedora_pkg":
if not (distro_flavor == "fedora" and nm_stream_base == "fedora"):
run = False
elif tag == "not_with_fedora_pkg":
if distro_flavor == "fedora" and nm_stream_base == "fedora":
run = False
if not has_any:
return None
if not run:
return None
tags_ver = self.test_version_tag_filter_for_stream(tags_ver, nm_stream)
# match one release below for upstream builds:
# @ver+=1.51.3 matches 1.51.2-3668.copr, but not 1.51.2-3.el10
if nm_stream == "upstream":
nm_version[2] += 1
if not self.test_version_tag_eval(tags_ver, nm_version):
return None
if distro_flavor == "rhel" and not self.test_version_tag_eval(
tags_rhelver, distro_version
):
return None
if distro_flavor == "fedora" and not self.test_version_tag_eval(
tags_fedoraver, distro_version
):
return None
return test_tags
class TestNotFoundException(Exception):
"""
Exception to be thrown when test is not found.
"""
pass
class SkipTestException(Exception):
"""
Exception to be thrown when test shoud be skipped, to prevent further code execution.
"""
pass
class HitRaceException(Exception):
"""
Exception to be thrown, when some race condition is hit.
"""
pass
class InvalidTagsException(Exception):
"""
Exception to be thrown, when some invalid tags are detected.
"""
pass
def test_tags_select(self, test_tags_list, nm_version_info, distro_version_info):
"""
Picks the test that satisfies the versions.
:param test_tags_list: list of tests (test is list of tags)
:type test_tags_list: list of list of str
:param nm_version_info: parsed NetworkManager version :code:`(stream, version)`
:type nm_version_info: tuple of string and list of int
:param distro_version_info: parsed distro version :code:`(flavor, version)`
:type distro_version_info: tuple of string and list of int
:raises self.InvalidTagsException: when multiple tests matches
:raises self.SkipTestException: when none test is matched
:return: tags of the matched test
:rtype: list of string
"""
(nm_stream, nm_version) = nm_version_info
# Bump upstream micro version (NM-major.minor.micro):
# e.g. @ver+=1.47.5 should match upstream 1.47.4-dev (1.47.4 + some commits)
# So, @ver tags should match the next tagged NM release when the feature was added/fixed.
# We want this in upstream only, versioning in y/z-stream is exact.
if nm_stream == "upstream" and len(nm_version) >= 3:
nm_version[2] += 1
(distro_flavor, distro_version) = distro_version_info
result = None
for test_tags in test_tags_list:
t = self.test_tags_match_version(
test_tags, nm_version_info, distro_version_info
)
if not t:
continue
if result:
raise self.InvalidTagsException(
"multiple matches in environment '%s': %r and %r"
% (
self.ver_param_to_str(
nm_stream, nm_version, distro_flavor, distro_version
),
result,
test_tags,
)
)
result = t
if not result:
raise self.SkipTestException(
"skipped in environment '%s'"
% (
self.ver_param_to_str(
nm_stream, nm_version, distro_flavor, distro_version
),
)
)
return result
def test_version_tag_eval(self, ver_tags, version):
"""
This is how we interpret the "ver+"/"ver-" version tags.
This scheme makes sense for versioning schemes where we have a main
branch (where major releases get tagged) and stable branches (with
minor releases). This is the versioning scheme of NetworkManager
("ver" tag) but it also works for "rhelver"/"fedoraver".
Currently it only supports a main branch (with major releases)
and stable branches (with minor releases) that branch off the
main branch. It does not support a second level of bugfix branches
that branch off stable branches, but that could be implemented too.
Notes:
1) the version tags '-'/'+' are just convenience forms of '-='/'+='. They
need no special consideration ("+1.28.5" is exactly the same as "+=1.28.6").
2) if both '-=' and '+=' are present, they might either define single closed range
e.g. "ver+=1.26, ver-=1.30", or a "hole" (buggy interval), e.g. "ver-=1.26, ver+=1.30".
3) version tags can either specify the full version ("ver+=1.26.4") or only the major
component ("ver+=1.27").
Of all the version tags of the same '+'/'-' group, the shortest and highest one also
determines the next major version.
For example, with "ver+=1.26.4, ver+=1.28.2" both tags have 3 components (they
both are the "shortest"). Of these, "ver+=1.28.2" is the highest one. From that we
automatically also get "ver+=1.29[.0]" and ver+=2[.0.0]".
This means, if you specify the latest stable version (1.28.2) that introduced a feature,
then automatically all newer major versions are covered.
Basically, the shortest and highest tag determines the major branch. If you have
more tags of the same '+'/'-' type, then those are only for the stable branch.
With example "ver+=1.26.4, ver+=1.28.2", the first tag only covers 1.26.4+ stable
versions, nothing else.
4) for each '+' version tag that comes right after '+' version tag (when sorted ascending),
there is added '-' tag, keeping only first 2 parts of version:
- "ver+=1.28.6 ver+=1.30.1" is equivalent to "ver+=1.28.6 ver-1.30 ver+=1.30.1"
meaning, that 1.29.x is satisfied, but 1.30.0 is skipped
5) for each '-' version tag that comes right before '-' version tag (when sorted ascending),
there is added '+' tag keeping only first 2 parts of version and adding "9999.9999" :
- "ver-=1.28.6 ver-=1.30.1" is equivalent to "ver-=1.28.6 ver+1.28.9999.9999 ver-=1.30.1"
:param ver_tags: parsed version tags of the test in form of :code:`(operation, version)`
:type ver_tags: list of tuple of string and list of int
:param version: parsed version to compare (of NetworkManager or distro)
:type version: list of int
:return: whether the version satisfy conditions prescribed by tags
:rtype: bool
"""
l_version = len(version)
assert l_version > 0
assert all([v >= 0 for v in version])
ver_tags = list(ver_tags)
if not ver_tags:
# no version tags means it's a PASS.
return True
# Check for the always enabled/disabled tag (which is
# encoded by op="+"/"-" and len=[]). If such a tag
# is present, it must be alone.
for op, ver in ver_tags:
if ver:
continue
assert op in ["+", "-"]
assert len(ver_tags) == 1
return op == "+"
for op, ver in ver_tags:
assert op in ["+=", "+", "-=", "-"]
assert all([type(v) is int and v >= 0 for v in ver])
if len(ver) > l_version:
raise ValueError(
'unexpectedly long version tag %s%s to compare "%s"'
% (op, ver, version)
)
# make all the tags equal length, treat "+" and "-=" as upper range
def _fill_ver(op, ver):
ver = list(ver)
while len(ver) < l_version:
if op in ["+", "-="]:
ver.append(9999)
else:
ver.append(0)
return (op, ver)
ver_tags = [_fill_ver(op, ver) for op, ver in ver_tags]
l_keep = 2
if l_version <= 2:
l_keep = 1
def _compute_aux_tag(sign, version):
inv_sign = sign.replace("+", "-") if "+" in sign else sign.replace("-", "+")
# If version is += 1.28.2, do not add -=1.28.0, but rather -1.28.0
# However, +=1.28.0 should be -=1.28.0 - so it is overlapping, rather than touching
if any(version[l_keep:]):
inv_sign = inv_sign.replace("=", "")
if "+" in sign:
version = version[:l_keep] + [0] * (l_version - l_keep)
else:
version = version[:l_keep] + [9999] * (l_version - l_keep)
return (inv_sign, version)
# Compute auxiliary tags to make stops at version breaks
# This trims version into first 2 parts
# - if we are in "+" pass, and we see +=1.34.2 +=1.36.8
# add tag -1.36.0 so, versions between 1.34.2 and 1.35
# are not skipped
# - if we are in "-" pass and we see -=1.34.2 -=1.36.8
# add tag +1.34.9999 so, versions 1.35.X are not skipped
def _add_aux_tags(ver_tags, sign):
new_tags = []
if not ver_tags:
return ver_tags
# first 2
ver_tags = list(ver_tags)
last_tag = None
for tag in ver_tags:
if sign in tag[0]:
if last_tag is not None and last_tag != tag[1][:2]:
aux_tag = _compute_aux_tag(*tag)
if aux_tag:
new_tags.append(aux_tag)
last_tag = tag[1][:2]
else:
last_tag = None
return new_tags
# this is to compare the tags with same version
# but different operator
_op_idx = lambda op: ["-", "-=", "+=", "+"].index(op)
# key for sorting the version tags
_cmp_ver = lambda x: x[1] + [_op_idx(x[0])]
ver_tags.sort(key=_cmp_ver)
# "-" pass to compute range for "-" and "-=" tags
# process in descending order
ver_tags_1 = list(reversed(_add_aux_tags(reversed(ver_tags), "-")))
# "+" pass to compute range for "+" and "+=" tags
# do this over original list, as "-" pass might add
# "+" tags, which we should ignore here
ver_tags_2 = _add_aux_tags(ver_tags, "+")
# join 2 lists, duplicates should not be an issue
ver_tags = sorted(ver_tags + ver_tags_1 + ver_tags_2, key=_cmp_ver)
def _eval(tag, version):
if tag is None:
return True
op, tag_ver = tag
if op == "+":
return version > tag_ver
elif op == "+=":
return version >= tag_ver
elif op == "-":
return version < tag_ver
elif op == "-=":
return version <= tag_ver
def _search_closest_tags(ver_tags, version):
lo, hi = None, None
for v in ver_tags:
if v[1] <= version:
lo = v
for v in reversed(ver_tags):
if v[1] >= version:
hi = v
return lo, hi
low_range, high_range = _search_closest_tags(ver_tags, version)
# check if closest tags are satisfied or not
if not _eval(low_range, version):
return False
if not _eval(high_range, version):
return False
return True
def test_find_feature_file(self, test_name, feature="*"):
"""
Return feature filename for given test name, gets data from :code:`mapper.yaml`
to prevent parsing all the feature files.
:param test_name: tag name of the test
:type test_name: str
:param feature: feature name, to narrow th search, defaults to "*"
:type feature: str, optional
:return: feature filename
:rtype: str
"""
test_name = self.test_name_normalize(test_name=test_name)
mapper_features = [
i["feature"]
for i in self.get_mapper_tests(self.get_mapper_obj(), feature)
if i["testname"] == test_name
]
if not mapper_features:
raise _Misc.TestNotFoundException(
f"test with tag '{test_name}' not defined in feature \"{feature}\"!\n"
)
return f"{nmci.util.base_dir('features', 'scenarios')}/{mapper_features[0]}.feature"
def test_version_check(self, test_name, feature="*"):
"""
This is called by version control [:code:`nmci/helpers/version_control.py`](nmci/helpers/version_control.py)
this checks for tests with given tag and returns all tags of the first test satisfying all conditions
this parses tags: :code:`ver{-,+,-=,+=}, rhelver{-,+,-=,+=}, fedoraver{-,+,-=,+=}, [not_with_]rhel_pkg, [not_with_]fedora_pkg`.
{rhel,fedora}ver tags restricts only their distros, so rhelver+=8 runs on all Fedoras, if fedoraver not restricted
to not to run on rhel / fedora use tags rhelver-=0 / fedoraver-=0 (or something similar)
:code:`{rhel,fedora}_pkg means` to run only on stock RHEL/Fedora package
:code:`not_with_{rhel,fedora}_pkg` means to run only on daily build (not patched stock package)
similarly, :code:`*_pkg` restricts only their distros, :code:`rhel_pkg` will run on all Fedoras (build and stock pkg)
since the first satisfying test is returned, the last test does not have to contain distro restrictions
and it will run only in remaining conditions - so order of the tests matters in this case
:param test_name: tag name of the test
:type test_name: str
:param feature: name of the feature, defaults to "*"
:type feature: str, optional
:return: feature filename and testname and list of all test tags
:rtype: tuple of str and str and list of str
"""
test_name = self.test_name_normalize(test_name=test_name)
feature_file = self.test_find_feature_file(test_name=test_name, feature=feature)
test_tags_list = self.test_load_tags_from_features(
feature_file=feature_file, test_name=test_name
)
if not test_tags_list:
raise _Misc.TestNotFoundException(
f"test with tag '{test_name}' not defined!\n"
)
try:
result = self.test_tags_select(
test_tags_list, self.nm_version_detect(), self.distro_detect()
)
except self.SkipTestException as e:
raise self.SkipTestException(f"skip test '{test_name}': {e}")
except Exception as e:
raise Exception(f"error checking test '{test_name}': {e}")
return (feature_file, test_name, list(result))
def nmlog_parse_dnsmasq(self, ifname):
"""
Return dnsmasq log as python object/dict.
Executes [:code:`nmci/helpers/nmlog-parse-dnsmasq.sh`](nmci/helpers/nmlog-parse-dnsmasq.sh)
:param ifname: interafce name
:type ifname: str
:return: parsed logs
:rtype: dict
"""
s = nmci.process.run_stdout(
[nmci.util.util_dir("helpers/nmlog-parse-dnsmasq.sh"), ifname], timeout=20
)
import json
return json.loads(s)
def get_dns_info(self, dns_plugin, ifindex=None, ifname=None):
"""
Get DNS interface info. At least one of ifindex and ifname must be set.
:param dns_plugin: one of :code:`systemd-resolved` or :code:`dnsmasq`
:type dns_plugin: str
:param ifindex: interfave index, defaults to None
:type ifindex: int or str, optional
:param ifname: interface name, defaults to None
:type ifname: str, optional
:return: dns information
:rtype: dict
"""