2016-11-19 10:24:33 +00:00
|
|
|
from python_terraform import *
|
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
|
2016-11-18 07:35:14 +00:00
|
|
|
|
2016-12-20 10:53:01 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2017-05-09 07:39:09 +00:00
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
logging.getLogger().addHandler(ch)
|
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
|
|
|
|
|
|
|
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 = [
|
2017-05-09 07:39:09 +00:00
|
|
|
['method', 'expected_output', 'expected_ret_code'],
|
2016-11-18 07:35:14 +00:00
|
|
|
[
|
|
|
|
[
|
2016-12-20 10:53:01 +00:00
|
|
|
lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) ,
|
2017-05-09 07:39:09 +00:00
|
|
|
"doesn't need to do anything",
|
|
|
|
0
|
|
|
|
],
|
|
|
|
# try import aws instance
|
|
|
|
[
|
|
|
|
lambda x: x.cmd('import', 'aws_instance.foo', 'i-abcd1234', no_color=IsFlagged),
|
|
|
|
'Import complete!',
|
|
|
|
1
|
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
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def purge(dir, pattern):
|
|
|
|
for f in os.listdir(dir):
|
|
|
|
if re.search(pattern, f):
|
|
|
|
if os.path.isfile(f):
|
|
|
|
os.remove(os.path.join(dir, f))
|
|
|
|
|
|
|
|
purge('.', '.tfstate')
|
|
|
|
|
|
|
|
@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)
|
2017-05-09 07:39:09 +00:00
|
|
|
def test_cmd(self, method, expected_output, expected_ret_code):
|
2016-11-24 07:13:43 +00:00
|
|
|
tf = Terraform(working_dir=current_path)
|
2016-11-18 07:35:14 +00:00
|
|
|
ret, out, err = method(tf)
|
|
|
|
assert expected_output in out
|
2017-05-09 07:39:09 +00:00
|
|
|
assert expected_ret_code == ret
|
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-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 == ''
|
|
|
|
|
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
|
|
|
|
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)
|
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
|
|
|
|
2016-11-18 07:35:14 +00:00
|
|
|
def test_get_output(self):
|
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
|
|
|
tf.apply('var_to_output')
|
2016-11-18 07:35:14 +00:00
|
|
|
assert tf.output('test_output') == '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'})
|
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
|
|
|
|
|
|
|
def test_import(self):
|
|
|
|
tf = Terraform(working_dir=current_path)
|
2017-05-09 07:48:28 +00:00
|
|
|
ret, out, err = tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged)
|
|
|
|
assert 'Import complete!' in out
|
|
|
|
assert 1 == ret
|