Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
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
Beispiel #9
0
def test_variables():
    c = tf.Context()
    c += tf.variables(
        foo=42,
        bar='whatever'
    )
    assert {'variable': {'foo': {'default': 42},
                         'bar': {'default': 'whatever'}}} == c.data
Beispiel #10
0
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}}}
Beispiel #11
0
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()
Beispiel #12
0
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}}}
Beispiel #13
0
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
Beispiel #14
0
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
Beispiel #15
0
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
Beispiel #16
0
def test_resource():
    c = tf.Context()
    c += tf.Resource("type", "name", dict(
        var='value'
    ))
    assert {'resource': {'type': {'name': {'var': 'value'}}}} == c.data
Beispiel #17
0
def test_hcl_as_data(hcl, data):
    c1 = tf.Context()
    c1 += tf.many_from_hcl(hcl)
    assert data == c1.data
Beispiel #18
0
def test_iadd():
    a = tf.Context()
    assert {} == a.data
    a += tf.Variable('foo', {'type': 'string'})
    assert {'variable': {'foo': {'type': 'string'}}} == a.data
Beispiel #19
0
def test_init():
    c = tf.Context()
    assert c.data == {}
Beispiel #20
0
def test_implicit_variable2():
    c = tf.Context()

    c.variable["name"] = 'foo'

    assert {'variable': {'name': {'default': 'foo'}}} == c.data
Beispiel #21
0
def test_implicit_module2():
    c = tf.Context()

    c.module["name"] = {'source': 'here'}

    assert {'module': {'name': {'source': 'here'}}} == c.data
Beispiel #22
0
def test_implicit_module1():
    c = tf.Context()

    c.module.name = {}

    assert {'module': {'name': {}}} == c.data
Beispiel #23
0
def test_variable1():
    c = tf.Context()
    c += tf.Variable('foo')
    assert {'variable': {'foo': {}}} == c.data
Beispiel #24
0
def test_variable2():
    c = tf.Context()
    c += tf.Variable('foo', dict(
        type='string'
    ))
    assert {'variable': {'foo': {'type': 'string'}}} == c.data