Пример #1
0
def setup_function(function):

    global maindomain
    maindomain = _get_maindomain()

    assert backup_test_dependencies_are_met()

    clean_tmp_backup_directory()
    reset_ssowat_conf()
    delete_all_backups()
    uninstall_test_apps_if_needed()

    assert len(backup_list()["archives"]) == 0

    markers = {
        m.name: {
            'args': m.args,
            'kwargs': m.kwargs
        }
        for m in function.__dict__.get("pytestmark", [])
    }

    if "with_wordpress_archive_from_2p4" in markers:
        add_archive_wordpress_from_2p4()
        assert len(backup_list()["archives"]) == 1

    if "with_legacy_app_installed" in markers:
        assert not app_is_installed("legacy_app")
        install_app("legacy_app_ynh", "/yolo")
        assert app_is_installed("legacy_app")

    if "with_backup_recommended_app_installed" in markers:
        assert not app_is_installed("backup_recommended_app")
        install_app("backup_recommended_app_ynh", "/yolo",
                    "&helper_to_test=ynh_restore_file")
        assert app_is_installed("backup_recommended_app")

    if "with_backup_recommended_app_installed_with_ynh_restore" in markers:
        assert not app_is_installed("backup_recommended_app")
        install_app("backup_recommended_app_ynh", "/yolo",
                    "&helper_to_test=ynh_restore")
        assert app_is_installed("backup_recommended_app")

    if "with_system_archive_from_2p4" in markers:
        add_archive_system_from_2p4()
        assert len(backup_list()["archives"]) == 1

    if "with_permission_app_installed" in markers:
        assert not app_is_installed("permissions_app")
        user_create("alice", "Alice", "White", maindomain, "test123Ynh")
        install_app("permissions_app_ynh", "/urlpermissionapp" "&admin=alice")
        assert app_is_installed("permissions_app")

    if "with_custom_domain" in markers:
        domain = markers['with_custom_domain']['args'][0]
        if domain not in domain_list()['domains']:
            domain_add(domain)
Пример #2
0
def _test_backup_and_restore_app(mocker, app):

    # Create a backup of this app
    with message(mocker, "backup_created"):
        backup_create(system=None, apps=[app])

    archives = backup_list()["archives"]
    assert len(archives) == 1

    archives_info = backup_info(archives[0], with_details=True)
    assert archives_info["system"] == {}
    assert len(archives_info["apps"].keys()) == 1
    assert app in archives_info["apps"].keys()

    # Uninstall the app
    app_remove(app)
    assert not app_is_installed(app)
    assert app + ".main" not in user_permission_list()["permissions"]

    # Restore the app
    with message(mocker, "restore_complete"):
        backup_restore(system=None, name=archives[0], apps=[app])

    assert app_is_installed(app)

    # Check permission
    per_list = user_permission_list()["permissions"]
    assert app + ".main" in per_list
Пример #3
0
def test_backup_and_restore_all_sys(mocker):

    # Create the backup
    with message(mocker, "backup_created"):
        backup_create(system=[], apps=None)

    archives = backup_list()["archives"]
    assert len(archives) == 1

    archives_info = backup_info(archives[0], with_details=True)
    assert archives_info["apps"] == {}
    assert len(archives_info["system"].keys()) == len(
        os.listdir("/usr/share/yunohost/hooks/backup/")
    )

    # Remove ssowat conf
    assert os.path.exists("/etc/ssowat/conf.json")
    os.system("rm -rf /etc/ssowat/")
    assert not os.path.exists("/etc/ssowat/conf.json")

    # Restore the backup
    with message(mocker, "restore_complete"):
        backup_restore(name=archives[0], force=True, system=[], apps=None)

    # Check ssowat conf is back
    assert os.path.exists("/etc/ssowat/conf.json")
Пример #4
0
def test_restore_app_already_installed(mocker):

    assert not _is_installed("wordpress")

    with message(mocker, "restore_complete"):
        backup_restore(
            system=None, name=backup_list()["archives"][0], apps=["wordpress"]
        )

    assert _is_installed("wordpress")

    with raiseYunohostError(mocker, "restore_already_installed_apps"):
        backup_restore(
            system=None, name=backup_list()["archives"][0], apps=["wordpress"]
        )

    assert _is_installed("wordpress")
Пример #5
0
def test_restore_archive_with_custom_hook(mocker):

    custom_restore_hook_folder = os.path.join(CUSTOM_HOOK_FOLDER, "restore")
    os.system("touch %s/99-yolo" % custom_restore_hook_folder)

    # Backup with custom hook system
    with message(mocker, "backup_created"):
        backup_create(system=[], apps=None)
    archives = backup_list()["archives"]
    assert len(archives) == 1

    # Restore system with custom hook
    with message(mocker, "restore_complete"):
        backup_restore(
            name=backup_list()["archives"][0], system=[], apps=None, force=True
        )

    os.system("rm %s/99-yolo" % custom_restore_hook_folder)
