def test_policy_update_not_found(self, mock_enforce): self._mock_enforce_setup(mock_enforce, 'update', True) pid = 'non-existent-policy' body = { 'policy': { 'name': 'new_policy', 'level': 70, } } req = self._patch('/policies/%(policy_id)s' % {'policy_id': pid}, json.dumps(body)) error = senlin_exc.PolicyNotFound(policy=pid) mock_call = self.patchobject(rpc_client.EngineClient, 'call') mock_call.side_effect = shared.to_remote_error(error) resp = shared.request_with_middleware(fault.FaultWrapper, self.controller.update, req, tenant_id=self.project, policy_id=pid, body=body) self.assertEqual(404, resp.json['code']) self.assertEqual('PolicyNotFound', resp.json['error']['type'])
def policy_update(context, policy_id, values): with session_for_write() as session: policy = session.query(models.Policy).get(policy_id) if not policy: raise exception.PolicyNotFound(policy=policy_id) policy.update(values) policy.save(session) return policy
def test_policy_delete_not_found(self, mock_find): mock_find.side_effect = exc.PolicyNotFound(policy='Bogus') ex = self.assertRaises(rpc.ExpectedException, self.eng.policy_delete, self.ctx, 'Bogus') self.assertEqual(exc.PolicyNotFound, ex.exc_info[0]) self.assertEqual('The policy (Bogus) could not be found.', six.text_type(ex.exc_info[1])) mock_find.assert_called_once_with(self.ctx, 'Bogus')
def test_cluster_policy_detach_policy_not_found(self, mock_policy, mock_cluster): mock_cluster.return_value = mock.Mock() mock_policy.side_effect = exc.PolicyNotFound(policy='Bogus') ex = self.assertRaises(rpc.ExpectedException, self.eng.cluster_policy_detach, self.ctx, 'FAKE_CLUSTER', 'Bogus') self.assertEqual(exc.BadRequest, ex.exc_info[0]) self.assertEqual( "The request is malformed: The specified policy " "(Bogus) is not found.", six.text_type(ex.exc_info[1])) mock_cluster.assert_called_once_with(self.ctx, 'FAKE_CLUSTER') mock_policy.assert_called_once_with(self.ctx, 'Bogus')
def test_cluster_policy_get_policy_not_found(self, mock_policy, mock_cluster): mock_cluster.return_value = mock.Mock(id='FAKE_CLUSTER') mock_policy.side_effect = exc.PolicyNotFound(policy='Bogus') ex = self.assertRaises(rpc.ExpectedException, self.eng.cluster_policy_get, self.ctx, 'CLUSTER', 'Bogus') self.assertEqual(exc.PolicyNotFound, ex.exc_info[0]) self.assertEqual("The policy (Bogus) could not be found.", six.text_type(ex.exc_info[1])) mock_cluster.assert_called_once_with(self.ctx, 'CLUSTER') mock_policy.assert_called_once_with(self.ctx, 'Bogus')
def load(cls, context, policy_id=None, db_policy=None): """Retrieve and reconstruct a policy object from DB. :param context: DB context for object retrieval. :param policy_id: Optional parameter specifying the ID of policy. :param db_policy: Optional parameter referencing a policy DB object. :returns: An object of the proper policy class. """ if db_policy is None: db_policy = db_api.policy_get(context, policy_id) if db_policy is None: raise exception.PolicyNotFound(policy=policy_id) return cls._from_db_record(db_policy)
def test_policy_delete_not_found(self, mock_enforce): self._mock_enforce_setup(mock_enforce, 'delete', True) pid = 'aaaa-bbbb-cccc' req = self._delete('/policies/%(policy_id)s' % {'policy_id': pid}) error = senlin_exc.PolicyNotFound(policy=pid) mock_call = self.patchobject(rpc_client.EngineClient, 'call') mock_call.side_effect = shared.to_remote_error(error) resp = shared.request_with_middleware(fault.FaultWrapper, self.controller.delete, req, tenant_id=self.project, policy_id=pid) self.assertEqual(404, resp.json['code']) self.assertEqual('PolicyNotFound', resp.json['error']['type'])
def load(cls, context, policy_id=None, db_policy=None, project_safe=True): """Retrieve and reconstruct a policy object from DB. :param context: DB context for object retrieval. :param policy_id: Optional parameter specifying the ID of policy. :param db_policy: Optional parameter referencing a policy DB object. :param project_safe: Optional parameter specifying whether only policies belong to the context.project will be loaded. :returns: An object of the proper policy class. """ if db_policy is None: db_policy = po.Policy.get(context, policy_id, project_safe=project_safe) if db_policy is None: raise exception.PolicyNotFound(policy=policy_id) return cls._from_object(db_policy)