def test_set_desired_file_access(self): #setup file_path = rc("temp-keyfile") if os.path.exists(file_path): os.remove(file_path) with open(file_path, "w") as file: file.write("content") #check assumptions stat = os.stat(file_path) self.assertNotEqual('600', oct(stat.st_mode)[-3:]) current_user = pwd.getpwuid(os.getuid())[0] if current_user != settings.pacemaker_uname: file_user = pwd.getpwuid(stat.st_uid)[0] self.assertNotEqual(file_user, settings.pacemaker_uname) current_group = grp.getgrgid(os.getgid())[0] if current_group != settings.pacemaker_gname: file_group = grp.getgrgid(stat.st_gid)[0] self.assertNotEqual(file_group, settings.pacemaker_gname) #run tested method env.set_keyfile_access(file_path) #check stat = os.stat(file_path) self.assertEqual('600', oct(stat.st_mode)[-3:]) file_user = pwd.getpwuid(stat.st_uid)[0] self.assertEqual(file_user, settings.pacemaker_uname) file_group = grp.getgrgid(stat.st_gid)[0] self.assertEqual(file_group, settings.pacemaker_gname)
def test_do_everything_to_set_desired_file_access(self, settings, getpwnam, getgrnam, chown, chmod): file_path = "/tmp/some_booth_file" env.set_keyfile_access(file_path) getpwnam.assert_called_once_with(settings.pacemaker_uname) getgrnam.assert_called_once_with(settings.pacemaker_gname) chown.assert_called_once_with( file_path, getpwnam.return_value.pw_uid, getgrnam.return_value.gr_gid, )
def test_do_everything_to_set_desired_file_access( self, settings, getpwnam, getgrnam, chown, chmod ): # pylint: disable=unused-argument file_path = "/tmp/some_booth_file" env.set_keyfile_access(file_path) getpwnam.assert_called_once_with(settings.pacemaker_uname) getgrnam.assert_called_once_with(settings.pacemaker_gname) chown.assert_called_once_with( file_path, getpwnam.return_value.pw_uid, getgrnam.return_value.gr_gid, )
def test_raises_when_cannot_get_gid(self): assert_raise_library_error( lambda: env.set_keyfile_access("/booth"), (severities.ERROR, report_codes.UNABLE_TO_DETERMINE_GROUP_GID, { "group": "some-group", }), )
def test_raises_when_cannot_get_uid(self): assert_raise_library_error( lambda: env.set_keyfile_access("/booth"), (severities.ERROR, report_codes.UNABLE_TO_DETERMINE_USER_UID, { "user": "******", }), )
def test_raises_when_cannot_chmod(self): assert_raise_library_error( lambda: env.set_keyfile_access("/booth"), (severities.ERROR, report_codes.FILE_IO_ERROR, { 'reason': 'err', 'file_role': u'BOOTH_KEY', 'file_path': '/booth', 'operation': u'chmod', }), )
def test_raises_when_cannot_get_gid(self): assert_raise_library_error( lambda: env.set_keyfile_access("/booth"), ( severities.ERROR, report_codes.UNABLE_TO_DETERMINE_GROUP_GID, { "group": "some-group", } ), )
def test_raises_when_cannot_get_uid(self): assert_raise_library_error( lambda: env.set_keyfile_access("/booth"), ( severities.ERROR, report_codes.UNABLE_TO_DETERMINE_USER_UID, { "user": "******", } ), )
def test_raises_when_cannot_chmod(self): assert_raise_library_error( lambda: env.set_keyfile_access("/booth"), ( severities.ERROR, report_codes.FILE_IO_ERROR, { 'reason': 'err', 'file_role': u'BOOTH_KEY', 'file_path': '/booth', 'operation': u'chmod', } ), )