Fix unit tests for Terraform 0.13

This commit is contained in:
aubustou 2020-10-21 00:08:58 +02:00
parent 151c5dc92a
commit 6ea1320bef
5 changed files with 31 additions and 51 deletions

View file

@ -90,7 +90,7 @@ class Terraform(object):
self,
dir_or_plan=None,
input=False,
skip_plan=False,
skip_plan=True,
no_color=IsFlagged,
**kwargs,
):
@ -104,10 +104,12 @@ class Terraform(object):
:param kwargs: same as kwags in method 'cmd'
:returns return_code, stdout, stderr
"""
if not skip_plan:
return self.plan(dir_or_plan=dir_or_plan, **kwargs)
default = kwargs
default["input"] = input
default["no_color"] = no_color
default["auto-approve"] = skip_plan is True
default["auto-approve"] = True
option_dict = self._generate_default_options(default)
args = self._generate_default_args(dir_or_plan)
return self.cmd("apply", *args, **option_dict)
@ -358,8 +360,6 @@ class Terraform(object):
dict of named dicts each with 'value', 'sensitive', and 'type',
if NAME is not provided
"""
full_value = kwargs.pop("full_value", False)
name_provided = bool(len(args))
kwargs["json"] = IsFlagged
if not kwargs.get("capture_output", True) is True:
raise ValueError("capture_output is required for this method")
@ -369,14 +369,7 @@ class Terraform(object):
if ret:
return None
out = out.lstrip()
value = json.loads(out)
if name_provided and not full_value:
value = value["value"]
return value
return json.loads(out.lstrip())
def read_state_file(self, file_path=None):
"""Read .tfstate file

View file

@ -1,2 +1,10 @@
[wheel]
universal = 1
[isort]
line_length=88
known_third_party=
indent=' '
multi_line_output=3
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
include_trailing_comma=true

View file

@ -5,12 +5,12 @@ variable "test_var" {
provider "archive" {}
variable "test_list_var" {
type = "list"
type = list(string)
default = ["a", "b"]
}
variable "test_map_var" {
type = "map"
type = map
default = {
"a" = "a"
@ -19,13 +19,13 @@ variable "test_map_var" {
}
output "test_output" {
value = "${var.test_var}"
value = var.test_var
}
output "test_list_output" {
value = "${var.test_list_var}"
value = var.test_list_var
}
output "test_map_output" {
value = "${var.test_map_var}"
value = var.test_map_var
}

View file

@ -1,16 +1,13 @@
try:
from cStringIO import StringIO # Python 2
except ImportError:
from io import StringIO
import fnmatch
import logging
import os
import re
import shutil
from contextlib import contextmanager
from io import StringIO
import pytest
from python_terraform import *
from python_terraform import IsFlagged, IsNotFlagged, Terraform, TerraformCommandError
logging.basicConfig(level=logging.DEBUG)
root_logger = logging.getLogger()
@ -46,10 +43,7 @@ CMD_CASES = [
"plan", "var_to_output", no_color=IsFlagged, var={"test_var": "test"}
),
# 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
"Plan: 0 to add, 0 to change, 0 to destroy.",
0,
False,
"",
@ -231,21 +225,22 @@ class TestTerraform(object):
"var_to_output",
{"test_list_var": ["c", "d"]},
None,
"test_list_output=[c,d]",
'test_list_output=["c","d",]',
{},
),
(
"var_to_output",
{"test_map_var": {"c": "c", "d": "d"}},
None,
"test_map_output={a=ab=bc=cd=d}",
'test_map_output={"c"="c""d"="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}",
# Values are overriden
'test_map_output={"e"="e""f"="f"}',
{},
),
(
@ -261,7 +256,6 @@ class TestTerraform(object):
tf = Terraform(
working_dir=current_path, variables=variables, var_file=var_files
)
# after 0.10.0 we always need to init
tf.init(folder)
ret, out, err = tf.apply(folder, **options)
assert ret == 0
@ -322,7 +316,7 @@ class TestTerraform(object):
tf = Terraform(working_dir=current_path, variables=variables)
tf.init(folder)
ret, out, err = tf.apply(
folder, var={"test_var": "test2"}, no_color=IsNotFlagged
folder, var={"test_var": "test2"}, no_color=IsNotFlagged,
)
out = out.replace("\n", "")
assert "\x1b[0m\x1b[1m\x1b[32mApply" in out
@ -344,21 +338,6 @@ class TestTerraform(object):
else:
assert result == "test"
@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"})

View file

@ -5,12 +5,12 @@ variable "test_var" {
provider "archive" {}
variable "test_list_var" {
type = "list"
type = list(string)
default = ["a", "b"]
}
variable "test_map_var" {
type = "map"
type = map
default = {
"a" = "a"
@ -19,13 +19,13 @@ variable "test_map_var" {
}
output "test_output" {
value = "${var.test_var}"
value = var.test_var
}
output "test_list_output" {
value = "${var.test_list_var}"
value = var.test_list_var
}
output "test_map_output" {
value = "${var.test_map_var}"
value = var.test_map_var
}