From 3f18abb40e34149e3ef782ca435e2494d2800b3f Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 9 May 2017 16:05:48 +0800 Subject: [PATCH] fix tests with given environment variables --- python_terraform/__init__.py | 17 ++++++++++++----- test/test_terraform.py | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 8eee5ab..fe0d3d3 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -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)) diff --git a/test/test_terraform.py b/test/test_terraform.py index 6ff8e3b..727fc80 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -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