2017-05-09 09:47:36 +00:00
|
|
|
try:
|
|
|
|
from cStringIO import StringIO # Python 2
|
|
|
|
except ImportError:
|
|
|
|
from io import StringIO
|
2016-11-19 10:24:33 +00:00
|
|
|
from python_terraform import *
|
2019-06-25 18:53:39 +00:00
|
|
|
from contextlib import contextmanager
|
2016-11-18 07:35:14 +00:00
|
|
|
import pytest
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import re
|
2016-12-20 16:55:55 +00:00
|
|
|
import shutil
|
2017-08-04 23:02:49 +00:00
|
|
|
import fnmatch
|
2016-11-18 07:35:14 +00:00
|
|
|
|
2016-12-20 10:53:01 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2017-05-09 09:47:36 +00:00
|
|
|
root_logger = logging.getLogger()
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
|
2016-11-24 07:13:43 +00:00
|
|
|
current_path = os.path.dirname(os.path.realpath(__file__))
|
2016-11-18 07:35:14 +00:00
|
|
|
|
2017-08-08 21:58:33 +00:00
|
|
|
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!"
|
2016-11-18 07:35:14 +00:00
|
|
|
STRING_CASES = [
|
|
|
|
[
|
|
|
|
lambda x: x.generate_cmd_string('apply', 'the_folder',
|
2016-12-20 10:53:01 +00:00
|
|
|
no_color=IsFlagged),
|
|
|
|
"terraform apply -no-color the_folder"
|
2016-11-18 07:35:14 +00:00
|
|
|
],
|
|
|
|
[
|
2016-12-20 10:53:01 +00:00
|
|
|
lambda x: x.generate_cmd_string('push', 'path', vcs=True,
|
2016-11-18 07:35:14 +00:00
|
|
|
token='token',
|
|
|
|
atlas_address='url'),
|
2016-12-20 10:53:01 +00:00
|
|
|
"terraform push -vcs=true -token=token -atlas-address=url path"
|
2016-11-18 07:35:14 +00:00
|
|
|
],
|
|
|
|
]
|
|
|
|
|
|
|
|
CMD_CASES = [
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
['method', 'expected_output', 'expected_ret_code', 'expected_exception', 'expected_logs', 'folder'],
|
2016-11-18 07:35:14 +00:00
|
|
|
[
|
|
|
|
[
|
2019-06-25 18:53:39 +00:00
|
|
|
lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}),
|
2017-10-16 16:03:45 +00:00
|
|
|
# Expected output varies by terraform version
|
|
|
|
["doesn't need to do anything", # Terraform < 0.10.7 (used in travis env)
|
|
|
|
"no\nactions need to be performed"], # Terraform >= 0.10.7
|
2017-05-09 09:47:36 +00:00
|
|
|
0,
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
False,
|
2017-08-04 23:17:52 +00:00
|
|
|
'',
|
|
|
|
'var_to_output'
|
2017-05-09 07:39:09 +00:00
|
|
|
],
|
|
|
|
# try import aws instance
|
|
|
|
[
|
|
|
|
lambda x: x.cmd('import', 'aws_instance.foo', 'i-abcd1234', no_color=IsFlagged),
|
2017-05-09 09:47:36 +00:00
|
|
|
'',
|
|
|
|
1,
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
False,
|
|
|
|
'command: terraform import -no-color aws_instance.foo i-abcd1234',
|
|
|
|
''
|
|
|
|
],
|
|
|
|
# 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,
|
2017-08-04 23:17:52 +00:00
|
|
|
'command: terraform import -no-color aws_instance.foo i-abcd1234',
|
|
|
|
''
|
2017-08-08 21:58:33 +00:00
|
|
|
],
|
|
|
|
# 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),
|
|
|
|
'',
|
|
|
|
0,
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
False,
|
2017-08-08 21:58:33 +00:00
|
|
|
'',
|
|
|
|
'var_to_output'
|
2019-06-25 18:53:39 +00:00
|
|
|
],
|
|
|
|
# test workspace command (commands with subcommand)
|
|
|
|
[
|
|
|
|
lambda x: x.cmd('workspace', 'show', no_color=IsFlagged),
|
|
|
|
'',
|
|
|
|
0,
|
|
|
|
False,
|
|
|
|
'command: terraform workspace show -no-color',
|
|
|
|
''
|
|
|
|
],
|
2016-11-18 07:35:14 +00:00
|
|
|
]
|
|
|
|
]
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2017-01-04 05:21:30 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-12-20 16:55:55 +00:00
|
|
|
def fmt_test_file(request):
|
|
|
|
target = os.path.join(current_path, 'bad_fmt', 'test.backup')
|
|
|
|
orgin = os.path.join(current_path, 'bad_fmt', 'test.tf')
|
|
|
|
shutil.copy(orgin,
|
|
|
|
target)
|
|
|
|
|
|
|
|
def td():
|
|
|
|
shutil.move(target, orgin)
|
|
|
|
|
|
|
|
request.addfinalizer(td)
|
|
|
|
return
|
|
|
|
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2017-05-09 09:47:36 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def string_logger(request):
|
|
|
|
log_stream = StringIO()
|
|
|
|
handler = logging.StreamHandler(log_stream)
|
|
|
|
root_logger.addHandler(handler)
|
|
|
|
|
|
|
|
def td():
|
|
|
|
root_logger.removeHandler(handler)
|
2017-08-04 21:14:57 +00:00
|
|
|
log_stream.close()
|
2017-05-09 09:47:36 +00:00
|
|
|
|
|
|
|
request.addfinalizer(td)
|
2017-08-04 21:14:57 +00:00
|
|
|
return lambda: str(log_stream.getvalue())
|
2017-05-09 09:47:36 +00:00
|
|
|
|
|
|
|
|
2019-06-25 18:53:39 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def workspace_setup_teardown():
|
|
|
|
"""
|
|
|
|
Fixture used in workspace related tests
|
|
|
|
|
|
|
|
Create and tear down a workspace
|
|
|
|
*Use as a contextmanager*
|
|
|
|
"""
|
|
|
|
@contextmanager
|
|
|
|
def wrapper(workspace_name, create=True, delete=True, *args, **kwargs):
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
tf.init()
|
|
|
|
if create:
|
|
|
|
tf.create_workspace(workspace_name, *args, **kwargs)
|
|
|
|
yield tf
|
|
|
|
if delete:
|
|
|
|
tf.set_workspace('default')
|
|
|
|
tf.delete_workspace(workspace_name)
|
|
|
|
|
|
|
|
yield wrapper
|
|
|
|
|
|
|
|
|
2016-12-20 10:53:01 +00:00
|
|
|
class TestTerraform(object):
|
2016-11-18 07:35:14 +00:00
|
|
|
def teardown_method(self, method):
|
|
|
|
""" teardown any state that was previously setup with a setup_method
|
|
|
|
call.
|
|
|
|
"""
|
2017-08-22 20:12:13 +00:00
|
|
|
exclude = ['test_tfstate_file',
|
|
|
|
'test_tfstate_file2',
|
|
|
|
'test_tfstate_file3']
|
2016-11-18 07:35:14 +00:00
|
|
|
|
|
|
|
def purge(dir, pattern):
|
2017-08-04 23:02:49 +00:00
|
|
|
for root, dirnames, filenames in os.walk(dir):
|
2017-08-22 20:12:13 +00:00
|
|
|
dirnames[:] = [d for d in dirnames if d not in exclude]
|
2017-08-04 23:02:49 +00:00
|
|
|
for filename in fnmatch.filter(filenames, pattern):
|
|
|
|
f = os.path.join(root, filename)
|
|
|
|
os.remove(f)
|
|
|
|
for dirname in fnmatch.filter(dirnames, pattern):
|
|
|
|
d = os.path.join(root, dirname)
|
|
|
|
shutil.rmtree(d)
|
|
|
|
|
|
|
|
purge('.', '*.tfstate')
|
2017-08-22 20:12:13 +00:00
|
|
|
purge('.', '*.tfstate.backup')
|
2017-08-04 23:02:49 +00:00
|
|
|
purge('.', '*.terraform')
|
2017-08-08 21:58:33 +00:00
|
|
|
purge('.', FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS)
|
2016-11-18 07:35:14 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize([
|
|
|
|
"method", "expected"
|
|
|
|
], STRING_CASES)
|
|
|
|
def test_generate_cmd_string(self, method, expected):
|
2016-12-20 11:58:04 +00:00
|
|
|
tf = Terraform(working_dir=current_path)
|
2016-11-18 07:35:14 +00:00
|
|
|
result = method(tf)
|
|
|
|
|
|
|
|
strs = expected.split()
|
|
|
|
for s in strs:
|
|
|
|
assert s in result
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(*CMD_CASES)
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
def test_cmd(self, method, expected_output, expected_ret_code, expected_exception, expected_logs, string_logger, folder):
|
2016-11-24 07:13:43 +00:00
|
|
|
tf = Terraform(working_dir=current_path)
|
2017-08-04 23:17:52 +00:00
|
|
|
tf.init(folder)
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
try:
|
2017-10-16 16:03:45 +00:00
|
|
|
ret, out, err = method(tf)
|
|
|
|
assert not expected_exception
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
except TerraformCommandError as e:
|
2017-10-16 16:03:45 +00:00
|
|
|
assert expected_exception
|
|
|
|
ret = e.returncode
|
|
|
|
out = e.out
|
|
|
|
err = e.err
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
|
2017-08-04 21:14:57 +00:00
|
|
|
logs = string_logger()
|
2017-05-09 09:47:36 +00:00
|
|
|
logs = logs.replace('\n', '')
|
2017-10-13 21:20:36 +00:00
|
|
|
if isinstance(expected_output, list):
|
|
|
|
ok = False
|
|
|
|
for xo in expected_output:
|
|
|
|
if xo in out:
|
|
|
|
ok = True
|
|
|
|
break
|
|
|
|
if not ok:
|
2017-10-16 16:03:45 +00:00
|
|
|
assert expected_output[0] in out
|
2017-10-13 21:20:36 +00:00
|
|
|
else:
|
2017-10-16 16:03:45 +00:00
|
|
|
assert expected_output in out
|
2017-05-09 07:39:09 +00:00
|
|
|
assert expected_ret_code == ret
|
2017-05-09 09:47:36 +00:00
|
|
|
assert expected_logs in logs
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2016-12-20 10:53:01 +00:00
|
|
|
@pytest.mark.parametrize(
|
2017-01-03 15:09:23 +00:00
|
|
|
("folder", "variables", "var_files", "expected_output", "options"),
|
2016-12-20 10:53:01 +00:00
|
|
|
[
|
2017-01-03 15:09:23 +00:00
|
|
|
("var_to_output",
|
|
|
|
{'test_var': 'test'}, None, "test_output=test", {}),
|
|
|
|
("var_to_output", {'test_list_var': ['c', 'd']}, None, "test_list_output=[c,d]", {}),
|
|
|
|
("var_to_output", {'test_map_var': {"c": "c", "d": "d"}}, None, "test_map_output={a=ab=bc=cd=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}", {}),
|
|
|
|
("var_to_output", {}, None, "\x1b[0m\x1b[1m\x1b[32mApplycomplete!", {"no_color": IsNotFlagged})
|
2016-12-20 10:53:01 +00:00
|
|
|
])
|
2017-01-03 15:09:23 +00:00
|
|
|
def test_apply(self, folder, variables, var_files, expected_output, options):
|
2016-12-20 11:58:04 +00:00
|
|
|
tf = Terraform(working_dir=current_path, variables=variables, var_file=var_files)
|
2017-08-04 23:06:55 +00:00
|
|
|
# after 0.10.0 we always need to init
|
|
|
|
tf.init(folder)
|
2017-01-03 15:09:23 +00:00
|
|
|
ret, out, err = tf.apply(folder, **options)
|
2016-12-20 10:53:01 +00:00
|
|
|
assert ret == 0
|
|
|
|
assert expected_output in out.replace('\n', '').replace(' ', '')
|
|
|
|
assert err == ''
|
|
|
|
|
2019-06-25 18:53:39 +00:00
|
|
|
def test_apply_with_var_file(self, string_logger):
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
|
|
|
|
tf.init()
|
|
|
|
tf.apply(var_file=os.path.join(current_path, 'tfvar_file', 'test.tfvars'))
|
|
|
|
logs = string_logger()
|
2019-06-25 20:33:38 +00:00
|
|
|
logs = logs.split('\n')
|
|
|
|
for log in logs:
|
|
|
|
if log.startswith('command: terraform apply'):
|
|
|
|
assert log.count('-var-file=') == 1
|
2019-06-25 18:53:39 +00:00
|
|
|
|
2016-12-20 16:43:44 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
['cmd', 'args', 'options'],
|
|
|
|
[
|
|
|
|
# bool value
|
|
|
|
('fmt', ['bad_fmt'], {'list': False, 'diff': False})
|
|
|
|
]
|
|
|
|
)
|
2016-12-20 16:55:55 +00:00
|
|
|
def test_options(self, cmd, args, options, fmt_test_file):
|
2016-12-20 16:43:44 +00:00
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
ret, out, err = getattr(tf, cmd)(*args, **options)
|
|
|
|
assert ret == 0
|
|
|
|
assert out == ''
|
2016-12-20 10:53:01 +00:00
|
|
|
|
2016-11-18 07:35:14 +00:00
|
|
|
def test_state_data(self):
|
2016-11-24 07:13:43 +00:00
|
|
|
cwd = os.path.join(current_path, 'test_tfstate_file')
|
|
|
|
tf = Terraform(working_dir=cwd, state='tfstate.test')
|
2016-11-18 07:35:14 +00:00
|
|
|
tf.read_state_file()
|
|
|
|
assert tf.tfstate.modules[0]['path'] == ['root']
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2017-08-22 20:12:13 +00:00
|
|
|
def test_state_default(self):
|
|
|
|
cwd = os.path.join(current_path, 'test_tfstate_file2')
|
|
|
|
tf = Terraform(working_dir=cwd)
|
|
|
|
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)
|
|
|
|
tf.read_state_file()
|
|
|
|
assert tf.tfstate.modules[0]['path'] == ['default_backend']
|
|
|
|
|
2016-12-20 10:53:01 +00:00
|
|
|
def test_pre_load_state_data(self):
|
|
|
|
cwd = os.path.join(current_path, 'test_tfstate_file')
|
|
|
|
tf = Terraform(working_dir=cwd, state='tfstate.test')
|
|
|
|
assert tf.tfstate.modules[0]['path'] == ['root']
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2016-12-20 10:53:01 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("folder", 'variables'),
|
|
|
|
[
|
|
|
|
("var_to_output", {'test_var': 'test'})
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_override_default(self, folder, variables):
|
2016-12-20 11:58:04 +00:00
|
|
|
tf = Terraform(working_dir=current_path, variables=variables)
|
2017-08-04 23:17:52 +00:00
|
|
|
tf.init(folder)
|
2016-12-20 10:53:01 +00:00
|
|
|
ret, out, err = tf.apply(folder, var={'test_var': 'test2'},
|
2016-11-24 07:13:43 +00:00
|
|
|
no_color=IsNotFlagged)
|
|
|
|
out = out.replace('\n', '')
|
|
|
|
assert '\x1b[0m\x1b[1m\x1b[32mApply' in out
|
2017-01-04 05:21:30 +00:00
|
|
|
out = tf.output('test_output')
|
|
|
|
assert 'test2' in out
|
2016-11-24 07:13:43 +00:00
|
|
|
|
2017-08-04 21:14:57 +00:00
|
|
|
@pytest.mark.parametrize(
|
2017-08-04 22:36:11 +00:00
|
|
|
("param"),
|
2017-08-04 21:14:57 +00:00
|
|
|
[
|
2017-08-04 22:36:11 +00:00
|
|
|
({}),
|
|
|
|
({'module': 'test2'}),
|
2017-08-04 21:14:57 +00:00
|
|
|
]
|
|
|
|
)
|
2017-08-04 22:36:11 +00:00
|
|
|
def test_output(self, param, string_logger):
|
2016-12-20 11:58:04 +00:00
|
|
|
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
|
2017-08-04 23:17:52 +00:00
|
|
|
tf.init('var_to_output')
|
2016-12-20 10:53:01 +00:00
|
|
|
tf.apply('var_to_output')
|
2017-08-04 21:14:57 +00:00
|
|
|
result = tf.output('test_output', **param)
|
2017-08-08 21:58:33 +00:00
|
|
|
regex = re.compile("terraform output (-module=test2 -json|-json -module=test2) test_output")
|
2017-08-04 22:36:11 +00:00
|
|
|
log_str = string_logger()
|
2017-08-04 21:14:57 +00:00
|
|
|
if param:
|
2017-08-04 22:36:11 +00:00
|
|
|
assert re.search(regex, log_str), log_str
|
2017-08-04 21:14:57 +00:00
|
|
|
else:
|
|
|
|
assert result == 'test'
|
2016-11-19 10:24:33 +00:00
|
|
|
|
Add full support for 'output' command, and enable raise_on_error option
Add a general "raise_on_error" option to all terraform commands. If provided and
set to anything that evaluates to True, then TerraformCommandError (a subclass of
subprocess.CalledProcessError) will be raised if the returncode is not 0. The exception
object will have the following special proerties:
returncode: The returncode from the command, as in subprocess.CalledProcessError.
out: The contents of stdout if available, otherwise None
err: The contents of stderr if available, otherwise None
Terraform.output() no longer requires an argument for the output name; if omitted, it
returns a dict of all outputs, exactly as expected from 'terraform output -json'.
Terraform.output() now accepts an optional "full_value" option. If provided and True, and
an output name was provided, then the return value will be a dict with "value", "type",
and "sensitive" fields, exactly as expected from 'terraform output -json <output-name>'
Added tests for all of this new functionality...
2017-10-13 20:36:25 +00:00
|
|
|
@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:
|
|
|
|
assert result['test_output']['value'] == 'test'
|
|
|
|
|
2016-11-19 10:24:33 +00:00
|
|
|
def test_destroy(self):
|
2016-12-20 11:58:04 +00:00
|
|
|
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
|
2017-08-04 23:17:52 +00:00
|
|
|
tf.init('var_to_output')
|
2016-12-20 10:53:01 +00:00
|
|
|
ret, out, err = tf.destroy('var_to_output')
|
2016-11-19 10:24:33 +00:00
|
|
|
assert ret == 0
|
|
|
|
assert 'Destroy complete! Resources: 0 destroyed.' in out
|
2016-12-20 10:53:01 +00:00
|
|
|
|
2017-01-04 05:21:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("plan", "variables", "expected_ret"),
|
|
|
|
[
|
|
|
|
('vars_require_input', {}, 1)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_plan(self, plan, variables, expected_ret):
|
|
|
|
tf = Terraform(working_dir=current_path, variables=variables)
|
|
|
|
ret, out, err = tf.plan(plan)
|
2017-01-04 15:46:15 +00:00
|
|
|
assert ret == expected_ret
|
2017-01-04 05:21:30 +00:00
|
|
|
|
|
|
|
def test_fmt(self, fmt_test_file):
|
2016-12-20 11:58:04 +00:00
|
|
|
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
|
2016-12-20 10:53:01 +00:00
|
|
|
ret, out, err = tf.fmt(diff=True)
|
|
|
|
assert ret == 0
|
2017-05-09 07:39:09 +00:00
|
|
|
|
2017-05-09 09:47:36 +00:00
|
|
|
def test_import(self, string_logger):
|
2017-05-09 09:51:52 +00:00
|
|
|
tf = Terraform(working_dir=current_path)
|
2017-05-09 09:47:36 +00:00
|
|
|
tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged)
|
2017-08-04 21:14:57 +00:00
|
|
|
assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in string_logger()
|
2019-05-07 18:39:07 +00:00
|
|
|
|
2019-06-25 18:53:39 +00:00
|
|
|
def test_create_workspace(self, workspace_setup_teardown):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name, create=False) as tf:
|
|
|
|
ret, out, err = tf.create_workspace('test')
|
2019-05-07 18:39:07 +00:00
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
|
|
|
|
2019-06-25 18:53:39 +00:00
|
|
|
def test_create_workspace_with_args(
|
|
|
|
self, workspace_setup_teardown, string_logger
|
|
|
|
):
|
|
|
|
workspace_name = 'test'
|
2019-06-25 20:33:38 +00:00
|
|
|
state_file_path = os.path.join(current_path, 'test_tfstate_file2', 'terraform.tfstate')
|
2019-06-25 18:53:39 +00:00
|
|
|
with workspace_setup_teardown(workspace_name, create=False) as tf:
|
2019-06-25 21:12:41 +00:00
|
|
|
ret, out, err = tf.create_workspace('test', current_path, no_color=IsFlagged)
|
2019-06-25 18:53:39 +00:00
|
|
|
|
2019-05-07 18:39:07 +00:00
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
|
|
|
|
2019-06-25 18:53:39 +00:00
|
|
|
logs = string_logger()
|
|
|
|
logs = logs.replace('\n', '')
|
2019-06-25 21:12:41 +00:00
|
|
|
expected_log = 'command: terraform workspace new -no-color test {}'.format(current_path)
|
2019-06-25 18:53:39 +00:00
|
|
|
assert expected_log in logs
|
|
|
|
|
|
|
|
def test_set_workspace(self, workspace_setup_teardown):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf:
|
|
|
|
ret, out, err = tf.set_workspace(workspace_name)
|
2019-05-07 18:39:07 +00:00
|
|
|
assert ret == 0
|
2019-05-20 18:34:42 +00:00
|
|
|
assert err == ''
|
|
|
|
|
2019-06-25 18:53:39 +00:00
|
|
|
def test_set_workspace_with_args(
|
|
|
|
self, workspace_setup_teardown, string_logger):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf:
|
|
|
|
ret, out, err = tf.set_workspace(workspace_name, current_path, no_color=IsFlagged)
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
logs = logs.replace('\n', '')
|
|
|
|
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):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf:
|
|
|
|
ret, out, err = tf.show_workspace()
|
2019-05-20 18:34:42 +00:00
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
2019-06-25 18:53:39 +00:00
|
|
|
|
|
|
|
def test_show_workspace_with_no_color(
|
|
|
|
self, workspace_setup_teardown, string_logger
|
|
|
|
):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf:
|
|
|
|
ret, out, err = tf.show_workspace(no_color=IsFlagged)
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
|
|
|
|
|
|
|
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):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name, delete=False) as tf:
|
|
|
|
tf.set_workspace('default')
|
|
|
|
ret, out, err = tf.delete_workspace(workspace_name)
|
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
|
|
|
|
|
|
|
def test_delete_workspace_with_args(
|
|
|
|
self, workspace_setup_teardown, string_logger
|
|
|
|
):
|
|
|
|
workspace_name = 'test'
|
|
|
|
with workspace_setup_teardown(workspace_name, delete=False) as tf:
|
|
|
|
tf.set_workspace('default')
|
|
|
|
ret, out, err = tf.delete_workspace(
|
2019-06-25 21:12:41 +00:00
|
|
|
workspace_name, current_path, force=IsFlagged,
|
2019-06-25 18:53:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
assert err == ''
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
logs = logs.replace('\n', '')
|
2019-06-25 21:12:41 +00:00
|
|
|
expected_log = 'command: terraform workspace delete -force test {}'.format(current_path)
|
2019-06-25 18:53:39 +00:00
|
|
|
assert expected_log in logs
|