Debian Codegraph @

tools/run_tests/xds_k8s_test_driver/framework/helpers/skips.py

raw
1 # Copyright 2022 The gRPC Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """The classes and predicates to assist validate test config for test cases."""
15 from dataclasses import dataclass
16 import enum
17 import logging
18 import re
19 from typing import Callable, Optional
20 import unittest
21
22 from packaging import version as pkg_version
23
24 from framework import xds_flags
25 from framework import xds_k8s_flags
26
27 logger = logging.getLogger(__name__)
28
29
30 class Lang(enum.Flag):
31 UNKNOWN = enum.auto()
32 CPP = enum.auto()
33 GO = enum.auto()
34 JAVA = enum.auto()
35 PYTHON = enum.auto()
36 NODE = enum.auto()
37
38 def __str__(self):
39 return str(self.name).lower()
40
41 @classmethod
42 def from_string(cls, lang: str):
43 try:
44 return cls[lang.upper()]
45 except KeyError:
46 return cls.UNKNOWN
47
48
49 @dataclass
50 class TestConfig:
51 """Describes the config for the test suite.
52
53 TODO(sergiitk): rename to LangSpec and rename skips.py to lang.py.
54 """
55 client_lang: Lang
56 server_lang: Lang
57 version: Optional[str]
58
59 def version_gte(self, another: str) -> bool:
60 """Returns a bool for whether the version is >= another one.
61
62 A version is greater than or equal to another version means its version
63 number is greater than or equal to another version's number. Version
64 "master" is always considered latest.
65 E.g., master >= v1.41.x >= v1.40.x >= v1.9.x.
66
67 Unspecified version is treated as 'master', but isn't explicitly set.
68 """
69 if self.version == 'master' or self.version is None:
70 return True
71 if another == 'master':
72 return False
73 return self._parse_version(self.version) >= self._parse_version(another)
74
75 def __str__(self):
76 return (f"TestConfig(client_lang='{self.client_lang}', "
77 f"server_lang='{self.server_lang}', version={self.version!r})")
78
79 @staticmethod
80 def _parse_version(s: str) -> pkg_version.Version:
81 if s.endswith(".x"):
82 s = s[:-2]
83 return pkg_version.Version(s)
84
85
86 def _get_lang(image_name: str) -> Lang:
87 return Lang.from_string(
88 re.search(r'/(\w+)-(client|server):', image_name).group(1))
89
90
91 def evaluate_test_config(check: Callable[[TestConfig], bool]) -> TestConfig:
92 """Evaluates the test config check against Abseil flags.
93
94 TODO(sergiitk): split into parse_lang_spec and check_is_supported.
95 """
96 # NOTE(lidiz) a manual skip mechanism is needed because absl/flags
97 # cannot be used in the built-in test-skipping decorators. See the
98 # official FAQs:
99 # https://abseil.io/docs/python/guides/flags#faqs
100 test_config = TestConfig(
101 client_lang=_get_lang(xds_k8s_flags.CLIENT_IMAGE.value),
102 server_lang=_get_lang(xds_k8s_flags.SERVER_IMAGE.value),
103 version=xds_flags.TESTING_VERSION.value)
104 if not check(test_config):
105 logger.info('Skipping %s', test_config)
106 raise unittest.SkipTest(f'Unsupported test config: {test_config}')
107
108 logger.info('Detected language and version: %s', test_config)
109 return test_config