import configparser
import os
from pathlib import Path

import backup as bckup
import ippops_homedir_backup as ihbck_mod
from ippops_homedir_backup import IPPOpsHomedirBackup
from utils.config_parse_helper import ConfigSchema

IPPOPS_HOMEDIR_TEST_CONFIG = os.path.join(os.path.dirname(__file__),
    './testing/ippops_homedir_test.config')


class TestArguments(object):

    def assert_defaults(self, ihb: IPPOpsHomedirBackup):

        assert ihb.default_dict == \
            { 'backup_paths' :  [ '/export/ippops3.0/backups/homedir_backups'
                                , '/data/ippops4.0/backups/homedir_backups'
                                , '/data/ippops5.0/backups/homedir_backups'
                                ]
            , 'source_machine' : 'ippops3'
            , 'verbose' : False
            }
        assert ihb.rsync_dict == \
            { 'sources' : ['/export/ippc20.0/home/panstarrs']
            , 'additional_args' : ['-a', '--delete-after']
            }

    def test_load_default_config_file(self):
        ihb = IPPOpsHomedirBackup(config_file=IPPOPS_HOMEDIR_TEST_CONFIG)
        self.assert_defaults(ihb)


class TestFullRunFromMain(object):

    def test_main_loads_and_runs_fine_when_given_config(self, tmpdir):
        config = configparser.ConfigParser()
        bpath1 = os.path.join(tmpdir, "path/1")
        bpath2 = os.path.join(tmpdir, "path/2")
        config['DEFAULT'] = \
            { 'backup_paths' :  f"{bpath1}, {bpath2}"
            , 'source_machine' : 'rocket'
            , 'verbose' : 'True'
            }
        tar_source =  os.path.join(tmpdir, "tar/source")
        config['TAR'] = \
            { 'sources' : f"{tar_source}"
            , 'additional_args' : "--verbose -z -p -cf"
            , 'target_filename' : 'svnroot.tar.gz'
            , 'prefix_date' : 'False'
            }
        os.makedirs(bpath1)
        os.makedirs(bpath2)
        os.makedirs(tar_source)

        schema = ConfigSchema(None)
        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
        schema.add_section(bckup.TAR_SCHEMA_SECTION)

        ihbck_mod.main(config, schema)
        assert True is True


class TestIppopsHomedirRsync(object):
    """Simple inherit of base class Backup"""

    def test_rsync_with_additional_args(self, tmpdir):

        # Setup config
        backup_1_path = os.path.join(tmpdir, "backup_1_path")
        backup_2_path = os.path.join(tmpdir, "backup_2_path")
        config = configparser.ConfigParser()
        config['DEFAULT'] = \
            { 'backup_paths' :  f"{backup_1_path}, {backup_2_path}"
            , 'source_machine' : 'rocket'
            , 'verbose' : 'True'
            }
        config['RSYNC'] = \
            { 'sources' : f"{os.path.join(tmpdir, 'home')}"
            , 'additional_args' : "-a --delete-after"
            }
        backup_1_path = os.path.join(tmpdir, "backup_1_path")
        backup_2_path = os.path.join(tmpdir, "backup_2_path")
        os.makedirs(backup_1_path)
        os.makedirs(backup_2_path)

        # Create files to be Rync'd
        homedir = os.path.join(tmpdir, "home")
        subdir1 = os.path.join(homedir, "some_user")
        subdir2 = os.path.join(homedir, "some_other_user")
        file1 = os.path.join(subdir1, "file_in_subdir1")
        file2 = os.path.join(subdir2, "file_in_subdir2")
        os.makedirs(homedir)
        os.makedirs(subdir1)
        os.makedirs(subdir2)
        Path(file1).touch()
        Path(file2).touch()

        # Setup Schema
        schema = ConfigSchema(None)
        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)

        # check that the rsync is successful (with options)
        ihb = IPPOpsHomedirBackup(config, schema)

        expected_sub1_backup1       = os.path.join(tmpdir, 'backup_1_path', 'home', 'some_user')
        expected_sub1_file1_backup1 = os.path.join(tmpdir, 'backup_1_path', 'home', 'some_user/file_in_subdir1')
        expected_sub2_backup1       = os.path.join(tmpdir, 'backup_1_path', 'home', 'some_other_user')
        expected_sub2_file2_backup1 = os.path.join(tmpdir, 'backup_1_path', 'home', 'some_other_user/file_in_subdir2')
        expected_sub1_backup2       = os.path.join(tmpdir, 'backup_2_path', 'home', 'some_user')
        expected_sub1_file1_backup2 = os.path.join(tmpdir, 'backup_2_path', 'home', 'some_user/file_in_subdir1')
        expected_sub2_backup2       = os.path.join(tmpdir, 'backup_2_path', 'home', 'some_other_user')
        expected_sub2_file2_backup2 = os.path.join(tmpdir, 'backup_2_path', 'home', 'some_other_user/file_in_subdir2')
        assert not os.path.exists(expected_sub1_backup1)
        assert not os.path.exists(expected_sub1_file1_backup1)
        assert not os.path.exists(expected_sub2_backup1)
        assert not os.path.exists(expected_sub2_file2_backup1)
        assert not os.path.exists(expected_sub1_backup2)
        assert not os.path.exists(expected_sub1_file1_backup2)
        assert not os.path.exists(expected_sub2_backup2)
        assert not os.path.exists(expected_sub2_file2_backup2)
        ihb._run_rsync()
        assert os.path.exists(expected_sub1_backup1)
        assert os.path.exists(expected_sub1_file1_backup1)
        assert os.path.exists(expected_sub2_backup1)
        assert os.path.exists(expected_sub2_file2_backup1)
        assert os.path.exists(expected_sub1_backup2)
        assert os.path.exists(expected_sub1_file1_backup2)
        assert os.path.exists(expected_sub2_backup2)
        assert os.path.exists(expected_sub2_file2_backup2)
