def test_instance_usage_exception(self): now = self.override_times() exception = api_exceptions.ApiException('apiException', message='apiException') self.mox.StubOutWithMock(api, 'usage_get') api.usage_get( IsA(http.HttpRequest), self.TEST_TENANT, datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second), now).AndRaise(exception) self.mox.StubOutWithMock(messages, 'error') messages.error(IsA(http.HttpRequest), IsA(basestring)) self.mox.ReplayAll() res = self.client.get( reverse('horizon:nova:instances_and_volumes:instances:usage')) self.assertTemplateUsed( res, 'nova/instances_and_volumes/instances/usage.html') self.assertEqual(res.context['usage'], {}) self.reset_times()
def test_instance_usage_exception(self): now = self.override_times() exception = api_exceptions.ApiException('apiException', message='apiException') self.mox.StubOutWithMock(api, 'usage_get') api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second), now).AndRaise(exception) self.mox.StubOutWithMock(messages, 'error') messages.error(IsA(http.HttpRequest), IsA(basestring)) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:instances:usage')) self.assertTemplateUsed(res, 'nova/instances/usage.html') self.assertEqual(res.context['usage'], {}) self.mox.VerifyAll() self.reset_times()
def test_usage_default_tenant(self): now = timezone.now() usage_obj = api.nova.Usage(self.usages.first()) self.mox.StubOutWithMock(api, "usage_get") 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) self.mox.ReplayAll() res = self.client.get(reverse("horizon:nova:overview:index")) self.assertTemplateUsed(res, "nova/overview/usage.html") self.assertTrue(isinstance(res.context["usage"], usage.TenantUsage))
def test_usage_exception(self): now = timezone.now() self.mox.StubOutWithMock(api, "usage_get") 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 ) self.mox.ReplayAll() res = self.client.get(reverse("horizon:nova:overview:index")) self.assertTemplateUsed(res, "nova/overview/usage.html") self.assertEqual(res.context["usage"].usage_list, [])
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(self): now = timezone.now() self.mox.StubOutWithMock(api, 'usage_get') 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) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/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()) 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)) \ .AndReturn(usage_obj) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage)) self.assertContains(res, 'form-horizontal')
def test_usage_exception(self): now = datetime.datetime.utcnow() self.mox.StubOutWithMock(api, 'usage_get') timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertEqual(res.context['usage'].usage_list, [])
def usage(request, tenant_id): today = datetime.date.today() dateform = forms.DateForm(request.GET, initial={'year': today.year, "month": today.month}) if dateform.is_valid(): req_year = int(dateform.cleaned_data['year']) req_month = int(dateform.cleaned_data['month']) else: req_year = today.year req_month = today.month date_start, date_end, datetime_start, datetime_end = \ GlobalSummary.get_start_and_end_date(req_year, req_month) if date_start > GlobalSummary.current_month(): messages.error(request, _('No data for the selected period')) date_end = date_start datetime_end = datetime_start usage = {} try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except api_exceptions.ApiException, e: LOG.exception('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 test_usage_default_tenant(self): now = datetime.datetime.utcnow() usage_obj = api.nova.Usage(self.usages.first()) self.mox.StubOutWithMock(api, 'usage_get') timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)) \ .AndReturn(usage_obj) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage))
def test_usage_csv(self): now = self.override_times() usage_obj = api.nova.Usage(self.usages.first()) self.mox.StubOutWithMock(api, "usage_get") timestamp = datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second) api.usage_get( IsA(http.HttpRequest), self.tenant.id, timestamp, datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second), ).AndReturn(usage_obj) self.mox.ReplayAll() res = self.client.get(reverse("horizon:nova:overview:index") + "?format=csv") self.assertTemplateUsed(res, "nova/overview/usage.csv") self.assertTrue(isinstance(res.context["usage"], usage.TenantUsage))
def test_usage_exception(self): now = self.override_times() self.mox.StubOutWithMock(api, "usage_get") timestamp = datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second) exception = nova_exceptions.ClientException(500) api.usage_get( IsA(http.HttpRequest), self.tenant.id, timestamp, datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second), ).AndRaise(exception) self.mox.ReplayAll() res = self.client.get(reverse("horizon:nova:overview:index")) self.assertTemplateUsed(res, "nova/overview/usage.html") self.assertEqual(res.context["usage"].usage_list, [])
def test_usage_default_tenant(self): now = self.override_times() usage_obj = api.nova.Usage(self.usages.first()) self.mox.StubOutWithMock(api, 'usage_get') timestamp = datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)) \ .AndReturn(usage_obj) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage))
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_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_exception(self): now = self.override_times() self.mox.StubOutWithMock(api, 'usage_get') timestamp = datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second) exception = nova_exceptions.ClientException(500) api.usage_get(IsA(http.HttpRequest), self.tenant.id, timestamp, datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)) \ .AndRaise(exception) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertEqual(res.context['usage'].usage_list, [])
def test_usage_default_tenant(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')) self.assertTemplateUsed(res, 'project/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage))
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_exception(self): self.mox.StubOutWithMock(api, 'usage_get') timestamp = datetime.datetime(self.now.year, self.now.month, 1, self.now.hour, self.now.minute, self.now.second) exception = nova_exceptions.ClientException(500) api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, timestamp, self.now).AndRaise(exception) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertEqual(res.context['usage']._apiresource, None)
def test_usage(self): TEST_RETURN = 'testReturn' now = self.override_times() self.mox.StubOutWithMock(api, 'usage_get') api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second), now).AndReturn(TEST_RETURN) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertEqual(res.context['usage'], TEST_RETURN)
def usage(request, tenant_id=None): tenant_id = tenant_id or request.user.tenant_id today = time.today() date_start = datetime.date(today.year, today.month, 1) datetime_start = datetime.datetime.combine(date_start, time.time()) datetime_end = time.utcnow() show_terminated = request.GET.get('show_terminated', False) try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except: usage = api.nova.Usage(None) redirect = reverse("horizon:nova:overview:index") exceptions.handle(request, _('Unable to retrieve usage information.')) total_ram = 0 ram_unit = "MB" instances = [] terminated = [] if hasattr(usage, 'server_usages'): total_ram = usage.total_active_memory_mb now = datetime.datetime.now() for i in usage.server_usages: i['uptime_at'] = now - datetime.timedelta(seconds=i['uptime']) if i['ended_at'] and not show_terminated: terminated.append(i) else: instances.append(i) if total_ram >= 1024: ram_unit = "GB" total_ram /= 1024 if request.GET.get('format', 'html') == 'csv': template = 'nova/overview/usage.csv' mimetype = "text/csv" else: template = 'nova/overview/usage.html' mimetype = "text/html" dash_url = horizon.get_dashboard('nova').get_absolute_url() return shortcuts.render(request, template, { 'usage': usage, 'ram_unit': ram_unit, 'total_ram': total_ram, 'csv_link': '?format=csv', 'show_terminated': show_terminated, 'datetime_start': datetime_start, 'datetime_end': datetime_end, 'instances': instances, 'dash_url': dash_url}, content_type=mimetype)
def test_usage(self): now = self.override_times() self.mox.StubOutWithMock(api, 'usage_get') api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second), datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)) \ .AndReturn(self.usage) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index')) self.assertTemplateUsed(res, 'nova/overview/usage.html') self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage)) self.assertContains(res, 'form-horizontal')
def test_usage_csv(self): TEST_RETURN = 'testReturn' self.mox.StubOutWithMock(api, 'usage_get') timestamp = datetime.datetime(self.now.year, self.now.month, 1, self.now.hour, self.now.minute, self.now.second) api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, timestamp, self.now).AndReturn(TEST_RETURN) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:overview:index') + "?format=csv") self.assertTemplateUsed(res, 'nova/overview/usage.csv') self.assertEqual(res.context['usage'], TEST_RETURN)
def test_instance_usage_default_tenant(self): TEST_RETURN = 'testReturn' now = self.override_times() self.mox.StubOutWithMock(api, 'usage_get') api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second), now).AndReturn(TEST_RETURN) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:instances:usage')) self.assertTemplateUsed(res, 'nova/instances/usage.html') self.assertEqual(res.context['usage'], TEST_RETURN) self.reset_times()
def test_instance_csv_usage(self): TEST_RETURN = 'testReturn' now = self.override_times() self.mox.StubOutWithMock(api, 'usage_get') api.usage_get(IsA(http.HttpRequest), self.TEST_TENANT, datetime.datetime(now.year, now.month, 1, now.hour, now.minute, now.second), now).AndReturn(TEST_RETURN) self.mox.ReplayAll() res = self.client.get(reverse('horizon:nova:instances:usage') + "?format=csv") self.assertTemplateUsed(res, 'nova/instances/usage.csv') self.assertEqual(res.context['usage'], TEST_RETURN) self.reset_times()
def usage(request, tenant_id=None): tenant_id = tenant_id or request.user.tenant_id today = test.today() date_start = datetime.date(today.year, today.month, 1) datetime_start = datetime.datetime.combine(date_start, test.time()) datetime_end = test.utcnow() show_terminated = request.GET.get('show_terminated', False) usage = {} if not tenant_id: tenant_id = request.user.tenant_id try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except api_exceptions.ApiException, e: LOG.exception(_('ApiException in instance usage')) 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 = datetime.datetime.now() 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 tenant_usage(request): tenant_id = request.user.tenant (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.exception('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 = datetime.datetime.now() 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, )