def test_file_check_perms(tfile3): expected_result = ( { "comment": "The file {} is set to be changed".format(tfile3), "changes": { "selinux": {"New": "Type: lost_found_t", "Old": "Type: user_tmp_t"}, "mode": "0664", }, "name": tfile3, "result": True, }, {"luser": "******", "lmode": "0644", "lgroup": "root"}, ) # Disable lsattr calls with patch("salt.utils.path.which") as m_which: m_which.return_value = None result = filemod.check_perms( tfile3, {}, "root", "root", 664, seuser=None, serole=None, setype="lost_found_t", serange=None, ) assert result == expected_result
def test_check_perms_should_report_no_attr_changes_if_there_are_none(): filename = "/path/to/fnord" attrs = "aAcCdDeijPsStTu" higher_than = "1.41.13" patch_chattr = patch( "salt.modules.file._chattr_version", Mock(return_value=higher_than), ) patch_exists = patch( "os.path.exists", Mock(return_value=True), ) patch_stats = patch( "salt.modules.file.stats", Mock(return_value={"user": "******", "group": "bar", "mode": "123"}), ) patch_run = patch.dict( filemod.__salt__, {"cmd.run": MagicMock(return_value="--------- " + filename)}, ) with patch_chattr, patch_exists, patch_stats, patch_run: actual_ret, actual_perms = filemod.check_perms( name=filename, ret=None, user="******", group="bar", mode="123", attrs=attrs, follow_symlinks=False, ) assert actual_ret.get("changes", {}).get("attrs") is None, actual_ret
def test_check_perms_should_report_attrs_new_and_old_if_they_changed(): filename = "/path/to/fnord" attrs = "aAcCdDeijPsStTu" existing_attrs = "aeiu" expected = { "attrs": { "old": existing_attrs, "new": attrs }, } higher_than = "1.41.13" patch_chattr = patch( "salt.modules.file._chattr_version", Mock(return_value=higher_than), ) patch_stats = patch( "salt.modules.file.stats", Mock(return_value={ "user": "******", "group": "bar", "mode": "123" }), ) patch_cmp = patch( "salt.modules.file._cmp_attrs", MagicMock(side_effect=[ filemod.AttrChanges( added="aAcCdDeijPsStTu", removed="", ), filemod.AttrChanges( None, None, ), ]), ) patch_chattr = patch( "salt.modules.file.chattr", MagicMock(), ) def fake_cmd(cmd, *args, **kwargs): if cmd == ["lsattr", "/path/to/fnord"]: return textwrap.dedent(""" {}---- {} """.format(existing_attrs, filename)).strip() else: assert False, "not sure how to handle {}".format(cmd) patch_run = patch.dict( filemod.__salt__, {"cmd.run": MagicMock(side_effect=fake_cmd)}, ) patch_ver = patch( "salt.modules.file._chattr_has_extended_attrs", MagicMock(return_value=True), ) with patch_chattr, patch_stats, patch_cmp, patch_run, patch_ver: actual_ret, actual_perms = filemod.check_perms( name=filename, ret=None, user="******", group="bar", mode="123", attrs=attrs, follow_symlinks=False, ) assert actual_ret["changes"] == expected