use log to test
This commit is contained in:
parent
a6d4ee0316
commit
b922750de0
3 changed files with 45 additions and 16 deletions
7
test/import_test/test.tf
Normal file
7
test/import_test/test.tf
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
provider "archive" {}
|
||||||
|
|
||||||
|
data "archive_file" "init" {
|
||||||
|
type = "zip"
|
||||||
|
source_file = "${path.module}/init.tpl"
|
||||||
|
output_path = "${path.module}/init.zip"
|
||||||
|
}
|
|
@ -1,3 +1,7 @@
|
||||||
|
try:
|
||||||
|
from cStringIO import StringIO # Python 2
|
||||||
|
except ImportError:
|
||||||
|
from io import StringIO
|
||||||
from python_terraform import *
|
from python_terraform import *
|
||||||
import pytest
|
import pytest
|
||||||
import os
|
import os
|
||||||
|
@ -6,8 +10,9 @@ import re
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
ch = logging.StreamHandler(sys.stdout)
|
root_logger = logging.getLogger()
|
||||||
logging.getLogger().addHandler(ch)
|
# ch = logging.StreamHandler(sys.stdout)
|
||||||
|
# root_logger.addHandler(ch)
|
||||||
current_path = os.path.dirname(os.path.realpath(__file__))
|
current_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
STRING_CASES = [
|
STRING_CASES = [
|
||||||
|
@ -25,18 +30,20 @@ STRING_CASES = [
|
||||||
]
|
]
|
||||||
|
|
||||||
CMD_CASES = [
|
CMD_CASES = [
|
||||||
['method', 'expected_output', 'expected_ret_code'],
|
['method', 'expected_output', 'expected_ret_code', 'expected_logs'],
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) ,
|
lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) ,
|
||||||
"doesn't need to do anything",
|
"doesn't need to do anything",
|
||||||
0
|
0,
|
||||||
|
''
|
||||||
],
|
],
|
||||||
# try import aws instance
|
# try import aws instance
|
||||||
[
|
[
|
||||||
lambda x: x.cmd('import', 'aws_instance.foo', 'i-abcd1234', no_color=IsFlagged),
|
lambda x: x.cmd('import', 'aws_instance.foo', 'i-abcd1234', no_color=IsFlagged),
|
||||||
'Import complete!',
|
'',
|
||||||
1
|
1,
|
||||||
|
'command: terraform import -no-color aws_instance.foo i-abcd1234'
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
@ -56,6 +63,19 @@ def fmt_test_file(request):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def string_logger(request):
|
||||||
|
log_stream = StringIO()
|
||||||
|
handler = logging.StreamHandler(log_stream)
|
||||||
|
root_logger.addHandler(handler)
|
||||||
|
|
||||||
|
def td():
|
||||||
|
root_logger.removeHandler(handler)
|
||||||
|
|
||||||
|
request.addfinalizer(td)
|
||||||
|
return log_stream
|
||||||
|
|
||||||
|
|
||||||
class TestTerraform(object):
|
class TestTerraform(object):
|
||||||
def teardown_method(self, method):
|
def teardown_method(self, method):
|
||||||
""" teardown any state that was previously setup with a setup_method
|
""" teardown any state that was previously setup with a setup_method
|
||||||
|
@ -82,12 +102,14 @@ class TestTerraform(object):
|
||||||
assert s in result
|
assert s in result
|
||||||
|
|
||||||
@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, expected_logs, string_logger):
|
||||||
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)
|
||||||
|
logs = str(string_logger.getvalue())
|
||||||
|
logs = logs.replace('\n', '')
|
||||||
assert expected_output in out
|
assert expected_output in out
|
||||||
assert expected_ret_code == ret
|
assert expected_ret_code == ret
|
||||||
|
assert expected_logs in logs
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("folder", "variables", "var_files", "expected_output", "options"),
|
("folder", "variables", "var_files", "expected_output", "options"),
|
||||||
|
@ -172,9 +194,9 @@ class TestTerraform(object):
|
||||||
ret, out, err = tf.fmt(diff=True)
|
ret, out, err = tf.fmt(diff=True)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
def test_import(self):
|
def test_import(self, string_logger):
|
||||||
os.environ['AWS_DEFAULT_REGION'] = "us-west-1"
|
tf = Terraform(working_dir=os.path.join(current_path, 'import_test'))
|
||||||
tf = Terraform(working_dir=current_path)
|
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)
|
logs = string_logger.getvalue()
|
||||||
assert 'Import complete!' in out
|
print(logs)
|
||||||
assert 1 == ret
|
assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in logs
|
||||||
|
|
Loading…
Reference in a new issue