Coverage for tests/unit/test_utils.py: 100%
70 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-25 19:21 -0600
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-25 19:21 -0600
1"""Test functions in es_testbed.helpers.utils"""
2# pylint: disable=missing-function-docstring,redefined-outer-name
3import typing as t
4import pytest
5from dotmap import DotMap
6from es_testbed import defaults as dft
7from es_testbed import exceptions as ex
8from es_testbed.helpers import utils as u
10FMAP: t.Dict[str, t.Dict] = {
11 'hot': dft.ilmhot(),
12 'warm': dft.ilmwarm(),
13 'cold': dft.ilmcold(),
14 'frozen': dft.ilmfrozen(),
15 'delete': dft.ilmdelete(),
16}
17REPO: str = 'repo'
18TIERS: t.Sequence[str] = ['hot', 'warm', 'cold', 'frozen', 'delete']
19TREPO: t.Dict[str, t.Union[str, None]] = {
20 'hot': None,
21 'warm': None,
22 'cold': REPO,
23 'frozen': REPO,
24 'delete': None,
25}
27def searchable(repo: str=None) -> t.Union[t.Dict[str, t.Dict[str, str]], None]:
28 if repo:
29 return {'searchable_snapshot': {'snapshot_repository': repo}}
30 return {}
32def forcemerge(fm: bool=False, mns: int=1) -> t.Union[t.Dict[str, t.Dict[str, str]], None]:
33 if fm:
34 return {'forcemerge': {'max_num_segments': mns}}
35 return {}
37@pytest.fixture
38def tiertestval():
39 def _tiertestval(tier: str, repo: str=None, fm: bool=False, mns: int=1):
40 retval = {tier: FMAP[tier]}
41 retval[tier]['actions'].update(searchable(repo))
42 retval[tier]['actions'].update(forcemerge(fm=fm, mns=mns))
43 return retval
44 return _tiertestval
46@pytest.fixture
47def builtphase():
48 def _builtphase(tier: str, repo: str=None, fm: bool=False, mns: int=1):
49 return u.build_ilm_phase(tier, actions=forcemerge(fm=fm, mns=mns), repository=repo)
50 return _builtphase
52def test_build_ilm_phase_defaults(builtphase, tiertestval):
53 for tier in TIERS:
54 assert builtphase(tier, repo=TREPO[tier]) == tiertestval(tier, repo=TREPO[tier])
56def test_build_ilm_phase_add_action():
57 expected = {'foo': 'bar'}
58 tier = 'warm'
59 assert u.build_ilm_phase(tier, actions=expected)[tier]['actions'] == expected
61def test_build_ilm_phase_fail_repo(builtphase):
62 with pytest.raises(ex.TestbedMisconfig):
63 builtphase('cold', repo=None)
65# This allows me to run multiple testing scenarios in the same test space
66def test_build_ilm_policy(tiertestval):
67 # 3 tests building different ILM policies with different tiers
68 tgroups = [['hot', 'delete'], ['hot', 'frozen', 'delete'], ['hot', 'cold', 'delete']]
69 # Each tier group corresponds to a forcemerge plan by list index, with each index a tuple for
70 # forcemerge True/False and max_num_segment count
71 fmerge = [(False, 0), (False, 0), (True, 3)]
72 for idx, tgrp in enumerate(tgroups): # Iterate over testing scenarios
73 phases = {} # Build out the phase dict for each scenario
74 fm, mns = fmerge[idx] # Extract whether to use forcemerge by index/tuple
75 for tier in tgrp: # Iterate over each tier in the testing scenario
76 phases.update(tiertestval(tier, repo=TREPO[tier])) # Update with values per tier
77 if fm: # If we're doing forcemerge
78 phases['hot']['actions'].update(forcemerge(fm=fm, mns=mns)) # Update the hot tier
79 # To keep the line more readable, build the kwargs as a dict first
80 kwargs = {'repository': REPO, 'forcemerge': fm, 'max_num_segments': mns}
81 # Then pass it as **kwargs
82 assert u.build_ilm_policy(tgrp, **kwargs) == {'phases': phases}
83 # Our policy is easier to build at the last minute rather than constantly passing
84 # dict['phases']['tier']
86def test_build_ilm_policy_fail_repo():
87 with pytest.raises(ex.TestbedMisconfig):
88 u.build_ilm_policy(['hot', 'frozen'], repository=None)
90@pytest.fixture
91def fieldmatch():
92 def _fieldmatch(val: str, num: int):
93 return f'{val}{num}'
94 return _fieldmatch
96def test_doc_gen_matching(fieldmatch):
97 i = 0
98 for res in u.doc_gen(count=3, start_at=0, match=True):
99 doc = DotMap(res)
100 tests = [(doc.message, 'message'), (doc.nested.key, 'nested'), (doc.deep.l1.l2.l3, 'deep')]
101 for test in tests:
102 dm, val = test
103 assert dm == fieldmatch(val, i)
104 i += 1