Example #1
0
def test_binary_template_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo.bin"): {
                        'encoding': 'latin-1',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    with open(join(str(tmpdir), "bundles", "test", "files", "foo.bin"), 'wb') as f:
        f.write("ö".encode('utf-8'))

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo.bin"), 'rb') as f:
        content = f.read()
    assert content.decode('latin-1') == "ö"
Example #2
0
def test_fault_content_skipped_mako(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "secret"): {
                            'content':
                            "${repo.vault.password_for('test', key='unavailable')}",
                            'content_type': 'mako',
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert not exists(join(str(tmpdir), "secret"))
def test_fault_content_jinja2(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "secret"): {
                        'content': "{{ repo.vault.password_for('test') }}",
                        'content_type': 'jinja2',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "secret")) as f:
        content = f.read()
    assert content == "sQDdTXu5OmCki8gdGgYdfTxooevckXcB"
Example #4
0
def test_skip_trigger(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "foo"): {
                            'content':
                            "nope",
                            'tags': ["nope"],
                            'triggers':
                            ["file:{}".format(join(str(tmpdir), "bar"))],
                        },
                        join(str(tmpdir), "bar"): {
                            'content': "nope",
                            'triggered': True,
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    result = run("bw apply --skip tag:nope -- localhost", path=str(tmpdir))
    assert result[2] == 0
    assert not exists(join(str(tmpdir), "foo"))
    assert not exists(join(str(tmpdir), "bar"))
Example #5
0
def test_fault_content_unavailable_skipped(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {},
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    with open(join(str(tmpdir), "bundles", "test", "items.py"), 'w') as f:
        f.write("""
files = {
    "/tmp/bw_test_faultunavailable": {
        'content': repo.vault.password_for("fault", key="missing"),
    },
}
""")
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert b"file:/tmp/bw_test_faultunavailable skipped (Fault unavailable)" in stdout
    assert not exists("/tmp/bw_test_faultunavailable")
Example #6
0
def test_binary_template_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo.bin"): {
                        'encoding': 'latin-1',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    with open(join(str(tmpdir), "bundles", "test", "files", "foo.bin"),
              'wb') as f:
        f.write("ö".encode('utf-8'))

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo.bin"), 'rb') as f:
        content = f.read()
    assert content.decode('latin-1') == "ö"
Example #7
0
def test_skip_node(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "foo"): {
                            'content': "nope",
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    result = run("bw apply --skip node:localhost -- localhost",
                 path=str(tmpdir))
    assert result[2] == 0
    assert not exists(join(str(tmpdir), "foo"))
Example #8
0
def test_fault_content_mako_metadata(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "secret"): {
                            'content': "${node.metadata['secret']}",
                            'content_type': 'mako',
                        },
                    },
                },
            },
        },
    )

    with open(join(str(tmpdir), "nodes.py"), 'w') as f:
        f.write("""
nodes = {{
    "localhost": {{
        'bundles': ["test"],
        'metadata': {{'secret': vault.password_for("test")}},
        'os': "{}",
    }},
}}
""".format(host_os()))

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "secret")) as f:
        content = f.read()
    assert content == "sQDdTXu5OmCki8gdGgYdfTxooevckXcB"
