def test_terminate_instance_exception(self): formData = {'method': 'TerminateInstance', 'instance': self.servers[0].id, } self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.servers[0].id)).AndReturn(self.servers[0]) exception = api_exceptions.ApiException('ApiException', message='apiException') self.mox.StubOutWithMock(api, 'server_delete') api.server_delete(IsA(http.HttpRequest), self.servers[0]).AndRaise(exception) self.mox.StubOutWithMock(messages, 'error') messages.error(IsA(http.HttpRequest), IsA(unicode)) self.mox.ReplayAll() res = self.client.post(reverse('dash_instances', args=[self.TEST_TENANT]), formData) self.assertRedirectsNoFollow(res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_create_snapshot_post(self): SNAPSHOT_NAME = 'snappy' new_snapshot = self.mox.CreateMock(api.Image) new_snapshot.name = SNAPSHOT_NAME formData = {'method': 'CreateSnapshot', 'tenant_id': self.TEST_TENANT, 'instance_id': self.good_server.id, 'name': SNAPSHOT_NAME} self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.good_server.id)).AndReturn(self.good_server) self.mox.StubOutWithMock(api, 'snapshot_create') api.snapshot_create(IsA(http.HttpRequest), str(self.good_server.id), SNAPSHOT_NAME).\ AndReturn(new_snapshot) self.mox.ReplayAll() res = self.client.post(reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.good_server.id]), formData) self.assertRedirectsNoFollow(res, reverse('dash_snapshots', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_create_snapshot_post(self): SNAPSHOT_NAME = 'snappy' new_snapshot = self.mox.CreateMock(api.Image) new_snapshot.name = SNAPSHOT_NAME formData = { 'method': 'CreateSnapshot', 'tenant_id': self.TEST_TENANT, 'instance_id': self.good_server.id, 'name': SNAPSHOT_NAME } self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.good_server.id)).AndReturn(self.good_server) self.mox.StubOutWithMock(api, 'snapshot_create') api.snapshot_create(IsA(http.HttpRequest), str(self.good_server.id), SNAPSHOT_NAME).\ AndReturn(new_snapshot) self.mox.ReplayAll() res = self.client.post( reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.good_server.id]), formData) self.assertRedirectsNoFollow( res, reverse('dash_snapshots', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_instance_update_post_api_exception(self): INSTANCE_ID = self.servers[0].id NAME = 'myname' DESC = 'mydesc' formData = {'method': 'UpdateInstance', 'instance': INSTANCE_ID, 'name': NAME, 'tenant_id': self.TEST_TENANT, 'description': DESC} self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), unicode(INSTANCE_ID)).AndReturn(self.servers[0]) exception = api_exceptions.ApiException('apiException') self.mox.StubOutWithMock(api, 'server_update') api.server_update(IsA(http.HttpRequest), str(INSTANCE_ID), NAME, DESC).\ AndRaise(exception) self.mox.ReplayAll() res = self.client.post(reverse('dash_instances_update', args=[self.TEST_TENANT, INSTANCE_ID]), formData) self.assertRedirectsNoFollow(res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_create_snapshot_get(self): self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.good_server.id)).AndReturn(self.good_server) self.mox.ReplayAll() res = self.client.get(reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.good_server.id])) self.assertTemplateUsed(res, 'dash_snapshots_create.html') self.mox.VerifyAll()
def test_create_snapshot_get_with_invalid_status(self): self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.bad_server.id)).AndReturn(self.bad_server) self.mox.ReplayAll() res = self.client.get( reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.bad_server.id])) self.assertRedirectsNoFollow( res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_create_snapshot_get(self): self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.good_server.id)).AndReturn(self.good_server) self.mox.ReplayAll() res = self.client.get( reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.good_server.id])) self.assertTemplateUsed(res, 'django_openstack/dash/snapshots/create.html') self.mox.VerifyAll()
def test_create_snapshot_get_with_invalid_status(self): self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.bad_server.id)).AndReturn(self.bad_server) self.mox.ReplayAll() res = self.client.get(reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.bad_server.id])) self.assertRedirectsNoFollow(res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_instance_update_get(self): INSTANCE_ID = self.servers[0].id self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), unicode(INSTANCE_ID)).AndReturn(self.servers[0]) self.mox.ReplayAll() res = self.client.get(reverse('dash_instances_update', args=[self.TEST_TENANT, INSTANCE_ID])) self.assertTemplateUsed(res, 'dash_instance_update.html') self.mox.VerifyAll()
def test_instance_update_get(self): INSTANCE_ID = self.servers[0].id self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), unicode(INSTANCE_ID)).AndReturn(self.servers[0]) self.mox.ReplayAll() res = self.client.get(reverse('dash_instances_update', args=[self.TEST_TENANT, INSTANCE_ID])) self.assertTemplateUsed(res, 'django_openstack/dash/instances/update.html') self.mox.VerifyAll()
def test_create_get_server_exception(self): self.mox.StubOutWithMock(api, 'server_get') exception = api_exceptions.ApiException('apiException') api.server_get(IsA(http.HttpRequest), str(self.good_server.id)).AndRaise(exception) self.mox.ReplayAll() res = self.client.get(reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.good_server.id])) self.assertRedirectsNoFollow(res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def detail(request, instance_id): try: instance = api.server_get(request, instance_id) try: console = api.console_create(request, instance_id, 'vnc') vnc_url = "%s&title=%s(%s)" % (console.output, instance.name, instance_id) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance vnc \ connection') messages.error( request, _('Unable to get vnc console for instance %(inst)s: %(message)s' ) % { "inst": instance_id, "message": e.message }) return redirect('dash_instances', tenant_id) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance info') messages.error( request, _('Unable to get information for instance %(inst)s: %(message)s') % { "inst": instance_id, "message": e.message }) return redirect('dash_instances', tenant_id)
def test_create_get_server_exception(self): self.mox.StubOutWithMock(api, 'server_get') exception = api_exceptions.ApiException('apiException') api.server_get(IsA(http.HttpRequest), str(self.good_server.id)).AndRaise(exception) self.mox.ReplayAll() res = self.client.get( reverse('dash_snapshots_create', args=[self.TEST_TENANT, self.good_server.id])) self.assertRedirectsNoFollow( res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_instance_update_get_server_get_exception(self): INSTANCE_ID = self.servers[0].id exception = api_exceptions.ApiException('apiException') self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), unicode(INSTANCE_ID)).AndRaise(exception) self.mox.ReplayAll() res = self.client.get(reverse('dash_instances_update', args=[self.TEST_TENANT, INSTANCE_ID])) self.assertRedirectsNoFollow(res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def update(request, tenant_id, instance_id): try: instance = api.server_get(request, instance_id) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance info') messages.error( request, 'Unable to get information for instance %s: %s' % (instance_id, e.message)) return shortcuts.redirect('dash_instances', tenant_id)
def update(request, tenant_id, instance_id): try: instance = api.server_get(request, instance_id) except api_exceptions.ApiException, e: LOG.exception(_('ApiException while fetching instance info')) messages.error(request, _('Unable to get information for instance %(inst)s: %(message)s') % {"inst": instance_id, "message": e.message}) return shortcuts.redirect('dash_instances', tenant_id)
def update(request, tenant_id, instance_id): try: instance = api.server_get(request, instance_id) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance info') 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: messages.error(request, 'Unable to terminate %s: %s' % (instance_id, e.message,))
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.exception('ApiException while terminating instance "%s"' % instance_id) messages.error( request, _("Unable to terminate %(inst)s: %(message)s") % {"inst": instance_id, "message": e.message} )
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 vnc(request, tenant_id, instance_id): try: console = api.console_create(request, instance_id, 'vnc') instance = api.server_get(request, instance_id) return shortcuts.redirect(console.output + ("&title=%s(%s)" % (instance.name, instance_id))) except api_exceptions.ApiException, e: LOG.exception(_('ApiException while fetching instance vnc connection')) messages.error(request, _('Unable to get vnc console for instance %(inst)s: %(message)s') % {"inst": instance_id, "message": e.message}) return shortcuts.redirect('dash_instances', tenant_id)
def vnc(request, tenant_id, instance_id): try: console = api.console_create(request, instance_id, 'vnc') instance = api.server_get(request, instance_id) return shortcuts.redirect(console.output + ("&title=%s(%s)" % (instance.name, instance_id))) except api_exceptions.ApiException, e: LOG.error('ApiException while fetching instance vnc connection', exc_info=True) messages.error(request, 'Unable to get vnc console for instance %s: %s' % (instance_id, e.message)) return shortcuts.redirect('dash_instances', tenant_id)
def detail(request, tenant_id, volume_id): try: volume = api.volume_get(request, volume_id) attachment = volume.attachments[0] if attachment: instance = api.server_get( request, volume.attachments[0]['serverId']) else: instance = None except novaclient_exceptions.ClientException, e: LOG.exception("ClientException in volume get") messages.error(request, _('Error fetching volume: %s') % e.message) return redirect('dash_volumes', tenant_id)
def vnc(request, tenant_id, instance_id): try: console = api.console_create(request, instance_id, 'vnc') instance = api.server_get(request, instance_id) return shortcuts.redirect(console.output + ("&title=%s(%s)" % (instance.name, instance_id))) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance vnc connection') messages.error( request, 'Unable to get vnc console for instance %s: %s' % (instance_id, e.message)) return shortcuts.redirect('dash_instances', tenant_id)
def update(request, tenant_id, instance_id): try: instance = api.server_get(request, instance_id) except api_exceptions.ApiException, e: LOG.exception(_('ApiException while fetching instance info')) messages.error( request, _('Unable to get information for instance %(inst)s: %(message)s') % { "inst": instance_id, "message": e.message }) return shortcuts.redirect('dash_instances', tenant_id)
def test_terminate_instance(self): formData = {'method': 'TerminateInstance', 'instance': self.servers[0].id, } self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.servers[0].id)).AndReturn(self.servers[0]) self.mox.StubOutWithMock(api, 'server_delete') api.server_delete(IsA(http.HttpRequest), self.servers[0]) self.mox.ReplayAll() res = self.client.post(reverse('dash_instances', args=[self.TEST_TENANT]), formData) self.assertRedirectsNoFollow(res, reverse('dash_instances', args=[self.TEST_TENANT])) self.mox.VerifyAll()
def test_server_reboot(self): INSTANCE_ID = "2" HARDNESS = "diamond" self.mox.StubOutWithMock(api, "server_get") server = self.mox.CreateMock(OSCompute.Server) server.reboot(OSCompute.servers.REBOOT_HARD).AndReturn(TEST_RETURN) api.server_get(IsA(http.HttpRequest), INSTANCE_ID).AndReturn(server) server = self.mox.CreateMock(OSCompute.Server) server.reboot(HARDNESS).AndReturn(TEST_RETURN) api.server_get(IsA(http.HttpRequest), INSTANCE_ID).AndReturn(server) self.mox.ReplayAll() ret_val = api.server_reboot(self.request, INSTANCE_ID) self.assertIsNone(ret_val) ret_val = api.server_reboot(self.request, INSTANCE_ID, hardness=HARDNESS) self.assertIsNone(ret_val) self.mox.VerifyAll()
def create(request, tenant_id, instance_id): form, handled = CreateSnapshot.maybe_handle(request, initial={'tenant_id': tenant_id, 'instance_id': instance_id}) if handled: return handled try: instance = api.server_get(request, instance_id) except api_exceptions.ApiException, e: msg = "Unable to retreive instance: %s" % str(e) LOG.error(msg) messages.error(request, msg) return shortcuts.redirect('dash_instances', tenant_id)
def handle(self, request, data): try: LOG.info('Creating snapshot "%s"' % data['name']) snapshot = api.snapshot_create(request, data['instance_id'], data['name']) instance = api.server_get(request, data['instance_id']) messages.info(request, 'Snapshot "%s" created for instance "%s"' %\ (data['name'], instance.name)) return shortcuts.redirect('dash_snapshots', data['tenant_id']) except api_exceptions.ApiException, e: msg = 'Error Creating Snapshot: %s' % e.message LOG.error(msg, exc_info=True) messages.error(request, msg) return shortcuts.redirect(request.build_absolute_uri())
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.exception('ApiException while terminating instance "%s"' % instance_id) messages.error( request, 'Unable to terminate %s: %s' % ( instance_id, e.message, ))
def test_instance_vnc(self): INSTANCE_ID = self.servers[0].id CONSOLE_OUTPUT = '/vncserver' console_mock = self.mox.CreateMock(api.Console) console_mock.output = CONSOLE_OUTPUT self.mox.StubOutWithMock(api, 'console_create') self.mox.StubOutWithMock(api, 'server_get') api.server_get(IsA(http.HttpRequest), str(self.servers[0].id)).AndReturn(self.servers[0]) api.console_create(IgnoreArg(), unicode(INSTANCE_ID), 'vnc').AndReturn(console_mock) self.mox.ReplayAll() res = self.client.get(reverse('dash_instances_vnc', args=[self.TEST_TENANT, INSTANCE_ID])) self.assertRedirectsNoFollow(res, CONSOLE_OUTPUT + '&title=serverName(1)') self.mox.VerifyAll()
def test_server_get(self): INSTANCE_ID = "2" extras_api = self.stub_extras_api() extras_api.servers = self.mox.CreateMockAnything() extras_api.servers.get(INSTANCE_ID).AndReturn(TEST_RETURN) self.mox.ReplayAll() ret_val = api.server_get(self.request, INSTANCE_ID) self.assertIsInstance(ret_val, api.Server) self.assertEqual(ret_val._apiresource, TEST_RETURN) self.mox.VerifyAll()
def handle(self, request, data): try: LOG.info('Creating snapshot "%s"' % data['name']) snapshot = api.snapshot_create(request, data['instance_id'], data['name']) instance = api.server_get(request, data['instance_id']) messages.info(request, 'Snapshot "%s" created for instance "%s"' %\ (data['name'], instance.name)) return shortcuts.redirect('dash_snapshots', data['tenant_id']) except api_exceptions.ApiException, e: msg = 'Error Creating Snapshot: %s' % e.message LOG.exception(msg) messages.error(request, msg) return shortcuts.redirect(request.build_absolute_uri())
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.exception( _('ApiException while terminating instance "%s"') % instance_id) messages.error( request, _('Unable to terminate %(inst)s: %(message)s') % { "inst": instance_id, "message": e.message })
def handle(self, request, data): try: LOG.info('Creating snapshot "%s"' % data['name']) snapshot = api.snapshot_create(request, data['instance_id'], data['name']) instance = api.server_get(request, data['instance_id']) messages.info(request, _('Snapshot "%(name)s" created for instance "%(inst)s"') % {"name": data['name'], "inst": instance.name}) return shortcuts.redirect('dash_snapshots', data['tenant_id']) except api_exceptions.ApiException, e: msg = _('Error Creating Snapshot: %s') % e.message LOG.exception(msg) messages.error(request, msg) return shortcuts.redirect(request.build_absolute_uri())
def detail(request, tenant_id, instance_id): try: instance = api.server_get(request, instance_id) try: console = api.console_create(request, instance_id, 'vnc') vnc_url = "%s&title=%s(%s)" % (console.output, instance.name, instance_id) except api_exceptions.ApiException, e: LOG.exception(_('ApiException while fetching instance vnc \ connection')) messages.error(request, _('Unable to get vnc console for instance %(inst)s: %(msg)s') % {"inst": instance_id, "msg": e.message}) return shortcuts.redirect('dash_instances', tenant_id) except api_exceptions.ApiException, e: LOG.exception(_('ApiException while fetching instance info')) messages.error(request, _('Unable to get information for instance %(inst)s: %(msg)s') % {"inst": instance_id, "msg": e.message}) return shortcuts.redirect('dash_instances', tenant_id)
def detail(request, instance_id): try: instance = api.server_get(request, instance_id) try: console = api.console_create(request, instance_id, 'vnc') vnc_url = "%s&title=%s(%s)" % (console.output, instance.name, instance_id) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance vnc \ connection') messages.error(request, 'Unable to get vnc console for instance %s: %s' % (instance_id, e.message)) return redirect('dash_instances', tenant_id) except api_exceptions.ApiException, e: LOG.exception('ApiException while fetching instance info') messages.error(request, 'Unable to get information for instance %s: %s' % (instance_id, e.message)) return redirect('dash_instances', tenant_id)
@login_required def update(request, tenant_id, instance_id): if request.POST: form = UpdateInstance(request.POST) if form.is_valid(): data = form.clean() instance_id = data['instance'] name = data['name'] description = data.get('description', '') try: api.server_update(request, instance_id, name, description) messages.success(request, "Instance %s updated" % instance_id) except api_exceptions.ApiException, e: messages.error(request, 'Unable to update instance: %s' % e.message) return redirect('dash_instances', tenant_id) else: instance = api.server_get(request, instance_id) form = UpdateInstance(initial={'instance': instance_id, 'tenant_id': tenant_id, 'name': instance.name, 'description': instance.attrs['description']}) return render_to_response('dash_instance_update.html', { 'instance': instance, 'form': form, }, context_instance=template.RequestContext(request))