fix tests with given environment variables

This commit is contained in:
beelit94 2017-05-09 16:05:48 +08:00
parent 733b8006c5
commit 3f18abb40e
2 changed files with 14 additions and 5 deletions

View file

@ -33,8 +33,9 @@ class Terraform(object):
variables=None,
parallelism=None,
var_file=None,
terraform_bin_path=None):
"""
terraform_bin_path=None,
is_env_vars_included=True):
"""
:param working_dir: the folder of the working folder, if not given,
will be current working folder
:param targets: list of target
@ -47,7 +48,10 @@ class Terraform(object):
:param var_file: passed as value of -var-file option,
could be string or list, list stands for multiple -var-file option
:param terraform_bin_path: binary path of terraform
:type is_env_vars_included: bool
:param is_env_vars_included: included env variables when calling terraform cmd
"""
self.is_env_vars_included = is_env_vars_included
self.working_dir = working_dir
self.state = state
self.targets = [] if targets is None else targets
@ -225,9 +229,12 @@ class Terraform(object):
working_folder = self.working_dir if self.working_dir else None
p = subprocess.Popen(cmd_string, stdout=stdout,
stderr=stderr, shell=True,
cwd=working_folder)
environ_vars = {}
if self.is_env_vars_included:
environ_vars = os.environ.copy()
p = subprocess.Popen(cmd_string, stdout=stdout, stderr=stderr, shell=True,
cwd=working_folder, env=environ_vars)
out, err = p.communicate()
ret_code = p.returncode
log.debug('output: {o}'.format(o=out))

View file

@ -83,6 +83,7 @@ class TestTerraform(object):
@pytest.mark.parametrize(*CMD_CASES)
def test_cmd(self, method, expected_output, expected_ret_code):
os.environ['AWS_DEFAULT_REGION'] = "us-west-1"
tf = Terraform(working_dir=current_path)
ret, out, err = method(tf)
assert expected_output in out
@ -172,6 +173,7 @@ class TestTerraform(object):
assert ret == 0
def test_import(self):
os.environ['AWS_DEFAULT_REGION'] = "us-west-1"
tf = Terraform(working_dir=current_path)
ret, out, err = tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged)
assert 'Import complete!' in out