def test_single_parse(): c1 = tf.Context() c1 += tf.many_from_hcl(""" terraform { foo = "bar" } """) c2 = tf.Context() c2 += tf.Terraform({'foo': 'bar'}) assert c1.data == c2.data
def test_implicit_resource(): a = tf.Context() a.resource.a.b = {'k': 'v1'} a.resource.a.c = {'k': 'v2'} b = tf.Context() b += tf.Resource("a", "b", {'k': 'v1'}) b += tf.Resource("a", "c", {'k': 'v2'}) assert a.data == b.data
def test_outputs(): c = tf.Context() c += tf.outputs( a='b', c='${var.c}') assert {'output': {'a': {'value': 'b'}, 'c': {'value': '${var.c}'}}} == c.data
def test_hcl_nested_named_block(): c = tf.Context() c += tf.many_from_hcl(""" terraform { backend "foo" { foo = "bar" bar = ["baz"] map = { a = "b" } } } """) assert { 'terraform': { 'backend': { 'foo': { 'foo': 'bar', 'bar': ['baz'], 'map': { 'a': 'b' } } } } } == c.data
def test_duplicate_terraform(): t1 = tf.Terraform(dict(var1='val1')) t2 = tf.Terraform(dict(var2='val2')) c = tf.Context(strict=True) c += t1 with pytest.raises(tf.DuplicateBlockError): c += t2
def test_duplicate_resource(): r1 = tf.Resource("type", "name", dict(var1='val1')) r2 = tf.Resource("type", "name", dict(var2='val2')) c = tf.Context(strict=True) c += r1 with pytest.raises(tf.DuplicateBlockError): c += r2
def test_duplicate_variable(): v1 = tf.Variable("var1", dict(default=1)) v2 = tf.Variable("var1", dict(other=42)) c = tf.Context(strict=True) c += v1 with pytest.raises(tf.DuplicateBlockError): c += v2
def test_module(): c = tf.Context() c += tf.Module('foo1', { 'source': '../../module/', 'list_of_things': [1, 2, 3] }) assert {'module': {'foo1': {'source': '../../module/', 'list_of_things': [1, 2, 3]}}} == c.data
def test_variables(): c = tf.Context() c += tf.variables( foo=42, bar='whatever' ) assert {'variable': {'foo': {'default': 42}, 'bar': {'default': 'whatever'}}} == c.data
def test_share_resource(): t = tf.Variable("var1", dict(default=1)) c = tf.Context() c += t t.name = 'var2' c += t assert c.data == {'variable': {'var2': {'default': 1}}}
def test_create_output_dir(fixtures_dir): output_dir = fixtures_dir / 'generator_data' / 'simple' / 'does_not_exist' output_file = output_dir / 'output.tf.json' shutil.rmtree(str(output_dir), ignore_errors=True) c = tf.Context(output=output_file) c += tf.Terraform(dict(foo=42)) c.render() assert output_file.exists()
def test_copy_resource(): t = tf.Variable("var1", dict(default=1)) c = tf.Context() c += deepcopy(t) t.name = 'var2' c += t assert c.data == {'variable': {'var1': {'default': 1}, 'var2': {'default': 1}}}
def test_multiple_variables(): c = tf.Context() c += tf.Variable('foo', dict( type='string' )) c += tf.Variable('bar', dict( default='whatever' )) assert {'variable': {'foo': {'type': 'string'}, 'bar': {'default': 'whatever'}}} == c.data
def test_hcl_as_object(): c1 = tf.Context() c1 += tf.many_from_hcl(""" terraform { foo = "bar" } resource "type" "name" { whatever { here = "there" } } """) c2 = tf.Context() c2 += tf.Terraform({'foo': 'bar'}) c2 += tf.Resource('type', 'name', { 'whatever': { 'here': 'there' } }) assert c1.data == c2.data
def test_many_from_hcl_order(): c = tf.Context() many = tf.many_from_hcl(""" variable "" { default = "1" } variable "a" { default = "1" } variable "a/b" { default = "1" } variable "a/b/c" { default = "1" } """) assert [tf.Variable(name="", body={'default': '1'}), tf.Variable(name="a", body={'default': '1'}), tf.Variable(name="a/b", body={'default': '1'}), tf.Variable(name="a/b/c", body={'default': '1'})] == many
def test_resource(): c = tf.Context() c += tf.Resource("type", "name", dict( var='value' )) assert {'resource': {'type': {'name': {'var': 'value'}}}} == c.data
def test_hcl_as_data(hcl, data): c1 = tf.Context() c1 += tf.many_from_hcl(hcl) assert data == c1.data
def test_iadd(): a = tf.Context() assert {} == a.data a += tf.Variable('foo', {'type': 'string'}) assert {'variable': {'foo': {'type': 'string'}}} == a.data
def test_init(): c = tf.Context() assert c.data == {}
def test_implicit_variable2(): c = tf.Context() c.variable["name"] = 'foo' assert {'variable': {'name': {'default': 'foo'}}} == c.data
def test_implicit_module2(): c = tf.Context() c.module["name"] = {'source': 'here'} assert {'module': {'name': {'source': 'here'}}} == c.data
def test_implicit_module1(): c = tf.Context() c.module.name = {} assert {'module': {'name': {}}} == c.data
def test_variable1(): c = tf.Context() c += tf.Variable('foo') assert {'variable': {'foo': {}}} == c.data
def test_variable2(): c = tf.Context() c += tf.Variable('foo', dict( type='string' )) assert {'variable': {'foo': {'type': 'string'}}} == c.data