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') == "ö"
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"
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"))
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")
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"))
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"
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))
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"))
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
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"))
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
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"
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"
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"
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"))
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_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}"
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
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"))
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"
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"
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') == "ö"
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")
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")
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"
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"))
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
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"))
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_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"
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""
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_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"))
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"))
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))
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))
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(), }, }, )
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"
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"))
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"
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"))
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') == "ö"
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"
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}"