@ -6,19 +6,19 @@ import shutil
from contextlib import contextmanager
from io import StringIO
from typing import Callable
from packaging import version
import pytest
from _pytest . logging import LogCaptureFixture , caplog
from dda_ python_terraform import IsFlagged , IsNotFlagged , Terraform , TerraformCommandError
from python_terraform import IsFlagged , IsNotFlagged , Terraform , TerraformCommandError
logging . basicConfig ( level = logging . DEBUG )
root_logger = logging . getLogger ( )
current_path = os . path . dirname ( os . path . realpath ( __file__ ) )
semantic_version = os . environ . get ( " TFVER " )
version = 1.0 if ( os . environ . get ( " TFVER " ) and os . environ . get (
" TFVER " ) . startswith ( " 1 " ) ) else 0.13
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = " test ' test.out! "
STRING_CASES = [
@ -60,7 +60,7 @@ CMD_CASES_0_x = [
var = { " test_var " : " test " } ,
raise_on_error = False ,
) ,
# Expected output varies by terraform semantic_ version
# Expected output varies by terraform version
" Plan: 0 to add, 0 to change, 0 to destroy. " ,
0 ,
False ,
@ -131,7 +131,7 @@ CMD_CASES_1_x = [
var = { " test_var " : " test " } ,
raise_on_error = False ,
) ,
# Expected output varies by terraform semantic_ version
# Expected output varies by terraform version
" Changes to Outputs: " ,
0 ,
False ,
@ -236,7 +236,7 @@ def workspace_setup_teardown():
@contextmanager
def wrapper ( workspace_name , create = True , delete = True , * args , * * kwargs ) :
tf = Terraform ( working_dir = current_path , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = current_path , terraform_ version= version)
tf . init ( )
if create :
tf . create_workspace ( workspace_name , * args , * * kwargs )
@ -271,14 +271,14 @@ class TestTerraform:
@pytest.mark.parametrize ( [ " method " , " expected " ] , STRING_CASES )
def test_generate_cmd_string ( self , method : Callable [ . . . , str ] , expected : str ) :
tf = Terraform ( working_dir = current_path , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = current_path , terraform_ version= version)
result = method ( tf )
strs = expected . split ( )
for s in strs :
assert s in result
@pytest.mark.parametrize ( * ( CMD_CASES_1_x if version . parse ( semantic_version ) > = version . parse ( " 1.0.0 " ) else CMD_CASES_0_x ) )
@pytest.mark.parametrize ( * ( CMD_CASES_1_x if version > = 1.0 else CMD_CASES_0_x ) )
def test_cmd (
self ,
method : Callable [ . . . , str ] ,
@ -290,7 +290,7 @@ class TestTerraform:
folder : str ,
) :
with caplog . at_level ( logging . INFO ) :
tf = Terraform ( working_dir = current_path , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = current_path , terraform_ version= version)
tf . init ( folder )
try :
ret , out , _ = method ( tf )
@ -305,10 +305,10 @@ class TestTerraform:
assert expected_logs in caplog . text
@pytest.mark.parametrize ( * ( APPLY_CASES_1_x if version . parse ( semantic_version ) > = version . parse ( " 1.0.0 " ) else APPLY_CASES_0_x ) )
@pytest.mark.parametrize ( * ( APPLY_CASES_1_x if version > = 1.0 else APPLY_CASES_0_x ) )
def test_apply ( self , folder , variables , var_files , expected_output , options ) :
tf = Terraform (
working_dir = current_path , variables = variables , var_file = var_files , terraform_ semantic_ version= semantic_ version
working_dir = current_path , variables = variables , var_file = var_files , terraform_ version= version
)
tf . init ( folder )
ret , out , err = tf . apply ( folder , * * options )
@ -316,29 +316,9 @@ class TestTerraform:
assert expected_output in out . replace ( " \n " , " " ) . replace ( " " , " " )
assert err == " "
def test_apply_plan ( self ) :
# test is only applicable to version > 1.0.0
if version . parse ( semantic_version ) < version . parse ( " 1.0.0 " ) :
return
tf = Terraform (
working_dir = current_path , terraform_semantic_version = semantic_version
)
out_folder = ' var_to_output '
out_file_name = ' test.out '
out_file_path = f ' { out_folder } / { out_file_name } '
tf . init ( out_folder )
ret , _ , err = tf . plan ( out_folder , detailed_exitcode = IsNotFlagged , out = out_file_name )
assert ret == 0
assert err == " "
ret , _ , err = tf . apply ( out_file_path , skip_plan = True )
assert ret == 0
assert err == " "
def test_apply_with_var_file ( self , caplog : LogCaptureFixture ) :
with caplog . at_level ( logging . INFO ) :
tf = Terraform ( working_dir = current_path , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = current_path , terraform_version = version )
folder = " var_to_output "
tf . init ( folder )
tf . apply (
@ -358,7 +338,7 @@ class TestTerraform:
] ,
)
def test_options ( self , cmd , args , options , fmt_test_file ) :
tf = Terraform ( working_dir = current_path , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = current_path , terraform_ version= version)
ret , out , err = getattr ( tf , cmd ) ( * args , * * options )
assert ret == 0
assert out == " "
@ -366,26 +346,26 @@ class TestTerraform:
def test_state_data ( self ) :
cwd = os . path . join ( current_path , " test_tfstate_file " )
tf = Terraform ( working_dir = cwd , state = " tfstate.test " ,
terraform_ semantic_ version= semantic_ version)
terraform_ version= version)
tf . read_state_file ( )
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " root " ]
def test_state_default ( self ) :
cwd = os . path . join ( current_path , " test_tfstate_file2 " )
tf = Terraform ( working_dir = cwd , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = cwd , terraform_ version= version)
tf . read_state_file ( )
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " default " ]
def test_state_default_backend ( self ) :
cwd = os . path . join ( current_path , " test_tfstate_file3 " )
tf = Terraform ( working_dir = cwd , terraform_ semantic_ version= semantic_ version)
tf = Terraform ( working_dir = cwd , terraform_ version= version)
tf . read_state_file ( )
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " default_backend " ]
def test_pre_load_state_data ( self ) :
cwd = os . path . join ( current_path , " test_tfstate_file " )
tf = Terraform ( working_dir = cwd , state = " tfstate.test " ,
terraform_ semantic_ version= semantic_ version)
terraform_ version= version)
assert tf . tfstate . modules [ 0 ] [ " path " ] == [ " root " ]
@pytest.mark.parametrize (
@ -393,7 +373,7 @@ class TestTerraform:
)
def test_override_default ( self , folder , variables ) :
tf = Terraform ( working_dir = current_path ,
variables = variables , terraform_ semantic_ version= semantic_ version)
variables = variables , terraform_ version= version)
tf . init ( folder )
ret , out , err = tf . apply (
folder , var = { " test_var " : " test2 " } , no_color = IsNotFlagged ,
@ -409,7 +389,7 @@ class TestTerraform:
required_output = " test_output "
with caplog . at_level ( logging . INFO ) :
tf = Terraform (
working_dir = current_path , variables = { " test_var " : expected_value } , terraform_ semantic_ version= semantic_ version
working_dir = current_path , variables = { " test_var " : expected_value } , terraform_ version= version
)
tf . init ( " var_to_output " )
tf . apply ( " var_to_output " )
@ -423,7 +403,7 @@ class TestTerraform:
def test_destroy ( self ) :
tf = Terraform ( working_dir = current_path , variables = {
" test_var " : " test " } , terraform_ semantic_ version= semantic_ version)
" test_var " : " test " } , terraform_ version= version)
tf . init ( " var_to_output " )
ret , out , err = tf . destroy ( " var_to_output " )
assert ret == 0
@ -434,7 +414,7 @@ class TestTerraform:
)
def test_plan ( self , plan , variables , expected_ret ) :
tf = Terraform ( working_dir = current_path ,
variables = variables , terraform_ semantic_ version= semantic_ version)
variables = variables , terraform_ version= version)
tf . init ( plan )
with pytest . raises ( TerraformCommandError ) as e :
tf . plan ( plan )
@ -444,7 +424,7 @@ class TestTerraform:
def test_fmt ( self , fmt_test_file ) :
tf = Terraform ( working_dir = current_path , variables = {
" test_var " : " test " } , terraform_ semantic_ version= semantic_ version)
" test_var " : " test " } , terraform_ version= version)
ret , out , err = tf . fmt ( diff = True )
assert ret == 0
@ -549,9 +529,3 @@ class TestTerraform:
in caplog . messages
)
"""
def test_list_workspace ( self ) :
tf = Terraform ( working_dir = current_path )
workspaces = tf . list_workspace ( )
assert len ( workspaces ) > 0
assert ' default ' in workspaces