/
common.py
213 lines (153 loc) · 5.96 KB
/
common.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Common base test classes and helper methods."""
from typing import Any
from typing import Dict
import hypothesis.strategies as st
import pytest
from attr import attrib
from attr import attrs
from flyingcircus.core import ATTRSCONFIG
from flyingcircus.core import AWSObject
from flyingcircus.core import Resource
from flyingcircus.core import ResourceProperties
from flyingcircus.core import create_object_converter
class CommonAWSObjectTests:
"""Shared tests to be applied to all AWSObject implementations."""
def test_init_should_only_have_named_parameters_in_spec(self):
# TODO we don't want to simply do **kwargs plucking. we want named params for the benefit of autocomplete
assert False
def test_get_logical_name_is_not_implemented_or_string(self):
assert False
@attrs(**ATTRSCONFIG)
class ZeroAttributeObject(AWSObject):
"""Test object with no attributes.
This is the same as using AWSObject directly, but naming it explicitly
makes the test's intention clearer.
"""
pass
@attrs(**ATTRSCONFIG)
class SingleAttributeObject(AWSObject):
"""Test object with 1 attribute"""
one = attrib(default=None)
@attrs(**ATTRSCONFIG)
class DualAttributeObject(AWSObject):
"""Test object with 2 attributes"""
one = attrib(default=None)
two = attrib(default=None)
@attrs(**ATTRSCONFIG)
class MixedAttributeObject(AWSObject):
"""Test object with AWS attributes and internal attributes"""
one = attrib(default=None)
two = attrib(default=None)
_a = attrib(default=None)
_b = attrib(default=None)
@attrs(**ATTRSCONFIG)
class NestedAttributeObject(AWSObject):
top: SingleAttributeObject = attrib(
factory=SingleAttributeObject,
converter=create_object_converter(SingleAttributeObject),
)
@attrs(**ATTRSCONFIG)
class InheritedAttributeObject(SingleAttributeObject):
two = attrib(default=None)
@st.composite
def aws_attribute_strategy(draw):
"""A strategy that produces an attribute for an AWS CFN object."""
return draw(
st.one_of(
st.text(),
st.integers(),
st.floats(),
st.booleans(),
st.dictionaries(st.text(), st.text()),
aws_object_strategy(),
)
)
@st.composite
def aws_object_strategy(draw):
"""A strategy that produces an AWS CFN object."""
attributes = draw(st.sets(aws_logical_name_strategy()))
@attrs(these={name: attrib(default=None) for name in attributes}, **ATTRSCONFIG)
class HypothesisedAWSObject(AWSObject):
pass
return draw(
st.builds(
HypothesisedAWSObject,
**{name: aws_attribute_strategy() for name in attributes},
)
)
@st.composite
def aws_logical_name_strategy(draw):
"""A strategy that produces a valid logical name for an AWS Stack object"""
return draw(
st.builds(
lambda a, b: a + b,
st.text("ABCDEFGHIJKLMNOPQRSTUVWXYZ", min_size=1, max_size=1),
st.text("abcdefghijklmnnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"),
)
)
@st.composite
def aws_deletion_policy_strategy(draw, include_none=True):
policy_values = ["Delete", "Retain", "Snapshot"]
if include_none:
policy_values.append(None)
return draw(st.sampled_from(policy_values))
@attrs(**ATTRSCONFIG)
class SimpleResourceProperties(ResourceProperties):
props = attrib(default=None)
kudos = attrib(default=None)
@attrs(**ATTRSCONFIG)
class SimpleResource(Resource):
"""A minimal resource."""
RESOURCE_TYPE = "NameSpace::Service::SimpleResource"
Properties: SimpleResourceProperties = attrib(factory=SimpleResourceProperties)
@attrs(**ATTRSCONFIG)
class FullResourceProperties(ResourceProperties):
props = attrib(default=None)
kudos = attrib(default=None)
@attrs(**ATTRSCONFIG)
class FullResource(Resource):
"""A basic resource with all Resource attributes defined."""
RESOURCE_TYPE = "NameSpace::Service::FullResource"
CreationPolicy: Dict[str, Any] = attrib(factory=dict)
Properties: FullResourceProperties = attrib(factory=FullResourceProperties)
UpdatePolicy: Dict[str, Any] = attrib(factory=dict)
@attrs(**ATTRSCONFIG)
class TaggableProperties(ResourceProperties):
SomeProperty = attrib(default=None)
AnotherProperty = attrib(default=None)
Tags = attrib(factory=list)
@attrs(**ATTRSCONFIG)
class TaggableResource(Resource):
RESOURCE_TYPE = "NameSpace::Service::TaggableResource"
Properties: TaggableProperties = attrib(factory=TaggableProperties)
LOREM_IPSUM = """\
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum."""
class BaseTaggingTest:
"""Base class for testing tagging on AWS Objects"""
# Helper Methods
# --------------
def verify_tag_doesnt_exist(self, res, key, value):
if not hasattr(res, "Properties") or not hasattr(res.Properties, "Tags"):
return
for tag in getattr(res.Properties, "Tags", []):
assert not (tag["Key"] == key and tag["Value"] == value)
def verify_tag_exists(self, res, key, value):
for tag in res.Properties.Tags:
if tag["Key"] == key and tag["Value"] == value:
return
assert False, "Tag not found in resource"
def parametrize_tagging_techniques():
"""Decorator that parametrizes tests across the two techniques of applying tags."""
return pytest.mark.parametrize(
argnames="apply_tags",
argvalues=[
lambda res, key, value: res.tag({key: value}),
lambda res, key, value: res.tag(**{key: value}),
],
ids=["dict", "keywords"],
)