@@ -21,7 +21,6 @@ def error_list_format(self,error_list):
21
21
22
22
def test_resource (self ):
23
23
validator = t .Validator (os .path .join (self .path ,"fixtures/resource" ))
24
- validator .assert_resource_property_value_equals ('aws_instance' , 'value' , 1 )
25
24
validator .resources ('aws_instance' ).property ('value' ).should_equal (1 )
26
25
expected_error = self .error_list_format ([
27
26
"[aws_instance.bar.value] should be '2'. Is: '1'" ,
@@ -32,15 +31,13 @@ def test_resource(self):
32
31
33
32
def test_nested_resource (self ):
34
33
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 )
36
34
validator .resources ('aws_instance' ).property ('nested_resource' ).property ('value' ).should_equal (1 )
37
35
expected_error = self .error_list_format ("[aws_instance.foo.nested_resource.value] should be '2'. Is: '1'" )
38
36
with self .assertRaisesRegexp (AssertionError , expected_error ):
39
37
validator .resources ('aws_instance' ).property ('nested_resource' ).property ('value' ).should_equal (2 )
40
38
41
39
def test_resource_not_equals (self ):
42
40
validator = t .Validator (os .path .join (self .path , "fixtures/resource" ))
43
- validator .assert_resource_property_value_not_equals ('aws_instance' , 'value' , 0 )
44
41
validator .resources ('aws_instance' ).property ('value' ).should_not_equal (0 )
45
42
expected_error = self .error_list_format ([
46
43
"[aws_instance.bar.value] should not be '1'. Is: '1'" ,
@@ -51,16 +48,14 @@ def test_resource_not_equals(self):
51
48
52
49
def test_nested_resource_not_equals (self ):
53
50
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 )
55
51
validator .resources ('aws_instance' ).property ('nested_resource' ).property ('value' ).should_not_equal (0 )
56
52
expected_error = self .error_list_format ("[aws_instance.foo.nested_resource.value] should not be '1'. Is: '1'" )
57
53
with self .assertRaisesRegexp (AssertionError ,expected_error ):
58
54
validator .resources ('aws_instance' ).property ('nested_resource' ).property ('value' ).should_not_equal (1 )
59
55
60
- def test_resource_required_properties (self ):
56
+ def test_resource_required_properties_with_list_input (self ):
61
57
required_properties = ['value' , 'value2' ]
62
58
validator = t .Validator (os .path .join (self .path , "fixtures/resource" ))
63
- validator .assert_resource_has_properties ('aws_instance' , required_properties )
64
59
validator .resources ('aws_instance' ).should_have_properties (required_properties )
65
60
required_properties = ['value' , 'value2' , 'abc123' ,'def456' ]
66
61
expected_error = self .error_list_format ([
@@ -72,7 +67,12 @@ def test_resource_required_properties(self):
72
67
with self .assertRaisesRegexp (AssertionError , expected_error ):
73
68
validator .resources ('aws_instance' ).should_have_properties (required_properties )
74
69
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 ):
76
76
excluded_properties = ['value' , 'value2' ]
77
77
non_excluded_properties = ['value3' ,'value4' ]
78
78
validator = t .Validator (os .path .join (self .path , "fixtures/resource" ))
@@ -86,10 +86,21 @@ def test_resource_excluded_properties(self):
86
86
with self .assertRaisesRegexp (AssertionError , expected_error ):
87
87
validator .resources ('aws_instance' ).should_not_have_properties (excluded_properties )
88
88
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 ):
90
102
required_properties = ['value' ,'value2' ]
91
103
validator = t .Validator (os .path .join (self .path , "fixtures/nested_resource" ))
92
- validator .assert_nested_resource_has_properties ('aws_instance' ,'nested_resource' ,required_properties )
93
104
validator .resources ('aws_instance' ).property ('nested_resource' ).should_have_properties (required_properties )
94
105
required_properties = ['value' , 'value2' , 'abc123' , 'def456' ]
95
106
expected_error = self .error_list_format ([
@@ -99,7 +110,19 @@ def test_nested_resource_required_properties(self):
99
110
with self .assertRaisesRegexp (AssertionError , expected_error ):
100
111
validator .resources ('aws_instance' ).property ('nested_resource' ).should_have_properties (required_properties )
101
112
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 ):
103
126
excluded_properties = ['value' , 'value2' ]
104
127
non_excluded_properties = ['value3' ,'value4' ]
105
128
validator = t .Validator (os .path .join (self .path , "fixtures/nested_resource" ))
@@ -111,9 +134,21 @@ def test_nested_resource_excluded_properties(self):
111
134
with self .assertRaisesRegexp (AssertionError , expected_error ):
112
135
validator .resources ('aws_instance' ).property ('nested_resource' ).should_not_have_properties (excluded_properties )
113
136
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
+
114
150
def test_resource_property_value_matches_regex (self ):
115
151
validator = t .Validator (os .path .join (self .path , "fixtures/resource" ))
116
- validator .assert_resource_property_value_matches_regex ('aws_instance' ,"value" ,'[0-9]' )
117
152
validator .resources ('aws_instance' ).property ('value' ).should_match_regex ('[0-9]' )
118
153
expected_error = self .error_list_format ([
119
154
"[aws_instance.bar.value] should match regex '[a-z]'" ,
@@ -124,7 +159,6 @@ def test_resource_property_value_matches_regex(self):
124
159
125
160
def test_nested_resource_property_value_matches_regex (self ):
126
161
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]' )
128
162
validator .resources ('aws_instance' ).property ('nested_resource' ).property ('value' ).should_match_regex ('[0-9]' )
129
163
expected_error = self .error_list_format ("[aws_instance.foo.nested_resource.value] should match regex '[a-z]'" )
130
164
with self .assertRaisesRegexp (AssertionError , expected_error ):
@@ -133,31 +167,28 @@ def test_nested_resource_property_value_matches_regex(self):
133
167
def test_variable_substitution (self ):
134
168
validator = t .Validator (os .path .join (self .path , "fixtures/variable_substitution" ))
135
169
validator .enable_variable_expansion ()
136
- validator .assert_resource_property_value_equals ('aws_instance' ,'value' ,1 )
137
170
validator .resources ('aws_instance' ).property ('value' ).should_equal (1 )
138
171
expected_error = self .error_list_format ("[aws_instance.foo.value] should be '2'. Is: '1'" )
139
172
with self .assertRaisesRegexp (AssertionError ,expected_error ):
140
173
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
+
141
177
142
178
def test_missing_variable_substitution (self ):
143
179
validator = t .Validator (os .path .join (self .path , "fixtures/missing_variable" ))
144
180
validator .enable_variable_expansion ()
145
- self .assertRaises (t .TerraformVariableException , validator .assert_resource_property_value_equals , 'aws_instance' ,
146
- 'value' , 1 )
147
-
148
181
expected_error = self .error_list_format ("There is no Terraform variable 'missing'" )
149
182
with self .assertRaisesRegexp (t .TerraformVariableException , expected_error ):
150
183
validator .resources ('aws_instance' ).property ('value' ).should_equal (1 )
151
184
152
185
# def test_missing_required_nested_resource_fails(self):
153
186
# 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)
155
187
# self.assertRaises(AssertionError,validator.resources('aws_instance').property('tags').property('encrypted').should_equal(1))
156
188
157
189
def test_properties_on_nonexistant_resource_type (self ):
158
190
required_properties = ['value' , 'value2' ]
159
191
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 )
161
192
validator .resources ('aws_rds_instance' ).property ('nested_resource' ).should_have_properties (required_properties )
162
193
163
194
def test_searching_for_property_on_nonexistant_nested_resource (self ):
@@ -174,25 +205,20 @@ def test_searching_for_property_on_nonexistant_nested_resource(self):
174
205
175
206
def test_searching_for_property_value_using_regex (self ):
176
207
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 )
178
208
validator .resources ('aws_instance' ).find_property ('^CPM_Service_[A-Za-z]+$' ).should_equal (1 )
179
-
180
209
expected_error = self .error_list_format ("[aws_instance.foo.CPM_Service_wibble] should be '2'. Is: '1'" )
181
210
with self .assertRaisesRegexp (AssertionError ,expected_error ):
182
211
validator .resources ('aws_instance' ).find_property ('^CPM_Service_[A-Za-z]+$' ).should_equal (2 )
183
212
184
213
def test_searching_for_nested_property_value_using_regex (self ):
185
214
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 )
187
215
validator .resources ('aws_instance' ).property ('tags' ).find_property ('^CPM_Service_[A-Za-z]+$' ).should_equal (1 )
188
-
189
216
expected_error = self .error_list_format ("[aws_instance.foo.tags.CPM_Service_wibble] should be '2'. Is: '1'" )
190
217
with self .assertRaisesRegexp (AssertionError ,expected_error ):
191
218
validator .resources ('aws_instance' ).property ('tags' ).find_property ('^CPM_Service_[A-Za-z]+$' ).should_equal (2 )
192
219
193
220
def test_resource_type_list (self ):
194
221
validator = t .Validator (os .path .join (self .path , "fixtures/resource" ))
195
- validator .assert_resource_property_value_equals (['aws_instance' ,'aws_elb' ], 'value' , 1 )
196
222
validator .resources (['aws_instance' ,'aws_elb' ]).property ('value' ).should_equal (1 )
197
223
expected_error = self .error_list_format ([
198
224
"[aws_elb.buzz.value] should be '2'. Is: '1'" ,
@@ -205,9 +231,7 @@ def test_resource_type_list(self):
205
231
206
232
def test_nested_resource_type_list (self ):
207
233
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 )
209
234
validator .resources (['aws_instance' , 'aws_elb' ]).property ('tags' ).property ('value' ).should_equal (1 )
210
-
211
235
expected_error = self .error_list_format ([
212
236
"[aws_elb.foo.tags.value] should be '2'. Is: '1'" ,
213
237
"[aws_instance.foo.tags.value] should be '2'. Is: '1'"
@@ -221,90 +245,67 @@ def test_invalid_terraform_syntax(self):
221
245
def test_multiple_variable_substitutions (self ):
222
246
validator = t .Validator (os .path .join (self .path , "fixtures/multiple_variables" ))
223
247
validator .enable_variable_expansion ()
224
- validator .assert_resource_property_value_equals ('aws_instance' ,'value' ,12 )
225
248
validator .resources ('aws_instance' ).property ('value' ).should_equal (12 )
226
-
227
249
expected_error = self .error_list_format ("[aws_instance.foo.value] should be '21'. Is: '12'" )
228
250
with self .assertRaisesRegexp (AssertionError ,expected_error ):
229
251
validator .resources ('aws_instance' ).property ('value' ).should_equal (21 )
230
252
231
253
def test_nested_multiple_variable_substitutions (self ):
232
254
validator = t .Validator (os .path .join (self .path , "fixtures/multiple_variables" ))
233
255
validator .enable_variable_expansion ()
234
- validator .assert_nested_resource_property_value_equals ('aws_instance' ,'value_block' ,'value' ,21 )
235
256
validator .resources ('aws_instance' ).property ('value_block' ).property ('value' ).should_equal (21 )
236
-
237
257
expected_error = self .error_list_format ("[aws_instance.foo.value_block.value] should be '12'. Is: '21'" )
238
258
with self .assertRaisesRegexp (AssertionError ,expected_error ):
239
259
validator .resources ('aws_instance' ).property ('value_block' ).property ('value' ).should_equal (12 )
240
260
241
261
def test_variable_expansion (self ):
242
262
validator = t .Validator (os .path .join (self .path , "fixtures/variable_expansion" ))
243
- validator .assert_resource_property_value_equals ('aws_instance' ,'value' ,'${var.bar}' )
244
263
validator .resources ('aws_instance' ).property ('value' ).should_equal ('${var.bar}' )
245
-
246
264
expected_error = self .error_list_format ("[aws_instance.foo.value] should be '${bar.var}'. Is: '${var.bar}'" )
247
265
with self .assertRaisesRegexp (AssertionError , expected_error ):
248
266
validator .resources ('aws_instance' ).property ('value' ).should_equal ('${bar.var}' )
249
267
250
268
def test_resource_name_matches_regex (self ):
251
269
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_]*$' )
254
270
255
271
validator .resources ('aws_foo' ).name_should_match_regex ('^[a-z0-9_]*$' )
256
-
257
272
expected_error = self .error_list_format ("[aws_instance.TEST_RESOURCE] name should match regex '^[a-z0-9_]*$'" )
258
273
with self .assertRaisesRegexp (AssertionError ,expected_error ):
259
274
validator .resources ('aws_instance' ).name_should_match_regex ('^[a-z0-9_]*$' )
260
275
261
276
def test_variable_has_default_value (self ):
262
277
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
-
266
278
expected_error = self .error_list_format ("Variable 'bar' should have a default value" )
267
279
with self .assertRaisesRegexp (AssertionError , expected_error ):
268
280
validator .variable ('bar' ).default_value_exists ()
269
281
270
282
def test_variable_default_value_equals (self ):
271
283
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
-
276
284
expected_error = self .error_list_format ("Variable 'bar' should have a default value of 2. Is: None" )
277
285
with self .assertRaisesRegexp (AssertionError , expected_error ):
278
286
validator .variable ('bar' ).default_value_equals (2 )
279
287
validator .variable ('bar' ).default_value_equals (None )
280
288
281
289
def test_variable_default_value_matches_regex (self ):
282
290
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
-
286
291
expected_error = self .error_list_format ("Variable 'bizz' should have a default value that matches regex '^123'. Is: abc" )
287
292
with self .assertRaisesRegexp (AssertionError , expected_error ):
288
293
validator .variable ('bizz' ).default_value_matches_regex ('^123' )
289
294
290
295
def test_no_exceptions_raised_when_no_resources_present (self ):
291
296
validator = t .Validator (os .path .join (self .path , "fixtures/no_resources" ))
292
- validator .assert_resource_property_value_equals ('aws_instance' , 'value' , 1 )
293
297
validator .resources ('aws_instance' ).property ('value' ).should_equal (1 )
294
298
295
299
def test_lowercase_formatting_in_variable_substitution (self ):
296
300
validator = t .Validator (os .path .join (self .path , "fixtures/lower_format_variable" ))
297
301
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" )
300
302
301
303
validator .resources ('aws_instance' ).property ('value' ).should_equal ('abc' )
302
304
validator .resources ('aws_instance2' ).property ('value' ).should_equal ('abcDEF' )
303
305
304
306
def test_parsing_variable_with_unimplemented_interpolation_function (self ):
305
307
validator = t .Validator (os .path .join (self .path , "fixtures/unimplemented_interpolation" ))
306
308
validator .enable_variable_expansion ()
307
- self .assertRaises (t .TerraformUnimplementedInterpolationException ,validator .assert_resource_property_value_equals , 'aws_instance' , 'value' , "abc" )
308
309
self .assertRaises (t .TerraformUnimplementedInterpolationException , validator .resources ('aws_instance' ).property ('value' ).should_equal ,'abc' )
309
310
310
311
def test_boolean_equal (self ):
@@ -379,8 +380,3 @@ def test_encryption_scenario(self):
379
380
with self .assertRaisesRegexp (AssertionError ,expected_error ):
380
381
validator .resources ("aws_ebs_volume_invalid2" ).should_have_properties ("encrypted" )
381
382
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