Example #9
0
def test_action_return_codes(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'actions': {
                        "single-code": {
                            'command': "true",
                            'expected_return_code': 0,
                        },
                        "multi-code-list": {
                            'command': "false",
                            'expected_return_code': [1],
                        },
                        "multi-code-tuple": {
                            'command': "false",
                            'expected_return_code': (1, ),
                        },
                        "multi-code-set": {
                            'command': "false",
                            'expected_return_code': {1},
                        }
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
Example #10
0
def test_skip_group(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content': "nope",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
        groups={
            "foo": {
                'members': ["localhost"]
            },
        },
    )
    result = run("bw apply --skip group:foo localhost", path=str(tmpdir))
    assert result[2] == 0
    assert not exists(join(str(tmpdir), "foo"))
Example #11
0
def test_empty_tags(tmpdir):
    make_repo(
        tmpdir,
        nodes={
            "localhost": {
                'bundles': ["bundle1"],
                'os': host_os(),
            },
        },
        bundles={
            "bundle1": {
                'attrs': {
                    'tags': {
                        "empty": {
                            'needs': {"action:early"},
                        },
                    },
                },
                'items': {
                    'actions': {
                        "early": {
                            'command': "true",
                        },
                        "late": {
                            'command': "true",
                            'needs': {"tag:empty"},
                        },
                    },
                },
            },
        },
    )
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
Example #12
0
def test_add_lock_apply_remove(tmpdir):
    make_repo(
        tmpdir,
        nodes={
            "localhost": {
                'bundles': ["bundle1"],
                'os': host_os(),
            },
        },
        bundles={
            "bundle1": {
                'files': {
                    "/tmp/bw_test_lock_add": {
                        'content': "foo",
                    },
                },
            },
        },
    )
    run("rm -f /tmp/bw_test_lock_add")
    stdout, stderr, rcode = run(
        "BW_IDENTITY=jdoe bw lock add -c höhöhö -e 1m -i file:/tmp/bw_test_lock_add localhost",
        path=str(tmpdir))
    assert rcode == 0
    lock_id = get_lock_id(stdout.decode('utf-8'))
    assert len(lock_id) == 4
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    stdout, stderr, rcode = run("cat /tmp/bw_test_lock_add", path=str(tmpdir))
    assert rcode != 0
    stdout, stderr, rcode = run("bw lock remove localhost {}".format(lock_id),
                                path=str(tmpdir))
    assert rcode == 0
def test_skip_group(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content': "nope",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
        groups={
            "foo": {'members': ["localhost"]},
        },
    )
    result = run("bw apply --skip group:foo localhost", path=str(tmpdir))
    assert result[2] == 0
    assert not exists(join(str(tmpdir), "foo"))
Example #14
0
def test_fault_content_error(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {},
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    with open(join(str(tmpdir), "bundles", "test", "items.py"), 'w') as f:
        f.write("""
files = {{
    "{}": {{
        'content': repo.vault.password_for("test", key='unavailable'),
        'error_on_missing_fault': True,
    }},
}}
""".format(join(str(tmpdir), "secret")))

    stdout, stderr, rcode = run("bw -d apply localhost", path=str(tmpdir))
    print(stdout)
    assert rcode == 1
Example #15
0
def test_fault_content_error(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {},
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    with open(join(str(tmpdir), "bundles", "test", "items.py"), 'w') as f:
        f.write("""
files = {{
    "{}": {{
        'content': repo.vault.password_for("test", key='unavailable'),
        'error_on_missing_fault': True,
    }},
}}
""".format(join(str(tmpdir), "secret")))

    stdout, stderr, rcode = run("bw -d apply localhost", path=str(tmpdir))
    print(stdout)
    assert rcode == 1
Example #16
0
def test_fault_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {},
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    with open(join(str(tmpdir), "bundles", "test", "items.py"), 'w') as f:
        f.write("""
files = {{
    "{}": {{
        'content': repo.vault.password_for("test"),
    }},
}}
""".format(join(str(tmpdir), "secret")))

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "secret")) as f:
        content = f.read()
    assert content == "sQDdTXu5OmCki8gdGgYdfTxooevckXcB"
Example #17
0
def test_fault_content_mako_metadata(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "secret"): {
                        'content': "${node.metadata['secret']}",
                        'content_type': 'mako',
                    },
                },
            },
        },
    )

    with open(join(str(tmpdir), "nodes.py"), 'w') as f:
        f.write("""
nodes = {{
    "localhost": {{
        'bundles': ["test"],
        'metadata': {{'secret': vault.password_for("test")}},
        'os': "{}",
    }},
}}
""".format(host_os()))

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "secret")) as f:
        content = f.read()
    assert content == "sQDdTXu5OmCki8gdGgYdfTxooevckXcB"
Example #18
0
def test_fault_content_jinja2(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "secret"): {
                            'content': "{{ repo.vault.password_for('test') }}",
                            'content_type': 'jinja2',
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "secret")) as f:
        content = f.read()
    assert content == "sQDdTXu5OmCki8gdGgYdfTxooevckXcB"
Example #19
0
def test_fault_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {},
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    with open(join(str(tmpdir), "bundles", "test", "items.py"), 'w') as f:
        f.write("""
files = {{
    "{}": {{
        'content': repo.vault.password_for("test"),
    }},
}}
""".format(join(str(tmpdir), "secret")))

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "secret")) as f:
        content = f.read()
    assert content == "sQDdTXu5OmCki8gdGgYdfTxooevckXcB"
