Coverage for tests/conftest.py: 91%
56 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-24 22:41 -0600
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-24 22:41 -0600
1"""Top-level conftest.py"""
2# pylint: disable=missing-function-docstring,redefined-outer-name
3from os import environ
4from datetime import datetime, timezone
5import random
6import string
7import pytest
8from elasticsearch8.exceptions import NotFoundError
9from es_client import Builder
10from es_client.helpers.logging import set_logging
11from es_testbed.defaults import NAMEMAPPER
13LOGLEVEL = 'DEBUG'
15@pytest.fixture(scope='session')
16def client():
17 """Return an Elasticsearch client"""
18 host = environ.get('TEST_ES_SERVER', 'http://127.0.0.1:9200')
19 file = environ.get('ES_CLIENT_FILE', None) # Path to es_client YAML config
20 if file: 20 ↛ 23line 20 didn't jump to line 23, because the condition on line 20 was never false
21 kwargs = {'configfile': file}
22 else:
23 kwargs = {'configdict': {'elasticsearch': {'client': {'hosts': host}}}}
24 set_logging({'loglevel': LOGLEVEL, 'blacklist': ['elastic_transport', 'urllib3']})
25 builder = Builder(**kwargs)
26 builder.connect()
27 return builder.client
29@pytest.fixture(scope='module')
30def cold():
31 """Return the prefix for cold indices"""
32 return 'restored-'
34@pytest.fixture(scope='module')
35def ymd():
36 return datetime.now(timezone.utc).strftime('%Y.%m.%d')
38@pytest.fixture(scope='module')
39def frozen():
40 """Return the prefix for frozen indices"""
41 return 'partial-'
43@pytest.fixture(scope='module')
44def namecore(prefix, uniq):
45 def _namecore(kind):
46 return f'{prefix}-{NAMEMAPPER[kind]}-{uniq}'
47 return _namecore
49@pytest.fixture(scope='module')
50def prefix():
51 """Return a random prefix"""
52 return randomstr(length=8, lowercase=True)
54def randomstr(length: int=16, lowercase: bool=False):
55 """Generate a random string"""
56 letters = string.ascii_uppercase
57 if lowercase: 57 ↛ 59line 57 didn't jump to line 59, because the condition on line 57 was never false
58 letters = string.ascii_lowercase
59 return str(''.join(random.choices(letters + string.digits, k=length)))
61@pytest.fixture(scope='module')
62def repo(client):
63 """Return the elasticsearch repository"""
64 name = environ.get('TEST_ES_REPO', 'found-snapshots') # Going with Cloud default
65 if not repo: 65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true
66 return False
67 try:
68 client.snapshot.get_repository(name=name)
69 except NotFoundError:
70 return False
71 return name # Return the repo name if it's online
73@pytest.fixture(scope='module')
74def uniq():
75 """Return a random uniq value"""
76 return randomstr(length=8, lowercase=True)