fix tests with given environment variables
This commit is contained in:
parent
733b8006c5
commit
3f18abb40e
2 changed files with 14 additions and 5 deletions
|
@ -33,8 +33,9 @@ class Terraform(object):
|
||||||
variables=None,
|
variables=None,
|
||||||
parallelism=None,
|
parallelism=None,
|
||||||
var_file=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,
|
:param working_dir: the folder of the working folder, if not given,
|
||||||
will be current working folder
|
will be current working folder
|
||||||
:param targets: list of target
|
:param targets: list of target
|
||||||
|
@ -47,7 +48,10 @@ class Terraform(object):
|
||||||
:param var_file: passed as value of -var-file option,
|
:param var_file: passed as value of -var-file option,
|
||||||
could be string or list, list stands for multiple -var-file option
|
could be string or list, list stands for multiple -var-file option
|
||||||
:param terraform_bin_path: binary path of terraform
|
: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.working_dir = working_dir
|
||||||
self.state = state
|
self.state = state
|
||||||
self.targets = [] if targets is None else targets
|
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
|
working_folder = self.working_dir if self.working_dir else None
|
||||||
|
|
||||||
p = subprocess.Popen(cmd_string, stdout=stdout,
|
environ_vars = {}
|
||||||
stderr=stderr, shell=True,
|
if self.is_env_vars_included:
|
||||||
cwd=working_folder)
|
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()
|
out, err = p.communicate()
|
||||||
ret_code = p.returncode
|
ret_code = p.returncode
|
||||||
log.debug('output: {o}'.format(o=out))
|
log.debug('output: {o}'.format(o=out))
|
||||||
|
|
|
@ -83,6 +83,7 @@ class TestTerraform(object):
|
||||||
|
|
||||||
@pytest.mark.parametrize(*CMD_CASES)
|
@pytest.mark.parametrize(*CMD_CASES)
|
||||||
def test_cmd(self, method, expected_output, expected_ret_code):
|
def test_cmd(self, method, expected_output, expected_ret_code):
|
||||||
|
os.environ['AWS_DEFAULT_REGION'] = "us-west-1"
|
||||||
tf = Terraform(working_dir=current_path)
|
tf = Terraform(working_dir=current_path)
|
||||||
ret, out, err = method(tf)
|
ret, out, err = method(tf)
|
||||||
assert expected_output in out
|
assert expected_output in out
|
||||||
|
@ -172,6 +173,7 @@ class TestTerraform(object):
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
def test_import(self):
|
def test_import(self):
|
||||||
|
os.environ['AWS_DEFAULT_REGION'] = "us-west-1"
|
||||||
tf = Terraform(working_dir=current_path)
|
tf = Terraform(working_dir=current_path)
|
||||||
ret, out, err = tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged)
|
ret, out, err = tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged)
|
||||||
assert 'Import complete!' in out
|
assert 'Import complete!' in out
|
||||||
|
|
Loading…
Reference in a new issue