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-11-19 10:24:33 +00:00
|
|
|
logging.basicConfig(level=logging.WARN)
|
2016-11-18 07:35:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
STRING_CASES = [
|
|
|
|
[
|
|
|
|
lambda x: x.generate_cmd_string('apply', 'the_folder',
|
2016-11-19 10:24:33 +00:00
|
|
|
no_color=IsFlagged,
|
2016-11-18 07:35:14 +00:00
|
|
|
var={'a': 'b', 'c': 'd'}),
|
|
|
|
"terraform apply -var='a=b' -var='c=d' -no-color the_folder"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
lambda x: x.generate_cmd_string('push', 'path',
|
|
|
|
var={'a': 'b'}, vcs=True,
|
|
|
|
token='token',
|
|
|
|
atlas_address='url'),
|
|
|
|
"terraform push -var='a=b' -vcs=true -token=token -atlas-address=url path"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
|
|
|
|
CMD_CASES = [
|
|
|
|
['method', 'expected_output'],
|
|
|
|
[
|
|
|
|
[
|
2016-11-19 10:24:33 +00:00
|
|
|
lambda x: x.cmd('plan', 'apply_tf', no_color=IsFlagged, var={'test_var': 'test'}) ,
|
2016-11-18 09:08:10 +00:00
|
|
|
"doesn't need to do anything"
|
2016-11-18 07:35:14 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2015-12-31 07:15:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTerraform:
|
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):
|
|
|
|
tf = Terraform()
|
|
|
|
result = method(tf)
|
|
|
|
|
|
|
|
strs = expected.split()
|
|
|
|
for s in strs:
|
|
|
|
assert s in result
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(*CMD_CASES)
|
|
|
|
def test_cmd(self, method, expected_output):
|
2016-02-25 09:22:11 +00:00
|
|
|
tf = Terraform()
|
2016-11-18 07:35:14 +00:00
|
|
|
ret, out, err = method(tf)
|
|
|
|
assert expected_output in out
|
|
|
|
assert ret == 0
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2016-11-18 07:35:14 +00:00
|
|
|
def test_state_data(self):
|
2016-11-18 07:46:05 +00:00
|
|
|
tf = Terraform(working_dir='test_tfstate_file', 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-11-18 07:35:14 +00:00
|
|
|
def test_apply(self):
|
|
|
|
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
|
2016-11-19 10:24:33 +00:00
|
|
|
ret, out, err = tf.apply(var={'test_var': 'test2'})
|
|
|
|
assert ret == 0
|
2015-12-31 07:15:51 +00:00
|
|
|
|
2016-11-18 07:35:14 +00:00
|
|
|
def test_get_output(self):
|
|
|
|
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
|
|
|
|
tf.apply()
|
|
|
|
assert tf.output('test_output') == 'test'
|
2016-11-19 10:24:33 +00:00
|
|
|
|
|
|
|
def test_destroy(self):
|
|
|
|
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
|
|
|
|
ret, out, err = tf.destroy()
|
|
|
|
assert ret == 0
|
|
|
|
assert 'Destroy complete! Resources: 0 destroyed.' in out
|