def test_ajax_loading_instances(self): server = self.servers.first() flavor = self.flavors.list()[0] tenant = self.tenants.list()[0] api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.flavor_get(IsA(http.HttpRequest), server.flavor['id']).AndReturn(flavor) api.keystone.tenant_get(IsA(http.HttpRequest), server.tenant_id, admin=True).AndReturn(tenant) self.mox.ReplayAll() url = reverse('horizon:admin:instances:index') + \ "?action=row_update&table=instances&obj_id=" + server.id res = self.client.get(url, {}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertTemplateUsed(res, "horizon/common/_data_table_row.html") self.assertContains(res, "test_tenant", 1, 200) self.assertContains(res, "instance-host", 1, 200) self.assertContains(res, "server_1", 1, 200) self.assertContains(res, "10.0.0.1", 1, 200) self.assertContains(res, "512MB RAM | 1 VCPU | 0 Disk", 1, 200) self.assertContains(res, "Active", 1, 200) self.assertContains(res, "Running", 1, 200)
def test_instance_details_metadata(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.volume_instance_list(IsA(http.HttpRequest), server.id).AndReturn([]) api.flavor_get(IsA(http.HttpRequest), server.flavor['id']).AndReturn(self.flavors.first()) api.server_security_groups(IsA(http.HttpRequest), server.id).AndReturn( self.security_groups.list()) self.mox.ReplayAll() url = reverse('horizon:project:instances:detail', args=[server.id]) tg = InstanceDetailTabs(self.request, instance=server) qs = "?%s=%s" % (tg.param_name, tg.get_tab("overview").get_id()) res = self.client.get(url + qs) self.assertContains(res, "<dd>keyName</dd>", 1) self.assertContains(res, "<dt>someMetaLabel</dt>", 1) self.assertContains(res, "<dd>someMetaData</dd>", 1) self.assertContains(res, "<dt>some<b>html</b>label</dt>", 1) self.assertContains(res, "<dd><!--</dd>", 1) self.assertContains(res, "<dt>empty</dt>", 1) self.assertContains(res, "<dd><em>N/A</em></dd>", 1)
def test_instance_details_metadata(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.instance_volumes_list(IsA(http.HttpRequest), server.id).AndReturn([]) api.flavor_get(IsA(http.HttpRequest), server.flavor['id']).AndReturn(self.flavors.first()) api.server_security_groups(IsA(http.HttpRequest), server.id).AndReturn(self.security_groups.list()) self.mox.ReplayAll() url = reverse('horizon:project:instances:detail', args=[server.id]) tg = InstanceDetailTabs(self.request, instance=server) qs = "?%s=%s" % (tg.param_name, tg.get_tab("overview").get_id()) res = self.client.get(url + qs) self.assertContains(res, "<dd>keyName</dd>", 1) self.assertContains(res, "<dt>someMetaLabel</dt>", 1) self.assertContains(res, "<dd>someMetaData</dd>", 1) self.assertContains(res, "<dt>some<b>html</b>label</dt>", 1) self.assertContains(res, "<dd><!--</dd>", 1) self.assertContains(res, "<dt>empty</dt>", 1) self.assertContains(res, "<dd><em>N/A</em></dd>", 1)
def test_create_instance_snapshot(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.snapshot_create(IsA(http.HttpRequest), server.id, "snapshot1").AndReturn(self.snapshots.first()) api.snapshot_list_detailed(IsA(http.HttpRequest), marker=None).AndReturn([[], False]) api.image_list_detailed(IsA(http.HttpRequest), marker=None).AndReturn([[], False]) api.volume_snapshot_list(IsA(http.HttpRequest)).AndReturn([]) self.mox.ReplayAll() formData = { 'instance_id': server.id, 'method': 'CreateSnapshot', 'name': 'snapshot1' } url = reverse('horizon:project:images_and_snapshots:snapshots:create', args=[server.id]) redir_url = reverse('horizon:project:images_and_snapshots:index') res = self.client.post(url, formData) self.assertRedirects(res, redir_url)
def test_instance_update_get(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) self.mox.ReplayAll() url = reverse('horizon:project:instances:update', args=[server.id]) res = self.client.get(url) self.assertTemplateUsed(res, 'project/instances/update.html')
def test_create_snapshot_get(self): server = self.servers.first() self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) self.mox.ReplayAll() url = reverse('horizon:project:images_and_snapshots:snapshots:create', args=[server.id]) res = self.client.get(url) self.assertTemplateUsed(res, 'project/images_and_snapshots/snapshots/create.html')
def test_create_snapshot_get(self): server = self.servers.first() self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) self.mox.ReplayAll() url = reverse('horizon:project:images_and_snapshots:snapshots:create', args=[server.id]) res = self.client.get(url) self.assertTemplateUsed( res, 'project/images_and_snapshots/snapshots/create.html')
def test_instance_update_get_server_get_exception(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndRaise(self.exceptions.nova) self.mox.ReplayAll() url = reverse("horizon:project:instances:update", args=[server.id]) res = self.client.get(url) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_create_get_server_exception(self): server = self.servers.first() self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), server.id) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() url = reverse('horizon:project:images_and_snapshots:snapshots:create', args=[server.id]) res = self.client.get(url) redirect = reverse("horizon:project:instances:index") self.assertRedirectsNoFollow(res, redirect)
def test_instance_update_get_server_get_exception(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() url = reverse('horizon:project:instances:update', args=[server.id]) res = self.client.get(url) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_instance_update_post_api_exception(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.server_update(IsA(http.HttpRequest), server.id, server.name).AndRaise(self.exceptions.nova) self.mox.ReplayAll() formData = {"method": "UpdateInstance", "instance": server.id, "name": server.name, "tenant_id": self.tenant.id} url = reverse("horizon:project:instances:update", args=[server.id]) res = self.client.post(url, formData) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_instance_details_volumes(self): server = self.servers.first() volumes = [self.volumes.list()[1]] api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.instance_volumes_list(IsA(http.HttpRequest), server.id).AndReturn(volumes) api.flavor_get(IsA(http.HttpRequest), server.flavor["id"]).AndReturn(self.flavors.first()) api.server_security_groups(IsA(http.HttpRequest), server.id).AndReturn(self.security_groups.first()) self.mox.ReplayAll() url = reverse("horizon:project:instances:detail", args=[server.id]) res = self.client.get(url) self.assertItemsEqual(res.context["instance"].volumes, volumes)
def test_create_instance_snapshot(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.snapshot_create(IsA(http.HttpRequest), server.id, "snapshot1").AndReturn(self.snapshots.first()) api.snapshot_list_detailed(IsA(http.HttpRequest), marker=None).AndReturn([[], False]) api.image_list_detailed(IsA(http.HttpRequest), marker=None).AndReturn([[], False]) api.volume_snapshot_list(IsA(http.HttpRequest)).AndReturn([]) self.mox.ReplayAll() formData = {"instance_id": server.id, "method": "CreateSnapshot", "name": "snapshot1"} url = reverse("horizon:project:images_and_snapshots:snapshots:create", args=[server.id]) redir_url = reverse("horizon:project:images_and_snapshots:index") res = self.client.post(url, formData) self.assertRedirects(res, redir_url)
def test_instance_vnc(self): server = self.servers.first() CONSOLE_OUTPUT = '/vncserver' console_mock = self.mox.CreateMock(api.VNCConsole) console_mock.url = CONSOLE_OUTPUT self.mox.StubOutWithMock(api, 'server_vnc_console') self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.server_vnc_console(IgnoreArg(), server.id).AndReturn(console_mock) self.mox.ReplayAll() url = reverse('horizon:project:instances:vnc', args=[server.id]) res = self.client.get(url) redirect = CONSOLE_OUTPUT + '&title=%s(1)' % server.name self.assertRedirectsNoFollow(res, redirect)
def test_instance_vnc(self): server = self.servers.first() CONSOLE_OUTPUT = "/vncserver" console_mock = self.mox.CreateMock(api.VNCConsole) console_mock.url = CONSOLE_OUTPUT self.mox.StubOutWithMock(api, "server_vnc_console") self.mox.StubOutWithMock(api, "server_get") api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.server_vnc_console(IgnoreArg(), server.id).AndReturn(console_mock) self.mox.ReplayAll() url = reverse("horizon:project:instances:vnc", args=[server.id]) res = self.client.get(url) redirect = CONSOLE_OUTPUT + "&title=%s(1)" % server.name self.assertRedirectsNoFollow(res, redirect)
def test_instance_details_volumes(self): server = self.servers.first() volumes = [self.volumes.list()[1]] api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.volume_instance_list(IsA(http.HttpRequest), server.id).AndReturn(volumes) api.flavor_get(IsA(http.HttpRequest), server.flavor['id']).AndReturn(self.flavors.first()) api.server_security_groups(IsA(http.HttpRequest), server.id).AndReturn( self.security_groups.first()) self.mox.ReplayAll() url = reverse('horizon:project:instances:detail', args=[server.id]) res = self.client.get(url) self.assertItemsEqual(res.context['instance'].volumes, volumes)
def test_instance_update_post_api_exception(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.server_update(IsA(http.HttpRequest), server.id, server.name) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() formData = {'method': 'UpdateInstance', 'instance': server.id, 'name': server.name, 'tenant_id': self.tenant.id} url = reverse('horizon:project:instances:update', args=[server.id]) res = self.client.post(url, formData) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_server_get(self): server = self.servers.first() novaclient = self.stub_novaclient() novaclient.servers = self.mox.CreateMockAnything() novaclient.servers.get(server.id).AndReturn(server) self.mox.ReplayAll() ret_val = api.server_get(self.request, server.id) self.assertIsInstance(ret_val, api.nova.Server)
def get_object(self, *args, **kwargs): if not hasattr(self, "_object"): instance_id = self.kwargs['instance_id'] try: self._object = api.server_get(self.request, instance_id) except: redirect = reverse("horizon:project:instances:index") msg = _('Unable to retrieve instance details.') exceptions.handle(self.request, msg, redirect=redirect) return self._object
def test_instance_update_post(self): server = self.servers.first() api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.server_update(IsA(http.HttpRequest), server.id, server.name).AndReturn(server) self.mox.ReplayAll() formData = {'method': 'UpdateInstance', 'instance': server.id, 'name': server.name, 'tenant_id': self.tenant.id} url = reverse('horizon:project:instances:update', args=[server.id]) res = self.client.post(url, formData) self.assertRedirectsNoFollow(res, INDEX_URL)
def vnc(request, instance_id): try: console = api.server_vnc_console(request, instance_id) instance = api.server_get(request, instance_id) return shortcuts.redirect(console.url + ("&title=%s(%s)" % (instance.name, instance_id))) except: redirect = reverse("horizon:project:instances:index") msg = _('Unable to get VNC console for instance "%s".') % instance_id exceptions.handle(request, msg, redirect=redirect)
def get_object(self): if not hasattr(self, "_object"): try: self._object = api.server_get(self.request, self.kwargs["instance_id"]) except: redirect = reverse('horizon:project:instances:index') exceptions.handle(self.request, _("Unable to retrieve instance."), redirect=redirect) return self._object
def test_create_snapshot_post(self): server = self.servers.first() snapshot = self.snapshots.first() self.mox.StubOutWithMock(api, 'server_get') self.mox.StubOutWithMock(api, 'snapshot_create') api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.snapshot_create(IsA(http.HttpRequest), server.id, snapshot.name) \ .AndReturn(snapshot) self.mox.ReplayAll() formData = {'method': 'CreateSnapshot', 'tenant_id': self.tenant.id, 'instance_id': server.id, 'name': snapshot.name} url = reverse('horizon:project:images_and_snapshots:snapshots:create', args=[server.id]) res = self.client.post(url, formData) self.assertRedirectsNoFollow(res, INDEX_URL)
def update(request, tenant_id, instance_id): try: instance = api.server_get(request, instance_id) except api_exceptions.ApiException, e: LOG.error('ApiException while fetching instance info', exc_info=True) messages.error(request, 'Unable to get information for instance %s: %s' % (instance_id, e.message)) return shortcuts.redirect('dash_instances', tenant_id)
def handle(self, request, data): instance_id = data['instance'] instance = api.server_get(request, instance_id) try: api.server_delete(request, instance) except api_exceptions.ApiException, e: LOG.error('ApiException while terminating instance "%s"' % instance_id, exc_info=True) messages.error(request, 'Unable to terminate %s: %s' % (instance_id, e.message,))
def test_create_snapshot_post(self): server = self.servers.first() snapshot = self.snapshots.first() self.mox.StubOutWithMock(api, 'server_get') self.mox.StubOutWithMock(api, 'snapshot_create') api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.snapshot_create(IsA(http.HttpRequest), server.id, snapshot.name) \ .AndReturn(snapshot) self.mox.ReplayAll() formData = { 'method': 'CreateSnapshot', 'tenant_id': self.tenant.id, 'instance_id': server.id, 'name': snapshot.name } url = reverse('horizon:project:images_and_snapshots:snapshots:create', args=[server.id]) res = self.client.post(url, formData) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_instance_details_volume_sorting(self): server = self.servers.first() volumes = self.volumes.list()[1:3] api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) api.instance_volumes_list(IsA(http.HttpRequest), server.id).AndReturn(volumes) api.flavor_get(IsA(http.HttpRequest), server.flavor['id']).AndReturn(self.flavors.first()) api.server_security_groups(IsA(http.HttpRequest), server.id).AndReturn(self.security_groups.first()) self.mox.ReplayAll() url = reverse('horizon:project:instances:detail', args=[server.id]) res = self.client.get(url) self.assertItemsEqual(res.context['instance'].volumes, volumes) self.assertEquals(res.context['instance'].volumes[0].device, "/dev/hda") self.assertEquals(res.context['instance'].volumes[1].device, "/dev/hdk")
def handle(self, request, data): try: snapshot = api.snapshot_create(request, data['instance_id'], data['name']) # NOTE(gabriel): This API call is only to display a pretty name. instance = api.server_get(request, data['instance_id']) vals = {"name": data['name'], "inst": instance.name} messages.success(request, _('Snapshot "%(name)s" created for ' 'instance "%(inst)s"') % vals) return snapshot except: redirect = reverse("horizon:project:instances:index") exceptions.handle(request, _('Unable to create snapshot.'), redirect=redirect)
def get_data(self): if not hasattr(self, "_instance"): try: instance_id = self.kwargs['instance_id'] instance = api.server_get(self.request, instance_id) instance.volumes = api.instance_volumes_list(self.request, instance_id) # Sort by device name instance.volumes.sort(key=lambda vol: vol.device) instance.full_flavor = api.flavor_get(self.request, instance.flavor["id"]) instance.security_groups = api.server_security_groups( self.request, instance_id) except: redirect = reverse('horizon:project:instances:index') exceptions.handle(self.request, _('Unable to retrieve details for ' 'instance "%s".') % instance_id, redirect=redirect) self._instance = instance return self._instance
def get_data(self): if not hasattr(self, "_instance"): try: instance_id = self.kwargs['instance_id'] instance = api.server_get(self.request, instance_id) #instance.volumes = api.volume_instance_list(self.request, # instance_id) # Sort by device name #instance.volumes.sort(key=lambda vol: vol.device) instance.full_flavor = api.flavor_get(self.request, instance.flavor["id"]) instance.security_groups = api.server_security_groups( self.request, instance_id) except: redirect = reverse('horizon:project:instances:index') exceptions.handle(self.request, _('Unable to retrieve details for ' 'instance "%s".') % instance_id, redirect=redirect) self._instance = instance return self._instance
def get_data(self, request, instance_id): instance = api.server_get(request, instance_id) instance.full_flavor = api.flavor_get(request, instance.flavor["id"]) return instance