-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathconftest.py
199 lines (156 loc) · 5.07 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import copy
import os
import mock
import pytest
import requests
from packaging import version
from datadog_checks.base.utils.common import exclude_undefined_keys
from datadog_checks.dev import WaitFor, docker_run
from datadog_checks.elastic import ESCheck
from .common import CLUSTER_TAG, ELASTIC_CLUSTER_TAG, ELASTIC_FLAVOR, ELASTIC_VERSION, HERE, PASSWORD, URL, USER
CUSTOM_TAGS = ['foo:bar', 'baz']
INSTANCE = {
'url': URL,
'username': USER,
'password': PASSWORD,
'tags': CUSTOM_TAGS,
'tls_verify': False,
'custom_queries': [
{
'endpoint': '/_search',
'data_path': 'hits.total',
'columns': [
{'value_path': 'value', 'name': 'elasticsearch.custom.metric', 'type': 'gauge'},
{'value_path': 'relation', 'name': 'dynamic_tag', 'type': 'tag'},
],
},
],
}
BENCHMARK_INSTANCE = {
'url': URL,
'username': USER,
'password': PASSWORD,
'tags': CUSTOM_TAGS,
'tls_verify': False,
}
def ping_elastic():
"""
The PUT request we use to ping the server will create an index named `testindex`
as soon as ES is available. This is just one possible ping strategy but it's needed
as a fixture for tests that require that index to exist in order to pass.
"""
response = requests.put('{}/testindex'.format(URL), auth=(USER, PASSWORD), verify=False)
response.raise_for_status()
def create_slm():
if version.parse(ELASTIC_VERSION) < version.parse('7.4.0'):
return
create_backup_body = {"type": "fs", "settings": {"location": "data"}}
response = requests.put(
'{}/_snapshot/my_repository?pretty'.format(INSTANCE['url']),
json=create_backup_body,
auth=(INSTANCE['username'], INSTANCE['password']),
verify=False,
)
response.raise_for_status()
create_slm_body = {
"schedule": "0 30 1 * * ?",
"name": "<daily-snap-{now/d}>",
"repository": "my_repository",
"config": {"indices": ["data-*", "important"], "ignore_unavailable": False, "include_global_state": False},
"retention": {"expire_after": "30d", "min_count": 5, "max_count": 50},
}
response = requests.put(
'{}/_slm/policy/daily-snapshots?pretty'.format(INSTANCE['url']),
json=create_slm_body,
auth=(INSTANCE['username'], INSTANCE['password']),
verify=False,
)
response.raise_for_status()
def index_starts_with_dot():
create_dot_testindex = requests.put('{}/.testindex'.format(URL), auth=(USER, PASSWORD), verify=False)
create_dot_testindex.raise_for_status()
@pytest.fixture(scope='session')
def dd_environment():
# opensearch doesn't play well with xpack env vars
compose_file = '{}-docker-compose.yaml'.format(ELASTIC_FLAVOR)
compose_file = os.path.join(HERE, 'compose', compose_file)
with docker_run(
compose_file=compose_file,
conditions=[
WaitFor(ping_elastic, attempts=100),
WaitFor(index_starts_with_dot, attempts=100),
WaitFor(create_slm, attempts=5),
],
attempts=2,
attempts_wait=10,
):
yield INSTANCE
@pytest.fixture
def elastic_check():
return ESCheck('elastic', {}, instances=[INSTANCE])
@pytest.fixture
def instance():
return copy.deepcopy(INSTANCE)
@pytest.fixture
def benchmark_elastic_check():
return ESCheck('elastic', {}, instances=[BENCHMARK_INSTANCE])
@pytest.fixture
def benchmark_instance():
return copy.deepcopy(BENCHMARK_INSTANCE)
@pytest.fixture
def instance_normalize_hostname():
return {
'url': URL,
'username': USER,
'password': PASSWORD,
'tags': CUSTOM_TAGS,
'node_name_as_host': True,
'tls_verify': False,
}
@pytest.fixture
def version_metadata():
if '-' in ELASTIC_VERSION:
base, release = ELASTIC_VERSION.split('-')
parts = base.split('.')
else:
release = None
parts = ELASTIC_VERSION.split('.')
major = parts[0]
minor = parts[1] if len(parts) > 1 else mock.ANY
patch = parts[2] if len(parts) > 2 else mock.ANY
return exclude_undefined_keys(
{
'version.scheme': 'semver',
'version.major': major,
'version.minor': minor,
'version.patch': patch,
'version.raw': mock.ANY,
'version.release': release,
}
)
def _cluster_tags():
tags = ['url:{}'.format(URL)] + ELASTIC_CLUSTER_TAG + CLUSTER_TAG
tags.extend(CUSTOM_TAGS)
return tags
@pytest.fixture
def new_cluster_tags():
tags = ['url:{}'.format(URL)] + ELASTIC_CLUSTER_TAG
tags.extend(CUSTOM_TAGS)
return tags
@pytest.fixture
def cluster_tags():
return _cluster_tags()
@pytest.fixture
def node_tags():
tags = _cluster_tags()
tags.append('node_name:test-node')
return tags
@pytest.fixture
def slm_tags():
tags = _cluster_tags()
tags.append('policy:daily-snapshots')
tags.append('repository:my_repository')
return tags