Пример #6
0
def test_restore_archive_with_no_json(mocker):

    # Create a backup with no info.json associated
    os.system("touch /tmp/afile")
    os.system("tar -czvf /home/yunohost.backup/archives/badbackup.tar.gz /tmp/afile")

    assert "badbackup" in backup_list()["archives"]

    with raiseYunohostError(mocker, "backup_archive_cant_retrieve_info_json"):
        backup_restore(name="badbackup", force=True)
Пример #7
0
def test_restore_system_from_Ynh2p4(monkeypatch, mocker):

    # Backup current system
    with message(mocker, "backup_created"):
        backup_create(system=[], apps=None)
    archives = backup_list()["archives"]
    assert len(archives) == 2

    # Restore system archive from 2.4
    try:
        with message(mocker, "restore_complete"):
            backup_restore(
                name=backup_list()["archives"][1], system=[], apps=None, force=True
            )
    finally:
        # Restore system as it was
        backup_restore(
            name=backup_list()["archives"][0], system=[], apps=None, force=True
        )
Пример #8
0
def test_restore_archive_with_bad_archive(mocker):

    # Break the archive
    os.system(
        "head -n 1000 /home/yunohost.backup/archives/backup_wordpress_from_2p4.tar.gz > /home/yunohost.backup/archives/backup_wordpress_from_2p4.tar.gz"
    )

    assert "backup_wordpress_from_2p4" in backup_list()["archives"]

    with raiseYunohostError(mocker, "backup_archive_open_failed"):
        backup_restore(name="backup_wordpress_from_2p4", force=True)

    clean_tmp_backup_directory()
Пример #9
0
def test_restore_app_not_in_backup(mocker):

    assert not _is_installed("wordpress")
    assert not _is_installed("yoloswag")

    with message(mocker, "backup_archive_app_not_found", app="yoloswag"):
        with raiseYunohostError(mocker, "restore_nothings_done"):
            backup_restore(
                system=None, name=backup_list()["archives"][0], apps=["yoloswag"]
            )

    assert not _is_installed("wordpress")
    assert not _is_installed("yoloswag")
Пример #10
0
def test_backup_only_ldap(mocker):

    # Create the backup
    with message(mocker, "backup_created"):
        backup_create(system=["conf_ldap"], apps=None)

    archives = backup_list()["archives"]
    assert len(archives) == 1

    archives_info = backup_info(archives[0], with_details=True)
    assert archives_info["apps"] == {}
    assert len(archives_info["system"].keys()) == 1
    assert "conf_ldap" in archives_info["system"].keys()
Пример #11
0
def test_restore_app_not_enough_free_space(monkeypatch, mocker):
    def custom_free_space_in_directory(dirpath):
        return 0

    monkeypatch.setattr("yunohost.backup.free_space_in_directory",
                        custom_free_space_in_directory)

    assert not _is_installed("wordpress")

    with raiseYunohostError(mocker, 'restore_not_enough_disk_space'):
        backup_restore(system=None,
                       name=backup_list()["archives"][0],
                       apps=["wordpress"])

    assert not _is_installed("wordpress")
Пример #12
0
def test_restore_app_script_failure_handling(monkeypatch, mocker):
    def custom_hook_exec(name, *args, **kwargs):
        if os.path.basename(name).startswith("restore"):
            monkeypatch.undo()
            raise Exception

    monkeypatch.setattr("yunohost.backup.hook_exec", custom_hook_exec)

    assert not _is_installed("wordpress")

    with message(mocker, "restore_app_failed", app="wordpress"):
        with raiseYunohostError(mocker, "restore_nothings_done"):
            backup_restore(
                system=None, name=backup_list()["archives"][0], apps=["wordpress"]
            )

    assert not _is_installed("wordpress")
Пример #13
0
def test_backup_with_different_output_directory(mocker):

    # Create the backup
    with message(mocker, "backup_created"):
        backup_create(system=["conf_ssh"],
                      apps=None,
                      output_directory="/opt/test_backup_output_directory",
                      name="backup")

    assert os.path.exists("/opt/test_backup_output_directory/backup.tar")

    archives = backup_list()["archives"]
    assert len(archives) == 1

    archives_info = backup_info(archives[0], with_details=True)
    assert archives_info["apps"] == {}
    assert len(archives_info["system"].keys()) == 1
    assert "conf_ssh" in archives_info["system"].keys()
Пример #14
0
def test_restore_app_wordpress_from_Ynh2p4(mocker):

    with message(mocker, "restore_complete"):
        backup_restore(
            system=None, name=backup_list()["archives"][0], apps=["wordpress"]
        )
Пример #15
0
def delete_all_backups():

    for archive in backup_list()["archives"]:
        backup_delete(archive)