def test_server_user(host, pytestconfig): server_user_name = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_user_name", param_value=pytestconfig.getoption("user_name"), default_value="backuppc-server") custom_data_dir_name = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_custom_data_dir", param_value=pytestconfig.getoption("custom_data_dir"), default_value=None) server_user = host.user(server_user_name) assert server_user.exists assert server_user.home == "/var/lib/backuppc" assert server_user.shell == "/bin/false" home_dir = host.file("/var/lib/backuppc") assert home_dir.exists if custom_data_dir_name: custom_data_dir = host.file(custom_data_dir_name) assert home_dir.exists assert home_dir.is_symlink assert home_dir.linked_to == custom_data_dir_name assert custom_data_dir.user == server_user_name assert custom_data_dir.group == server_user_name assert custom_data_dir.mode == 0o750 else: assert home_dir.is_directory assert not home_dir.is_symlink assert home_dir.user == server_user_name assert home_dir.group == server_user_name assert home_dir.mode == 0o750
def test_user_settings(host, pytestconfig): expected_user_name = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_client_user_name", param_value=pytestconfig.getoption("user_name"), default_value="backuppc") user_info = host.user(expected_user_name) assert user_info.exists expected_ssh_pub_key = pytestconfig.getoption("ssh_public_key_file") if expected_ssh_pub_key is None: expected_ssh_pub_key = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_client_ssh_auth_key", param_value=None, default_value=None) if "{{" in expected_ssh_pub_key: env = jinja2.Environment(undefined=jinja2.StrictUndefined) env.globals["lookup"] = custom_lookup_func expected_ssh_pub_key = env.from_string( expected_ssh_pub_key).render() else: with open(expected_ssh_pub_key, "r") as fh: expected_ssh_pub_key = fh.read().strip() keys = host.file(user_info.home + "/.ssh/authorized_keys").content_string.split("\n") assert expected_ssh_pub_key in keys expected_sudoers_entries = [{ "command": "/usr/bin/rsync", "comment": f"Allow {expected_user_name} to read files with rsync over SSH" }] # Most likely it's possible to pass an array of dictionaries from Molecule's # YAML to Testinfra as a parameter. But definitely it's not going to be easy :) # So instead of wrestling with ugly escaping we just use hard-coded value # for tests. if pytestconfig.getoption("use_test_sudo_entries"): expected_sudoers_entries += [{ "command": "/etc/custom/command_1", "comment": "Test command 1" }, { "command": "/etc/custom/command_2", "comment": "Test command 2" }] else: expected_sudoers_entries += test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_client_custom_sudo_commands", param_value=None, default_value=[]) sudoers_entries = host.file( "/etc/sudoers.d/backuppc").content_string.split("\n") for entry in expected_sudoers_entries: print(entry) assert f"# {entry['comment']}" in sudoers_entries assert f"{expected_user_name} ALL=NOPASSWD: {entry['command']}" in sudoers_entries
def test_backuppc_status_page(host, pytestconfig): www_user = pytestconfig.getoption("www_user") www_password = pytestconfig.getoption("www_password") # Temporarily use curl until HTTP module is implemented # https://github.com/philpep/testinfra/issues/407 status_text = host.check_output( f"curl -u {www_user}:{www_password} -s http://localhost/BackupPC_Admin" ) assert "BackupPC Server Status" in status_text expected_backuppc_ver = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_version", param_value=pytestconfig.getoption("backuppc_version"), default_value="latest") if expected_backuppc_ver == "latest": expected_backuppc_ver = test_utils.get_github_release_info( "backuppc/backuppc/releases/latest")["tag_name"] backuppc_ver = "unknown" for line in status_text.splitlines(): print("===>", line) if ", started at" in line: backuppc_ver = line.split(", started at")[0] backuppc_ver = backuppc_ver.split("version ")[1] assert expected_backuppc_ver == backuppc_ver
def test_rsync_bpc_version(host, pytestconfig): expected_rsync_bpc_ver = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_rsync_bpc_version", param_value=pytestconfig.getoption("rsync_bpc_version"), default_value="latest") if expected_rsync_bpc_ver == "latest": expected_rsync_bpc_ver = test_utils.get_github_release_info( "backuppc/rsync-bpc/releases/latest")["tag_name"] rsync_bpc_ver = host.run_test( "/usr/local/bin/rsync_bpc --version").stderr.splitlines()[0].split()[2] assert expected_rsync_bpc_ver == rsync_bpc_ver
def test_backuppc_xs_version(host, pytestconfig): expected_backuppc_xs_ver = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_backuppc_xs_version", param_value=pytestconfig.getoption("backuppc_xs_version"), default_value="latest") if expected_backuppc_xs_ver == "latest": expected_backuppc_xs_ver = test_utils.get_github_release_info( "backuppc/backuppc-xs/releases/latest")["tag_name"] backuppc_xs_ver = host.check_output( "perl -e '" "use lib \"/usr/local/BackupPC/lib\";" "use BackupPC::XS; print $BackupPC::XS::VERSION'") assert expected_backuppc_xs_ver == backuppc_xs_ver
def test_default_system_locale(host, pytestconfig): expected_lang = test_utils.get_parameter_value( host=host, ansible_var_name="linux_locale_default_lang", param_value=pytestconfig.getoption("default_lang"), default_value="en_US.UTF-8") expected_lc = test_utils.get_parameter_value( host=host, ansible_var_name="linux_locale_default_LC", param_value=pytestconfig.getoption("default_lc"), default_value="en_US.UTF-8") locale_data = {} for line in host.check_output('sudo su - -c "locale"').splitlines(): lc_name, lc_value = line.split("=") locale_data[lc_name] = lc_value.replace('"', "") assert locale_data["LANG"] == expected_lang for lc_parameter in ("LC_NUMERIC", "LC_TIME", "LC_MONETARY", "LC_PAPER", "LC_NAME", "LC_ADDRESS", "LC_TELEPHONE", "LC_MEASUREMENT", "LC_IDENTIFICATION"): assert locale_data[lc_parameter] == expected_lc
def test_backuppc_version(host, pytestconfig): expected_backuppc_ver = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_version", param_value=pytestconfig.getoption("backuppc_version"), default_value="latest") if expected_backuppc_ver == "latest": expected_backuppc_ver = test_utils.get_github_release_info( "backuppc/backuppc/releases/latest")["tag_name"] backuppc_ver = "unknown" for line in host.file( "/usr/local/BackupPC/bin/BackupPC").content_string.splitlines(): if "# Version" in line: backuppc_ver = line.split(",")[0].replace("# Version ", "") assert expected_backuppc_ver == backuppc_ver
def test_timezone(host, pytestconfig): expected_timezone = test_utils.get_parameter_value( host=host, ansible_var_name="linux_timezone_name", param_value=pytestconfig.getoption("timezone"), default_value="Europe/Kaliningrad") current_timezone = None for line in host.check_output("timedatectl status").splitlines(): value_name, value = line.split(":", maxsplit=1) if value_name.strip() == "Time zone": # Here we are look for values like this: # Time zone: Europe/Kaliningrad (EET, +0200) current_timezone = value.split("(")[0].strip() if current_timezone is None: raise Exception("Couldn't detect current timezone") assert current_timezone == expected_timezone
def test_backuppc_conf_file(host, pytestconfig): apache_require = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_server_apache_require", param_value=pytestconfig.getoption("apache_require"), default_value="Require all granted").lower().replace("require ", "") with apacheconfig.make_loader() as loader: config = loader.loads( host.file( "/etc/apache2/conf-available/backuppc.conf").content_string) # BackupPC source code has this trailing space cgi_bin_dir = pytestconfig.getoption("cgi_bin_dir") + " " module_2_4_plus = next( item for item in config["Directory"][cgi_bin_dir]["IfModule"] if "authz_core_module" in item) assert { "Require": [apache_require, "valid-user"] } in module_2_4_plus["authz_core_module"]["RequireAll"]
def test_rsync_service(host, pytestconfig): if host.system_info.distribution == "ubuntu": rsync_service = host.service("rsync") elif host.system_info.distribution == "centos": rsync_service = host.service("rsyncd") else: raise Exception("Unsupported distribution: " + host.system_info.distribution) assert rsync_service.is_enabled assert rsync_service.is_running expected_rsync_address = test_utils.get_parameter_value( host=host, ansible_var_name="backuppc_client_rsync_address", param_value=pytestconfig.getoption("rsync_address"), default_value="") if expected_rsync_address: assert host.socket(f"tcp://{expected_rsync_address}:873").is_listening else: assert host.socket("tcp://873").is_listening