Skip to content

Commit 54f9282

Browse files
authoredJan 28, 2025··
Break up is_supported check into parsing and evaluating steps (#149)
Separates `evaluate_test_config` into `parse_lang_spec_from_flags` and `evaluate_is_supported` so that custom override logic can be inserted between them. This change exposed a bug with `cls.lang_spec.server_lang` getting out of sync with `cls.server_image` when the image is overridden in suite's `setUpClass()`.
1 parent 3c7640f commit 54f9282

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed
 

‎framework/xds_k8s_testcase.py

+31-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from absl.testing import absltest
2929
from google.protobuf import json_format
3030
import grpc
31+
from typing_extensions import TypeAlias
3132

3233
from framework import xds_flags
3334
from framework import xds_k8s_flags
@@ -68,6 +69,8 @@
6869
ClientDeploymentArgs = k8s_xds_client_runner.ClientDeploymentArgs
6970
KubernetesServerRunner = k8s_xds_server_runner.KubernetesServerRunner
7071
KubernetesClientRunner = k8s_xds_client_runner.KubernetesClientRunner
72+
TestConfig: TypeAlias = skips.TestConfig
73+
Lang: TypeAlias = skips.Lang
7174
_LoadBalancerStatsResponse = grpc_testing.LoadBalancerStatsResponse
7275
_LoadBalancerAccumulatedStatsResponse = (
7376
grpc_testing.LoadBalancerAccumulatedStatsResponse
@@ -89,36 +92,35 @@
8992
_TD_CONFIG_MAX_WAIT_SEC: Final[int] = int(TD_CONFIG_MAX_WAIT.total_seconds())
9093

9194

92-
def evaluate_test_config(
93-
check: Callable[[skips.TestConfig], bool]
94-
) -> skips.TestConfig:
95-
"""Evaluates the test config check against Abseil flags.
95+
def parse_lang_spec_from_flags() -> TestConfig:
96+
test_config = TestConfig(
97+
client_lang=skips.get_lang(xds_k8s_flags.CLIENT_IMAGE.value),
98+
server_lang=skips.get_lang(xds_k8s_flags.SERVER_IMAGE.value),
99+
version=xds_flags.TESTING_VERSION.value,
100+
)
101+
logger.info("Detected language and version: %s", test_config)
102+
return test_config
96103

97-
TODO(sergiitk): split into parse_lang_spec and check_is_supported.
98-
"""
104+
105+
def evaluate_is_supported(
106+
test_config: TestConfig, is_supported_fn: Callable[[TestConfig], bool]
107+
):
108+
"""Evaluates the suite-specific is_supported against the test_config."""
99109
# NOTE(lidiz) a manual skip mechanism is needed because absl/flags
100110
# cannot be used in the built-in test-skipping decorators. See the
101111
# official FAQs:
102112
# https://abseil.io/docs/python/guides/flags#faqs
103-
test_config = skips.TestConfig(
104-
client_lang=skips.get_lang(xds_k8s_flags.CLIENT_IMAGE.value),
105-
server_lang=skips.get_lang(xds_k8s_flags.SERVER_IMAGE.value),
106-
version=xds_flags.TESTING_VERSION.value,
107-
)
108-
if not check(test_config):
113+
if not is_supported_fn(test_config):
109114
logger.info("Skipping %s", test_config)
110115
raise absltest.SkipTest(f"Unsupported test config: {test_config}")
111116

112-
logger.info("Detected language and version: %s", test_config)
113-
return test_config
114-
115117

116118
class TdPropagationRetryableError(Exception):
117119
"""Indicates that TD config hasn't propagated yet, and it's safe to retry"""
118120

119121

120122
class XdsKubernetesBaseTestCase(base_testcase.BaseTestCase):
121-
lang_spec: skips.TestConfig
123+
lang_spec: TestConfig
122124
client_namespace: str
123125
client_runner: KubernetesClientRunner
124126
ensure_firewall: bool = False
@@ -151,7 +153,7 @@ class XdsKubernetesBaseTestCase(base_testcase.BaseTestCase):
151153
enable_dualstack: bool = False
152154

153155
@staticmethod
154-
def is_supported(config: skips.TestConfig) -> bool:
156+
def is_supported(config: TestConfig) -> bool:
155157
"""Overridden by the test class to decide if the config is supported.
156158
157159
Returns:
@@ -168,9 +170,20 @@ def setUpClass(cls):
168170
logger.info("----- Testing %s -----", cls.__name__)
169171
logger.info("Logs timezone: %s", time.localtime().tm_zone)
170172

173+
lang_spec = parse_lang_spec_from_flags()
174+
175+
# Currently it's possible to lang_spec.server_lang to be out of sync
176+
# with the server_image when a test_suite overrides the server_image
177+
# at the end of its setUpClass().
178+
# A common example is overriding the server image to canonical.
179+
# This can be fixed by moving server image overrides to its own
180+
# class method and re-parsing the lang spec.
181+
# TODO(sergiitk): provide custom server_image_override(TestConfig)
182+
171183
# Raises unittest.SkipTest if given client/server/version does not
172184
# support current test case.
173-
cls.lang_spec = evaluate_test_config(cls.is_supported)
185+
evaluate_is_supported(lang_spec, cls.is_supported)
186+
cls.lang_spec = lang_spec
174187

175188
# Must be called before KubernetesApiManager or GcpApiManager init.
176189
xds_flags.set_socket_default_timeout_from_flag()

‎framework/xds_url_map_testcase.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ def setUpClass(cls):
220220
logging.info("----- Testing %s -----", cls.__name__)
221221
logging.info("Logs timezone: %s", time.localtime().tm_zone)
222222

223+
lang_spec = xds_k8s_testcase.parse_lang_spec_from_flags()
224+
223225
# Raises unittest.SkipTest if given client/server/version does not
224226
# support current test case.
225-
xds_k8s_testcase.evaluate_test_config(cls.is_supported)
227+
xds_k8s_testcase.evaluate_is_supported(lang_spec, cls.is_supported)
226228

227229
# Configure cleanup to run after all tests regardless of
228230
# whether setUpClass failed.

0 commit comments

Comments
 (0)
Please sign in to comment.