def test_get_resource_available_rq_dne(self, mock_rq, mock_log): mock_rq.objects.get.side_effect = ResourceQuota.DoesNotExist mock_rq.DoesNotExist = ResourceQuota.DoesNotExist ret = utilities.get_resource_available(mock.MagicMock(), 'codename') mock_log.warning.assert_called_with( 'Invalid Codename: ResourceQuota with codename codename does not exist.' ) self.assertEquals(None, ret)
def test_get_resource_not_available(self, mock_rq, mock_get_quota): rq = mock.MagicMock() mock_rq.objects.get.return_value = rq rqh = mock.MagicMock() rq.handler.return_value = rqh rqh.get_current_use.return_value = 6 mock_get_quota.return_value = {'quota': 3} ret = utilities.get_resource_available(TethysApp(), 'codename') self.assertEqual(0, ret['resource_available'])
def test_get_resource_available_app(self, mock_rq, mock_get_quota): rq = mock.MagicMock() rq.units = 'gb' mock_rq.objects.get.return_value = rq rqh = mock.MagicMock() rq.handler.return_value = rqh rqh.get_current_use.return_value = 1 mock_get_quota.return_value = {'quota': 5} ret = utilities.get_resource_available(TethysApp(), 'codename') self.assertEqual(4, ret['resource_available']) self.assertEqual('gb', ret['units'])
def check(self): """ Checks if entity use is at/below/above quota. Uses get_resource_available() to compute the current use of the resource. Returns: Boolean: True if entity use is at or below quota and False if entity use is above the quota. """ # noqa: E501 # self.codename isn't complete because the actual ResourceQuota codename is 'applies_to' specific codename = "{}_{}".format(self.entity.__class__.__name__.lower(), self.codename) resource_available = get_resource_available(self.entity, codename) if resource_available and resource_available['resource_available'] == 0: return False else: return True