def test_takelscripts_entrypoint_mkdir_homedir_child(monkeypatch, caplog, tmp_path): uid = int( subprocess.run(['id', '--user'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')) gid = int( subprocess.run(['id', '--group'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')) monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(gid=gid, home=str(tmp_path / 'home/testuser'), uid=uid)) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() entrypoint._mkdir_homedir_child_('tmp1/tmp2/tmp3') expected_log_begin = "creating homedir child directory: " expected_log_end = "/home/testuser/tmp1/tmp2/tmp3" assert (tmp_path / 'home/testuser/tmp1/tmp2/tmp3').is_dir() assert expected_log_begin in caplog.text assert expected_log_end in caplog.text
def test_takelscripts_entrypoint_add_docker(monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(docker=True, home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() entrypoint.add_docker() docker_config_path = tmp_path / 'home/testuser/.docker' docker_config_file = docker_config_path / 'config.json' expected_log_start = 'adding config: docker' expected_log_end = 'added config: docker' # currently the entrypoint script creates a docker config only in case # the host has no config or uses osxkeychain to store password hashes # so this test should respect that... dunno how to test this right now.. # expected_log_path = 'creating homedir child directory: ' + \ # str(docker_config_path) # expected_log_file = 'creating docker config file: ' + \ # str(docker_config_file) assert docker_config_path.is_dir() assert docker_config_file.is_file() assert expected_log_start in caplog.text assert expected_log_end in caplog.text
def test_takelscripts_entrypoint_add_gopass_config(monkeypatch, caplog, tmp_path): gopass_config = dedent(f"""\ autoclip: false autoimport: true cliptimeout: 45 exportkeys: false mime: true nocolor: false nopager: false notifications: true path: {tmp_path}/home/testuser/.local/share/gopass/stores/root safecontent: false mount "my-passwords" => "{tmp_path}""" """/home/testuser/.local/share/gopass/stores/""" """password-store-my-passwords" """) monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(gopass=True, home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path / 'hostdir') monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_add_gopass_get_config_', lambda x: gopass_config) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) hostdir_config_path = tmp_path / 'hostdir/.config/gopass' hostdir_config_path.mkdir(parents=True) hostdir_config_file = hostdir_config_path / 'config.yml' hostdir_config_file.write_text(gopass_config) entrypoint = EntryPoint() entrypoint.add_gopass() expected_log_start = 'adding config: gopass' expected_log_end = 'added config: gopass' expected_config_file = 'using gopass config file: ' + \ str(tmp_path) + \ '/hostdir/.config/gopass/config.yml' expected_log_symlink1 = 'symlink: .config/gopass' expected_log_symlink2 = 'symlink: .local/share/gopass/stores' assert expected_log_start in caplog.text assert expected_log_end in caplog.text assert expected_config_file in caplog.text assert expected_log_symlink1 in caplog.text assert expected_log_symlink2 in caplog.text
def test_takelscripts_entrypoint_run(monkeypatch, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() result = entrypoint._run_(['echo', '"banana"']) assert "banana" in result.stdout.decode('utf-8') assert 'running command: echo "banana"' in caplog.text
def test_takelscripts_entrypoint_add_user(monkeypatch, caplog, tmp_path): uid = int( subprocess.run(['id', '--user'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')) gid = int( subprocess.run(['id', '--group'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')) monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(gid=gid, home=str(tmp_path / 'home/testuser'), uid=uid)) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_run_', log_argument) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_copy_file_', log_arguments_copy_file) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_create_group_', lambda x, y, z: Namespace(returncode=0)) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_copy_takelage_yml_', lambda x: True) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_copy_bashrc_', lambda x: True) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_mkdir_bashrc_d_', lambda x: True) entrypoint = EntryPoint() entrypoint.add_user() expected_log_user = '******' expected_log_grouos = 'adding user to groups: sudo,tty,docker' expected_command_begin = \ "['useradd', " \ "'--create-home', " \ "'--home-dir', " expected_command_end = \ "'--groups', " \ "'sudo,tty,docker', " \ "'--shell', " \ "'/bin/bash', " \ "'--non-unique', " \ "'testuser']" expected_log_done = 'created user: testuser' assert (tmp_path / 'home').is_dir() assert expected_log_user in caplog.text assert expected_log_grouos in caplog.text assert expected_command_begin in caplog.text assert expected_command_end in caplog.text assert expected_log_done in caplog.text
def test_takelscripts_entrypoint_add_gpg(monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(gpg=True, home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_copy_file_', log_arguments_copy_file) entrypoint = EntryPoint() entrypoint.add_gpg() expected_log_start = 'adding config: gpg' expected_log_end = 'added config: gpg' expected_log_chmod_gnupg = 'chmod 700 gnupg directory:' expected_log_symlink_file1 = 'symlink: .gnupg/pubring.kbx' expected_log_symlink_file2 = 'symlink: .gnupg/trustdb.gpg' expected_log_symlink_file3 = 'symlink: .gnupg/private-keys-v1.d' expected_log_symlink_file4 = 'symlink: .gnupg/openpgp-revocs.d' expected_log_symlink_file5 = 'symlink: .gnupg/crls.d' expected_log_copy_file1 = \ "{'source': '/srv/.gnupg/dirmngr.conf', 'destination': '" expected_log_copy_file2 = \ "{'source': '/srv/.gnupg/gpg-agent.conf', 'destination': '" expected_log_copy_file3 = \ "{'source': '/srv/.gnupg/gpg.conf', 'destination': '" gnupg_dir = tmp_path / 'home/testuser/.gnupg' gnupg_dir_mode = gnupg_dir.stat().st_mode assert gnupg_dir.is_dir() assert 'drwx------' == filemode(gnupg_dir_mode) assert expected_log_start in caplog.text assert expected_log_end in caplog.text assert expected_log_chmod_gnupg in caplog.text assert expected_log_symlink_file1 in caplog.text assert expected_log_symlink_file2 in caplog.text assert expected_log_symlink_file3 in caplog.text assert expected_log_symlink_file4 in caplog.text assert expected_log_symlink_file5 in caplog.text assert expected_log_copy_file1 in caplog.text assert expected_log_copy_file2 in caplog.text assert expected_log_copy_file3 in caplog.text
def test_takelscripts_entrypoint_init_debug(monkeypatch, caplog): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(extra='.config/first:.config/second')) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() output_args = \ "command line arguments: " + \ "Namespace(" + \ "debug=True, " + \ "docker=False, " + \ "docker_daemon_port=17873, " + \ "extra='.config/first:.config/second', " + \ "gid=1600, " + \ "git=False, " + \ "gopass=False, " + \ "gpg=False, " + \ "hg=False, " + \ "home='/home/testuser', " + \ "mutagen=False, " + \ "runcmd='', " + \ "ssh=False, " + \ "uid=1500, " + \ "username='******')" assert entrypoint._hostdir == Path('/hostdir') assert '*******************************************' in caplog.text assert 'starting configuration:' in caplog.text assert output_args in caplog.text assert "hostdir: /hostdir" in caplog.text
def test_takelscripts_entrypoint_run_and_fork(monkeypatch, tmp_path, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() entrypoint._run_and_fork_(['touch', str(tmp_path / 'test.txt')]) expected_log_begin = "running command in background: touch /" expected_log_end = "/test.txt" assert (tmp_path / 'test.txt').exists assert expected_log_begin in caplog.text assert expected_log_end in caplog.text
def test_takelscripts_entrypoint_copy_bashrc(monkeypatch, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_copy_file_', log_arguments_copy_file) entrypoint = EntryPoint() entrypoint._copy_bashrc_() expected_log = \ "{'source': '/root/.bashrc', 'destination': '/home/testuser/.bashrc'}" assert expected_log in caplog.text
def test_takelscripts_entrypoint_mkdir_parent(monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() entrypoint._mkdir_parents_(tmp_path / 'tmp1/tmp2/tmp3') expected_log_begin = "creating parent directory: " expected_log_end = "/tmp1/tmp2" assert (tmp_path / 'tmp1/tmp2').is_dir() assert not (tmp_path / 'tmp1/tmp2/tmp3').is_dir() assert expected_log_begin in caplog.text assert expected_log_end in caplog.text
def test_takelscripts_entrypoint_copy_takelage_yml_notexists( monkeypatch, caplog, tmp_path): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) entrypoint = EntryPoint() entrypoint._copy_takelage_yml_() filename = '.takelage.yml' assert filename not in caplog.text
def test_takelscripts_entrypoint_add_mutagen_socket_dir( monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(mutagen=True, home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) entrypoint = EntryPoint() entrypoint.add_mutagen() mutagen_socket_dir = tmp_path / 'home/testuser/.mutagen/daemon' mutagen_socket_dir_mode = mutagen_socket_dir.stat().st_mode assert mutagen_socket_dir.is_dir() assert 'drwxr-xr-x' == filemode(mutagen_socket_dir_mode)
def test_takelscripts_entrypoint_chown_home(monkeypatch, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_run_', log_argument) entrypoint = EntryPoint() entrypoint.chown_home() command = \ "['/bin/chown', " \ "'--recursive', " \ "'testuser.testuser', " \ "'/home/testuser']" assert command in caplog.text
def test_takelscripts_entrypoint_add_ssh(monkeypatch, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(ssh=True)) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) entrypoint = EntryPoint() entrypoint.add_ssh() expected_log_start = 'adding config: ssh' expected_log_end = 'added config: ssh' expected_log = 'symlink: .ssh' assert expected_log_start in caplog.text assert expected_log_end in caplog.text assert expected_log in caplog.text
def test_takelscripts_entrypoint_init_nodebug(monkeypatch, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(debug=False)) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) EntryPoint() assert '*******************************************' in caplog.text assert 'starting configuration:' in caplog.text
def test_takelscripts_entrypoint_copy_takelage_yml_exists( monkeypatch, caplog, tmp_path): test_takelage_yml = tmp_path / '.takelage.yml' test_takelage_yml.touch() monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) entrypoint = EntryPoint() entrypoint._copy_takelage_yml_() symlink = 'symlink: .takelage.yml' assert symlink in caplog.text
def test_takelscripts_entrypoint_mkdir_bashrc_d_exists(monkeypatch, caplog, tmp_path): bashrc_d_hostdir = tmp_path / '.bashrc.d' bashrc_d_hostdir.touch() monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path) entrypoint = EntryPoint() entrypoint._mkdir_bashrc_d_() expected_log = '.bashrc.d' assert expected_log in caplog.text
def test_takelscripts_entrypoint_mkdir_bashrc_d_notexists( monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path) entrypoint = EntryPoint() entrypoint._mkdir_bashrc_d_() expected_log_begin = "creating homedir child directory: " expected_log_end = ".bashrc.d" assert (tmp_path / 'home/testuser/.bashrc.d/profile').is_file() assert (tmp_path / 'home/testuser/.bashrc.d/takelage').is_file() assert expected_log_begin in caplog.text assert expected_log_end in caplog.text
def test_takelscripts_entrypoint_add_gopass_noconfig(monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(gopass=True, home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path / 'hostdir') monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) entrypoint = EntryPoint() entrypoint.add_gopass() expected_log_start = 'adding config: gopass' expected_log_error = 'no gopass config file found' assert expected_log_start in caplog.text assert expected_log_error in caplog.text
def test_takelscripts_entrypoint_chown_tty(monkeypatch, caplog, tmp_path): testfile = tmp_path / 'test' testfile.touch() uid = int( subprocess.run(['id', '--user'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8')) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(uid=uid)) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_run_', lambda x, y: Namespace(stdout=str(testfile).encode('utf-8'))) entrypoint = EntryPoint() entrypoint.chown_tty() expected_log = 'readable and writeable for user' assert expected_log in caplog.text
def test_takelscripts_entrypoint_create_group(monkeypatch, caplog): monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default()) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_run_', log_argument) entrypoint = EntryPoint() entrypoint._create_group_('testuser', 1600) command = "['groupadd', " \ "'--gid', " \ "'1600', " \ "'--non-unique', " \ "'testuser']" expected_log = "creating group: {'name': 'testuser', 'gid': 1600}" assert command in caplog.text assert expected_log in caplog.text
def test_takelscripts_entrypoint_add_extra(monkeypatch, caplog): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(extra='.config/first:.config/second')) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_symlink_', log_argument_symlink) entrypoint = EntryPoint() entrypoint.add_extra() expected_log_start = 'adding config: extra' expected_log_end = 'added config: extra' expected_log_symlink1 = 'symlink: .config/first' expected_log_symlink2 = 'symlink: .config/second' assert expected_log_start in caplog.text assert expected_log_end in caplog.text assert expected_log_symlink1 in caplog.text assert expected_log_symlink2 in caplog.text
def test_takelscripts_entrypoint_symlink_create(monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path / 'hostdir') entrypoint = EntryPoint() (tmp_path / 'hostdir').mkdir() (tmp_path / 'hostdir/.testconfig').touch() entrypoint._symlink_('.testconfig') expected_log_begin = "creating symlink: {'source': '/" expected_log_middle = "/hostdir/.testconfig', 'destination': '/" expected_log_end = "/home/testuser/.testconfig'}" assert (tmp_path / 'home/testuser/.testconfig').is_symlink() assert expected_log_begin in caplog.text assert expected_log_middle in caplog.text assert expected_log_end in caplog.text
def test_takelscripts_entrypoint_copy_file(monkeypatch, caplog, tmp_path): monkeypatch.setattr( takelscripts.entrypoint.EntryPoint, '_parse_args_', lambda x: args_default(home=str(tmp_path / 'home/testuser'))) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_logger_init_', mock_logger_init) monkeypatch.setattr(takelscripts.entrypoint.EntryPoint, '_get_hostdir_', lambda x: tmp_path / 'hostdir') entrypoint = EntryPoint() src = (tmp_path / 'src') src.touch() dest = (tmp_path / 'dest') entrypoint._copy_file_(src, dest) expected_log_begin = "copying file: {'source': '/" expected_log_middle = "/src', 'destination': '/" expected_log_end = "/dest'}" assert (tmp_path / 'dest').is_file() assert expected_log_begin in caplog.text assert expected_log_middle in caplog.text assert expected_log_end in caplog.text