@ -1,15 +1,16 @@
try :
import fnmatch
from cStringIO import StringIO # Python 2
except ImportError :
from io import StringIO
from python_terraform import *
from contextlib import contextmanager
import pytest
import os
import logging
import logging
import os
import re
import re
import shutil
import shutil
import fnmatch
from contextlib import contextmanager
from io import StringIO
from typing import Callable
import pytest
from _pytest . logging import LogCaptureFixture , caplog
from python_terraform import IsFlagged , IsNotFlagged , Terraform , TerraformCommandError
logging . basicConfig ( level = logging . DEBUG )
logging . basicConfig ( level = logging . DEBUG )
root_logger = logging . getLogger ( )
root_logger = logging . getLogger ( )
@ -18,78 +19,92 @@ current_path = os.path.dirname(os.path.realpath(__file__))
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = " test ' test.out! "
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = " test ' test.out! "
STRING_CASES = [
STRING_CASES = [
[
[
lambda x : x . generate_cmd_string ( ' apply ' , ' the_folder ' ,
lambda x : x . generate_cmd_string ( " apply " , " the_folder " , no_color = IsFlagged ) ,
no_color = IsFlagged ) ,
" terraform apply -no-color the_folder " ,
" terraform apply -no-color the_folder "
] ,
] ,
[
[
lambda x : x . generate_cmd_string (
lambda x : x . generate_cmd_string ( ' push ' , ' path ' , vcs = True ,
" push " , " path " , vcs = True , token = " token " , atlas_address = " url "
token = ' token ' ,
) ,
atlas_address = ' url ' ) ,
" terraform push -vcs=true -token=token -atlas-address=url path " ,
" terraform push -vcs=true -token=token -atlas-address=url path "
] ,
] ,
]
]
CMD_CASES = [
CMD_CASES = [
[ ' method ' , ' expected_output ' , ' expected_ret_code ' , ' expected_exception ' , ' expected_logs ' , ' folder ' ] ,
[
" method " ,
" expected_output " ,
" expected_ret_code " ,
" expected_exception " ,
" expected_logs " ,
" folder " ,
] ,
[
[
[
[
lambda x : x . cmd ( ' plan ' , ' var_to_output ' , no_color = IsFlagged , var = { ' test_var ' : ' test ' } ) ,
lambda x : x . cmd (
" plan " ,
" var_to_output " ,
no_color = IsFlagged ,
var = { " test_var " : " test " } ,
raise_on_error = False ,
) ,
# Expected output varies by terraform version
# Expected output varies by terraform version
[ " doesn ' t need to do anything " , # Terraform < 0.10.7 (used in travis env)
" Plan: 0 to add, 0 to change, 0 to destroy. " ,
" no \n actions need to be performed " ] , # Terraform >= 0.10.7
0 ,
0 ,
False ,
False ,
' ' ,
" " ,
' var_to_output '
" var_to_output " ,
] ,
] ,
# try import aws instance
# try import aws instance
[
[
lambda x : x . cmd ( ' import ' , ' aws_instance.foo ' , ' i-abcd1234 ' , no_color = IsFlagged ) ,
lambda x : x . cmd (
' ' ,
" import " ,
" aws_instance.foo " ,
" i-abcd1234 " ,
no_color = IsFlagged ,
raise_on_error = False ,
) ,
" " ,
1 ,
1 ,
False ,
False ,
' command: terraform import -no-color aws_instance.foo i-abcd1234 ' ,
" Error: No Terraform configuration files " ,
' '
" " ,
] ,
# try import aws instance with raise_on_error
[
lambda x : x . cmd ( ' import ' , ' aws_instance.foo ' , ' i-abcd1234 ' , no_color = IsFlagged , raise_on_error = True ) ,
' ' ,
1 ,
True ,
' command: terraform import -no-color aws_instance.foo i-abcd1234 ' ,
' '
] ,
] ,
# test with space and special character in file path
# test with space and special character in file path
[
[
lambda x : x . cmd ( ' plan ' , ' var_to_output ' , out = FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS ) ,
lambda x : x . cmd (
' ' ,
" plan " ,
" var_to_output " ,
out = FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS ,
raise_on_error = False ,
) ,
" " ,
0 ,
0 ,
False ,
False ,
' ' ,
" " ,
' var_to_output '
" var_to_output " ,
] ,
] ,
# test workspace command (commands with subcommand)
# test workspace command (commands with subcommand)
[
[
lambda x : x . cmd ( ' workspace ' , ' show ' , no_color = IsFlagged ) ,
lambda x : x . cmd (
' ' ,
" workspace " , " show " , no_color = IsFlagged , raise_on_error = False
) ,
" " ,
0 ,
0 ,
False ,
False ,
' command: terraform workspace show -no-color ' ,
" Command: terraform workspace show -no-color " ,
' '
" " ,
] ,
] ,
]
] ,
]
]
@pytest.fixture ( scope = ' function ' )
@pytest.fixture ( scope = " function " )
def fmt_test_file ( request ) :
def fmt_test_file ( request ) :
target = os . path . join ( current_path , ' bad_fmt ' , ' test.backup ' )
target = os . path . join ( current_path , " bad_fmt " , " test.backup " )
orgin = os . path . join ( current_path , ' bad_fmt ' , ' test.tf ' )
orgin = os . path . join ( current_path , " bad_fmt " , " test.tf " )
shutil . copy ( orgin ,
shutil . copy ( orgin , target )
target )
def td ( ) :
def td ( ) :
shutil . move ( target , orgin )
shutil . move ( target , orgin )
@ -98,28 +113,28 @@ def fmt_test_file(request):
return
return
@pytest.fixture ( )
# @pytest.fixture( )
def string_logger ( request ) :
# def string_logger(request) -> Callable[..., str] :
log_stream = StringIO ( )
# log_stream = StringIO()
handler = logging . StreamHandler ( log_stream )
# handler = logging.StreamHandler(log_stream)
root_logger . addHandler ( handler )
# root_logger.addHandler(handler)
def td ( ) :
# def td() :
root_logger . removeHandler ( handler )
# root_logger.removeHandler(handler)
log_stream . close ( )
# log_stream.close( )
request . addfinalizer ( td )
# request.addfinalizer(td)
return lambda : str ( log_stream . getvalue ( ) )
# return lambda: str(log_stream.getvalue() )
@pytest.fixture ( )
@pytest.fixture ( )
def workspace_setup_teardown ( ) :
def workspace_setup_teardown ( ) :
"""
""" Fixture used in workspace related tests.
Fixture used in workspace related tests
Create and tear down a workspace
Create and tear down a workspace
* Use as a contextmanager *
* Use as a contextmanager *
"""
"""
@contextmanager
@contextmanager
def wrapper ( workspace_name , create = True , delete = True , * args , * * kwargs ) :
def wrapper ( workspace_name , create = True , delete = True , * args , * * kwargs ) :
tf = Terraform ( working_dir = current_path )
tf = Terraform ( working_dir = current_path )
@ -128,22 +143,18 @@ def workspace_setup_teardown():
tf . create_workspace ( workspace_name , * args , * * kwargs )
tf . create_workspace ( workspace_name , * args , * * kwargs )
yield tf
yield tf
if delete :
if delete :
tf . set_workspace ( ' default ' )
tf . set_workspace ( " default " )
tf . delete_workspace ( workspace_name )
tf . delete_workspace ( workspace_name )
yield wrapper
yield wrapper
class TestTerraform ( object ) :
class TestTerraform :
def teardown_method ( self , method ) :
def teardown_method ( self , _ ) - > None :
""" teardown any state that was previously setup with a setup_method
""" Teardown any state that was previously setup with a setup_method call. """
call .
exclude = [ " test_tfstate_file " , " test_tfstate_file2 " , " test_tfstate_file3 " ]
"""
exclude = [ ' test_tfstate_file ' ,
' test_tfstate_file2 ' ,
' test_tfstate_file3 ' ]
def purge ( dir , pattern ):
def purge ( dir : str , pattern : str ) - > None :
for root , dirnames , filenames in os . walk ( dir ) :
for root , dirnames , filenames in os . walk ( dir ) :
dirnames [ : ] = [ d for d in dirnames if d not in exclude ]
dirnames [ : ] = [ d for d in dirnames if d not in exclude ]
for filename in fnmatch . filter ( filenames , pattern ) :
for filename in fnmatch . filter ( filenames , pattern ) :
@ -153,15 +164,13 @@ class TestTerraform(object):
d = os . path . join ( root , dirname )
d = os . path . join ( root , dirname )
shutil . rmtree ( d )
shutil . rmtree ( d )
purge ( ' . ' , ' *.tfstate ' )
purge ( " . " , " *.tfstate " )
purge ( ' . ' , ' *.tfstate.backup ' )
purge ( " . " , " *.tfstate.backup " )
purge ( ' . ' , ' *.terraform ' )
purge ( " . " , " *.terraform " )
purge ( ' . ' , FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS )
purge ( " . " , FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS )
@pytest.mark.parametrize ( [
@pytest.mark.parametrize ( [ " method " , " expected " ] , STRING_CASES )
" method " , " expected "
def test_generate_cmd_string ( self , method : Callable [ . . . , str ] , expected : str ) :
] , STRING_CASES )
def test_generate_cmd_string ( self , method , expected ) :
tf = Terraform ( working_dir = current_path )
tf = Terraform ( working_dir = current_path )
result = method ( tf )
result = method ( tf )
@ -170,288 +179,271 @@ class TestTerraform(object):
assert s in result
assert s in result
@pytest.mark.parametrize ( * CMD_CASES )
@pytest.mark.parametrize ( * CMD_CASES )
def test_cmd ( self , method , expected_output , expected_ret_code , expected_exception , expected_logs , string_logger , folder ) :
def test_cmd (
tf = Terraform ( working_dir = current_path )
self ,
tf . init ( folder )
method : Callable [ . . . , str ] ,
try :
expected_output : str ,
ret , out , err = method ( tf )
expected_ret_code : int ,
assert not expected_exception
expected_exception : bool ,
except TerraformCommandError as e :
expected_logs : str ,
assert expected_exception
caplog : LogCaptureFixture ,
ret = e . returncode
folder : str ,
out = e . out
) :
err = e . err
with caplog . at_level ( logging . INFO ) :
tf = Terraform ( working_dir = current_path )
logs = string_logger ( )
tf . init ( folder )
logs = logs . replace ( ' \n ' , ' ' )
try :
if isinstance ( expected_output , list ) :
ret , out , _ = method ( tf )
ok = False
assert not expected_exception
for xo in expected_output :
except TerraformCommandError as e :
if xo in out :
assert expected_exception
ok = True
ret = e . returncode
break
out = e . out
if not ok :
assert expected_output [ 0 ] in out
assert expected_output in out
else :
assert expected_output in out
assert expected_ret_code == ret
assert expected_ret_code == ret
assert expected_logs in logs
assert expected_logs in caplog . text
@pytest.mark.parametrize (
@pytest.mark.parametrize (
( " folder " , " variables " , " var_files " , " expected_output " , " options " ) ,
( " folder " , " variables " , " var_files " , " expected_output " , " options " ) ,
[
[
( " var_to_output " ,
( " var_to_output " , { " test_var " : " test " } , None , " test_output=test " , { } ) ,
{ ' test_var ' : ' test ' } , None , " test_output=test " , { } ) ,
(
( " var_to_output " , { ' test_list_var ' : [ ' c ' , ' d ' ] } , None , " test_list_output=[c,d] " , { } ) ,
" var_to_output " ,
( " var_to_output " , { ' test_map_var ' : { " c " : " c " , " d " : " d " } } , None , " test_map_output= { a=ab=bc=cd=d} " , { } ) ,
{ " test_list_var " : [ " c " , " d " ] } ,
( " var_to_output " , { ' test_map_var ' : { " c " : " c " , " d " : " d " } } , ' var_to_output/test_map_var.json ' , " test_map_output= { a=ab=bc=cd=de=ef=f} " , { } ) ,
None ,
( " var_to_output " , { } , None , " \x1b [0m \x1b [1m \x1b [32mApplycomplete! " , { " no_color " : IsNotFlagged } )
' test_list_output=[ " c " , " d " ,] ' ,
] )
{ } ,
) ,
(
" var_to_output " ,
{ " test_map_var " : { " c " : " c " , " d " : " d " } } ,
None ,
' test_map_output= { " c " = " c " " d " = " d " } ' ,
{ } ,
) ,
(
" var_to_output " ,
{ " test_map_var " : { " c " : " c " , " d " : " d " } } ,
" var_to_output/test_map_var.json " ,
# Values are overriden
' test_map_output= { " e " = " e " " f " = " f " } ' ,
{ } ,
) ,
(
" var_to_output " ,
{ } ,
None ,
" \x1b [0m \x1b [1m \x1b [32mApplycomplete! " ,
{ " no_color " : IsNotFlagged } ,
) ,
] ,
)
def test_apply ( self , folder , variables , var_files , expected_output , options ) :
def test_apply ( self , folder , variables , var_files , expected_output , options ) :
tf = Terraform ( working_dir = current_path , variables = variables , var_file = var_files )
tf = Terraform (
# after 0.10.0 we always need to init
working_dir = current_path , variables = variables , var_file = var_files
)
tf . init ( folder )
tf . init ( folder )
ret , out , err = tf . apply ( folder , * * options )
ret , out , err = tf . apply ( folder , * * options )
assert ret == 0
assert ret == 0
assert expected_output in out . replace ( ' \n ' , ' ' ) . replace ( ' ' , ' ' )
assert expected_output in out . replace ( " \n " , " " ) . replace ( " " , " " )
assert err == ' '
assert err == " "
def test_apply_with_var_file ( self , string_logger ) :
def test_apply_with_var_file ( self , caplog : LogCaptureFixture ) :
tf = Terraform ( working_dir = current_path )
with caplog . at_level ( logging . INFO ) :
tf = Terraform ( working_dir = current_path )
tf . init ( )
tf . apply ( var_file = os . path . join ( current_path , ' tfvar_file ' , ' test.tfvars ' ) )
folder = " var_to_output "
logs = string_logger ( )
tf . init ( folder )
logs = logs . split ( ' \n ' )
tf . apply (
for log in logs :
folder ,
if log . startswith ( ' command: terraform apply ' ) :
var_file = os . path . join ( current_path , " tfvar_files " , " test.tfvars " ) ,
assert log . count ( ' -var-file= ' ) == 1
)
for log in caplog . messages :
if log . startswith ( " Command: terraform apply " ) :
assert log . count ( " -var-file= " ) == 1
@pytest.mark.parametrize (
@pytest.mark.parametrize (
[ ' cmd ' , ' args ' , ' options ' ] ,
[ " cmd " , " args " , " options " ] ,
[
[
# bool value
# bool value
( ' fmt ' , [ ' bad_fmt ' ] , { ' list ' : False , ' diff ' : False } )
( " fmt " , [ " bad_fmt " ] , { " list " : False , " diff " : False } )
]
] ,
)
)
def test_options ( self , cmd , args , options , fmt_test_file ) :
def test_options ( self , cmd , args , options , fmt_test_file ) :
tf = Terraform ( working_dir = current_path )
tf = Terraform ( working_dir = current_path )
ret , out , err = getattr ( tf , cmd ) ( * args , * * options )
ret , out , err = getattr ( tf , cmd ) ( * args , * * options )
assert ret == 0
assert ret == 0
assert out == ' '
assert out == " "
def test_state_data ( self ) :
def test_state_data ( self ) :
cwd = os . path . join ( current_path , ' test_tfstate_file ' )
cwd = os . path . join ( current_path , " test_tfstate_file " )
tf = Terraform ( working_dir = cwd , state = ' tfstate.test ' )
tf = Terraform ( working_dir = cwd , state = " tfstate.test " )
tf . read_state_file ( )
tf . read_state_file ( )
assert tf . tfstate . modules [ 0 ] [ ' path ' ] == [ ' root ' ]
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " root " ]
def test_state_default ( self ) :
def test_state_default ( self ) :
cwd = os . path . join ( current_path , ' test_tfstate_file2 ' )
cwd = os . path . join ( current_path , " test_tfstate_file2 " )
tf = Terraform ( working_dir = cwd )
tf = Terraform ( working_dir = cwd )
tf . read_state_file ( )
tf . read_state_file ( )
assert tf . tfstate . modules [ 0 ] [ ' path ' ] == [ ' default ' ]
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " default " ]
def test_state_default_backend ( self ) :
def test_state_default_backend ( self ) :
cwd = os . path . join ( current_path , ' test_tfstate_file3 ' )
cwd = os . path . join ( current_path , " test_tfstate_file3 " )
tf = Terraform ( working_dir = cwd )
tf = Terraform ( working_dir = cwd )
tf . read_state_file ( )
tf . read_state_file ( )
assert tf . tfstate . modules [ 0 ] [ ' path ' ] == [ ' default_backend ' ]
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " default_backend " ]
def test_pre_load_state_data ( self ) :
def test_pre_load_state_data ( self ) :
cwd = os . path . join ( current_path , ' test_tfstate_file ' )
cwd = os . path . join ( current_path , " test_tfstate_file " )
tf = Terraform ( working_dir = cwd , state = ' tfstate.test ' )
tf = Terraform ( working_dir = cwd , state = " tfstate.test " )
assert tf . tfstate . modules [ 0 ] [ ' path ' ] == [ ' root ' ]
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " root " ]
@pytest.mark.parametrize (
@pytest.mark.parametrize (
( " folder " , ' variables ' ) ,
( " folder " , " variables " ) , [ ( " var_to_output " , { " test_var " : " test " } ) ]
[
( " var_to_output " , { ' test_var ' : ' test ' } )
]
)
)
def test_override_default ( self , folder , variables ) :
def test_override_default ( self , folder , variables ) :
tf = Terraform ( working_dir = current_path , variables = variables )
tf = Terraform ( working_dir = current_path , variables = variables )
tf . init ( folder )
tf . init ( folder )
ret , out , err = tf . apply ( folder , var = { ' test_var ' : ' test2 ' } ,
ret , out , err = tf . apply (
no_color = IsNotFlagged )
folder , var = { " test_var " : " test2 " } , no_color = IsNotFlagged ,
out = out . replace ( ' \n ' , ' ' )
)
assert ' \x1b [0m \x1b [1m \x1b [32mApply ' in out
out = out . replace ( " \n " , " " )
out = tf . output ( ' test_output ' )
assert " \x1b [0m \x1b [1m \x1b [32mApply " in out
assert ' test2 ' in out
out = tf . output ( " test_output " )
assert " test2 " in out
@pytest.mark.parametrize (
( " param " ) ,
@pytest.mark.parametrize ( " output_all " , [ True , False ] )
[
def test_output ( self , caplog : LogCaptureFixture , output_all : bool ) :
( { } ) ,
expected_value = " test "
( { ' module ' : ' test2 ' } ) ,
required_output = " test_output "
]
with caplog . at_level ( logging . INFO ) :
)
tf = Terraform (
def test_output ( self , param , string_logger ) :
working_dir = current_path , variables = { " test_var " : expected_value }
tf = Terraform ( working_dir = current_path , variables = { ' test_var ' : ' test ' } )
)
tf . init ( ' var_to_output ' )
tf . init ( " var_to_output " )
tf . apply ( ' var_to_output ' )
tf . apply ( " var_to_output " )
result = tf . output ( ' test_output ' , * * param )
params = tuple ( ) if output_all else ( required_output , )
regex = re . compile ( " terraform output (-module=test2 -json|-json -module=test2) test_output " )
result = tf . output ( * params )
log_str = string_logger ( )
if output_all :
if param :
assert result [ required_output ] [ " value " ] == expected_value
assert re . search ( regex , log_str ) , log_str
else :
assert result == ' test '
@pytest.mark.parametrize (
( " param " ) ,
[
( { } ) ,
( { ' module ' : ' test2 ' } ) ,
]
)
def test_output_full_value ( self , param , string_logger ) :
tf = Terraform ( working_dir = current_path , variables = { ' test_var ' : ' test ' } )
tf . init ( ' var_to_output ' )
tf . apply ( ' var_to_output ' )
result = tf . output ( ' test_output ' , * * dict ( param , full_value = True ) )
regex = re . compile ( " terraform output (-module=test2 -json|-json -module=test2) test_output " )
log_str = string_logger ( )
if param :
assert re . search ( regex , log_str ) , log_str
else :
assert result [ ' value ' ] == ' test '
@pytest.mark.parametrize (
( " param " ) ,
[
( { } ) ,
( { ' module ' : ' test2 ' } ) ,
]
)
def test_output_all ( self , param , string_logger ) :
tf = Terraform ( working_dir = current_path , variables = { ' test_var ' : ' test ' } )
tf . init ( ' var_to_output ' )
tf . apply ( ' var_to_output ' )
result = tf . output ( * * param )
regex = re . compile ( " terraform output (-module=test2 -json|-json -module=test2) " )
log_str = string_logger ( )
if param :
assert re . search ( regex , log_str ) , log_str
else :
else :
assert result [ ' test_output ' ] [ ' value ' ] == ' test '
assert result == expected_value
assert expected_value in caplog . messages [ - 1 ]
def test_destroy ( self ) :
def test_destroy ( self ) :
tf = Terraform ( working_dir = current_path , variables = { ' test_var ' : ' test ' } )
tf = Terraform ( working_dir = current_path , variables = { " test_var " : " test " } )
tf . init ( ' var_to_output ' )
tf . init ( " var_to_output " )
ret , out , err = tf . destroy ( ' var_to_output ' )
ret , out , err = tf . destroy ( " var_to_output " )
assert ret == 0
assert ret == 0
assert ' Destroy complete! Resources: 0 destroyed. ' in out
assert " Destroy complete! Resources: 0 destroyed. " in out
@pytest.mark.parametrize (
@pytest.mark.parametrize (
( " plan " , " variables " , " expected_ret " ) ,
( " plan " , " variables " , " expected_ret " ) , [ ( " vars_require_input " , { } , 1 ) ]
[
( ' vars_require_input ' , { } , 1 )
]
)
)
def test_plan ( self , plan , variables , expected_ret ) :
def test_plan ( self , plan , variables , expected_ret ) :
tf = Terraform ( working_dir = current_path , variables = variables )
tf = Terraform ( working_dir = current_path , variables = variables )
ret , out , err = tf . plan ( plan )
tf . init ( plan )
assert ret == expected_ret
with pytest . raises ( TerraformCommandError ) as e :
tf . plan ( plan )
assert (
e . value . err
== """ \n Error: Missing required argument \n \n The argument " region " is required, but was not set. \n \n """
)
def test_fmt ( self , fmt_test_file ) :
def test_fmt ( self , fmt_test_file ) :
tf = Terraform ( working_dir = current_path , variables = { ' test_var ' : ' test ' } )
tf = Terraform ( working_dir = current_path , variables = { " test_var " : " test " } )
ret , out , err = tf . fmt ( diff = True )
ret , out , err = tf . fmt ( diff = True )
assert ret == 0
assert ret == 0
def test_import ( self , string_logger ) :
tf = Terraform ( working_dir = current_path )
tf . import_cmd ( ' aws_instance.foo ' , ' i-abc1234 ' , no_color = IsFlagged )
assert ' command: terraform import -no-color aws_instance.foo i-abc1234 ' in string_logger ( )
def test_create_workspace ( self , workspace_setup_teardown ) :
def test_create_workspace ( self , workspace_setup_teardown ) :
workspace_name = ' test '
workspace_name = " test "
with workspace_setup_teardown ( workspace_name , create = False ) as tf :
with workspace_setup_teardown ( workspace_name , create = False ) as tf :
ret , out , err = tf . create_workspace ( ' test ' )
ret , out , err = tf . create_workspace ( " test " )
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
def test_create_workspace_with_args (
def test_create_workspace_with_args ( self , workspace_setup_teardown , caplog ) :
self , workspace_setup_teardown , string_logger
workspace_name = " test "
) :
state_file_path = os . path . join (
workspace_name = ' test '
current_path , " test_tfstate_file2 " , " terraform.tfstate "
state_file_path = os . path . join ( current_path , ' test_tfstate_file2 ' , ' terraform.tfstate ' )
)
with workspace_setup_teardown ( workspace_name , create = False ) as tf :
with workspace_setup_teardown (
ret , out , err = tf . create_workspace ( ' test ' , current_path , no_color = IsFlagged )
workspace_name , create = False
) as tf , caplog . at_level ( logging . INFO ) :
ret , out , err = tf . create_workspace (
" test " , current_path , no_color = IsFlagged
)
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
assert (
logs = string_logger ( )
f " Command: terraform workspace new -no-color test { current_path } "
logs = logs . replace ( ' \n ' , ' ' )
in caplog . messages
expected_log = ' command: terraform workspace new -no-color test {} ' . format ( current_path )
)
assert expected_log in logs
def test_set_workspace ( self , workspace_setup_teardown ) :
def test_set_workspace ( self , workspace_setup_teardown ) :
workspace_name = ' test '
workspace_name = " test "
with workspace_setup_teardown ( workspace_name ) as tf :
with workspace_setup_teardown ( workspace_name ) as tf :
ret , out , err = tf . set_workspace ( workspace_name )
ret , out , err = tf . set_workspace ( workspace_name )
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
def test_set_workspace_with_args (
def test_set_workspace_with_args ( self , workspace_setup_teardown , caplog ) :
self , workspace_setup_teardown , string_logger ) :
workspace_name = " test "
workspace_name = ' test '
with workspace_setup_teardown ( workspace_name ) as tf , caplog . at_level (
with workspace_setup_teardown ( workspace_name ) as tf :
logging . INFO
ret , out , err = tf . set_workspace ( workspace_name , current_path , no_color = IsFlagged )
) :
ret , out , err = tf . set_workspace (
workspace_name , current_path , no_color = IsFlagged
)
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
assert (
logs = string_logger ( )
f " Command: terraform workspace select -no-color test { current_path } "
logs = logs . replace ( ' \n ' , ' ' )
in caplog . messages
expected_log = ' command: terraform workspace select -no-color test {} ' . format ( current_path )
)
assert expected_log in logs
def test_show_workspace ( self , workspace_setup_teardown ) :
def test_show_workspace ( self , workspace_setup_teardown ) :
workspace_name = ' test '
workspace_name = " test "
with workspace_setup_teardown ( workspace_name ) as tf :
with workspace_setup_teardown ( workspace_name ) as tf :
ret , out , err = tf . show_workspace ( )
ret , out , err = tf . show_workspace ( )
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
def test_show_workspace_with_no_color (
def test_show_workspace_with_no_color ( self , workspace_setup_teardown , caplog ) :
self , workspace_setup_teardown , string_logger
workspace_name = " test "
) :
with workspace_setup_teardown ( workspace_name ) as tf , caplog . at_level (
workspace_name = ' test '
logging . INFO
with workspace_setup_teardown ( workspace_name ) as tf :
) :
ret , out , err = tf . show_workspace ( no_color = IsFlagged )
ret , out , err = tf . show_workspace ( no_color = IsFlagged )
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
assert " Command: terraform workspace show -no-color " in caplog . messages
logs = string_logger ( )
logs = logs . replace ( ' \n ' , ' ' )
expected_log = ' command: terraform workspace show -no-color '
assert expected_log in logs
def test_delete_workspace ( self , workspace_setup_teardown ) :
def test_delete_workspace ( self , workspace_setup_teardown ) :
workspace_name = ' test '
workspace_name = " test "
with workspace_setup_teardown ( workspace_name , delete = False ) as tf :
with workspace_setup_teardown ( workspace_name , delete = False ) as tf :
tf . set_workspace ( ' default ' )
tf . set_workspace ( " default " )
ret , out , err = tf . delete_workspace ( workspace_name )
ret , out , err = tf . delete_workspace ( workspace_name )
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
def test_delete_workspace_with_args (
def test_delete_workspace_with_args ( self , workspace_setup_teardown , caplog ) :
self , workspace_setup_teardown , string_logger
workspace_name = " test "
) :
with workspace_setup_teardown (
workspace_name = ' test '
workspace_name , delete = False
with workspace_setup_teardown ( workspace_name , delete = False ) as tf :
) as tf , caplog . at_level ( logging . INFO ) :
tf . set_workspace ( ' default ' )
tf . set_workspace ( " default " )
ret , out , err = tf . delete_workspace (
ret , out , err = tf . delete_workspace (
workspace_name , current_path , force = IsFlagged ,
workspace_name , current_path , force = IsFlagged ,
)
)
assert ret == 0
assert ret == 0
assert err == ' '
assert err == " "
assert (
logs = string_logger ( )
f " Command: terraform workspace delete -force test { current_path } "
logs = logs . replace ( ' \n ' , ' ' )
in caplog . messages
expected_log = ' command: terraform workspace delete -force test {} ' . format ( current_path )
)
assert expected_log in logs