Esempio n. 1
0
def test_environment_are_all_roles():
    host1 = Host("host1", "127.0.0.1")
    host2 = Host("host2", "127.0.0.1")

    role1 = Role("role1", [host1, host2])
    role2 = Role("role2", [host2])

    test_env = Environment("test_env_are_all_roles", [role1, role2])
    assert test_env.are_all_roles([role1, role2])
    assert not test_env.are_all_roles([role1])
Esempio n. 2
0
def test_finalize_environment():
    host1 = Host("host1", "127.0.0.1")
    host2 = Host("host2", "127.0.0.1")

    role1 = Role("role1", [host1, host2])
    role2 = Role("role2", [host2])

    test_env = Environment("test_env_finalize_environment", [role1, role2])
    Environment.finalize_environment("test_env_finalize_environment")

    roledefs = dict()
    for role in test_env.roles:
        roledefs[role.name] = {
            "hosts": [host.full_address for host in role.hosts]
        }
    assert env.roledefs == roledefs
Esempio n. 3
0
def test_create_environment_with_duplicate_name_will_raise():
    localhost = Host("localhost", "127.0.0.1")
    test_role = Role("test", [localhost])
    Environment("test_env_dup", [test_role], aabb="aabb", attr="attr")

    with pytest.raises(Exception):
        Environment("test_env_dup", [test_role], aabb="aabb", attr="attr")
Esempio n. 4
0
def test_create_environment():
    localhost = Host("localhost", "127.0.0.1")
    test_role = Role("test", [localhost])
    test_env = Environment("test_env", [test_role], aabb="aabb", attr="attr")

    assert Environment.get_environment("test_env") is test_env

    assert test_env.aabb == "aabb"
    assert test_env.attr == "attr"
    with pytest.raises(AttributeError):
        assert test_env.aabb_attr is None

    assert repr(test_env) == "Environment(\"test_env\")"
Esempio n. 5
0
def test_host_role_relationship():
    host1 = Host("host1", "127.0.0.1")
    host2 = Host("host2", "127.0.0.1")

    role1 = Role("role1", [host1, host2])
    role2 = Role("role2", [host2])

    assert host1.has_role(role1)
    assert not host1.has_role(role2)

    assert host2.has_role(role1)
    assert host2.has_role(role2)

    assert role1.has_host(host1)
    assert role1.has_host(host2)

    assert not role2.has_host(host1)
    assert role2.has_host(host2)

    role3 = Role("role3", [])
    role3.add_host(host1)
    host2.add_role(role3)
    assert host1.has_role(role3)
    assert host2.has_role(role3)
    assert role3.has_host(host1)
    assert role3.has_host(host2)
Esempio n. 6
0
def test_create_role():
    host = Host("localhost", "127.0.0.1")
    role = Role("test", [host])
    assert role.name == "test"
    assert role.hosts == {host}
    assert repr(role) == "Role(\"test\")"
Esempio n. 7
0
def test_create_host():
    host = Host("localhost", "127.0.0.1")
    assert host.name == "localhost"
    assert host.address == "127.0.0.1"
    assert repr(host) == "Host(\"localhost\", \"127.0.0.1\")"
Esempio n. 8
0
def fab_task():
    file_path = generate_random_file_path()
    run("touch {0}".format(file_path))
    assert exists(file_path)
    run("rm {0}".format(file_path))


def fab_sudo_task():
    file_path = generate_random_file_path()
    sudo("touch {0}".format(file_path))
    assert exists(file_path)
    sudo("rm {0}".format(file_path))


setup_key()
host = Host("host", "127.0.0.1", user=getpass.getuser())
host2 = Host("host2", "localhost", user=getpass.getuser())
role = Role("role", [host, host2])
role2 = Role("role2", [host2])
environment = Environment("test", [role, role2])


def test_run_per_host():
    fab_task_ = run_per_host(fab_task)
    fab_task_("test", "all")


def test_run_per_host_ask_input():
    fab_task_ = run_per_host(inputs=["sudo password:"******"builtins.input",
                    lambda x: os.environ.get("sudo_password")):