|
| 1 | +"""Declarative environment requirements for RLTest tests. |
| 2 | +
|
| 3 | +A test can declare the Env parameters it needs *before* it runs, so the runner |
| 4 | +can construct the env on its behalf and inject it as a parameter. Two benefits: |
| 5 | +
|
| 6 | +1. Single source of truth: the declared spec is exactly the shape of the env |
| 7 | + that gets injected, eliminating drift between a "what env I need" hint and |
| 8 | + the in-body ``Env(...)`` call. |
| 9 | +2. Future schedulers can read each test's spec at discovery time and route |
| 10 | + same-spec tests adjacently to maximize Redis-instance reuse via |
| 11 | + ``Env.compareEnvs`` (env.py:191). |
| 12 | +
|
| 13 | +A spec is declared by applying ``@env_spec(...)`` to a test function or to a |
| 14 | +test class. A class-level spec applies to every method of that class; |
| 15 | +method-level decoration is not supported (see ``env_spec`` below). |
| 16 | +
|
| 17 | +For file-wide defaults, define a local dict and spread it into each |
| 18 | +decoration:: |
| 19 | +
|
| 20 | + BASE = dict(moduleArgs='DEFAULT_DIALECT 2') |
| 21 | +
|
| 22 | + @env_spec(**BASE, shardsCount=3) |
| 23 | + def test_cluster(env): |
| 24 | + ... |
| 25 | +
|
| 26 | +How env is delivered: |
| 27 | +
|
| 28 | +- Function tests receive the constructed env as a parameter (``def |
| 29 | + test_x(env):``). |
| 30 | +- Class tests receive it once, through ``__init__(self, env)``, and are |
| 31 | + responsible for stashing it for their methods to use. By convention that |
| 32 | + attribute is ``self.env``, but the runner does not enforce the name — it |
| 33 | + hands env to ``__init__`` and then forgets about it. Test methods **never** |
| 34 | + receive env as a parameter; they reach it through ``self``. |
| 35 | +
|
| 36 | +Example:: |
| 37 | +
|
| 38 | + @env_spec(shardsCount=3) |
| 39 | + def test_cluster(env): |
| 40 | + env.expect('FT.SEARCH', 'idx', '*').noError() |
| 41 | +
|
| 42 | + @env_spec(moduleArgs='WORKERS 1') |
| 43 | + class TestWorkers: |
| 44 | + def __init__(self, env): |
| 45 | + self.env = env # required: methods access env via ``self`` |
| 46 | +
|
| 47 | + def test_x(self): |
| 48 | + self.env.expect(...) |
| 49 | +""" |
| 50 | +import inspect |
| 51 | + |
| 52 | +from RLTest.env import Env |
| 53 | + |
| 54 | +_SPEC_KEYS = frozenset(Env.EnvCompareParams) |
| 55 | +_ATTR = '_rltest_env_spec' |
| 56 | + |
| 57 | + |
| 58 | +def _looks_like_class_method(target): |
| 59 | + """Heuristic: is ``target`` a function defined inside a class body? |
| 60 | +
|
| 61 | + At decoration time the function isn't bound to the class yet, but Python |
| 62 | + has already populated ``__qualname__`` with the enclosing scope. Examples: |
| 63 | +
|
| 64 | + f -> top-level function (not a method) |
| 65 | + outer.<locals>.g -> nested function (not a method) |
| 66 | + C.m -> class method |
| 67 | + outer.<locals>.C.m -> class defined inside a function; still a method |
| 68 | +
|
| 69 | + The rule: take whatever follows the last ``<locals>.`` (the path *inside* |
| 70 | + the innermost enclosing function scope, or the whole qualname if there's |
| 71 | + no ``<locals>``). If that trailing segment contains a dot, the target is |
| 72 | + qualified by a class name and is therefore a method. |
| 73 | + """ |
| 74 | + qn = getattr(target, '__qualname__', '') |
| 75 | + if not qn: |
| 76 | + return False |
| 77 | + trailing = qn.rsplit('<locals>.', 1)[-1] |
| 78 | + return '.' in trailing |
| 79 | + |
| 80 | + |
| 81 | +def env_spec(**kwargs): |
| 82 | + """Declare the env requirements of a test function or test class. |
| 83 | +
|
| 84 | + Allowed keys are the entries of ``Env.EnvCompareParams``; unknown keys |
| 85 | + raise ``ValueError`` at decoration time so typos can't silently disable |
| 86 | + spec-driven behaviour. |
| 87 | +
|
| 88 | + Applying ``@env_spec`` to a method inside a class is rejected: class tests |
| 89 | + share a single env across all their methods (that's the whole point of a |
| 90 | + class test). If one method needs a different env, lift it out into a |
| 91 | + standalone function or its own class. To declare a class-wide spec, |
| 92 | + decorate the class itself. |
| 93 | + """ |
| 94 | + unknown = set(kwargs) - _SPEC_KEYS |
| 95 | + if unknown: |
| 96 | + raise ValueError( |
| 97 | + "unknown env_spec keys: {}; allowed keys are: {}".format( |
| 98 | + sorted(unknown), sorted(_SPEC_KEYS) |
| 99 | + ) |
| 100 | + ) |
| 101 | + |
| 102 | + spec = dict(kwargs) |
| 103 | + |
| 104 | + def deco(target): |
| 105 | + if inspect.isfunction(target) and _looks_like_class_method(target): |
| 106 | + raise TypeError( |
| 107 | + "@env_spec is not supported on class methods (got {}). " |
| 108 | + "Class tests share one env across all methods; decorate the " |
| 109 | + "class itself, or move the test out of the class.".format( |
| 110 | + target.__qualname__ |
| 111 | + ) |
| 112 | + ) |
| 113 | + setattr(target, _ATTR, spec) |
| 114 | + return target |
| 115 | + |
| 116 | + return deco |
| 117 | + |
| 118 | + |
| 119 | +def resolve_spec(target): |
| 120 | + """Return the declared env spec for ``target``, or ``None`` if none was |
| 121 | + declared via ``@env_spec(...)``. |
| 122 | +
|
| 123 | + ``target`` is a test function or test class. The ``None`` return is the |
| 124 | + sentinel callers use for "no declared spec — fall back to default env |
| 125 | + construction." |
| 126 | + """ |
| 127 | + spec = getattr(target, _ATTR, None) |
| 128 | + return dict(spec) if spec is not None else None |
| 129 | + |
| 130 | + |
| 131 | +def spec_key(spec): |
| 132 | + """Canonical hashable key for spec equivalence. |
| 133 | +
|
| 134 | + Two tests with the same ``spec_key`` produce envs that satisfy |
| 135 | + ``Env.compareEnvs``, so they're eligible to share a Redis instance via |
| 136 | + RLTest's opportunistic-reuse path (env.py:262). Future schedulers can use |
| 137 | + this as a grouping key. |
| 138 | + """ |
| 139 | + if spec is None: |
| 140 | + return () |
| 141 | + return tuple(sorted(spec.items())) |
0 commit comments