Example #20
0
def test_only_bundle_with_dep(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content_type': 'any',
                        'needs': ["file:" + join(str(tmpdir), "bar")],
                    },
                },
            },
            "test2": {
                'files': {
                    join(str(tmpdir), "bar"): {
                        'content_type': 'any',
                    },
                    join(str(tmpdir), "baz"): {
                        'content_type': 'any',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test", "test2"],
                'os': host_os(),
            },
        },
    )

    run("bw apply -o bundle:test localhost", path=str(tmpdir))
    assert exists(join(str(tmpdir), "foo"))
    assert exists(join(str(tmpdir), "bar"))
    assert not exists(join(str(tmpdir), "baz"))
Example #21
0
def test_any_content_exists(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content_type': 'any',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    with open(join(str(tmpdir), "foo"), 'wb') as f:
        f.write(b"existing content")

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo"), 'rb') as f:
        content = f.read()
    assert content == b"existing content"
Example #22
0
def test_text_template_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "foo"): {
                            'content_type': 'text',
                            'content': "${node.name}",
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo"), 'rb') as f:
        content = f.read()
    assert content == b"${node.name}"
Example #23
0
def test_empty_verify(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content_type': 'any',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    with open(join(str(tmpdir), "foo"), 'w') as f:
        f.write("test")

    stdout, stderr, rcode = run("bw verify localhost", path=str(tmpdir))
    assert rcode == 0
Example #24
0
def test_delete(tmpdir):
    with open(join(str(tmpdir), "foo"), 'w') as f:
        f.write("foo")
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "foo"): {
                            'delete': True,
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    assert not exists(join(str(tmpdir), "foo"))
Example #25
0
def test_mako_template_content_with_secret(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "foo"): {
                            'content_type': 'mako',
                            'content': "${repo.vault.password_for('testing')}",
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo"), 'rb') as f:
        content = f.read()
    assert content == b"faCTT76kagtDuZE5wnoiD1CxhGKmbgiX"
Example #26
0
def test_precedes_action(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'actions': {
                    "action1": {
                        'command': "echo 1 > {}".format(join(str(tmpdir), "file")),
                        'precedes': ["action:action2"],
                        'triggered': True,
                    },
                    "action2": {
                        'command': "echo 2 >> {}".format(join(str(tmpdir), "file")),
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "file")) as f:
        content = f.read()
    assert content == "1\n2\n"
Example #27
0
def test_binary_inline_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "foo.bin"): {
                            'content_type': 'base64',
                            'content': b64encode("ö".encode('latin-1')),
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo.bin"), 'rb') as f:
        content = f.read()
    assert content.decode('latin-1') == "ö"
Example #28
0
def test_fix_dir_target(tmpdir):
    mkdir(join(str(tmpdir), "dir1"))
    mkdir(join(str(tmpdir), "dir2"))
    symlink(join(str(tmpdir), "dir1"), join(str(tmpdir), "link"))
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'symlinks': {
                        join(str(tmpdir), "link"): {
                            'target': join(str(tmpdir), "dir2"),
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert readlink(join(str(tmpdir), "link")) == join(str(tmpdir), "dir2")
Example #29
0
def test_precedes_unless4(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "file"): {
                        'content': "1\n",
                        'triggered': True,
                        'precedes': ["action:action3"],
                    },
                },
                'actions': {
                    "action2": {
                        'command': "false",
                        'needs': ["file:{}".format(join(str(tmpdir), "file"))],
                    },
                    "action3": {
                        'command': "echo 3 >> {}".format(join(str(tmpdir), "file")),
                        'needs': ["action:action2"],
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "file")) as f:
        content = f.read()
    assert content == "1\n"
def test_fix_dir_target(tmpdir):
    mkdir(join(str(tmpdir), "dir1"))
    mkdir(join(str(tmpdir), "dir2"))
    symlink(join(str(tmpdir), "dir1"), join(str(tmpdir), "link"))
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'symlinks': {
                    join(str(tmpdir), "link"): {
                        'target': join(str(tmpdir), "dir2"),
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert readlink(join(str(tmpdir), "link")) == join(str(tmpdir), "dir2")
Example #31
0
def test_fix(tmpdir):
    symlink(join(str(tmpdir), "bar"), join(str(tmpdir), "foo"))
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'symlinks': {
                        join(str(tmpdir), "foo"): {
                            'target': "/dev/null",
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert readlink(join(str(tmpdir), "foo")) == "/dev/null"
Example #32
0
def test_deploy_from_url(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'git_deploy': {
                    join(str(tmpdir), "git_deployed_bw"): {
                        'repo': "https://github.com/bundlewrap/bundlewrap.git",
                        'rev': "master",
                    },
                },
                'directories': {
                    join(str(tmpdir), "git_deployed_bw"): {},
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    assert not exists(join(str(tmpdir), "git_deployed_bw", "LICENSE"))
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert exists(join(str(tmpdir), "git_deployed_bw", "LICENSE"))
    assert not exists(join(str(tmpdir), "git_deployed_bw", ".git"))
def test_skip_trigger(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content': "nope",
                        'tags': ["nope"],
                        'triggers': ["file:{}".format(join(str(tmpdir), "bar"))],
                    },
                    join(str(tmpdir), "bar"): {
                        'content': "nope",
                        'triggered': True,
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    result = run("bw apply --skip tag:nope localhost", path=str(tmpdir))
    assert result[2] == 0
    assert not exists(join(str(tmpdir), "foo"))
    assert not exists(join(str(tmpdir), "bar"))
Example #34
0
def test_cannot_deploy_into_purged(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'git_deploy': {
                    join(str(tmpdir), "git_deployed_bw"): {
                        'repo': "https://github.com/bundlewrap/bundlewrap.git",
                        'rev': "master",
                    },
                },
                'directories': {
                    join(str(tmpdir), "git_deployed_bw"): {
                        'purge': True,
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 1
    assert b"cannot git_deploy into purged directory" in stderr
Example #35
0
def test_apply(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "bundle1": {
                'files': {
                    join(str(tmpdir), "test"): {
                        'content': "test",
                    },
                },
            },
        },
        groups={
            "adhoc-localhost": {
                'bundles': ["bundle1"],
                'member_patterns': ["localhost"],
                'os': host_os(),
            },
        },
    )

    assert not exists(join(str(tmpdir), "test"))
    stdout, stderr, rcode = run("bw -A apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert exists(join(str(tmpdir), "test"))
Example #36
0
def test_add_lock_apply_remove(tmpdir):
    make_repo(
        tmpdir,
        nodes={
            "localhost": {
                'bundles': ["bundle1"],
                'os': host_os(),
            },
        },
        bundles={
            "bundle1": {
                'files': {
                    "/tmp/bw_test_lock_add": {
                        'content': "foo",
                    },
                },
            },
        },
    )
    run("rm -f /tmp/bw_test_lock_add")
    stdout, stderr, rcode = run("BW_IDENTITY=jdoe bw lock add -c höhöhö -e 1m -i file:/tmp/bw_test_lock_add localhost", path=str(tmpdir))
    assert rcode == 0
    lock_id = get_lock_id(stdout.decode('utf-8'))
    assert len(lock_id) == 4
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    stdout, stderr, rcode = run("cat /tmp/bw_test_lock_add", path=str(tmpdir))
    assert rcode != 0
    stdout, stderr, rcode = run("bw lock remove localhost {}".format(lock_id), path=str(tmpdir))
    assert rcode == 0
Example #37
0
def test_any_content_exists(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content_type': 'any',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    with open(join(str(tmpdir), "foo"), 'wb') as f:
        f.write(b"existing content")

    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo"), 'rb') as f:
        content = f.read()
    assert content == b"existing content"
def test_create(tmpdir):
    make_repo(
        tmpdir,
        bundles={"test": {"symlinks": {join(str(tmpdir), "foo"): {"target": "/dev/null"}}}},
        nodes={"localhost": {"bundles": ["test"], "os": host_os()}},
    )
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert readlink(join(str(tmpdir), "foo")) == "/dev/null"
Example #39
0
def test_run_ok(tmpdir):
    make_repo(
        tmpdir,
        nodes={
            "localhost": {
                'os': host_os(),
            },
        },
    )
    stdout, stderr, rcode = run("BW_TABLE_STYLE=grep bw run localhost true",
                                path=str(tmpdir))
    assert rcode == 0
    assert b"localhost\t0" in stdout
    assert stderr == b""
Example #40
0
    def test_create(tmpdir):
        make_repo(
            tmpdir,
            bundles={
                "test": {
                    'postgres_dbs': {
                        "bw-test1": {
                            'owner': "bw-test1",
                        },
                    },
                    'postgres_roles': {
                        "bw-test1": {
                            'superuser': True,
                            'password': '******',
                        },
                    },
                },
            },
            nodes={
                "localhost": {
                    'bundles': ["test"],
                    'os': host_os(),
                },
            },
        )

        stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
        assert rcode == 0

        stdout, stderr, rcode = run(
            "bw items --state localhost postgres_db:bw-test1",
            path=str(tmpdir))
        assert rcode == 0
        assert loads(stdout.decode()) == {'owner': "bw-test1"}

        stdout, stderr, rcode = run(
            "bw items --state localhost postgres_role:bw-test1",
            path=str(tmpdir))
        assert rcode == 0
        assert loads(stdout.decode()) == {
            'can_login': True,
            'password_hash': "md5ecba3aec62c5aabf6480de6352182004",
            'superuser': True,
        }

        stdout, stderr, rcode = run("dropdb bw-test1", path=str(tmpdir))
        assert rcode == 0
        stdout, stderr, rcode = run("dropuser bw-test1", path=str(tmpdir))
        assert rcode == 0
Example #41
0
def test_purge_special_chars(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'items': {
                    'files': {
                        join(str(tmpdir), "purgedir", "mänäged_file"): {
                            'content': "content",
                        },
                        join(str(tmpdir), "purgedir", "managed_`id`_file"): {
                            'content': "content",
                        },
                    },
                    'directories': {
                        join(str(tmpdir), "purgedir"): {
                            'purge': True,
                        },
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    mkdir(join(str(tmpdir), "purgedir"))

    with open(join(str(tmpdir), "purgedir", "unmänäged_file"), 'w') as f:
        f.write("content")
    with open(join(str(tmpdir), "purgedir", "unmanaged_`uname`_file"), 'w') as f:
        f.write("content")
    with open(join(str(tmpdir), "purgedir", "unmanaged_:'_file"), 'w') as f:
        f.write("content")

    run("bw apply localhost", path=str(tmpdir))

    assert not exists(join(str(tmpdir), "purgedir", "unmänäged_file"))
    assert not exists(join(str(tmpdir), "purgedir", "unmanaged_`uname`_file"))
    assert not exists(join(str(tmpdir), "purgedir", "unmanaged_:'_file"))
    assert exists(join(str(tmpdir), "purgedir", "mänäged_file"))
    assert exists(join(str(tmpdir), "purgedir", "managed_`id`_file"))
Example #42
0
def test_purge(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "purgedir", "managed_file"): {
                        'content': "content",
                    },
                    join(str(tmpdir), "purgedir", "subdir1", "managed_file"): {
                        'content': "content",
                    },
                },
                'directories': {
                    join(str(tmpdir), "purgedir"): {
                        'purge': True,
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    mkdir(join(str(tmpdir), "purgedir"))
    mkdir(join(str(tmpdir), "purgedir", "subdir2"))
    mkdir(join(str(tmpdir), "purgedir", "subdir3"))

    with open(join(str(tmpdir), "purgedir", "unmanaged_file"), 'w') as f:
        f.write("content")
    with open(join(str(tmpdir), "purgedir", "subdir3", "unmanaged_file"),
              'w') as f:
        f.write("content")

    run("bw apply localhost", path=str(tmpdir))

    assert not exists(join(str(tmpdir), "purgedir", "unmanaged_file"))
    assert not exists(
        join(str(tmpdir), "purgedir", "subdir3", "unmanaged_file"))
    assert not exists(join(str(tmpdir), "purgedir", "subdir2"))
    assert exists(join(str(tmpdir), "purgedir", "subdir1", "managed_file"))
    assert exists(join(str(tmpdir), "purgedir", "managed_file"))
    def test_create(tmpdir):
        make_repo(
            tmpdir,
            bundles={
                "test": {
                    'postgres_dbs': {
                        "bw-test1": {
                            'owner': "bw-test1",
                        },
                    },
                    'postgres_roles': {
                        "bw-test1": {
                            'superuser': True,
                            'password': '******',
                        },
                    },
                },
            },
            nodes={
                "localhost": {
                    'bundles': ["test"],
                    'os': host_os(),
                },
            },
        )

        stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
        assert rcode == 0

        stdout, stderr, rcode = run("bw items --state localhost postgres_db:bw-test1", path=str(tmpdir))
        assert rcode == 0
        assert loads(stdout.decode()) == {'owner': "bw-test1"}

        stdout, stderr, rcode = run("bw items --state localhost postgres_role:bw-test1", path=str(tmpdir))
        assert rcode == 0
        assert loads(stdout.decode()) == {
            'can_login': True,
            'password_hash': "md5ecba3aec62c5aabf6480de6352182004",
            'superuser': True,
        }

        stdout, stderr, rcode = run("dropdb bw-test1", path=str(tmpdir))
        assert rcode == 0
        stdout, stderr, rcode = run("dropuser bw-test1", path=str(tmpdir))
        assert rcode == 0
def test_purge(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "purgedir", "managed_file"): {
                        'content': "content",
                    },
                    join(str(tmpdir), "purgedir", "subdir1", "managed_file"): {
                        'content': "content",
                    },
                },
                'directories': {
                    join(str(tmpdir), "purgedir"): {
                        'purge': True,
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )

    mkdir(join(str(tmpdir), "purgedir"))
    mkdir(join(str(tmpdir), "purgedir", "subdir2"))
    mkdir(join(str(tmpdir), "purgedir", "subdir3"))

    with open(join(str(tmpdir), "purgedir", "unmanaged_file"), 'w') as f:
        f.write("content")
    with open(join(str(tmpdir), "purgedir", "subdir3", "unmanaged_file"), 'w') as f:
        f.write("content")

    run("bw apply localhost", path=str(tmpdir))

    assert not exists(join(str(tmpdir), "purgedir", "unmanaged_file"))
    assert not exists(join(str(tmpdir), "purgedir", "subdir3", "unmanaged_file"))
    assert not exists(join(str(tmpdir), "purgedir", "subdir2"))
    assert exists(join(str(tmpdir), "purgedir", "subdir1", "managed_file"))
    assert exists(join(str(tmpdir), "purgedir", "managed_file"))
Example #45
0
def test_action_success(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'actions': {
                    "success": {
                        'command': "true",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
Example #46
0
def test_action_fail(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'actions': {
                    "failure": {
                        'command': "false",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
Example #47
0
def test_fault_content_skipped_jinja2(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "secret"): {
                        'content': "{{ repo.vault.password_for('test', key='unavailable') }}",
                        'content_type': 'jinja2',
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
Example #48
0
def test_create(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'symlinks': {
                    join(str(tmpdir), "foo"): {
                        'target': "/dev/null",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    assert readlink(join(str(tmpdir), "foo")) == "/dev/null"
Example #49
0
def test_skip_node(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content': "nope",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply --skip node:localhost localhost", path=str(tmpdir))
    assert not exists(join(str(tmpdir), "foo"))
def test_skip_id(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content': "nope",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    result = run("bw apply --skip file:{} localhost".format(join(str(tmpdir), "foo")), path=str(tmpdir))
    assert result[2] == 0
    assert not exists(join(str(tmpdir), "foo"))
Example #51
0
def test_action_pipe_utf8(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'actions': {
                    "pipe": {
                        'command': "cat",
                        'data_stdin': "hello 🐧\n",
                        'expected_stdout': "hello 🐧\n",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
def test_fix_dir(tmpdir):
    mkdir(join(str(tmpdir), "foo"))
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'symlinks': {
                    join(str(tmpdir), "foo"): {
                        'target': "/dev/null",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    stdout, stderr, rcode = run("bw apply localhost", path=str(tmpdir))
    assert rcode == 0
    assert readlink(join(str(tmpdir), "foo")) == "/dev/null"
Example #53
0
def test_delete(tmpdir):
    with open(join(str(tmpdir), "foo"), 'w') as f:
        f.write("foo")
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'delete': True,
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    assert not exists(join(str(tmpdir), "foo"))
Example #54
0
def test_binary_inline_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo.bin"): {
                        'content_type': 'base64',
                        'content': b64encode("ö".encode('latin-1')),
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo.bin"), 'rb') as f:
        content = f.read()
    assert content.decode('latin-1') == "ö"
Example #55
0
def test_mako_template_content_with_secret(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content_type': 'mako',
                        'content': "${repo.vault.password_for('testing')}",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo"), 'rb') as f:
        content = f.read()
    assert content == b"faCTT76kagtDuZE5wnoiD1CxhGKmbgiX"
Example #56
0
def test_text_template_content(tmpdir):
    make_repo(
        tmpdir,
        bundles={
            "test": {
                'files': {
                    join(str(tmpdir), "foo"): {
                        'content_type': 'text',
                        'content': "${node.name}",
                    },
                },
            },
        },
        nodes={
            "localhost": {
                'bundles': ["test"],
                'os': host_os(),
            },
        },
    )
    run("bw apply localhost", path=str(tmpdir))
    with open(join(str(tmpdir), "foo"), 'rb') as f:
        content = f.read()
    assert content == b"${node.name}"