def test_is_reboot_required(monkeypatch): run_state = { r'if \[ ! -e /usr/bin/needs-restarting \] ; then echo notexists ; fi': { 'stdout': 'notexists', 'failed': False }, r'yum -y install yum-utils': { 'stdout': '', 'failed': False }, r'/usr/bin/needs-restarting -r': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert is_reboot_required() is False run_state = { r'if \[ ! -e /usr/bin/needs-restarting \] ; then echo notexists ; fi': { 'stdout': '', 'failed': False }, r'/usr/bin/needs-restarting -r': { 'stdout': 'Reboot is required to ensure that your system benefits from these updates.', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert is_reboot_required() is True
def test_disable_selinux(monkeypatch): run_state = { r'if \[ -e /etc/selinux/config \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'if \[ -e /usr/sbin/setenforce \] && \[ -e /usr/sbin/getenforce \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'STATUS=\$\(getenforce\) ; if \[ "\$STATUS" == "Enforcing" \] ; then setenforce 0 ; echo perm ; fi': { 'stdout': 'perm', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) monkeypatch.setattr(fabrix.system, 'edit_file', lambda x, y: True) assert disable_selinux() is True run_state = { r'if \[ -e /etc/selinux/config \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, r'if \[ -e /usr/sbin/setenforce \] && \[ -e /usr/sbin/getenforce \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert disable_selinux() is False
def test__copy_file_acl(monkeypatch): run_state = { r'if \[ -e /usr/bin/getfacl \] && \[ -e /usr/bin/setfacl \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_acl('/old', '/new') is None run_state = { r'if \[ -e /usr/bin/getfacl \] && \[ -e /usr/bin/setfacl \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'getfacl --absolute-names -- .* | setfacl --set-file=- -- .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_acl('/old', '/new') is None
def test_timedatectl_set_timezone(monkeypatch): run_state = { r'timedatectl set-timezone Europe/Kiev': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert timedatectl_set_timezone('Europe/Kiev') == ''
def test_localectl_set_locale(monkeypatch): run_state = { r'localectl set-locale LANG=en_US.UTF-8': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert localectl_set_locale('LANG=en_US.UTF-8') == ''
def test_run(monkeypatch): run_state = { r'command': { 'stdout': 'stdout', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabric.api, 'run', mock_run) assert run('command') == 'stdout'
def test_get_user_home_directory(monkeypatch): run_state = { r'getent passwd': { 'stdout': PASSWD, 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) assert get_user_home_directory("root") == "/root" assert get_user_home_directory("none") is None
def test_systemctl_set_default(monkeypatch): run_state = { r'systemctl daemon-reload ; systemctl set-default multi-user.target ; systemctl daemon-reload': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert systemctl_set_default('multi-user.target') == ''
def test_systemctl_preset(monkeypatch): name = 'name' run_state = { r'systemctl daemon-reload ; systemctl preset ' + name + ' ; systemctl daemon-reload': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert systemctl_preset(name) is None
def test_get_virtualization_type(monkeypatch): run_state = { r'hostnamectl status': { 'stdout': """ Static hostname: test-centos Icon name: computer-container Chassis: container Machine ID: f8889d6132eb43658e8f6ea6f30c394e Boot ID: 20e212fab38942e4b499f100f75f575c Virtualization: openvz Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 2.6.32-042stab124.2 Architecture: x86-64 """, 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert get_virtualization_type() == 'openvz' run_state = { r'hostnamectl status': { 'stdout': """ Static hostname: example.com Icon name: computer-server Chassis: server Machine ID: 4c500bbc51bb45539b5206bcbc523a42 Boot ID: 9c6aa7f2c066445e92f74f14d38bdc13 Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-693.2.2.el7.x86_64 Architecture: x86-64 """, 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) assert get_virtualization_type() is None
def test_remove_user(monkeypatch): run_state = { r'userdel .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) monkeypatch.setattr(fabrix.passwd, 'is_user_exists', lambda x: True) assert remove_user("user") is None monkeypatch.setattr(fabrix.passwd, 'is_user_exists', lambda x: False) assert remove_user("user") is None
def test_reboot_and_wait(monkeypatch): monkeypatch.setitem(env, "host_string", '11.11.11.11') run_state = { r'reboot': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.system, 'run', mock_run) monkeypatch.setattr(fabrix.system.connections, 'connect', lambda x: None) monkeypatch.setattr(time, 'sleep', lambda x: None) assert reboot_and_wait() is None
def test_is_group_exists(monkeypatch): run_state = { r'getent group': { 'stdout': GROUP, 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) assert is_group_exists("wheel") is True assert is_group_exists("nonex") is False assert is_group_not_exists("daemon") is False assert is_group_not_exists("not-ex") is True
def test_is_user_exists(monkeypatch): run_state = { r'getent passwd': { 'stdout': PASSWD, 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) assert is_user_exists("nginx") is True assert is_user_exists("not-exists") is False assert is_user_not_exists("nginx") is False assert is_user_not_exists("not-exists") is True
def test_delete_user_from_group(monkeypatch): run_state = { r'gpasswd .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) monkeypatch.setattr(fabrix.passwd, 'is_user_in_group', lambda x, y: True) assert delete_user_from_group("user", "group") is None monkeypatch.setattr(fabrix.passwd, 'is_user_in_group', lambda x, y: False) assert delete_user_from_group("user", "group") is None
def test_create_group(monkeypatch): run_state = { r'groupadd .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) monkeypatch.setattr(fabrix.passwd, 'is_group_not_exists', lambda x: True) assert create_group("group") is None monkeypatch.setattr(fabrix.passwd, 'is_group_not_exists', lambda x: False) assert create_group("group") is None
def test__copy_file_owner_and_mode(monkeypatch): run_state = { r'chown --reference=.* -- .*': { 'stdout': '', 'failed': False }, r'chmod --reference=.* -- .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_owner_and_mode('/old', '/new') is None
def test_yum_install(tmpdir, monkeypatch): run_state = { r'yum -y install a b c': { 'stdout': '', 'failed': False }, r'yum -y install A B C': { 'stdout': 'Nothing to do', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.rpmyum, 'run', mock_run) assert yum_install(["c", "b"], "a") is True assert yum_install(""" C B A """) is False
def test_is_user_in_group(monkeypatch): run_state = { r'getent group': { 'stdout': GROUP, 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.passwd, 'run', mock_run) assert is_user_in_group("postfix", "man") is False assert is_user_in_group("postfix", "zzz") is False assert is_user_in_group("postfix", "mail") is True assert is_user_not_in_group("postfix", "man") is True assert is_user_not_in_group("postfix", "zzz") is True assert is_user_not_in_group("postfix", "mail") is False
def test_yum_remove(tmpdir, monkeypatch): run_state = { r'yum -y remove a b c': { 'stdout': '', 'failed': False }, r'yum -y remove A B C': { 'stdout': 'No Packages marked for removal', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.rpmyum, 'run', mock_run) assert yum_remove(["c", "b"], "a") is True assert yum_remove(""" C B A """) is False
def test_is_file_exists(monkeypatch): with abort('remote filename must be absolute, .*'): is_file_exists("file") run_state = { r'if \[ -f /path/to/file \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'if \[ -f /path/to/none \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert is_file_exists('/path/to/file') is True assert is_file_exists('/path/to/none') is False
def test_is_directory_not_exists(monkeypatch): with abort('remote dirname must be absolute, .*'): is_directory_not_exists("dir") run_state = { r'if \[ ! -d /path/to/none \] ; then echo notexists ; fi': { 'stdout': 'notexists', 'failed': False }, r'if \[ ! -d /path/to/dire \] ; then echo notexists ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert is_directory_not_exists('/path/to/none') is True assert is_directory_not_exists('/path/to/dire') is False
def test_remove_directory(monkeypatch): with abort('remote directory name must be absolute, ".*" given'): remove_directory('dir') run_state = { r'if \[ -d /path/to/dire \] ; then rmdir -- /path/to/dire ; echo removed ; fi': { 'stdout': 'removed', 'failed': False }, r'if \[ -d /path/to/none \] ; then rmdir -- /path/to/none ; echo removed ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert remove_directory('/path/to/dire') is True assert remove_directory('/path/to/none') is False
def test_create_file(monkeypatch): with abort('remote file name must be absolute, ".*" given'): create_file('file') run_state = { r'if \[ ! -f /path/to/file \] ; then touch -- /path/to/file ; echo created ; fi': { 'stdout': 'created', 'failed': False }, r'if \[ ! -f /path/to/none \] ; then touch -- /path/to/none ; echo created ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert create_file('/path/to/file') is True assert create_file('/path/to/none') is False
def test_chown(tmpdir, monkeypatch): run_state = { r'chown --changes root:root -- /path/to/changed': { 'stdout': 'changed ownership of /srv/default/grub from ftp:ftp to root:root\n', 'failed': False }, r'chown --changes root:root -- /path/to/not-changed': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) with abort( 'chown: remote path \'.*\' must be absolute in file .* line .*'): chown("remote-file-name", "root", "root") assert chown("/path/to/changed", "root", "root") is True assert chown("/path/to/not-changed", "root", "root") is False
def test_yum_update(tmpdir, monkeypatch): run_state = { r'yum -y update a b c$': { 'stdout': '', 'failed': False }, r'yum -y update A B C$': { 'stdout': 'No packages marked for update', 'failed': False }, r'yum -y update $': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.rpmyum, 'run', mock_run) assert yum_update(["c", "b"], "a") is True assert yum_update(""" C B A """) is False assert yum_update() is True
def test_chmod(tmpdir, monkeypatch): run_state = { r'chmod --changes 0644 -- /path/to/changed': { 'stdout': 'mode of /srv/default/grub changed from 0644 (rw-r--r--) to 0600 (rw-------)\n', 'failed': False }, r'chmod --changes 0644 -- /path/to/not-changed': { 'stdout': '', 'failed': False }, r'chmod --changes -x\+X -- /path/to/not-changed': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) with abort( 'chmod: remote path \'.*\' must be absolute in file .* line .*'): chmod("remote-file-name", 0644) assert chmod("/path/to/changed", 0644) is True assert chmod("/path/to/not-changed", 0644) is False assert chmod("/path/to/not-changed", "-x+X") is False
def test__copy_file_selinux_context(monkeypatch): run_state = { r'if \[ -e /usr/sbin/getenforce \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_selinux_context('/old', '/new') is None run_state = { r'if \[ -e /usr/sbin/getenforce \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'getenforce': { 'stdout': 'Disabled', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_selinux_context('/old', '/new') is None run_state = { r'if \[ -e /usr/sbin/getenforce \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'getenforce': { 'stdout': 'Enabled', 'failed': False }, r'if \[ -e /usr/bin/chcon \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_selinux_context('/old', '/new') is None run_state = { r'if \[ -e /usr/sbin/getenforce \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'getenforce': { 'stdout': 'Enabled', 'failed': False }, r'if \[ -e /usr/bin/chcon \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'chcon --reference=.* -- .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) assert _copy_file_selinux_context('/old', '/new') is None
def test__atomic_write_file(monkeypatch): put_state = { r'/newfile-put-ok\.tmp\.\w+\.tmp$': False, r'/newfile-put-failed\.tmp\.\w+\.tmp$': True, r'/1hardlink\.tmp\.\w+\.tmp$': False, } mock_put = mock_put_factory(put_state) monkeypatch.setattr(fabrix.ioutil, 'put', mock_put) run_state = { r'if \[ -e /isnotfile \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'if \[ ! -f /isnotfile \] ; then echo isnotfile ; fi': { 'stdout': 'isnotfile', 'failed': False }, r'if \[ -e /7hardlinks \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'if \[ ! -f /7hardlinks \] ; then echo isnotfile ; fi': { 'stdout': '', 'failed': False }, r'stat --format "%h" -- /7hardlinks': { 'stdout': '7', 'failed': False }, r'if \[ -e /newfile-put-ok \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, r'mv -f -- /newfile-put-ok\.tmp\.\w+\.tmp /newfile-put-ok': { 'stdout': '', 'failed': False }, r'if \[ -e /newfile-put-failed \] ; then echo exists ; fi': { 'stdout': '', 'failed': False }, r'if \[ -e /1hardlink \] ; then echo exists ; fi': { 'stdout': 'exists', 'failed': False }, r'if \[ ! -f /1hardlink \] ; then echo isnotfile ; fi': { 'stdout': '', 'failed': False }, r'stat --format "%h" -- /1hardlink': { 'stdout': '1', 'failed': False }, r'mv -f -- /1hardlink\.tmp\.\w+\.tmp /1hardlink': { 'stdout': '', 'failed': False }, r'cp --attributes-only --preserve=xattr -- .* .*': { 'stdout': '', 'failed': False }, } mock_run = mock_run_factory(run_state) monkeypatch.setattr(fabrix.ioutil, 'run', mock_run) with abort('remote filename must be absolute, "%s" given' % 'not-absolute-path'): _atomic_write_file('not-absolute-path', 'text') with abort('remote filename must be regular file, "%s" given' % '/isnotfile'): _atomic_write_file('/isnotfile', 'text') with abort('file "%s" has %d hardlinks, it can\'t be atomically written' % ('/7hardlinks', 7)): _atomic_write_file('/7hardlinks', 'text') assert _atomic_write_file('/newfile-put-ok', 'text') is None with abort( r'uploading file /newfile-put-failed\.tmp\.\w+\.tmp to host .* failed' ): _atomic_write_file('/newfile-put-failed', 'text') def mock__copy_file_owner_and_mode(old_filename, new_filename): pass monkeypatch.setattr(fabrix.ioutil, '_copy_file_owner_and_mode', mock__copy_file_owner_and_mode) def mock__copy_file_acl(old_filename, new_filename): pass monkeypatch.setattr(fabrix.ioutil, '_copy_file_acl', mock__copy_file_acl) def mock__copy_file_selinux_context(old_filename, new_filename): pass monkeypatch.setattr(fabrix.ioutil, '_copy_file_selinux_context', mock__copy_file_selinux_context) assert _atomic_write_file('/1hardlink', 'text') is None