def test_unauthorized(self): exc = self.exceptions.keystone_unauthorized now = timezone.now() self.mox.StubOutWithMock(api, 'usage_get') api.usage_get(IsA(http.HttpRequest), self.tenant.id, datetime.datetime(now.year, now.month, 1, 0, 0, 0), Func(usage.almost_now)) \ .AndRaise(exc) self.mox.ReplayAll() url = reverse('horizon:project:overview:index') res = self.client.get(url) self.assertRedirects(res, reverse("login") + "?next=" + url)
def test_usage_exception_usage(self): now = timezone.now() quotas = self.quota_usages.first() self.mox.StubOutWithMock(api, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages') timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, Func(usage.almost_now)) \ .AndRaise(self.exceptions.nova) api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas) self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) self.assertTemplateUsed(res, 'project/overview/usage.html') self.assertEqual(res.context['usage'].usage_list, [])
def test_usage(self): now = timezone.now() usage_obj = api.nova.Usage(self.usages.first()) quotas = self.quota_usages.first() self.mox.StubOutWithMock(api, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages') api.usage_get(IsA(http.HttpRequest), self.tenant.id, datetime.datetime(now.year, now.month, 1, 0, 0, 0), Func(usage.almost_now)) \ .AndReturn(usage_obj) api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas) self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) self.assertTemplateUsed(res, 'project/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage)) self.assertContains(res, 'form-horizontal')
def test_usage_exception_usage(self): now = timezone.now() quota_data = self.quota_usages.first() self.mox.StubOutWithMock(api, 'usage_get') self.mox.StubOutWithMock(quotas, 'tenant_quota_usages') timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, Func(usage.almost_now)) \ .AndRaise(self.exceptions.nova) quotas.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quota_data) self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) self.assertTemplateUsed(res, 'project/overview/usage.html') self.assertEqual(res.context['usage'].usage_list, [])
def test_usage(self): now = timezone.now() usage_obj = api.nova.NovaUsage(self.usages.first()) quota_data = self.quota_usages.first() self.mox.StubOutWithMock(api, 'usage_get') self.mox.StubOutWithMock(quotas, 'tenant_quota_usages') api.usage_get(IsA(http.HttpRequest), self.tenant.id, datetime.datetime(now.year, now.month, 1, 0, 0, 0), Func(usage.almost_now)) \ .AndReturn(usage_obj) quotas.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quota_data) self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index')) self.assertTemplateUsed(res, 'project/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage)) self.assertContains(res, 'form-horizontal')
def test_usage_get(self): novaclient = self.stub_novaclient() novaclient.usage = self.mox.CreateMockAnything() novaclient.usage.get(self.tenant.id, 'start', 'end').AndReturn(self.usages.first()) self.mox.ReplayAll() ret_val = api.usage_get(self.request, self.tenant.id, 'start', 'end') self.assertIsInstance(ret_val, api.nova.Usage)
def test_usage_csv(self): now = timezone.now() usage_obj = api.nova.NovaUsage(self.usages.first()) quota_data = self.quota_usages.first() self.mox.StubOutWithMock(api, 'usage_get') self.mox.StubOutWithMock(quotas, 'tenant_quota_usages') timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, Func(usage.almost_now)) \ .AndReturn(usage_obj) quotas.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quota_data) self.mox.ReplayAll() res = self.client.get( reverse('horizon:project:overview:index') + "?format=csv") self.assertTemplateUsed(res, 'project/overview/usage.csv') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage))
def test_usage_csv(self): now = timezone.now() usage_obj = api.nova.Usage(self.usages.first()) quotas = self.quota_usages.first() self.mox.StubOutWithMock(api, 'usage_get') self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages') timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, Func(usage.almost_now)) \ .AndReturn(usage_obj) api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas) self.mox.ReplayAll() res = self.client.get(reverse('horizon:project:overview:index') + "?format=csv") self.assertTemplateUsed(res, 'project/overview/usage.csv') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage))
def usage(request, tenant_id=None): today = utils.today() date_start = datetime.date(today.year, today.month, 1) datetime_start = datetime.datetime.combine(date_start, utils.time()) datetime_end = utils.utcnow() show_terminated = request.GET.get('show_terminated', False) usage = {} if not tenant_id: tenant_id = request.user.tenant try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except api_exceptions.ApiException, e: LOG.error('ApiException in instance usage', exc_info=True) messages.error(request, 'Unable to get usage info: %s' % e.message)
def tenant_usage(request, tenant_id): (date_start, date_end, datetime_start, datetime_end) = _get_start_and_end_date(request) if date_start > _current_month(): messages.error(request, "No data for the selected period") date_end = date_start datetime_end = datetime_start dateform = forms.DateForm() dateform["date"].field.initial = date_start usage = {} try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except api_exceptions.ApiException, e: LOG.error( 'ApiException getting usage info for tenant "%s"' ' on date range "%s to %s"' % (tenant_id, datetime_start, datetime_end) ) messages.error(request, "Unable to get usage info: %s" % e.message)
def get_usage_list(self, start, end): show_terminated = self.request.GET.get('show_terminated', self.show_terminated) instances = [] terminated_instances = [] usage = api.usage_get(self.request, self.tenant_id, start, end) # Attribute may not exist if there are no instances if hasattr(usage, 'server_usages'): now = self.today for server_usage in usage.server_usages: # This is a way to phrase uptime in a way that is compatible # with the 'timesince' filter. (Use of local time intentional.) server_uptime = server_usage['uptime'] total_uptime = now - datetime.timedelta(seconds=server_uptime) server_usage['uptime_at'] = total_uptime if server_usage['ended_at'] and not show_terminated: terminated_instances.append(server_usage) else: instances.append(server_usage) usage.server_usages = instances return (usage, )
def get_usage_list(self, start, end): show_terminated = self.request.GET.get('show_terminated', self.show_terminated) instances = [] terminated_instances = [] usage = api.usage_get(self.request, self.tenant_id, start, end) # Attribute may not exist if there are no instances if hasattr(usage, 'server_usages'): now = self.today for server_usage in usage.server_usages: # This is a way to phrase uptime in a way that is compatible # with the 'timesince' filter. (Use of local time intentional.) server_uptime = server_usage['uptime'] total_uptime = now - datetime.timedelta(seconds=server_uptime) server_usage['uptime_at'] = total_uptime if server_usage['ended_at'] and not show_terminated: terminated_instances.append(server_usage) else: instances.append(server_usage) usage.server_usages = instances return (usage,)