88import logging
99import re
1010import shutil
11+ import fnmatch
1112
1213logging .basicConfig (level = logging .DEBUG )
1314root_logger = logging .getLogger ()
14- # ch = logging.StreamHandler(sys.stdout)
15- # root_logger.addHandler(ch)
1615current_path = os .path .dirname (os .path .realpath (__file__ ))
1716
17+ FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!"
1818STRING_CASES = [
1919 [
2020 lambda x : x .generate_cmd_string ('apply' , 'the_folder' ,
3030 ]
3131
3232CMD_CASES = [
33- ['method' , 'expected_output' , 'expected_ret_code' , 'expected_logs' ],
33+ ['method' , 'expected_output' , 'expected_ret_code' , 'expected_logs' , 'folder' ],
3434 [
3535 [
3636 lambda x : x .cmd ('plan' , 'var_to_output' , no_color = IsFlagged , var = {'test_var' : 'test' }) ,
3737 "doesn't need to do anything" ,
3838 0 ,
39- ''
39+ '' ,
40+ 'var_to_output'
4041 ],
4142 # try import aws instance
4243 [
4344 lambda x : x .cmd ('import' , 'aws_instance.foo' , 'i-abcd1234' , no_color = IsFlagged ),
4445 '' ,
4546 1 ,
46- 'command: terraform import -no-color aws_instance.foo i-abcd1234'
47+ 'command: terraform import -no-color aws_instance.foo i-abcd1234' ,
48+ ''
49+ ],
50+ # test with space and special character in file path
51+ [
52+ lambda x : x .cmd ('plan' , 'var_to_output' , out = FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS ),
53+ '' ,
54+ 0 ,
55+ '' ,
56+ 'var_to_output'
4757 ]
4858 ]
4959]
@@ -71,9 +81,10 @@ def string_logger(request):
7181
7282 def td ():
7383 root_logger .removeHandler (handler )
84+ log_stream .close ()
7485
7586 request .addfinalizer (td )
76- return log_stream
87+ return lambda : str ( log_stream . getvalue ())
7788
7889
7990class TestTerraform (object ):
@@ -83,12 +94,17 @@ def teardown_method(self, method):
8394 """
8495
8596 def purge (dir , pattern ):
86- for f in os .listdir (dir ):
87- if re .search (pattern , f ):
88- if os .path .isfile (f ):
89- os .remove (os .path .join (dir , f ))
90-
91- purge ('.' , '.tfstate' )
97+ for root , dirnames , filenames in os .walk (dir ):
98+ for filename in fnmatch .filter (filenames , pattern ):
99+ f = os .path .join (root , filename )
100+ os .remove (f )
101+ for dirname in fnmatch .filter (dirnames , pattern ):
102+ d = os .path .join (root , dirname )
103+ shutil .rmtree (d )
104+
105+ purge ('.' , '*.tfstate' )
106+ purge ('.' , '*.terraform' )
107+ purge ('.' , FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS )
92108
93109 @pytest .mark .parametrize ([
94110 "method" , "expected"
@@ -102,10 +118,11 @@ def test_generate_cmd_string(self, method, expected):
102118 assert s in result
103119
104120 @pytest .mark .parametrize (* CMD_CASES )
105- def test_cmd (self , method , expected_output , expected_ret_code , expected_logs , string_logger ):
121+ def test_cmd (self , method , expected_output , expected_ret_code , expected_logs , string_logger , folder ):
106122 tf = Terraform (working_dir = current_path )
123+ tf .init (folder )
107124 ret , out , err = method (tf )
108- logs = str ( string_logger . getvalue () )
125+ logs = string_logger ( )
109126 logs = logs .replace ('\n ' , '' )
110127 assert expected_output in out
111128 assert expected_ret_code == ret
@@ -123,6 +140,8 @@ def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, st
123140 ])
124141 def test_apply (self , folder , variables , var_files , expected_output , options ):
125142 tf = Terraform (working_dir = current_path , variables = variables , var_file = var_files )
143+ # after 0.10.0 we always need to init
144+ tf .init (folder )
126145 ret , out , err = tf .apply (folder , ** options )
127146 assert ret == 0
128147 assert expected_output in out .replace ('\n ' , '' ).replace (' ' , '' )
@@ -160,20 +179,36 @@ def test_pre_load_state_data(self):
160179 )
161180 def test_override_default (self , folder , variables ):
162181 tf = Terraform (working_dir = current_path , variables = variables )
182+ tf .init (folder )
163183 ret , out , err = tf .apply (folder , var = {'test_var' : 'test2' },
164184 no_color = IsNotFlagged )
165185 out = out .replace ('\n ' , '' )
166186 assert '\x1b [0m\x1b [1m\x1b [32mApply' in out
167187 out = tf .output ('test_output' )
168188 assert 'test2' in out
169189
170- def test_get_output (self ):
190+ @pytest .mark .parametrize (
191+ ("param" ),
192+ [
193+ ({}),
194+ ({'module' : 'test2' }),
195+ ]
196+ )
197+ def test_output (self , param , string_logger ):
171198 tf = Terraform (working_dir = current_path , variables = {'test_var' : 'test' })
199+ tf .init ('var_to_output' )
172200 tf .apply ('var_to_output' )
173- assert tf .output ('test_output' ) == 'test'
201+ result = tf .output ('test_output' , ** param )
202+ regex = re .compile ("terraform output (-module=test2 -json|-json -module=test2) test_output" )
203+ log_str = string_logger ()
204+ if param :
205+ assert re .search (regex , log_str ), log_str
206+ else :
207+ assert result == 'test'
174208
175209 def test_destroy (self ):
176210 tf = Terraform (working_dir = current_path , variables = {'test_var' : 'test' })
211+ tf .init ('var_to_output' )
177212 ret , out , err = tf .destroy ('var_to_output' )
178213 assert ret == 0
179214 assert 'Destroy complete! Resources: 0 destroyed.' in out
@@ -197,6 +232,4 @@ def test_fmt(self, fmt_test_file):
197232 def test_import (self , string_logger ):
198233 tf = Terraform (working_dir = current_path )
199234 tf .import_cmd ('aws_instance.foo' , 'i-abc1234' , no_color = IsFlagged )
200- logs = string_logger .getvalue ()
201- print (logs )
202- assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in logs
235+ assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in string_logger ()
0 commit comments