def fakerun(*popenargs, **kwargs): if get_command(popenargs) == "chroot": rootfs_path = get_command_parameter(popenargs, "chroot") if not os.path.exists(rootfs_path): os.mkdir(rootfs_path) elif get_command(popenargs) == "debootstrap": rootfs_path = popenargs[0][-2] apt_dir = os.path.join(rootfs_path, 'etc', 'apt') os.makedirs(apt_dir) pass elif get_command(popenargs) == "tar": archive = get_command_parameter(popenargs, '-acf') with open(archive, mode="w") as fakearchive: fakearchive.write("fake archive") elif popenargs[0][-2] == "dpkg" and popenargs[0][-1] == "--print-architecture": return subprocess.CompletedProcess("fakerun", 0, 'amd64') elif get_command(popenargs).endswith("lxd") and get_sub_command(popenargs) == "--version": return subprocess.CompletedProcess("fakerun", 0, '2.18') elif get_command(popenargs) == "ssh" and get_sub_command(popenargs) == "-G": return subprocess.CompletedProcess("fakerun", 0, 'ssh config') elif get_command(popenargs) == "printenv": return subprocess.CompletedProcess("fakerun", 0, '') elif get_command(popenargs) == 'getent' and get_sub_command(popenargs) == 'passwd': return subprocess.CompletedProcess("fakerun", 0, stdout='john:x:1000:1000:John Doe,,,:/no/such/directory:/bin/bash\n') elif get_command(popenargs) == "findmnt": return subprocess.CompletedProcess("fakerun", 0, '/foo/bar/baz') else: print('Passthrough: {}'.format(get_command(popenargs))) return subprocess.run(*popenargs, **kwargs) return subprocess.CompletedProcess("fakerun", 0, '')
def fakerun(*popenargs, **kwargs): if get_command(popenargs) == "chroot": rootfs_path = get_command_parameter(popenargs, "chroot") if not os.path.exists(rootfs_path): os.mkdir(rootfs_path) elif get_command(popenargs) == "debootstrap": rootfs_path = popenargs[0][-2] apt_dir = os.path.join(rootfs_path, 'etc', 'apt') os.makedirs(apt_dir) pass elif get_command(popenargs) == "tar": archive = get_command_parameter(popenargs, '-acf') with open(archive, mode="w") as fakearchive: fakearchive.write("fake archive") elif popenargs[0][-2] == "dpkg" and popenargs[0][-1] == "--print-architecture": return subprocess.CompletedProcess("fakerun", 0, 'amd64') elif get_command(popenargs) == "lxd" and get_sub_command(popenargs) == "--version": return subprocess.CompletedProcess("fakerun", 0, '2.18') elif get_command(popenargs) == "printenv": if get_sub_command(popenargs) == "HOME": return subprocess.CompletedProcess("fakerun", 0, '/no/such/directory') else: return subprocess.CompletedProcess("fakerun", 0, '') else: print('Passthrough: {}'.format(get_command(popenargs))) return subprocess.run(*popenargs, **kwargs) return subprocess.CompletedProcess("fakerun", 0, '')
def intercept_command_run(*popenargs, **kwargs): if get_command(popenargs) == 'gsettings': schema = get_command_parameter(popenargs, 'get') assert schema.startswith('org.gnome.system.proxy') key = get_command_parameter(popenargs, schema) return_value = 0 if key == 'mode': result = gsettings_proxy_mode elif key == 'ignore-hosts': result = gsettings_no_proxy elif key == 'host': result = 'foo:[email protected]' elif key == 'port': result = gsettings_proxy_port else: return_value = 1 result = '' return subprocess.CompletedProcess("fakerun", return_value, stdout=result) elif get_command(popenargs) == 'printenv': env_var = get_sub_command(popenargs) if env_var == 'no_proxy' and env_value_no_proxy: return subprocess.CompletedProcess("fakerun", 0, stdout=env_value_no_proxy) elif env_var != 'no_proxy' and env_var.endswith('_proxy') and env_value_proxy: return subprocess.CompletedProcess("fakerun", 0, stdout=env_value_proxy) else: return subprocess.CompletedProcess("fakerun", 1, stdout='') else: return subprocess.run(*popenargs, **kwargs)
def fake_ansible_playbook_run(*popenargs, **kwargs): if get_command(popenargs) == 'ansible-playbook': assert 'lxd' == get_command_parameter(popenargs, '--connection') verify_inventory(get_command_parameter(popenargs, '--inventory')) verify_extra_vars(get_command_parameter(popenargs, '--extra-vars').lstrip('@')) # TODO: verify --user for ssh connection return subprocess.CompletedProcess("fakerun", 0, '') else: return subprocess.run(*popenargs, **kwargs)
def fake_getent_passwd(*popenargs, **kwargs): if get_command(popenargs) == 'getent' and get_sub_command(popenargs) == 'passwd' and \ get_command_parameter(popenargs, 'passwd') == 'john': return subprocess.CompletedProcess("fakerun", 0, stdout='john:x:1000:1000:John Doe,,,:/home/john:/bin/bash\n') else: return subprocess.run(*popenargs, **kwargs)
def fake_lxc_exec_command(*popenargs, **kwargs): if get_command(popenargs).endswith('lxc') and get_sub_command(popenargs) == 'exec': if get_command_parameter(popenargs, '--') == 'test': return subprocess.CompletedProcess("failure", 1, 'failure') else: return subprocess.CompletedProcess("fakerun", 0, '') else: return subprocess.run(*popenargs, **kwargs)
def fake_lxc_exec_command(*popenargs, **kwargs): if get_command(popenargs).endswith('lxc') and get_sub_command(popenargs) == 'exec': if get_command_parameter(popenargs, '--') == 'true': cmd = ['bash', '-c', '>&2 echo -e "lxc command failed" ; exit 1'] return subprocess.run(cmd, **kwargs) else: return subprocess.CompletedProcess("fakerun", 0, '') else: return subprocess.run(*popenargs, **kwargs)
def fakerun(*popenargs, **kwargs): if popenargs[0][0] == "chroot": rootfs_path = popenargs[0][1] if not os.path.exists(rootfs_path): os.mkdir(rootfs_path) elif popenargs[0][0] == "debootstrap": rootfs_path = popenargs[0][-2] apt_dir = os.path.join(rootfs_path, 'etc', 'apt') os.makedirs(apt_dir) pass elif popenargs[0][0] == "tar": archive = get_command_parameter(popenargs[0], '-acf') with open(archive, mode="w") as fakearchive: fakearchive.write("fake archive") elif popenargs[0][-2] == "dpkg" and popenargs[0][ -1] == "--print-architecture": return subprocess.CompletedProcess("fakerun", 0, 'amd64') else: print('Passthrough: {}'.format(popenargs[0])) return subprocess.run(*popenargs, **kwargs) return subprocess.CompletedProcess("fakerun", 0, '')