def test_authorize_token(self): f = fakes.FakeAuthManager() user = engine.auth.manager.User('id1', 'user1', 'user1_key', None, None) f.add_user(user) f.create_project('user1_project', user) req = webob.Request.blank('/v2/', {'HTTP_HOST': 'foo'}) req.headers['X-Auth-User'] = '******' req.headers['X-Auth-Key'] = 'user1_key' result = req.get_response(fakes.wsgi_app(fake_auth=False)) self.assertEqual(result.status, '204 No Content') self.assertEqual(len(result.headers['X-Auth-Token']), 40) self.assertEqual(result.headers['X-Server-Management-Url'], "http://foo/v2/user1_project") self.assertEqual(result.headers['X-CDN-Management-Url'], "") self.assertEqual(result.headers['X-Storage-Url'], "") token = result.headers['X-Auth-Token'] self.stubs.Set(engine.api.x7.v2, 'APIRouter', fakes.FakeRouter) req = webob.Request.blank('/v2/user1_project') req.headers['X-Auth-Token'] = token result = req.get_response(fakes.wsgi_app(fake_auth=False)) self.assertEqual(result.status, '200 OK') self.assertEqual(result.headers['X-Test-Success'], 'True')
def test_vsa_volume_create(self): self.stubs.Set(volume.api.API, "create", stub_volume_create) vol = { "size": 100, "displayName": "VSA Volume Test Name", "displayDescription": "VSA Volume Test Desc" } body = {self.test_obj: vol} req = webob.Request.blank('/v2/777/zadr-vsa/123/%s' % self.test_objs) req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) if self.test_obj == "volume": self.assertEqual(resp.status_int, 200) resp_dict = json.loads(resp.body) self.assertTrue(self.test_obj in resp_dict) self.assertEqual(resp_dict[self.test_obj]['size'], vol['size']) self.assertEqual(resp_dict[self.test_obj]['displayName'], vol['displayName']) self.assertEqual(resp_dict[self.test_obj]['displayDescription'], vol['displayDescription']) else: self.assertEqual(resp.status_int, 400)
def test_create_root_volume(self): body = dict(server=dict( name='test_server', imageRef=IMAGE_UUID, flavorRef=2, min_count=1, max_count=1, block_device_mapping=[dict( volume_id=1, device_name='/dev/vda', virtual='root', delete_on_termination=False, )] )) global _block_device_mapping_seen _block_device_mapping_seen = None req = webob.Request.blank('/v2/fake/os-volumes_boot') req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 202) server = json.loads(res.body)['server'] self.assertEqual(FAKE_UUID, server['id']) self.assertEqual(FLAGS.password_length, len(server['adminPass'])) self.assertEqual(len(_block_device_mapping_seen), 1) self.assertEqual(_block_device_mapping_seen[0]['volume_id'], 1) self.assertEqual(_block_device_mapping_seen[0]['device_name'], '/dev/vda')
def test_keypair_import(self): body = { 'keypair': { 'name': 'create_test', 'public_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBYIznA' 'x9D7118Q1VKGpXy2HDiKyUTM8XcUuhQpo0srqb9rboUp4' 'a9NmCwpWpeElDLuva707GOUnfaBAvHBwsRXyxHJjRaI6Y' 'Qj2oLJwqvaSaWUbyT1vtryRqy6J3TecN0WINY71f4uymi' 'MZP0wby4bKBcYnac8KiCIlvkEl0ETjkOGUq8OyWRmn7lj' 'j5SESEUdBP0JnuTFKddWTU/wD6wydeJaUhBTqOlHn0kX1' 'GyqoNTE1UEhcM5ZRWgfUZfTjVyDF2kGj3vJLCJtJ8LoGc' 'j7YaN4uPg1rBle+izwE/tLonRrds+cev8p6krSSrxWOwB' 'bHkXa6OciiJDvkRzJXzf', }, } req = webob.Request.blank('/v2/123/os-keypairs') req.method = 'POST' req.body = json.dumps(body) req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) # FIXME(ja): sholud we check that public_key was sent to create? res_dict = json.loads(res.body) self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0) self.assertFalse('private_key' in res_dict['keypair'])
def test_get_version_list_xml(self): req = webob.Request.blank('/') req.accept = "application/xml" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/xml") root = etree.XML(res.body) print res.body xmlutil.validate_schema(root, 'versions') self.assertTrue(root.xpath('/ns:versions', namespaces=NS)) versions = root.xpath('ns:version', namespaces=NS) self.assertEqual(len(versions), 1) for i, v in enumerate(['v2.0']): version = versions[i] expected = VERSIONS[v] for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) (link, ) = version.xpath('atom:link', namespaces=NS) self.assertTrue( common.compare_links(link, [{ 'rel': 'self', 'href': 'http://localhost/%s/' % v }]))
def test_vsa_volume_show_no_volume(self): self.stubs.Set(volume.api.API, "get", stub_volume_get_notfound) req = webob.Request.blank('/v2/777/zadr-vsa/123/%s/333' % \ (self.test_objs)) resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 404)
def test_snapshot_create_force(self): global _last_param _last_param = {} snapshot = {"volume_id": 12, "force": True, "display_name": "Snapshot Test Name", "display_description": "Snapshot Test Desc"} body = dict(snapshot=snapshot) req = webob.Request.blank('/v1.1/777/os-snapshots') req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) LOG.debug(_("test_snapshot_create_force: param=%s"), _last_param) self.assertEqual(resp.status_int, 200) # Compare if parameters were correctly passed to stub self.assertEqual(_last_param['display_name'], "Snapshot Test Name") self.assertEqual(_last_param['display_description'], "Snapshot Test Desc") resp_dict = json.loads(resp.body) LOG.debug(_("test_snapshot_create_force: resp_dict=%s"), resp_dict) self.assertTrue('snapshot' in resp_dict) self.assertEqual(resp_dict['snapshot']['displayName'], snapshot['display_name']) self.assertEqual(resp_dict['snapshot']['displayDescription'], snapshot['display_description'])
def test_bad_user_bad_key(self): req = webob.Request.blank('/v2/') req.headers['X-Auth-User'] = '******' req.headers['X-Auth-Key'] = 'unknown_user_key' req.headers['X-Auth-Project-Id'] = 'user_project' result = req.get_response(fakes.wsgi_app(fake_auth=False)) self.assertEqual(result.status, '401 Unauthorized')
def test_multi_choice_server(self): uuid = str(utils.gen_uuid()) req = webob.Request.blank('/servers/' + uuid) req.accept = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 300) self.assertEqual(res.content_type, "application/json") expected = { "choices": [ { "id": "v2.0", "status": "CURRENT", "links": [ { "href": "http://localhost/v2/servers/" + uuid, "rel": "self", }, ], "media-types": [ { "base": "application/xml", "type": "application/vnd.x7.compute+xml" ";version=2" }, { "base": "application/json", "type": "application/vnd.x7.compute+json" ";version=2" }, ], }, ], } self.assertDictMatch(expected, json.loads(res.body))
def test_vsa_volume_create(self): self.stubs.Set(volume.api.API, "create", stub_volume_create) vol = {"size": 100, "displayName": "VSA Volume Test Name", "displayDescription": "VSA Volume Test Desc"} body = {self.test_obj: vol} req = webob.Request.blank('/v2/777/zadr-vsa/123/%s' % self.test_objs) req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) if self.test_obj == "volume": self.assertEqual(resp.status_int, 200) resp_dict = json.loads(resp.body) self.assertTrue(self.test_obj in resp_dict) self.assertEqual(resp_dict[self.test_obj]['size'], vol['size']) self.assertEqual(resp_dict[self.test_obj]['displayName'], vol['displayName']) self.assertEqual(resp_dict[self.test_obj]['displayDescription'], vol['displayDescription']) else: self.assertEqual(resp.status_int, 400)
def test_get_version_list_atom(self): req = webob.Request.blank('/') req.accept = "application/atom+xml" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/atom+xml") f = feedparser.parse(res.body) self.assertEqual(f.feed.title, 'Available API Versions') self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') self.assertEqual(f.feed.id, 'http://localhost/') self.assertEqual(f.feed.author, 'Rackspace') self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') self.assertEqual(f.feed.links[0]['href'], 'http://localhost/') self.assertEqual(f.feed.links[0]['rel'], 'self') self.assertEqual(len(f.entries), 1) entry = f.entries[0] self.assertEqual(entry.id, 'http://localhost/v2/') self.assertEqual(entry.title, 'Version v2.0') self.assertEqual(entry.updated, '2011-01-21T11:33:21Z') self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version v2.0 CURRENT (2011-01-21T11:33:21Z)') self.assertEqual(len(entry.links), 1) self.assertEqual(entry.links[0]['href'], 'http://localhost/v2/') self.assertEqual(entry.links[0]['rel'], 'self')
def test_multi_choice_image_xml(self): req = webob.Request.blank('/images/1') req.accept = "application/xml" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 300) self.assertEqual(res.content_type, "application/xml") root = etree.XML(res.body) self.assertTrue(root.xpath('/ns:choices', namespaces=NS)) versions = root.xpath('ns:version', namespaces=NS) self.assertEqual(len(versions), 1) version = versions[0] self.assertEqual(version.get('id'), 'v2.0') self.assertEqual(version.get('status'), 'CURRENT') media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) self.assertTrue( common.compare_media_types(media_types, VERSIONS['v2.0']['media-types'])) links = version.xpath('atom:link', namespaces=NS) self.assertTrue( common.compare_links(links, [{ 'rel': 'self', 'href': 'http://localhost/v2/images/1' }]))
def test_keypair_list(self): req = webob.Request.blank('/v2/123/os-keypairs') res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) response = {'keypairs': [{'keypair': fake_keypair('FAKE')}]} self.assertEqual(res_dict, response)
def test_get_version_list_302(self): req = webob.Request.blank('/v2') req.accept = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 302) redirect_req = webob.Request.blank('/v2/') self.assertEqual(res.location, redirect_req.url)
def test_vsa_create(self): global last_param last_param = {} vsa = { "displayName": "VSA Test Name", "displayDescription": "VSA Test Desc" } body = dict(vsa=vsa) req = webob.Request.blank('/v2/777/zadr-vsa') req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 200) # Compare if parameters were correctly passed to stub self.assertEqual(last_param['display_name'], "VSA Test Name") self.assertEqual(last_param['display_description'], "VSA Test Desc") resp_dict = json.loads(resp.body) self.assertTrue('vsa' in resp_dict) self.assertEqual(resp_dict['vsa']['displayName'], vsa['displayName']) self.assertEqual(resp_dict['vsa']['displayDescription'], vsa['displayDescription'])
def setUp(self): super(CloudpipeTest, self).setUp() self.flags(allow_admin_api=True) self.app = fakes.wsgi_app() inner_app = v2.APIRouter() adm_ctxt = context.get_admin_context() self.app = auth.InjectContext(adm_ctxt, inner_app) route = inner_app.map.match('/1234/os-cloudpipe') self.controller = route['controller'].controller fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) self.stubs.Set(db, "instance_get_all_by_project", db_instance_get_all_by_project) self.stubs.Set(db, "security_group_exists", db_security_group_exists) self.stubs.SmartSet(self.controller.cloudpipe, "launch_vpn_instance", pipelib_launch_vpn_instance) #self.stubs.SmartSet(self.controller.auth_manager, "get_project", # auth_manager_get_project) #self.stubs.SmartSet(self.controller.auth_manager, "get_projects", # auth_manager_get_projects) # NOTE(todd): The above code (just setting the stub, not invoking it) # causes failures in AuthManagerLdapTestCase. So use a fake object. self.controller.auth_manager = FakeAuthManager() self.stubs.Set(utils, 'vpn_ping', utils_vpn_ping) self.context = context.get_admin_context() global EMPTY_INSTANCE_LIST EMPTY_INSTANCE_LIST = True
def test_verify_index(self): req = webob.Request.blank( '/v2/123/os-simple-tenant-usage?start=%s&end=%s' % (START.isoformat(), STOP.isoformat())) req.method = "GET" req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app( fake_auth_context=self.admin_context)) self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) usages = res_dict['tenant_usages'] from engine import log as logging logging.warn(usages) for i in xrange(TENANTS): self.assertEqual(int(usages[i]['total_hours']), SERVERS * HOURS) self.assertEqual(int(usages[i]['total_local_gb_usage']), SERVERS * LOCAL_GB * HOURS) self.assertEqual(int(usages[i]['total_memory_mb_usage']), SERVERS * MEMORY_MB * HOURS) self.assertEqual(int(usages[i]['total_vcpus_usage']), SERVERS * VCPUS * HOURS) self.assertFalse(usages[i].get('server_usages'))
def test_vsa_create_no_body(self): req = webob.Request.blank('/v2/777/zadr-vsa') req.method = 'POST' req.body = json.dumps({}) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 422)
def test_malformed_xml(self): req = webob.Request.blank('/') req.method = 'POST' req.body = '<hi im not xml>' req.headers["content-type"] = "application/xml" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 400)
def test_verify_index_fails_for_nonadmin(self): req = webob.Request.blank('/v2/123/os-simple-tenant-usage?' 'detailed=1&start=%s&end=%s' % (START.isoformat(), STOP.isoformat())) req.method = "GET" req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 403)
def test_get_text_console_bad_body(self): body = {} req = webob.Request.blank('/v1.1/123/servers/1/action') req.method = "POST" req.body = json.dumps(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 400)
def test_unrescue(self): body = dict(unrescue=None) req = webob.Request.blank('/v2/123/servers/test_inst/action') req.method = "POST" req.body = json.dumps(body) req.headers["content-type"] = "application/json" resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 202)
def test_get_account(self): req = webob.Request.blank('/v2/fake/accounts/test1') res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(res.status_int, 200) self.assertEqual(res_dict['account']['id'], 'test1') self.assertEqual(res_dict['account']['name'], 'test1') self.assertEqual(res_dict['account']['manager'], 'id1')
def test_accept_version_v2(self): """Test Accept header specifying v2 returns v2 content.""" req = webob.Request.blank('/') req.accept = "application/json;version=2" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/json") body = json.loads(res.body) self.assertEqual(body['version']['id'], 'v2.0')
def test_path_version_v1_1(self): """Test URL path specifying v1.1 returns v2 content.""" req = webob.Request.blank('/v1.1/') req.accept = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/json") body = json.loads(res.body) self.assertEqual(body['version']['id'], 'v2.0')
def test_admin_api_actions(self): self.maxDiff = None app = fakes.wsgi_app() for _action in self._actions: req = webob.Request.blank('/v2/fake/servers/%s/action' % self.UUID) req.method = 'POST' req.body = json.dumps({_action: None}) req.content_type = 'application/json' res = req.get_response(app) self.assertEqual(res.status_int, 202)
def test_get_console_output_with_tail(self): body = {'os-getConsoleOutput': {'length': 3}} req = webob.Request.blank('/v2/123/servers/1/action') req.method = "POST" req.body = json.dumps(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) output = json.loads(res.body) self.assertEqual(res.status_int, 200) self.assertEqual(output, {'output': '2\n3\n4'})
def test_get_text_console_no_instance(self): self.stubs.Set(compute.API, 'get', fake_get_not_found) body = {'os-getConsoleOutput': {}} req = webob.Request.blank('/v1.1/123/servers/1/action') req.method = "POST" req.body = json.dumps(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 404)
def test_multi_choice_server_atom(self): """ Make sure multi choice responses do not have content-type application/atom+xml (should use default of json) """ req = webob.Request.blank('/servers') req.accept = "application/atom+xml" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 300) self.assertEqual(res.content_type, "application/json")
def test_verify_show_cant_view_other_tenant(self): req = webob.Request.blank('/v2/faketenant_1/os-simple-tenant-usage/' 'faketenant_0?start=%s&end=%s' % (START.isoformat(), STOP.isoformat())) req.method = "GET" req.headers["content-type"] = "application/json" res = req.get_response( fakes.wsgi_app(fake_auth_context=self.alt_user_context)) self.assertEqual(res.status_int, 403)
def test_verify_index_fails_for_nonadmin(self): req = webob.Request.blank( '/v2/123/os-simple-tenant-usage?' 'detailed=1&start=%s&end=%s' % (START.isoformat(), STOP.isoformat())) req.method = "GET" req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 403)
def test_snapshot_show_invalid_id(self): global _last_param _last_param = {} snapshot_id = 234 req = webob.Request.blank('/v1.1/777/os-snapshots/%d' % snapshot_id) req.method = 'GET' resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 404) self.assertEqual(str(_last_param['snapshot_id']), str(snapshot_id))
def test_vsa_show_invalid_id(self): global last_param last_param = {} vsa_id = 234 req = webob.Request.blank('/v2/777/zadr-vsa/%d' % vsa_id) req.method = 'GET' resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 404) self.assertEqual(str(last_param['vsa_id']), str(vsa_id))
def test_vsa_volume_delete(self): obj_num = 234 if self.test_objs == "volumes" else 345 req = webob.Request.blank('/v2/777/zadr-vsa/123/%s/%s' % \ (self.test_objs, obj_num)) req.method = 'DELETE' resp = req.get_response(fakes.wsgi_app()) if self.test_obj == "volume": self.assertEqual(resp.status_int, 202) else: self.assertEqual(resp.status_int, 400)
def test_bad_user_good_key(self): f = fakes.FakeAuthManager() user = engine.auth.manager.User('id1', 'user1', 'user1_key', None, None) f.add_user(user) req = webob.Request.blank('/v2/') req.headers['X-Auth-User'] = '******' req.headers['X-Auth-Key'] = 'user1_key' result = req.get_response(fakes.wsgi_app(fake_auth=False)) self.assertEqual(result.status, '401 Unauthorized')
def test_get_account_xml(self): req = webob.Request.blank('/v2/fake/accounts/test1.xml') res = req.get_response(fakes.wsgi_app()) res_tree = etree.fromstring(res.body) self.assertEqual(res.status_int, 200) self.assertEqual('account', res_tree.tag) self.assertEqual('test1', res_tree.get('id')) self.assertEqual('test1', res_tree.get('name')) self.assertEqual('id1', res_tree.get('manager'))
def test_rescue_with_preset_password(self): body = {"rescue": {"adminPass": "******"}} req = webob.Request.blank('/v2/123/servers/test_inst/action') req.method = "POST" req.body = json.dumps(body) req.headers["content-type"] = "application/json" resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 200) resp_json = json.loads(resp.body) self.assertEqual("AABBCC112233", resp_json['adminPass'])
def test_rescue_generates_password(self): body = dict(rescue=None) req = webob.Request.blank('/v2/123/servers/test_inst/action') req.method = "POST" req.body = json.dumps(body) req.headers["content-type"] = "application/json" resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 200) resp_json = json.loads(resp.body) self.assertEqual(FLAGS.password_length, len(resp_json['adminPass']))