Repository navigation
Expand file tree
/
Copy pathtest_cur.py
More file actions
353 lines (250 loc) · 11.5 KB
/
Copy pathtest_cur.py
File metadata and controls
353 lines (250 loc) · 11.5 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import uuid
import pytest
from botocore.exceptions import ClientError, ParamValidationError
name_prefix = "cur-test"
test_tags = [
{ "Key": "kids", "Value": "zalkpz" },
{ "Key": "dates", "Value": "091203060606" },
]
def _report_definition(name: str) -> dict:
return {
"ReportName": name,
"TimeUnit": "DAILY",
"Format": "textORcsv",
"Compression": "GZIP",
"AdditionalSchemaElements": ["RESOURCES"],
"S3Bucket": "cur-test-bucket",
"S3Prefix": "reports",
"S3Region": "us-east-1",
"AdditionalArtifacts": [],
"RefreshClosedReports": True,
"ReportVersioning": "OVERWRITE_REPORT",
}
def _uid() -> str:
return uuid.uuid4().hex[:8]
# -- DeleteReportDefinition ----------------------------------------------------
def test_cur_delete_report_definition(cur):
name = f"{name_prefix}-{_uid()}"
# First create one and then we'll immediately delete it.
cur.put_report_definition(ReportDefinition=_report_definition(name))
deleted = cur.delete_report_definition(ReportName=name)
assert deleted["ResponseMetadata"]["HTTPStatusCode"] == 200
reports = cur.describe_report_definitions()["ReportDefinitions"]
assert all(r["ReportName"] != name for r in reports)
def test_cur_delete_report_definition_not_found(cur):
name = f"not-found-{_uid()}"
with pytest.raises(ClientError) as e:
cur.delete_report_definition(ReportName=name)
assert e.value.response["Error"]["Code"] =="ValidationException"
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
# -- DescribeReportDefinitions -------------------------------------------------
def test_cur_describe_report_definitions(cur):
max = 4
name = f"{name_prefix}-{_uid()}"
# Create {max} report definitions.
for i in range(0, max):
cur.put_report_definition(
ReportDefinition=_report_definition(f"{name}-{i:02d}")
)
# Retrieve all report definitions. This will include the {max} just created,
# plus, potentially, any leftovers in the MiniStack state.
resp = cur.describe_report_definitions()
list = resp["ReportDefinitions"]
# Verify that each of the test reports exists.
for i in range(0, max):
assert any(r["ReportName"] == f"{name}-{i:02d}" for r in list)
def test_cur_describe_report_definitions_empty(cur):
# Get all the report definitions and delete them so we're empty.
resp = cur.describe_report_definitions()
list = resp["ReportDefinitions"]
for definition in list:
cur.delete_report_definition(ReportName=definition["ReportName"])
# Get all the definitins again, which should be empty.
resp = cur.describe_report_definitions()
list = resp["ReportDefinitions"]
assert list == []
# -- ModifyReportDefinition ----------------------------------------------------
# base = _report_definition(f"{name_prefix}-{_uid()}")
# modified = {**base, "S3Prefix": "xxx"}
# assert modified["S3Prefix"] == "xxx"
# assert all(modified[k] == v for k, v in base.items() if k != "S3Prefix")
def test_cur_modify_report_definition(cur):
original_name = f"{name_prefix}-{_uid()}"
original = _report_definition(original_name)
# The second definition will have these items changed.
modified_name = f"{original_name}-modified"
modified = {
**original,
"ReportName": modified_name,
"Compression": "ZIP",
"S3Prefix": "new-prefix",
}
# Put both the original and modified definitions.
for r in [original, modified]:
cur.put_report_definition(ReportDefinition=r)
# Retrieve all the definitions and make sure the two that we just put
# actually exist.
resp = cur.describe_report_definitions()
created = [
r for r in resp["ReportDefinitions"]
if r["ReportName"].startswith(original_name)
]
assert len(created) == 2
assert {r["ReportName"] for r in created} == {original_name, modified_name}
# Make sure the only the three test attributes were changed. Retrieve the
# two definitions from the created list and then compare them attribute-by-
# attribute.
by_name = {r["ReportName"]: r for r in created}
created_original = by_name[original_name]
created_modified = by_name[modified_name]
changed_keys = {"ReportName", "Compression", "S3Prefix"}
# Confirm changed attributes match expected values.
assert created_modified["ReportName"] == modified_name
assert created_modified["Compression"] == "ZIP"
assert created_modified["S3Prefix"] == "new-prefix"
# Confirm every other attribute is unchanged.
for k, v in created_original.items():
if k in changed_keys:
continue
assert created_modified[k] == v
# -- PutReportDefinition -------------------------------------------------------
def test_cur_put_report_definition(cur):
name = f"{name_prefix}-{_uid()}"
resp = cur.put_report_definition(
ReportDefinition=_report_definition(name)
)
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
list = cur.describe_report_definitions()["ReportDefinitions"]
assert any(r["ReportName"] == name for r in list)
def test_cur_put_report_definition_duplicate_name(cur):
name = f"{name_prefix}-{_uid()}"
cur.put_report_definition(ReportDefinition=_report_definition(name))
with pytest.raises(ClientError) as e:
cur.put_report_definition(ReportDefinition=_report_definition(name))
assert e.value.response["Error"]["Code"] == "DuplicateReportNameException"
def test_cur_put_report_definition_missing_report_name(cur):
bad = _report_definition(f"{name_prefix}-{_uid()}")
# This is an invalid report definition, but it is not caught by Boto3 like
# the missing test below
bad["ReportName"] = " "
with pytest.raises(ClientError) as e:
cur.put_report_definition(ReportDefinition=bad)
assert e.value.response["Error"]["Code"] == "ValidationException"
def test_cur_put_report_definition_missing_definition_body(cur):
# Note that we're checking ParamValidationError as opposed to the more
# typical ClientError. Boto3 is throwing this before the MiniStack client is
# hit.
with pytest.raises(ParamValidationError) as e:
cur.put_report_definition(ReportDefinition={})
assert "Missing required parameter in ReportDefinition" in str(e.value)
# -- TagResource ---------------------------------------------------------------
def test_cur_tag_resource(cur):
name = f"{name_prefix}-{_uid()}"
cur.put_report_definition(ReportDefinition=_report_definition(name))
resp = cur.tag_resource(
ReportName=name,
Tags=test_tags,
)
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
tags = cur.list_tags_for_resource(ReportName=name)["Tags"]
assert len(tags) == 2
assert all(tag in tags for tag in test_tags)
def test_cur_tag_resource_not_found(cur):
name = f"not-found-{_uid()}"
with pytest.raises(ClientError) as e:
cur.tag_resource(
ReportName=name,
Tags=[{"Key": "not", "Value": "found"}],
)
assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
def test_cur_tag_resource_overwrites(cur):
name = f"{name_prefix}-{_uid()}"
cur.put_report_definition(ReportDefinition=_report_definition(name))
# Add initial tags.
cur.tag_resource(
ReportName=name,
Tags=test_tags,
)
# Overwrite one tag, add another.
cur.tag_resource(
ReportName=name,
Tags=[
{"Key": "kids", "Value": "kpzzal"},
{"Key": "size", "Value": "6"},
],
)
tags = cur.list_tags_for_resource(ReportName=name)["Tags"]
assert {"Key": "kids", "Value": "kpzzal"} in tags
assert {"Key": "dates", "Value": "091203060606"} in tags
assert {"Key": "size", "Value": "6"} in tags
# The original key/value should not exist.
assert {"Key": "kids", "Value": "zalkpz"} not in tags
# -- UntagResource -------------------------------------------------------------
def test_cur_untag_resource(cur):
name = f"{name_prefix}-{_uid()}"
cur.put_report_definition(ReportDefinition=_report_definition(name))
cur.tag_resource(ReportName=name, Tags=test_tags)
resp = cur.untag_resource(ReportName=name, TagKeys=["kids"])
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
tags = cur.list_tags_for_resource(ReportName=name)["Tags"]
assert {"Key": "kids", "Value": "zalkpz"} not in tags
assert {"Key": "dates", "Value": "091203060606"} in tags
def test_cur_untag_resource_not_found(cur):
name = f"not-found-{_uid()}"
with pytest.raises(ClientError) as e:
cur.untag_resource(ReportName=name, TagKeys=["kids"])
assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
def test_cur_untag_resource_nonexistent_keys(cur):
name = f"{name_prefix}-{_uid()}"
cur.put_report_definition(ReportDefinition=_report_definition(name))
cur.tag_resource(ReportName=name, Tags=test_tags)
# Untag keys that don't exist - should succeed gracefully.
resp = cur.untag_resource(ReportName=name, TagKeys=["nonexistent", "also-no"])
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
# Original tags should still be there.
tags = cur.list_tags_for_resource(ReportName=name)["Tags"]
assert len(tags) == 2
assert all(tag in tags for tag in test_tags)
# -- Persistence ----------------------------------------------------------
#
# Regression for 1.3.36: the CUR module declared `get_state()` and
# `restore_state()` but the load_state() call at import time was missing,
# so MS wrote state on shutdown but never read it back on warm-boot.
def test_cur_state_round_trips_through_get_and_restore():
from ministack.services import cur
cur.reset()
cur._report_definitions["r1"] = {
"ReportName": "r1",
"TimeUnit": "HOURLY",
"Format": "Parquet",
"S3Bucket": "billing",
}
cur._report_tags["r1"] = {"team": "finops"}
snapshot = cur.get_state()
cur.reset()
assert "r1" not in cur._report_definitions
cur.load_persisted_state(snapshot)
assert cur._report_definitions["r1"]["S3Bucket"] == "billing"
assert cur._report_tags["r1"] == {"team": "finops"}
def test_cur_central_restore_recovers_reports_and_tags(monkeypatch, tmp_path):
"""CUR reports survive a disk round trip through the gateway loader."""
import ministack.app as app
from ministack.core import persistence
from ministack.core.responses import AccountScopedDict
from ministack.services import cur
monkeypatch.setattr(cur, "_report_definitions", AccountScopedDict())
monkeypatch.setattr(cur, "_report_tags", AccountScopedDict())
monkeypatch.setattr(persistence, "PERSIST_STATE", True)
monkeypatch.setattr(persistence, "STATE_DIR", str(tmp_path))
monkeypatch.setattr(app, "_state_map", {"cur": "cur"})
monkeypatch.setattr(app, "_loaded_modules", {})
report = _report_definition("restored-report")
cur._report_definitions[report["ReportName"]] = report
cur._report_tags[report["ReportName"]] = {"team": "finops"}
persistence.save_all({"cur": app._build_persistence_save_dict()["cur"]})
cur.reset()
app._load_persisted_state()
assert cur._report_definitions[report["ReportName"]] == report
assert cur._report_tags[report["ReportName"]] == {"team": "finops"}