Beispiel #1
0
def pytest_configure(config):
    tf.LazyPluginCacheDir.value = cache_dir = config.getoption(
        "dest_tf_plugin")
    if not os.path.exists(cache_dir):
        os.mkdir(cache_dir)

    tf.LazyModuleDir.value = config.getoption(
        "dest_tf_mod_dir") or config.getini("terraform-mod-dir")

    if tf.LazyReplay.value is None:
        tf.LazyReplay.value = config.getoption("dest_tf_replay")

    tf.LazyTfBin.value = config.getoption("dest_tf_binary") or tf.find_binary(
        "terraform")
    if tf.LazyTfBin.value is None and not tf.LazyReplay.value:
        raise ValueError(
            "pytest-terraform requires terraform binary on PATH or "
            "specified with --tf-binary")

    tf.PytestConfig.value = config

    if config.pluginmanager.hasplugin("xdist"):
        config.pluginmanager.register(xdist.XDistTerraform(config))
        tf.terraform.scope_class_map = d = defaultdict(
            lambda: xdist.ScopedTerraformFixture)
        d["function"] = tf.TerraformFixture
Beispiel #2
0
def pytest_configure(config):
    tf.LazyTfBin.value = config.getoption("dest_tf_binary") or tf.find_binary(
        "terraform")
    tf.LazyPluginCacheDir.value = cache_dir = config.getoption(
        "dest_tf_plugin")
    if not os.path.exists(cache_dir):
        os.mkdir(cache_dir)

    tf.LazyReplay.value = config.getoption("dest_tf_replay")
    tf.LazyModuleDir.value = config.getoption(
        "dest_tf_mod_dir") or config.getini("terraform-mod-dir")

    if config.pluginmanager.hasplugin("xdist"):
        config.pluginmanager.register(xdist.XDistTerraform(config))
        tf.terraform.scope_class_map = d = defaultdict(
            lambda: xdist.ScopedTerraformFixture)
        d["function"] = tf.TerraformFixture
def test_tf_runner(testdir, tmpdir):
    # ** requires network access to install plugin **
    with open(tmpdir.join("resources.tf"), "w") as fh:
        fh.write("""
resource "local_file" "foo" {
    content     = "foo!"
    filename = "${path.module}/foo.bar"
}
""")

    trunner = tf.TerraformRunner(tmpdir.strpath,
                                 tf_bin=tf.find_binary("terraform"))
    trunner.init()
    state = trunner.apply()
    assert state.get("foo")["content"] == "foo!"
    with open(tmpdir.join("foo.bar")) as fh:
        assert fh.read() == "foo!"

    trunner.destroy()
    assert False is tmpdir.join("foo.bar").exists()
    statejson = tf.TerraformStateJson("")
    statejson.update_dict(newobj)

    assert statejson.dict == newobj


def test_tf_statejson_update_bad():

    statejson = tf.TerraformStateJson("hello")

    with pytest.raises(ValueError):
        statejson.update({"hello": "world"})


@pytest.mark.skipif(not tf.find_binary("terraform"),
                    reason="Terraform binary missing")
def test_tf_runner(testdir, tmpdir):
    # ** requires network access to install plugin **
    with open(tmpdir.join("resources.tf"), "w") as fh:
        fh.write("""
resource "local_file" "foo" {
    content     = "foo!"
    filename = "${path.module}/foo.bar"
}
""")

    trunner = tf.TerraformRunner(tmpdir.strpath,
                                 tf_bin=tf.find_binary("terraform"))
    trunner.init()
    state = trunner.apply()