Skip to content

Commit b145a44

Browse files
author
Edmund Dipple
committed
Remove deprecated methods (elmundio87#17)
* Removed deprecated methods * Remove unused methods * Improve test coverage of required_properties methods * Improve test coverage of disable_variable_expansion * Added test for multiline regex
1 parent c8abeee commit b145a44

File tree

4 files changed

+73
-351
lines changed

4 files changed

+73
-351
lines changed

terraform_validate/fixtures/resource/1.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ resource "aws_instance" "bar" {
1010
value = 1
1111
value2 = 2
1212

13+
propertylist {
14+
value = 2
15+
}
16+
1317
}
1418

1519
resource "aws_elb" "buzz" {

terraform_validate/functional_test.py

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def error_list_format(self,error_list):
2121

2222
def test_resource(self):
2323
validator = t.Validator(os.path.join(self.path,"fixtures/resource"))
24-
validator.assert_resource_property_value_equals('aws_instance', 'value', 1)
2524
validator.resources('aws_instance').property('value').should_equal(1)
2625
expected_error = self.error_list_format([
2726
"[aws_instance.bar.value] should be '2'. Is: '1'",
@@ -32,15 +31,13 @@ def test_resource(self):
3231

3332
def test_nested_resource(self):
3433
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
35-
validator.assert_nested_resource_property_value_equals('aws_instance','nested_resource','value',1)
3634
validator.resources('aws_instance').property('nested_resource').property('value').should_equal(1)
3735
expected_error = self.error_list_format("[aws_instance.foo.nested_resource.value] should be '2'. Is: '1'")
3836
with self.assertRaisesRegexp(AssertionError, expected_error):
3937
validator.resources('aws_instance').property('nested_resource').property('value').should_equal(2)
4038

4139
def test_resource_not_equals(self):
4240
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
43-
validator.assert_resource_property_value_not_equals('aws_instance', 'value', 0)
4441
validator.resources('aws_instance').property('value').should_not_equal(0)
4542
expected_error = self.error_list_format([
4643
"[aws_instance.bar.value] should not be '1'. Is: '1'",
@@ -51,16 +48,14 @@ def test_resource_not_equals(self):
5148

5249
def test_nested_resource_not_equals(self):
5350
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
54-
validator.assert_nested_resource_property_value_not_equals('aws_instance', 'nested_resource', 'value', 0)
5551
validator.resources('aws_instance').property('nested_resource').property('value').should_not_equal(0)
5652
expected_error = self.error_list_format("[aws_instance.foo.nested_resource.value] should not be '1'. Is: '1'")
5753
with self.assertRaisesRegexp(AssertionError,expected_error):
5854
validator.resources('aws_instance').property('nested_resource').property('value').should_not_equal(1)
5955

60-
def test_resource_required_properties(self):
56+
def test_resource_required_properties_with_list_input(self):
6157
required_properties = ['value', 'value2']
6258
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
63-
validator.assert_resource_has_properties('aws_instance', required_properties)
6459
validator.resources('aws_instance').should_have_properties(required_properties)
6560
required_properties = ['value', 'value2', 'abc123','def456']
6661
expected_error = self.error_list_format([
@@ -72,7 +67,12 @@ def test_resource_required_properties(self):
7267
with self.assertRaisesRegexp(AssertionError, expected_error):
7368
validator.resources('aws_instance').should_have_properties(required_properties)
7469

75-
def test_resource_excluded_properties(self):
70+
def test_resource_required_properties_with_string_input(self):
71+
required_property = 'value'
72+
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
73+
validator.resources('aws_instance').should_have_properties(required_property)
74+
75+
def test_resource_excluded_properties_with_list_input(self):
7676
excluded_properties = ['value', 'value2']
7777
non_excluded_properties = ['value3','value4']
7878
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
@@ -86,10 +86,21 @@ def test_resource_excluded_properties(self):
8686
with self.assertRaisesRegexp(AssertionError, expected_error):
8787
validator.resources('aws_instance').should_not_have_properties(excluded_properties)
8888

89-
def test_nested_resource_required_properties(self):
89+
def test_resource_excluded_properties_with_string_input(self):
90+
excluded_property = 'value'
91+
non_excluded_property = 'value3'
92+
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
93+
validator.resources('aws_instance').should_not_have_properties(non_excluded_property)
94+
expected_error = self.error_list_format([
95+
"[aws_instance.bar] should not have property: 'value'",
96+
"[aws_instance.foo] should not have property: 'value'"
97+
])
98+
with self.assertRaisesRegexp(AssertionError, expected_error):
99+
validator.resources('aws_instance').should_not_have_properties(excluded_property)
100+
101+
def test_nested_resource_required_properties_with_list_input(self):
90102
required_properties = ['value','value2']
91103
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
92-
validator.assert_nested_resource_has_properties('aws_instance','nested_resource',required_properties)
93104
validator.resources('aws_instance').property('nested_resource').should_have_properties(required_properties)
94105
required_properties = ['value', 'value2', 'abc123', 'def456']
95106
expected_error = self.error_list_format([
@@ -99,7 +110,19 @@ def test_nested_resource_required_properties(self):
99110
with self.assertRaisesRegexp(AssertionError, expected_error):
100111
validator.resources('aws_instance').property('nested_resource').should_have_properties(required_properties)
101112

102-
def test_nested_resource_excluded_properties(self):
113+
def test_nested_resource_required_properties_with_string_input(self):
114+
required_property = 'value'
115+
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
116+
validator.resources('aws_instance').property('nested_resource').should_have_properties(required_property)
117+
required_property = 'def456'
118+
expected_error = self.error_list_format([
119+
"[aws_instance.foo.nested_resource] should have property: 'def456'"
120+
])
121+
with self.assertRaisesRegexp(AssertionError, expected_error):
122+
validator.resources('aws_instance').property('nested_resource').should_have_properties(
123+
required_property)
124+
125+
def test_nested_resource_excluded_properties_with_list_input(self):
103126
excluded_properties = ['value', 'value2']
104127
non_excluded_properties = ['value3','value4']
105128
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
@@ -111,9 +134,21 @@ def test_nested_resource_excluded_properties(self):
111134
with self.assertRaisesRegexp(AssertionError, expected_error):
112135
validator.resources('aws_instance').property('nested_resource').should_not_have_properties(excluded_properties)
113136

137+
def test_nested_resource_excluded_properties_with_string_input(self):
138+
excluded_property = 'value'
139+
non_excluded_property = 'value3'
140+
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
141+
validator.resources('aws_instance').property('nested_resource').should_not_have_properties(
142+
non_excluded_property)
143+
expected_error = self.error_list_format([
144+
"[aws_instance.foo.nested_resource] should not have property: 'value'"
145+
])
146+
with self.assertRaisesRegexp(AssertionError, expected_error):
147+
validator.resources('aws_instance').property('nested_resource').should_not_have_properties(
148+
excluded_property)
149+
114150
def test_resource_property_value_matches_regex(self):
115151
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
116-
validator.assert_resource_property_value_matches_regex('aws_instance',"value",'[0-9]')
117152
validator.resources('aws_instance').property('value').should_match_regex('[0-9]')
118153
expected_error = self.error_list_format([
119154
"[aws_instance.bar.value] should match regex '[a-z]'",
@@ -124,7 +159,6 @@ def test_resource_property_value_matches_regex(self):
124159

125160
def test_nested_resource_property_value_matches_regex(self):
126161
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
127-
validator.assert_nested_resource_property_value_matches_regex('aws_instance','nested_resource',"value",'[0-9]')
128162
validator.resources('aws_instance').property('nested_resource').property('value').should_match_regex('[0-9]')
129163
expected_error = self.error_list_format("[aws_instance.foo.nested_resource.value] should match regex '[a-z]'")
130164
with self.assertRaisesRegexp(AssertionError, expected_error):
@@ -133,31 +167,28 @@ def test_nested_resource_property_value_matches_regex(self):
133167
def test_variable_substitution(self):
134168
validator = t.Validator(os.path.join(self.path, "fixtures/variable_substitution"))
135169
validator.enable_variable_expansion()
136-
validator.assert_resource_property_value_equals('aws_instance','value',1)
137170
validator.resources('aws_instance').property('value').should_equal(1)
138171
expected_error = self.error_list_format("[aws_instance.foo.value] should be '2'. Is: '1'")
139172
with self.assertRaisesRegexp(AssertionError,expected_error):
140173
validator.resources('aws_instance').property('value').should_equal(2)
174+
validator.disable_variable_expansion()
175+
validator.resources('aws_instance').property('value').should_equal('${var.test_variable}')
176+
141177

142178
def test_missing_variable_substitution(self):
143179
validator = t.Validator(os.path.join(self.path, "fixtures/missing_variable"))
144180
validator.enable_variable_expansion()
145-
self.assertRaises(t.TerraformVariableException, validator.assert_resource_property_value_equals, 'aws_instance',
146-
'value', 1)
147-
148181
expected_error = self.error_list_format("There is no Terraform variable 'missing'")
149182
with self.assertRaisesRegexp(t.TerraformVariableException, expected_error):
150183
validator.resources('aws_instance').property('value').should_equal(1)
151184

152185
# def test_missing_required_nested_resource_fails(self):
153186
# validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
154-
# self.assertRaises(AssertionError,validator.assert_nested_resource_property_value_equals,'aws_instance', 'tags', 'encrypted', 1)
155187
# self.assertRaises(AssertionError,validator.resources('aws_instance').property('tags').property('encrypted').should_equal(1))
156188

157189
def test_properties_on_nonexistant_resource_type(self):
158190
required_properties = ['value', 'value2']
159191
validator = t.Validator(os.path.join(self.path, "fixtures/missing_variable"))
160-
validator.assert_nested_resource_has_properties('aws_rds_instance', 'nested_resource', required_properties)
161192
validator.resources('aws_rds_instance').property('nested_resource').should_have_properties(required_properties)
162193

163194
def test_searching_for_property_on_nonexistant_nested_resource(self):
@@ -174,25 +205,20 @@ def test_searching_for_property_on_nonexistant_nested_resource(self):
174205

175206
def test_searching_for_property_value_using_regex(self):
176207
validator = t.Validator(os.path.join(self.path, "fixtures/regex_variables"))
177-
validator.assert_resource_regexproperty_value_equals('aws_instance', '^CPM_Service_[A-Za-z]+$', 1)
178208
validator.resources('aws_instance').find_property('^CPM_Service_[A-Za-z]+$').should_equal(1)
179-
180209
expected_error = self.error_list_format("[aws_instance.foo.CPM_Service_wibble] should be '2'. Is: '1'")
181210
with self.assertRaisesRegexp(AssertionError,expected_error):
182211
validator.resources('aws_instance').find_property('^CPM_Service_[A-Za-z]+$').should_equal(2)
183212

184213
def test_searching_for_nested_property_value_using_regex(self):
185214
validator = t.Validator(os.path.join(self.path, "fixtures/regex_nested_variables"))
186-
validator.assert_nested_resource_regexproperty_value_equals('aws_instance', 'tags', '^CPM_Service_[A-Za-z]+$', 1)
187215
validator.resources('aws_instance').property('tags').find_property('^CPM_Service_[A-Za-z]+$').should_equal(1)
188-
189216
expected_error = self.error_list_format("[aws_instance.foo.tags.CPM_Service_wibble] should be '2'. Is: '1'")
190217
with self.assertRaisesRegexp(AssertionError,expected_error):
191218
validator.resources('aws_instance').property('tags').find_property('^CPM_Service_[A-Za-z]+$').should_equal(2)
192219

193220
def test_resource_type_list(self):
194221
validator = t.Validator(os.path.join(self.path, "fixtures/resource"))
195-
validator.assert_resource_property_value_equals(['aws_instance','aws_elb'], 'value', 1)
196222
validator.resources(['aws_instance','aws_elb']).property('value').should_equal(1)
197223
expected_error = self.error_list_format([
198224
"[aws_elb.buzz.value] should be '2'. Is: '1'",
@@ -205,9 +231,7 @@ def test_resource_type_list(self):
205231

206232
def test_nested_resource_type_list(self):
207233
validator = t.Validator(os.path.join(self.path, "fixtures/nested_resource"))
208-
validator.assert_nested_resource_property_value_equals(['aws_instance','aws_elb'],'tags', 'value', 1)
209234
validator.resources(['aws_instance', 'aws_elb']).property('tags').property('value').should_equal(1)
210-
211235
expected_error = self.error_list_format([
212236
"[aws_elb.foo.tags.value] should be '2'. Is: '1'",
213237
"[aws_instance.foo.tags.value] should be '2'. Is: '1'"
@@ -221,90 +245,67 @@ def test_invalid_terraform_syntax(self):
221245
def test_multiple_variable_substitutions(self):
222246
validator = t.Validator(os.path.join(self.path, "fixtures/multiple_variables"))
223247
validator.enable_variable_expansion()
224-
validator.assert_resource_property_value_equals('aws_instance','value',12)
225248
validator.resources('aws_instance').property('value').should_equal(12)
226-
227249
expected_error = self.error_list_format("[aws_instance.foo.value] should be '21'. Is: '12'")
228250
with self.assertRaisesRegexp(AssertionError,expected_error):
229251
validator.resources('aws_instance').property('value').should_equal(21)
230252

231253
def test_nested_multiple_variable_substitutions(self):
232254
validator = t.Validator(os.path.join(self.path, "fixtures/multiple_variables"))
233255
validator.enable_variable_expansion()
234-
validator.assert_nested_resource_property_value_equals('aws_instance','value_block','value',21)
235256
validator.resources('aws_instance').property('value_block').property('value').should_equal(21)
236-
237257
expected_error = self.error_list_format("[aws_instance.foo.value_block.value] should be '12'. Is: '21'")
238258
with self.assertRaisesRegexp(AssertionError,expected_error):
239259
validator.resources('aws_instance').property('value_block').property('value').should_equal(12)
240260

241261
def test_variable_expansion(self):
242262
validator = t.Validator(os.path.join(self.path, "fixtures/variable_expansion"))
243-
validator.assert_resource_property_value_equals('aws_instance','value','${var.bar}')
244263
validator.resources('aws_instance').property('value').should_equal('${var.bar}')
245-
246264
expected_error = self.error_list_format("[aws_instance.foo.value] should be '${bar.var}'. Is: '${var.bar}'")
247265
with self.assertRaisesRegexp(AssertionError, expected_error):
248266
validator.resources('aws_instance').property('value').should_equal('${bar.var}')
249267

250268
def test_resource_name_matches_regex(self):
251269
validator = t.Validator(os.path.join(self.path, "fixtures/resource_name"))
252-
validator.assert_resource_name_matches_regex('aws_foo', '^[a-z0-9_]*$')
253-
self.assertRaises(AssertionError,validator.assert_resource_name_matches_regex,'aws_instance','^[a-z0-9_]*$')
254270

255271
validator.resources('aws_foo').name_should_match_regex('^[a-z0-9_]*$')
256-
257272
expected_error = self.error_list_format("[aws_instance.TEST_RESOURCE] name should match regex '^[a-z0-9_]*$'")
258273
with self.assertRaisesRegexp(AssertionError,expected_error):
259274
validator.resources('aws_instance').name_should_match_regex('^[a-z0-9_]*$')
260275

261276
def test_variable_has_default_value(self):
262277
validator = t.Validator(os.path.join(self.path, "fixtures/default_variable"))
263-
validator.assert_variable_default_value_exists('foo')
264-
self.assertRaises(AssertionError, validator.assert_variable_default_value_exists, 'bar')
265-
266278
expected_error = self.error_list_format("Variable 'bar' should have a default value")
267279
with self.assertRaisesRegexp(AssertionError, expected_error):
268280
validator.variable('bar').default_value_exists()
269281

270282
def test_variable_default_value_equals(self):
271283
validator = t.Validator(os.path.join(self.path, "fixtures/default_variable"))
272-
validator.assert_variable_default_value_equals('foo',1)
273-
self.assertRaises(AssertionError, validator.assert_variable_default_value_equals, 'foo', 2)
274-
validator.assert_variable_default_value_equals('bar', None)
275-
276284
expected_error = self.error_list_format("Variable 'bar' should have a default value of 2. Is: None")
277285
with self.assertRaisesRegexp(AssertionError, expected_error):
278286
validator.variable('bar').default_value_equals(2)
279287
validator.variable('bar').default_value_equals(None)
280288

281289
def test_variable_default_value_matches_regex(self):
282290
validator = t.Validator(os.path.join(self.path, "fixtures/default_variable"))
283-
validator.assert_variable_default_value_matches_regex('bizz', '^.*')
284-
self.assertRaises(AssertionError, validator.assert_variable_default_value_matches_regex, 'bizz', '^123')
285-
286291
expected_error = self.error_list_format("Variable 'bizz' should have a default value that matches regex '^123'. Is: abc")
287292
with self.assertRaisesRegexp(AssertionError, expected_error):
288293
validator.variable('bizz').default_value_matches_regex('^123')
289294

290295
def test_no_exceptions_raised_when_no_resources_present(self):
291296
validator = t.Validator(os.path.join(self.path, "fixtures/no_resources"))
292-
validator.assert_resource_property_value_equals('aws_instance', 'value', 1)
293297
validator.resources('aws_instance').property('value').should_equal(1)
294298

295299
def test_lowercase_formatting_in_variable_substitution(self):
296300
validator = t.Validator(os.path.join(self.path, "fixtures/lower_format_variable"))
297301
validator.enable_variable_expansion()
298-
validator.assert_resource_property_value_equals('aws_instance', 'value', "abc")
299-
validator.assert_resource_property_value_equals('aws_instance2', 'value', "abcDEF")
300302

301303
validator.resources('aws_instance').property('value').should_equal('abc')
302304
validator.resources('aws_instance2').property('value').should_equal('abcDEF')
303305

304306
def test_parsing_variable_with_unimplemented_interpolation_function(self):
305307
validator = t.Validator(os.path.join(self.path, "fixtures/unimplemented_interpolation"))
306308
validator.enable_variable_expansion()
307-
self.assertRaises(t.TerraformUnimplementedInterpolationException,validator.assert_resource_property_value_equals, 'aws_instance', 'value', "abc")
308309
self.assertRaises(t.TerraformUnimplementedInterpolationException, validator.resources('aws_instance').property('value').should_equal,'abc')
309310

310311
def test_boolean_equal(self):
@@ -379,8 +380,3 @@ def test_encryption_scenario(self):
379380
with self.assertRaisesRegexp(AssertionError,expected_error):
380381
validator.resources("aws_ebs_volume_invalid2").should_have_properties("encrypted")
381382
validator.resources("aws_ebs_volume_invalid2").property("encrypted")
382-
383-
384-
if __name__ == '__main__':
385-
suite = unittest.TestLoader().loadTestsFromTestCase(TestValidatorFunctional)
386-
unittest.TextTestRunner(verbosity=0).run(suite)

0 commit comments

Comments
 (0)