Skip to content

Commit 6c8330a

Browse files
eredi93Edmund Dipple
authored andcommitted
new list methods (elmundio87#16)
* new list check methods
1 parent c5ad743 commit 6c8330a

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,12 @@ Will raise an AssertionError if the value of the property equals `unexpected_val
167167
### TerraformPropertyList.should_match_regex(regex)
168168

169169
Will raise an AssertionError if the value of the property does not match the value of `regex`
170-
170+
171+
### TerraformPropertyList.list_should_contain([value])
172+
173+
Will raise an AssertionError if the list value does not contain any of the `[value]`
174+
175+
### TerraformPropertyList.list_should_not_contain([value])
176+
177+
Will raise an AssertionError if the list value does contain any of the `[value]`
178+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "datadog_monitor" "foo" {
2+
tags = ["baz:biz"]
3+
}
4+
5+
resource "datadog_monitor" "bar" {
6+
tags = ["baz:biz", "foo:bar"]
7+
}

terraform_validate/functional_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ def test_boolean_equal(self):
315315
for value in values:
316316
validator.resources("aws_db_instance").property("storage_encrypted{0}".format(i)).should_equal(value)
317317

318+
def test_list_should_contain(self):
319+
validator = t.Validator(os.path.join(self.path, "fixtures/list_variable"))
320+
validator.resources("datadog_monitor").property("tags").list_should_contain(['baz:biz'])
321+
expected_error = self.error_list_format([
322+
"[datadog_monitor.bar.tags] '['baz:biz', 'foo:bar']' should contain '['too:biz']'.",
323+
"[datadog_monitor.foo.tags] '['baz:biz']' should contain '['too:biz']'."
324+
])
325+
with self.assertRaisesRegexp(AssertionError, expected_error):
326+
validator.resources("datadog_monitor").property("tags").list_should_contain('too:biz')
327+
328+
def test_list_should_not_contain(self):
329+
validator = t.Validator(os.path.join(self.path, "fixtures/list_variable"))
330+
validator.resources("datadog_monitor").property("tags").list_should_not_contain(['foo:baz'])
331+
validator.resources("datadog_monitor").property("tags").list_should_not_contain('foo:baz')
332+
expected_error = self.error_list_format("[datadog_monitor.bar.tags] '['baz:biz', 'foo:bar']' should not contain '['foo:bar']'.")
333+
with self.assertRaisesRegexp(AssertionError, expected_error):
334+
validator.resources("datadog_monitor").property("tags").list_should_not_contain('foo:bar')
318335

319336
def test_encryption_scenario(self):
320337
validator = t.Validator(os.path.join(self.path, "fixtures/enforce_encrypted"))

terraform_validate/terraform_validate.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,57 @@ def should_not_equal(self,expected_value):
133133
if len(errors) > 0:
134134
raise AssertionError("\n".join(sorted(errors)))
135135

136+
def list_should_contain(self,values_list):
137+
errors = []
138+
139+
if type(values_list) is not list:
140+
values_list = [values_list]
141+
142+
for property in self.properties:
143+
144+
actual_property_value = self.validator.substitute_variable_values_in_string(property.property_value)
145+
values_missing = []
146+
for value in values_list:
147+
if value not in actual_property_value :
148+
values_missing.append(value)
149+
150+
if len(values_missing) != 0:
151+
if type(actual_property_value) is list:
152+
actual_property_value = [str(x) for x in actual_property_value] # fix 2.6/7
153+
errors.append("[{0}.{1}.{2}] '{3}' should contain '{4}'.".format(property.resource_type,
154+
property.resource_name,
155+
property.property_name,
156+
actual_property_value,
157+
values_missing))
158+
if len(errors) > 0:
159+
raise AssertionError("\n".join(sorted(errors)))
160+
161+
def list_should_not_contain(self,values_list):
162+
errors = []
163+
164+
if type(values_list) is not list:
165+
values_list = [values_list]
166+
167+
for property in self.properties:
168+
169+
actual_property_value = self.validator.substitute_variable_values_in_string(property.property_value)
170+
values_missing = []
171+
for value in values_list:
172+
if value in actual_property_value :
173+
values_missing.append(value)
174+
175+
if len(values_missing) != 0:
176+
if type(actual_property_value) is list:
177+
actual_property_value = [str(x) for x in actual_property_value] # fix 2.6/7
178+
errors.append("[{0}.{1}.{2}] '{3}' should not contain '{4}'.".format(property.resource_type,
179+
property.resource_name,
180+
property.property_name,
181+
actual_property_value,
182+
values_missing))
183+
if len(errors) > 0:
184+
raise AssertionError("\n".join(sorted(errors)))
185+
186+
136187
def should_have_properties(self, properties_list):
137188
errors = []
138189

0 commit comments

Comments
 (0)