From 205810c3da4652fd0f5203f53299cd998ac7cf82 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Fri, 18 Feb 2011 11:44:06 +0100 Subject: added functionality to list only fixed ip addresses of one node and added exception handling to list method # nova-manage fixed list XXXX network IP address MAC address hostname host 10.xx.xx.0/24 10.xx.xx.5 02:16:3e:3f:33:b6 i-00000547 XXXX 10.xx.xx.0/24 10.xx.xx.9 02:16:3e:14:03:d6 i-00000548 XXXX 10.xx.xx.0/24 10.xx.xx.12 02:16:3e:20:1b:e7 i-00000549 XXXX --- bin/nova-manage | 19 ++++++++++++------- nova/db/api.py | 5 +++++ nova/db/sqlalchemy/api.py | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index 6d67252b8..60a00f1cf 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -439,10 +439,15 @@ class FixedIpCommands(object): def list(self, host=None): """Lists all fixed ips (optionally by host) arguments: [host]""" ctxt = context.get_admin_context() - if host == None: - fixed_ips = db.fixed_ip_get_all(ctxt) - else: - fixed_ips = db.fixed_ip_get_all_by_host(ctxt, host) + + try: + if host == None: + fixed_ips = db.fixed_ip_get_all(ctxt) + else: + fixed_ips = db.fixed_ip_get_all_by_host(ctxt, host) + except exception.NotFound as ex: + print "error: %s" % ex + sys.exit(2) print "%-18s\t%-15s\t%-17s\t%-15s\t%s" % (_('network'), _('IP address'), @@ -459,9 +464,9 @@ class FixedIpCommands(object): host = instance['host'] mac_address = instance['mac_address'] print "%-18s\t%-15s\t%-17s\t%-15s\t%s" % ( - fixed_ip['network']['cidr'], - fixed_ip['address'], - mac_address, hostname, host) + fixed_ip['network']['cidr'], + fixed_ip['address'], + mac_address, hostname, host) class FloatingIpCommands(object): diff --git a/nova/db/api.py b/nova/db/api.py index d7f3746d2..6053c0352 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -293,6 +293,11 @@ def fixed_ip_get_all(context): return IMPL.fixed_ip_get_all(context) +def fixed_ip_get_all_by_host(context, host): + """Get all defined fixed ips used by a host.""" + return IMPL.fixed_ip_get_all_by_host(context, host) + + def fixed_ip_get_by_address(context, address): """Get a fixed ip by address or raise if it does not exist.""" return IMPL.fixed_ip_get_by_address(context, address) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 2697fac73..d07c53759 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -594,6 +594,28 @@ def fixed_ip_get_all(context, session=None): return result +@require_admin_context +def fixed_ip_get_all_by_host(context, host=None): + session = get_session() + + # FIXME: I'm sure that SQLAlchemy can handle this in a nicer way + instances = session.query(models.Instance).\ + filter_by(state=1).\ + filter_by(host=host).\ + all() + + result = [] + for instance in instances: + result.append(session.query(models.FixedIp).\ + filter_by(instance_id=instance['id']).\ + first()) + + if not result: + raise exception.NotFound(_('No fixed ips for this host defined')) + + return result + + @require_context def fixed_ip_get_by_address(context, address, session=None): if not session: -- cgit From cf8cf8287169e3e0b996db7db5a135dea88db63a Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Fri, 18 Feb 2011 12:01:50 +0100 Subject: added new class Instances to manage instances and added a new listing method into the class # nova-manage instance list instance node type state launched image kernel ramdisk project user zone index i-00000547 XXXXXXX m1.small running 2011-02-18 08:36:37 ami-a03ndz0q ami-0isqekvw testing berendt None 0 i-00000548 XXXXXXX m1.small running 2011-02-18 08:37:17 ami-a03ndz0q ami-0isqekvw testing berendt None 1 i-00000549 XXXXXXX m1.small running 2011-02-18 08:37:52 ami-a03ndz0q ami-0isqekvw testing berendt None 2 --- bin/nova-manage | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/bin/nova-manage b/bin/nova-manage index 60a00f1cf..68847bdde 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -618,6 +618,45 @@ class DbCommands(object): print migration.db_version() +class InstanceCommands(object): + """Class for managing instances.""" + + def list(self, host=None, instance=None): + """Show a list of all instances""" + print "%-10s %-15s %-10s %-10s %-19s %-12s %-12s" \ + " %-12s %-10s %-10s %-10s %-5s" % ( + _('instance'), + _('node'), + _('type'), + _('state'), + _('launched'), + _('image'), + _('kernel'), + _('ramdisk'), + _('project'), + _('user'), + _('zone'), + _('index'), + ) + + for instance in db.instance_get_all(context.get_admin_context()): + print "%-10s %-15s %-10s %-10s %-19s %-12s %-12s %-12s" \ + " %-10s %-10s %-10s %-5d" % ( + instance['hostname'], + instance['host'], + instance['instance_type'], + instance['state_description'], + instance['launched_at'], + instance['image_id'], + instance['kernel_id'], + instance['ramdisk_id'], + instance['project_id'], + instance['user_id'], + instance['availability_zone'], + instance['launch_index'] + ) + + class VolumeCommands(object): """Methods for dealing with a cloud in an odd state""" @@ -677,7 +716,9 @@ CATEGORIES = [ ('service', ServiceCommands), ('log', LogCommands), ('db', DbCommands), - ('volume', VolumeCommands)] + ('volume', VolumeCommands) + ('instance', InstanceCommands), +] def lazy_match(name, key_value_tuples): -- cgit From 493aa34da71e7dc3c28c6a55254b6d7ed4d81b72 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Sat, 26 Feb 2011 22:13:28 +0100 Subject: beautification... --- bin/nova-manage | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index 68847bdde..2e4e091cf 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -623,8 +623,8 @@ class InstanceCommands(object): def list(self, host=None, instance=None): """Show a list of all instances""" - print "%-10s %-15s %-10s %-10s %-19s %-12s %-12s" \ - " %-12s %-10s %-10s %-10s %-5s" % ( + print "%-10s %-15s %-10s %-10s %-19s %-12s %-12s %-12s" \ + " %-10s %-10s %-10s %-5s" % ( _('instance'), _('node'), _('type'), @@ -636,7 +636,7 @@ class InstanceCommands(object): _('project'), _('user'), _('zone'), - _('index'), + _('index') ) for instance in db.instance_get_all(context.get_admin_context()): @@ -716,8 +716,8 @@ CATEGORIES = [ ('service', ServiceCommands), ('log', LogCommands), ('db', DbCommands), - ('volume', VolumeCommands) - ('instance', InstanceCommands), + ('volume', VolumeCommands), + ('instance', InstanceCommands) ] -- cgit From 2714b2df0d21ecb08966c4d145d2d75fa1bb201d Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Sun, 27 Feb 2011 00:07:03 +0100 Subject: fixed FIXME --- nova/db/sqlalchemy/api.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index d07c53759..828d24c78 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -598,17 +598,11 @@ def fixed_ip_get_all(context, session=None): def fixed_ip_get_all_by_host(context, host=None): session = get_session() - # FIXME: I'm sure that SQLAlchemy can handle this in a nicer way - instances = session.query(models.Instance).\ - filter_by(state=1).\ - filter_by(host=host).\ - all() - - result = [] - for instance in instances: - result.append(session.query(models.FixedIp).\ - filter_by(instance_id=instance['id']).\ - first()) + result = session.query(models.FixedIp).\ + join(models.FixedIp.instance).\ + filter_by(state=1).\ + filter_by(host=host).\ + all() if not result: raise exception.NotFound(_('No fixed ips for this host defined')) -- cgit From f72e5b618387a7b5a06f0e5b7e68af51c6667327 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Sun, 27 Feb 2011 00:13:05 +0100 Subject: added listing of instances running on a specific host chronos:~ # nova-manage fixed list ares network IP address MAC address hostname host 192.168.3.0/24 192.168.3.6 02:16:3e:75:d7:9a i-00000c1c ares --- bin/nova-manage | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/nova-manage b/bin/nova-manage index 2e4e091cf..12ccfa0e9 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -639,7 +639,13 @@ class InstanceCommands(object): _('index') ) - for instance in db.instance_get_all(context.get_admin_context()): + if host == None: + instances = db.instance_get_all(context.get_admin_context()) + else: + instances = db.instance_get_all_by_host( + context.get_admin_context(), host) + + for instance in instances: print "%-10s %-15s %-10s %-10s %-19s %-12s %-12s %-12s" \ " %-10s %-10s %-10s %-5d" % ( instance['hostname'], -- cgit From 0550124fcd863be60dd0e6fefb5f30641331b198 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Mon, 28 Feb 2011 18:06:11 -0500 Subject: add test for instance creation without personalities --- nova/tests/api/openstack/test_servers.py | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 7a25abe9d..7e5bc0080 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -232,6 +232,46 @@ class ServersTest(test.TestCase): self.assertEqual(res.status_int, 200) + def _create_instance_with_personality(self, personality): + + class FakeComputeAPI(object): + + def __init__(self): + self.onset_files = None + + def create(*args, **kwargs): + if 'onset_files' in kwargs: + self.onset_files = kwargs['onset_files'] + else: + self.onset_files = None + return [{'id': '1234', 'display_name': 'fakeinstance'}] + + def make_stub_method(canned_return): + def stub_method(*args, **kwargs): + return canned_return + return stub_method + + compute_api = FakeComputeAPI() + self.stubs.Set(nova.compute, 'API', make_stub_method(compute_api)) + self.stubs.Set(nova.api.openstack.servers.Controller, + '_get_kernel_ramdisk_from_image', make_stub_method((1, 1))) + self.stubs.Set(nova.api.openstack.common, + 'get_image_id_from_image_hash', make_stub_method(2)) + body = dict(server=dict( + name='server_test', imageId=2, flavorId=2, + metadata={}, + personality=personality)) + + req = webob.Request.blank('/v1.0/servers') + req.method = 'POST' + req.body = json.dumps(body) + return req.get_response(fakes.wsgi_app()), compute_api.onset_files + + def test_create_instance_with_no_personality(self): + res, onset_files = self._create_instance_with_personality(personality={}) + self.assertEquals(res.status_int, 200) + self.assertEquals(onset_files, None) + def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') req.method = 'PUT' -- cgit From f9d08c16d5c620c711d962a78be3a94b99364f14 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 1 Mar 2011 13:56:33 -0500 Subject: support adding a single personality in the osapi --- nova/api/openstack/servers.py | 22 ++++++++++++++++++++-- nova/tests/api/openstack/test_servers.py | 21 ++++++++++++++++----- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 73c7bfe17..92e5c9024 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -80,7 +80,6 @@ def _translate_detail_keys(inst): return dict(server=inst_dict) - def _translate_keys(inst): """ Coerces into dictionary format, excluding all model attributes save for id and name """ @@ -154,6 +153,22 @@ class Controller(wsgi.Controller): image = self._image_service.show(req.environ['nova.context'], image_id) return lookup('kernel_id'), lookup('ramdisk_id') + + def _get_onset_files_from_personality_attr(self, personality_attr): + """ + Create a list of onset files from the personality request attribute + + At this time, onset_files must be formatted as a list of + (file_path, file_content) pairs for compatibility with the + underlying compute service. + """ + onset_files = [] + for personality in personality_attr: + path = personality['path'] + contents = personality['contents'] + onset_files.append((path, contents)) + return onset_files + def create(self, req): """ Creates a new server for a given user """ env = self._deserialize(req.body, req) @@ -181,6 +196,9 @@ class Controller(wsgi.Controller): for k, v in env['server']['metadata'].items(): metadata.append({'key': k, 'value': v}) + personality = env['server'].get('personality', []) + onset_files = self._get_onset_files_from_personality_attr(personality) + instances = self.compute_api.create( context, instance_types.get_by_flavor_id(env['server']['flavorId']), @@ -192,7 +210,7 @@ class Controller(wsgi.Controller): key_name=key_pair['name'], key_data=key_pair['public_key'], metadata=metadata, - onset_files=env.get('onset_files', [])) + onset_files=onset_files) return _translate_keys(instances[0]) def update(self, req, id): diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 7e5bc0080..42665ba6d 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -232,6 +232,9 @@ class ServersTest(test.TestCase): self.assertEqual(res.status_int, 200) + def _personality_dict(self, path, contents): + return {'path': path, 'contents': contents} + def _create_instance_with_personality(self, personality): class FakeComputeAPI(object): @@ -239,7 +242,7 @@ class ServersTest(test.TestCase): def __init__(self): self.onset_files = None - def create(*args, **kwargs): + def create(self, *args, **kwargs): if 'onset_files' in kwargs: self.onset_files = kwargs['onset_files'] else: @@ -265,12 +268,20 @@ class ServersTest(test.TestCase): req = webob.Request.blank('/v1.0/servers') req.method = 'POST' req.body = json.dumps(body) - return req.get_response(fakes.wsgi_app()), compute_api.onset_files + return req, req.get_response(fakes.wsgi_app()), compute_api.onset_files def test_create_instance_with_no_personality(self): - res, onset_files = self._create_instance_with_personality(personality={}) - self.assertEquals(res.status_int, 200) - self.assertEquals(onset_files, None) + request, response, onset_files = \ + self._create_instance_with_personality(personality=[]) + self.assertEquals(response.status_int, 200) + self.assertEquals(onset_files, []) + + def test_create_instance_with_one_personality(self): + personality = [self._personality_dict('/my/path', 'myfilecontents')] + request, response, onset_files = \ + self._create_instance_with_personality(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(onset_files, [('/my/path', 'myfilecontents')]) def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') -- cgit From 94e42c3002f9043fc3c5b90a1cb5ad0c50ba261b Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 1 Mar 2011 16:21:37 -0500 Subject: ensure personality contents are b64 encoded --- nova/api/openstack/servers.py | 15 ++++++++++----- nova/tests/api/openstack/test_servers.py | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 8491fe697..8908bbdca 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import base64 import hashlib import json import traceback @@ -138,7 +139,7 @@ class Controller(wsgi.Controller): return faults.Fault(exc.HTTPNotFound()) return exc.HTTPAccepted() - def _get_onset_files_from_personality_attr(self, personality_attr): + def _get_onset_files_from_personality(self, personality): """ Create a list of onset files from the personality request attribute @@ -147,9 +148,13 @@ class Controller(wsgi.Controller): underlying compute service. """ onset_files = [] - for personality in personality_attr: - path = personality['path'] - contents = personality['contents'] + for item in personality: + path = item['path'] + try: + contents = base64.b64decode(item['contents']) + except TypeError: + raise exc.HTTPBadRequest(explanation= + 'Personality content for %s cannot be decoded' % path) onset_files.append((path, contents)) return onset_files @@ -181,7 +186,7 @@ class Controller(wsgi.Controller): metadata.append({'key': k, 'value': v}) personality = env['server'].get('personality', []) - onset_files = self._get_onset_files_from_personality_attr(personality) + onset_files = self._get_onset_files_from_personality(personality) instances = self.compute_api.create( context, diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 144dbd4af..4b40793a7 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import base64 import datetime import json @@ -272,16 +273,28 @@ class ServersTest(test.TestCase): def test_create_instance_with_no_personality(self): request, response, onset_files = \ - self._create_instance_with_personality(personality=[]) + self._create_instance_with_personality(personality=[]) self.assertEquals(response.status_int, 200) self.assertEquals(onset_files, []) - def test_create_instance_with_one_personality(self): - personality = [self._personality_dict('/my/path', 'myfilecontents')] + def test_create_instance_with_personality(self): + path = '/my/file/path' + contents = '#!/bin/bash\necho "Hello, World!"\n' + b64contents = base64.b64encode(contents) + personality = [self._personality_dict(path, b64contents)] request, response, onset_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [('/my/path', 'myfilecontents')]) + self.assertEquals(onset_files, [(path, contents)]) + + def test_create_instance_with_personality_with_non_b64_content(self): + path = '/my/file/path' + contents = '#!/bin/bash\necho "Oh no!"\n' + personality = [self._personality_dict(path, contents)] + request, response, onset_files = \ + self._create_instance_with_personality(personality) + self.assertEquals(response.status_int, 400) + self.assertEquals(onset_files, None) def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') -- cgit From 7b3ccd5fd1636ebc437a89a3667e6e712004e87f Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 1 Mar 2011 16:48:01 -0500 Subject: test osapi server create with multiple personalities --- nova/tests/api/openstack/test_servers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 4b40793a7..dd951e90c 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -296,6 +296,20 @@ class ServersTest(test.TestCase): self.assertEquals(response.status_int, 400) self.assertEquals(onset_files, None) + def test_create_instance_with_two_personalities(self): + files = [ + ('/etc/sudoers', 'ALL ALL=NOPASSWD: ALL\n'), + ('/etc/motd', 'Enjoy your root access!\n'), + ] + personality = [] + for path, content in files: + personality.append(self._personality_dict( + path, base64.b64encode(content))) + request, response, onset_files = \ + self._create_instance_with_personality(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(onset_files, files) + def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') req.method = 'PUT' -- cgit From d33866923958b3529a812f4eef7dea4a6591a423 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 2 Mar 2011 17:36:21 -0500 Subject: add support for quotas on file injection --- nova/quota.py | 29 +++++++++++++++++++++++------ nova/tests/test_quota.py | 10 ++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/nova/quota.py b/nova/quota.py index 6b52a97fa..14b388794 100644 --- a/nova/quota.py +++ b/nova/quota.py @@ -37,15 +37,22 @@ flags.DEFINE_integer('quota_floating_ips', 10, 'number of floating ips allowed per project') flags.DEFINE_integer('quota_metadata_items', 128, 'number of metadata items allowed per instance') +flags.DEFINE_integer('quota_file_injection_max_files', 5, + 'number of files allowed during file injection') +flags.DEFINE_integer('quota_file_injection_max_file_bytes', 10 * 1024, + 'number of bytes allowed per file during file injection') def get_quota(context, project_id): - rval = {'instances': FLAGS.quota_instances, - 'cores': FLAGS.quota_cores, - 'volumes': FLAGS.quota_volumes, - 'gigabytes': FLAGS.quota_gigabytes, - 'floating_ips': FLAGS.quota_floating_ips, - 'metadata_items': FLAGS.quota_metadata_items} + rval = { + 'instances': FLAGS.quota_instances, + 'cores': FLAGS.quota_cores, + 'volumes': FLAGS.quota_volumes, + 'gigabytes': FLAGS.quota_gigabytes, + 'floating_ips': FLAGS.quota_floating_ips, + 'metadata_items': FLAGS.quota_metadata_items, + } + try: quota = db.quota_get(context, project_id) for key in rval.keys(): @@ -106,6 +113,16 @@ def allowed_metadata_items(context, num_metadata_items): return min(num_metadata_items, num_allowed_metadata_items) +def allowed_file_injection_files(context): + """Return the number of files allowed per file injection""" + return FLAGS.quota_file_injection_max_files + + +def allowed_file_injection_file_bytes(context): + """Return the number of bytes allowed per file during injection""" + return FLAGS.quota_file_injection_max_file_bytes + + class QuotaError(exception.ApiError): """Quota Exceeeded""" pass diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py index 1e42fddf3..48e5a5538 100644 --- a/nova/tests/test_quota.py +++ b/nova/tests/test_quota.py @@ -176,3 +176,13 @@ class QuotaTestCase(test.TestCase): instance_type='m1.small', image_id='fake', metadata=metadata) + + def test_allowed_file_injection_files(self): + self.assertEqual( + quota.allowed_file_injection_files(self.context), + FLAGS.quota_file_injection_max_files) + + def test_allowed_file_injection_file_bytes(self): + self.assertEqual( + quota.allowed_file_injection_file_bytes(self.context), + FLAGS.quota_file_injection_max_file_bytes) -- cgit From 668cdc96b3f6fb412b9d1d4a3780744d6b2340b1 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 3 Mar 2011 00:59:09 -0500 Subject: more rigorous testing and error handling for os api personality --- nova/api/openstack/servers.py | 8 ++++++-- nova/tests/api/openstack/test_servers.py | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 8908bbdca..73c787828 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -149,9 +149,13 @@ class Controller(wsgi.Controller): """ onset_files = [] for item in personality: - path = item['path'] try: - contents = base64.b64decode(item['contents']) + path = item['path'] + contents = item['contents'] + except TypeError: + raise exc.HTTPBadRequest(explanation='Bad personality format') + try: + contents = base64.b64decode(contents) except TypeError: raise exc.HTTPBadRequest(explanation= 'Personality content for %s cannot be decoded' % path) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index dd951e90c..272d34e3a 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -265,6 +265,8 @@ class ServersTest(test.TestCase): name='server_test', imageId=2, flavorId=2, metadata={}, personality=personality)) + if personality is None: + del body['server']['personality'] req = webob.Request.blank('/v1.0/servers') req.method = 'POST' @@ -273,7 +275,7 @@ class ServersTest(test.TestCase): def test_create_instance_with_no_personality(self): request, response, onset_files = \ - self._create_instance_with_personality(personality=[]) + self._create_instance_with_personality(personality=None) self.assertEquals(response.status_int, 200) self.assertEquals(onset_files, []) @@ -296,10 +298,11 @@ class ServersTest(test.TestCase): self.assertEquals(response.status_int, 400) self.assertEquals(onset_files, None) - def test_create_instance_with_two_personalities(self): + def test_create_instance_with_three_personalities(self): files = [ ('/etc/sudoers', 'ALL ALL=NOPASSWD: ALL\n'), ('/etc/motd', 'Enjoy your root access!\n'), + ('/etc/dovecot.conf', 'dovecot\nconfig\nstuff\n'), ] personality = [] for path, content in files: @@ -310,6 +313,24 @@ class ServersTest(test.TestCase): self.assertEquals(response.status_int, 200) self.assertEquals(onset_files, files) + def test_create_instance_personality_empty_content(self): + path = '/my/file/path' + contents = '' + personality = [self._personality_dict(path, contents)] + request, response, onset_files = \ + self._create_instance_with_personality(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(onset_files, [(path, contents)]) + + def test_create_instance_personality_not_a_list(self): + path = '/my/file/path' + contents = 'myfilecontents' + personality = self._personality_dict(path, contents) + request, response, onset_files = \ + self._create_instance_with_personality(personality) + self.assertEquals(response.status_int, 400) + self.assertEquals(onset_files, None) + def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') req.method = 'PUT' -- cgit From 9cfe8ff2e8e66952c3202b852a88ee6fca6fb736 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 3 Mar 2011 16:31:01 -0500 Subject: pep8 --- nova/api/openstack/servers.py | 9 +++++---- nova/tests/api/openstack/test_servers.py | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 73c787828..ea13116fa 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -85,6 +85,7 @@ def _translate_detail_keys(inst): return dict(server=inst_dict) + def _translate_keys(inst): """ Coerces into dictionary format, excluding all model attributes save for id and name """ @@ -143,8 +144,8 @@ class Controller(wsgi.Controller): """ Create a list of onset files from the personality request attribute - At this time, onset_files must be formatted as a list of - (file_path, file_content) pairs for compatibility with the + At this time, onset_files must be formatted as a list of + (file_path, file_content) pairs for compatibility with the underlying compute service. """ onset_files = [] @@ -157,8 +158,8 @@ class Controller(wsgi.Controller): try: contents = base64.b64decode(contents) except TypeError: - raise exc.HTTPBadRequest(explanation= - 'Personality content for %s cannot be decoded' % path) + msg = 'Personality content for %s cannot be decoded' % path + raise exc.HTTPBadRequest(explanation=msg) onset_files.append((path, contents)) return onset_files diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 272d34e3a..53cfa3a6e 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -287,8 +287,8 @@ class ServersTest(test.TestCase): request, response, onset_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) - + self.assertEquals(onset_files, [(path, contents)]) + def test_create_instance_with_personality_with_non_b64_content(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Oh no!"\n' @@ -320,7 +320,7 @@ class ServersTest(test.TestCase): request, response, onset_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) + self.assertEquals(onset_files, [(path, contents)]) def test_create_instance_personality_not_a_list(self): path = '/my/file/path' -- cgit From e14f524eb92ae07704a2ec7dac0f97c60940a6ab Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 3 Mar 2011 17:49:41 -0500 Subject: enforce personality quotas --- nova/compute/api.py | 24 ++++++++++++++++++ nova/quota.py | 27 ++++++++++++-------- nova/tests/test_quota.py | 66 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 101 insertions(+), 16 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index 625778b66..44e583cd4 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -80,6 +80,26 @@ class API(base.Base): topic, {"method": "get_network_topic", "args": {'fake': 1}}) + def _check_personality_file_quota(self, context, personality_files): + limit = quota.allowed_personality_files(context) + if len(personality_files) > limit: + raise quota.QuotaError(_("Personality limit exceeded. You can " + "only have %d personalities when " + "creating an instance.") % limit, + "PersonalityLimitExceeded") + path_limit = quota.allowed_personality_path_bytes(context) + content_limit = quota.allowed_personality_content_bytes(context) + for path, content in personality_files: + if len(path) > path_limit: + raise quota.QuotaError( + _("Personality file path limit exceeded."), + "PersonalityLimitExceeded") + if len(content) > content_limit: + raise quota.QuotaError( + _("Personality file content limit exceeded."), + "PersonalityLimitExceeded") + return personality_files + def create(self, context, instance_type, image_id, kernel_id=None, ramdisk_id=None, min_count=1, max_count=1, @@ -124,6 +144,10 @@ class API(base.Base): LOG.warn(msg) raise quota.QuotaError(msg, "MetadataLimitExceeded") + if onset_files is not None: + onset_files = \ + self._check_personality_file_quota(context, onset_files) + image = self.image_service.show(context, image_id) if kernel_id is None: kernel_id = image.get('kernel_id', None) diff --git a/nova/quota.py b/nova/quota.py index 14b388794..4b777624c 100644 --- a/nova/quota.py +++ b/nova/quota.py @@ -37,10 +37,12 @@ flags.DEFINE_integer('quota_floating_ips', 10, 'number of floating ips allowed per project') flags.DEFINE_integer('quota_metadata_items', 128, 'number of metadata items allowed per instance') -flags.DEFINE_integer('quota_file_injection_max_files', 5, - 'number of files allowed during file injection') -flags.DEFINE_integer('quota_file_injection_max_file_bytes', 10 * 1024, - 'number of bytes allowed per file during file injection') +flags.DEFINE_integer('quota_personality_max_files', 5, + 'number of personality files allowed') +flags.DEFINE_integer('quota_personality_max_content_bytes', 10 * 1024, + 'number of bytes allowed per personality file') +flags.DEFINE_integer('quota_personality_max_path_bytes', 255, + 'number of bytes allowed per personality file path') def get_quota(context, project_id): @@ -113,14 +115,19 @@ def allowed_metadata_items(context, num_metadata_items): return min(num_metadata_items, num_allowed_metadata_items) -def allowed_file_injection_files(context): - """Return the number of files allowed per file injection""" - return FLAGS.quota_file_injection_max_files +def allowed_personality_files(context): + """Return the number of personality files allowed""" + return FLAGS.quota_personality_max_files -def allowed_file_injection_file_bytes(context): - """Return the number of bytes allowed per file during injection""" - return FLAGS.quota_file_injection_max_file_bytes +def allowed_personality_content_bytes(context): + """Return the number of bytes allowed per personality content""" + return FLAGS.quota_personality_max_content_bytes + + +def allowed_personality_path_bytes(context): + """Return the number of bytes allowed in a personality file path""" + return FLAGS.quota_personality_max_path_bytes class QuotaError(exception.ApiError): diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py index 48e5a5538..16a083788 100644 --- a/nova/tests/test_quota.py +++ b/nova/tests/test_quota.py @@ -177,12 +177,66 @@ class QuotaTestCase(test.TestCase): image_id='fake', metadata=metadata) - def test_allowed_file_injection_files(self): + def test_allowed_personality_files(self): self.assertEqual( - quota.allowed_file_injection_files(self.context), - FLAGS.quota_file_injection_max_files) + quota.allowed_personality_files(self.context), + FLAGS.quota_personality_max_files) + + def _create_with_personality(self, files): + api = compute.API() + api.create(self.context, min_count=1, max_count=1, + instance_type='m1.small', image_id='fake', + onset_files=files) + + def test_no_personality_files(self): + api = compute.API() + api.create(self.context, instance_type='m1.small', image_id='fake') + + def test_max_personality_files(self): + files = [] + for i in xrange(FLAGS.quota_personality_max_files): + files.append(('/my/path%d' % i, 'config = test\n')) + self._create_with_personality(files) # no QuotaError + + def test_too_many_personality_files(self): + files = [] + for i in xrange(FLAGS.quota_personality_max_files + 1): + files.append(('/my/path%d' % i, 'my\ncontent%d\n' % i)) + self.assertRaises(quota.QuotaError, + self._create_with_personality, files) - def test_allowed_file_injection_file_bytes(self): + def test_allowed_personality_content_bytes(self): self.assertEqual( - quota.allowed_file_injection_file_bytes(self.context), - FLAGS.quota_file_injection_max_file_bytes) + quota.allowed_personality_content_bytes(self.context), + FLAGS.quota_personality_max_content_bytes) + + def test_max_personality_content_bytes(self): + max = FLAGS.quota_personality_max_content_bytes + content = ''.join(['a' for i in xrange(max)]) + files = [('/test/path', content)] + self._create_with_personality(files) # no QuotaError + + def test_too_many_personality_content_bytes(self): + max = FLAGS.quota_personality_max_content_bytes + content = ''.join(['a' for i in xrange(max + 1)]) + files = [('/test/path', content)] + self.assertRaises(quota.QuotaError, + self._create_with_personality, files) + + def test_allowed_personality_path_bytes(self): + self.assertEqual( + quota.allowed_personality_path_bytes(self.context), + FLAGS.quota_personality_max_path_bytes) + + def test_max_personality_path_bytes(self): + max = FLAGS.quota_personality_max_path_bytes + path = ''.join(['a' for i in xrange(max)]) + files = [(path, 'config = quotatest')] + self._create_with_personality(files) # no QuotaError + + def test_too_many_personality_path_bytes(self): + max = FLAGS.quota_personality_max_path_bytes + path = ''.join(['a' for i in xrange(max + 1)]) + files = [(path, 'config = quotatest')] + self.assertRaises(quota.QuotaError, + self._create_with_personality, files) -- cgit From 5ae13551990be67e3509ddcd10d1872a91634d83 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 3 Mar 2011 18:27:57 -0500 Subject: rename onset_files to personality_files all the way down to compute manager --- nova/api/openstack/servers.py | 16 +++++++-------- nova/compute/api.py | 10 ++++----- nova/compute/manager.py | 2 +- nova/tests/api/openstack/test_servers.py | 35 ++++++++++++++++---------------- nova/tests/test_quota.py | 2 +- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index ea13116fa..8f6d8de66 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -140,15 +140,15 @@ class Controller(wsgi.Controller): return faults.Fault(exc.HTTPNotFound()) return exc.HTTPAccepted() - def _get_onset_files_from_personality(self, personality): + def _get_personality_files(self, personality): """ - Create a list of onset files from the personality request attribute + Create a list of personality files from the personality attribute - At this time, onset_files must be formatted as a list of + At this time, personality_files must be formatted as a list of (file_path, file_content) pairs for compatibility with the underlying compute service. """ - onset_files = [] + personality_files = [] for item in personality: try: path = item['path'] @@ -160,8 +160,8 @@ class Controller(wsgi.Controller): except TypeError: msg = 'Personality content for %s cannot be decoded' % path raise exc.HTTPBadRequest(explanation=msg) - onset_files.append((path, contents)) - return onset_files + personality_files.append((path, contents)) + return personality_files def create(self, req): """ Creates a new server for a given user """ @@ -191,7 +191,7 @@ class Controller(wsgi.Controller): metadata.append({'key': k, 'value': v}) personality = env['server'].get('personality', []) - onset_files = self._get_onset_files_from_personality(personality) + personality_files = self._get_personality_files(personality) instances = self.compute_api.create( context, @@ -204,7 +204,7 @@ class Controller(wsgi.Controller): key_name=key_pair['name'], key_data=key_pair['public_key'], metadata=metadata, - onset_files=onset_files) + personality_files=personality_files) return _translate_keys(instances[0]) def update(self, req, id): diff --git a/nova/compute/api.py b/nova/compute/api.py index 44e583cd4..13938dcde 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -106,7 +106,7 @@ class API(base.Base): display_name='', display_description='', key_name=None, key_data=None, security_group='default', availability_zone=None, user_data=None, metadata=[], - onset_files=None): + personality_files=None): """Create the number of instances requested if quota and other arguments check out ok. """ @@ -144,9 +144,9 @@ class API(base.Base): LOG.warn(msg) raise quota.QuotaError(msg, "MetadataLimitExceeded") - if onset_files is not None: - onset_files = \ - self._check_personality_file_quota(context, onset_files) + if personality_files is not None: + personality_files = \ + self._check_personality_file_quota(context, personality_files) image = self.image_service.show(context, image_id) if kernel_id is None: @@ -242,7 +242,7 @@ class API(base.Base): "args": {"topic": FLAGS.compute_topic, "instance_id": instance_id, "availability_zone": availability_zone, - "onset_files": onset_files}}) + "personality_files": personality_files}}) for group_id in security_groups: self.trigger_security_group_members_refresh(elevated, group_id) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index d659712ad..1a392dda8 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -174,7 +174,7 @@ class ComputeManager(manager.Manager): """Launch a new instance with specified options.""" context = context.elevated() instance_ref = self.db.instance_get(context, instance_id) - instance_ref.onset_files = kwargs.get('onset_files', []) + instance_ref.onset_files = kwargs.get('personality_files', []) if instance_ref['name'] in self.driver.list_instances(): raise exception.Error(_("Instance has already been created")) LOG.audit(_("instance %s: starting..."), instance_id, diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 53cfa3a6e..bf934113a 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -241,13 +241,13 @@ class ServersTest(test.TestCase): class FakeComputeAPI(object): def __init__(self): - self.onset_files = None + self.personality_files = None def create(self, *args, **kwargs): - if 'onset_files' in kwargs: - self.onset_files = kwargs['onset_files'] + if 'personality_files' in kwargs: + self.personality_files = kwargs['personality_files'] else: - self.onset_files = None + self.personality_files = None return [{'id': '1234', 'display_name': 'fakeinstance'}] def make_stub_method(canned_return): @@ -271,32 +271,33 @@ class ServersTest(test.TestCase): req = webob.Request.blank('/v1.0/servers') req.method = 'POST' req.body = json.dumps(body) - return req, req.get_response(fakes.wsgi_app()), compute_api.onset_files + return (req, req.get_response(fakes.wsgi_app()), + compute_api.personality_files) def test_create_instance_with_no_personality(self): - request, response, onset_files = \ + request, response, personality_files = \ self._create_instance_with_personality(personality=None) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, []) + self.assertEquals(personality_files, []) def test_create_instance_with_personality(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' b64contents = base64.b64encode(contents) personality = [self._personality_dict(path, b64contents)] - request, response, onset_files = \ + request, response, personality_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) + self.assertEquals(personality_files, [(path, contents)]) def test_create_instance_with_personality_with_non_b64_content(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Oh no!"\n' personality = [self._personality_dict(path, contents)] - request, response, onset_files = \ + request, response, personality_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 400) - self.assertEquals(onset_files, None) + self.assertEquals(personality_files, None) def test_create_instance_with_three_personalities(self): files = [ @@ -308,28 +309,28 @@ class ServersTest(test.TestCase): for path, content in files: personality.append(self._personality_dict( path, base64.b64encode(content))) - request, response, onset_files = \ + request, response, personality_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, files) + self.assertEquals(personality_files, files) def test_create_instance_personality_empty_content(self): path = '/my/file/path' contents = '' personality = [self._personality_dict(path, contents)] - request, response, onset_files = \ + request, response, personality_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) + self.assertEquals(personality_files, [(path, contents)]) def test_create_instance_personality_not_a_list(self): path = '/my/file/path' contents = 'myfilecontents' personality = self._personality_dict(path, contents) - request, response, onset_files = \ + request, response, personality_files = \ self._create_instance_with_personality(personality) self.assertEquals(response.status_int, 400) - self.assertEquals(onset_files, None) + self.assertEquals(personality_files, None) def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py index 16a083788..b26dec61a 100644 --- a/nova/tests/test_quota.py +++ b/nova/tests/test_quota.py @@ -186,7 +186,7 @@ class QuotaTestCase(test.TestCase): api = compute.API() api.create(self.context, min_count=1, max_count=1, instance_type='m1.small', image_id='fake', - onset_files=files) + personality_files=files) def test_no_personality_files(self): api = compute.API() -- cgit From abd5779068f3b979fc79dec7a68549999c58092d Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Fri, 4 Mar 2011 01:36:29 -0500 Subject: remove ensure_b64_encoding --- nova/compute/manager.py | 10 ++-------- nova/utils.py | 12 ------------ nova/virt/xenapi/vmops.py | 10 +++++----- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index d915dc069..3a712fd97 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -34,7 +34,6 @@ terminating it. :func:`nova.utils.import_object` """ -import base64 import datetime import random import string @@ -353,15 +352,10 @@ class ComputeManager(manager.Manager): LOG.warn(_('trying to inject a file into a non-running ' 'instance: %(instance_id)s (state: %(instance_state)s ' 'expected: %(expected_state)s)') % locals()) - # Files/paths *should* be base64-encoded at this point, but - # double-check to make sure. - b64_path = utils.ensure_b64_encoding(path) - b64_contents = utils.ensure_b64_encoding(file_contents) - plain_path = base64.b64decode(b64_path) nm = instance_ref['name'] - msg = _('instance %(nm)s: injecting file to %(plain_path)s') % locals() + msg = _('instance %(nm)s: injecting file to %(path)s') % locals() LOG.audit(msg) - self.driver.inject_file(instance_ref, b64_path, b64_contents) + self.driver.inject_file(instance_ref, path, file_contents) @exception.wrap_exception @checks_instance_lock diff --git a/nova/utils.py b/nova/utils.py index 0cf91e0cc..02b71900c 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -491,18 +491,6 @@ def loads(s): return json.loads(s) -def ensure_b64_encoding(val): - """Safety method to ensure that values expected to be base64-encoded - actually are. If they are, the value is returned unchanged. Otherwise, - the encoded value is returned. - """ - try: - dummy = base64.decode(val) - return val - except TypeError: - return base64.b64encode(val) - - def get_from_path(items, path): """ Returns a list of items matching the specified path. Takes an XPath-like expression e.g. prop1/prop2/prop3, and for each item in items, diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index 9ac83efb0..89d58a664 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -19,6 +19,7 @@ Management class for VM-related functions (spawn, reboot, etc). """ +import base64 import json import M2Crypto import os @@ -313,17 +314,16 @@ class VMOps(object): task = self._session.call_xenapi("Async.VM.start", vm, False, False) self._session.wait_for_task(task, instance.id) - def inject_file(self, instance, b64_path, b64_contents): + def inject_file(self, instance, path, contents): """Write a file to the VM instance. The path to which it is to be written and the contents of the file need to be supplied; both should be base64-encoded to prevent errors with non-ASCII characters being transmitted. If the agent does not support file injection, or the user has disabled it, a NotImplementedError will be raised. """ - # Files/paths *should* be base64-encoded at this point, but - # double-check to make sure. - b64_path = utils.ensure_b64_encoding(b64_path) - b64_contents = utils.ensure_b64_encoding(b64_contents) + # Files/paths must be base64-encoded for transmission to agent + b64_path = base64.b64encode(path) + b64_contents = base64.b64encode(contents) # Need to uniquely identify this request. transaction_id = str(uuid.uuid4()) -- cgit From f36b4fe22bcb187d5f426320bbe43fcf3cb1a30a Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Fri, 4 Mar 2011 14:44:29 -0500 Subject: refactor server tests to support xml and json separately --- nova/tests/api/openstack/test_servers.py | 103 ++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 35 deletions(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index bf934113a..9e7bc3aac 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -233,12 +233,9 @@ class ServersTest(test.TestCase): self.assertEqual(res.status_int, 200) - def _personality_dict(self, path, contents): - return {'path': path, 'contents': contents} + def _setup_mock_compute_api_for_personality(self): - def _create_instance_with_personality(self, personality): - - class FakeComputeAPI(object): + class MockComputeAPI(object): def __init__(self): self.personality_files = None @@ -255,28 +252,74 @@ class ServersTest(test.TestCase): return canned_return return stub_method - compute_api = FakeComputeAPI() + compute_api = MockComputeAPI() self.stubs.Set(nova.compute, 'API', make_stub_method(compute_api)) self.stubs.Set(nova.api.openstack.servers.Controller, '_get_kernel_ramdisk_from_image', make_stub_method((1, 1))) self.stubs.Set(nova.api.openstack.common, 'get_image_id_from_image_hash', make_stub_method(2)) - body = dict(server=dict( - name='server_test', imageId=2, flavorId=2, - metadata={}, - personality=personality)) - if personality is None: - del body['server']['personality'] - + return compute_api + + def _create_personality_request_dict(self, personality_files): + server = {} + server['name'] = 'new-server-test' + server['imageId'] = 1 + server['flavorId'] = 1 + if personality_files is not None: + personalities = [] + for path, contents in personality_files: + personalities.append({'path': path, 'contents': contents}) + server['personality'] = personalities + return {'server': server} + + def _create_personality_request_json(self, personality_files): + body_dict = self._create_personality_request_dict(personality_files) req = webob.Request.blank('/v1.0/servers') + req.content_type = 'application/json' req.method = 'POST' - req.body = json.dumps(body) - return (req, req.get_response(fakes.wsgi_app()), - compute_api.personality_files) + req.body = json.dumps(body_dict) + return req + + def _format_xml_request_body(self, body_dict): + server = body_dict['server'] + body_parts = [] + body_parts.extend([ + '', + '' % ( + server['name'], server['imageId'], server['flavorId'])]) + if 'metadata' in server: + metadata = server['metadata'] + body_parts.append('') + for item in metadata.iteritems(): + body_parts.append('%s' % item) + body_parts.append('') + if 'personality' in server: + personalities = server['personality'] + body_parts.append('') + for item in personalities.iteritems(): + body_parts.append('%s' % item) + body_parts.append('') + body_parts.append('') + return ''.join(body_parts) + + def _create_personality_request_xml(self, personality_files): + body_dict = self._create_personality_request_dict(personality_files) + req = webob.Request.blank('/v1.0/servers') + req.content_type = 'application/xml' + req.method = 'POST' + req.body = self._format_xml_request_body(body_dict) + return req + + def _create_instance_with_personality_json(self, personality): + compute_api = self._setup_mock_compute_api_for_personality() + request = self._create_personality_request_json(personality) + response = request.get_response(fakes.wsgi_app()) + return (request, response, compute_api.personality_files) def test_create_instance_with_no_personality(self): request, response, personality_files = \ - self._create_instance_with_personality(personality=None) + self._create_instance_with_personality_json(personality=None) self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, []) @@ -284,18 +327,18 @@ class ServersTest(test.TestCase): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' b64contents = base64.b64encode(contents) - personality = [self._personality_dict(path, b64contents)] + personality = [(path, b64contents)] request, response, personality_files = \ - self._create_instance_with_personality(personality) + self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, [(path, contents)]) def test_create_instance_with_personality_with_non_b64_content(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Oh no!"\n' - personality = [self._personality_dict(path, contents)] + personality = [(path, contents)] request, response, personality_files = \ - self._create_instance_with_personality(personality) + self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 400) self.assertEquals(personality_files, None) @@ -307,31 +350,21 @@ class ServersTest(test.TestCase): ] personality = [] for path, content in files: - personality.append(self._personality_dict( - path, base64.b64encode(content))) + personality.append((path, base64.b64encode(content))) request, response, personality_files = \ - self._create_instance_with_personality(personality) + self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, files) def test_create_instance_personality_empty_content(self): path = '/my/file/path' contents = '' - personality = [self._personality_dict(path, contents)] + personality = [(path, contents)] request, response, personality_files = \ - self._create_instance_with_personality(personality) + self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, [(path, contents)]) - def test_create_instance_personality_not_a_list(self): - path = '/my/file/path' - contents = 'myfilecontents' - personality = self._personality_dict(path, contents) - request, response, personality_files = \ - self._create_instance_with_personality(personality) - self.assertEquals(response.status_int, 400) - self.assertEquals(personality_files, None) - def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') req.method = 'PUT' -- cgit From a38e6c67c37a4d3336cf1dc3717fd5612a474183 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Fri, 4 Mar 2011 14:45:31 -0500 Subject: remove xml testing infrastructure since it is not feasible to use at present --- nova/tests/api/openstack/test_servers.py | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 9e7bc3aac..c125e6192 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -280,37 +280,6 @@ class ServersTest(test.TestCase): req.body = json.dumps(body_dict) return req - def _format_xml_request_body(self, body_dict): - server = body_dict['server'] - body_parts = [] - body_parts.extend([ - '', - '' % ( - server['name'], server['imageId'], server['flavorId'])]) - if 'metadata' in server: - metadata = server['metadata'] - body_parts.append('') - for item in metadata.iteritems(): - body_parts.append('%s' % item) - body_parts.append('') - if 'personality' in server: - personalities = server['personality'] - body_parts.append('') - for item in personalities.iteritems(): - body_parts.append('%s' % item) - body_parts.append('') - body_parts.append('') - return ''.join(body_parts) - - def _create_personality_request_xml(self, personality_files): - body_dict = self._create_personality_request_dict(personality_files) - req = webob.Request.blank('/v1.0/servers') - req.content_type = 'application/xml' - req.method = 'POST' - req.body = self._format_xml_request_body(body_dict) - return req - def _create_instance_with_personality_json(self, personality): compute_api = self._setup_mock_compute_api_for_personality() request = self._create_personality_request_json(personality) -- cgit From 10668b87f46a1fb5d039f6e7d7a7a55b89d7602a Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Fri, 4 Mar 2011 17:04:41 -0500 Subject: respond well if personality attribute is incomplete --- nova/api/openstack/servers.py | 3 ++ nova/tests/api/openstack/test_servers.py | 48 ++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 8b7b20b92..7c620dbc6 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -153,6 +153,9 @@ class Controller(wsgi.Controller): try: path = item['path'] contents = item['contents'] + except KeyError, key: + expl = 'Bad personality format: missing %s' % key + raise exc.HTTPBadRequest(explanation=expl) except TypeError: raise exc.HTTPBadRequest(explanation='Bad personality format') try: diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index c125e6192..8fb5a9aec 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -272,19 +272,24 @@ class ServersTest(test.TestCase): server['personality'] = personalities return {'server': server} - def _create_personality_request_json(self, personality_files): - body_dict = self._create_personality_request_dict(personality_files) + def _get_create_request_json(self, body_dict): req = webob.Request.blank('/v1.0/servers') req.content_type = 'application/json' req.method = 'POST' req.body = json.dumps(body_dict) return req - def _create_instance_with_personality_json(self, personality): + def _run_create_instance_with_mock_compute_api(self, request): compute_api = self._setup_mock_compute_api_for_personality() - request = self._create_personality_request_json(personality) response = request.get_response(fakes.wsgi_app()) - return (request, response, compute_api.personality_files) + return compute_api, response + + def _create_instance_with_personality_json(self, personality): + body_dict = self._create_personality_request_dict(personality) + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + return request, response, compute_api.personality_files def test_create_instance_with_no_personality(self): request, response, personality_files = \ @@ -302,6 +307,39 @@ class ServersTest(test.TestCase): self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, [(path, contents)]) + def test_create_instance_with_personality_no_path(self): + personality = [('/remove/this/path', + base64.b64encode('my\n\file\ncontents'))] + body_dict = self._create_personality_request_dict(personality) + del body_dict['server']['personality'][0]['path'] + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + + def test_create_instance_with_personality_no_contents(self): + personality = [('/test/path', + base64.b64encode('remove\nthese\ncontents'))] + body_dict = self._create_personality_request_dict(personality) + del body_dict['server']['personality'][0]['contents'] + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + + def test_create_instance_with_personality_not_a_list(self): + personality = [('/test/path', base64.b64encode('test\ncontents\n'))] + body_dict = self._create_personality_request_dict(personality) + body_dict['server']['personality'] = \ + body_dict['server']['personality'][0] + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + def test_create_instance_with_personality_with_non_b64_content(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Oh no!"\n' -- cgit From 7af17cbef6f3e1c5b052133e40e0edbd8ca9ffb3 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Sun, 6 Mar 2011 10:41:24 -0500 Subject: select cleanups --- nova/api/openstack/servers.py | 2 +- nova/quota.py | 14 ++++++-------- nova/virt/xenapi/vmops.py | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 7c620dbc6..93f504f91 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -153,7 +153,7 @@ class Controller(wsgi.Controller): try: path = item['path'] contents = item['contents'] - except KeyError, key: + except KeyError as key: expl = 'Bad personality format: missing %s' % key raise exc.HTTPBadRequest(explanation=expl) except TypeError: diff --git a/nova/quota.py b/nova/quota.py index 4b777624c..1d79f9ef2 100644 --- a/nova/quota.py +++ b/nova/quota.py @@ -46,14 +46,12 @@ flags.DEFINE_integer('quota_personality_max_path_bytes', 255, def get_quota(context, project_id): - rval = { - 'instances': FLAGS.quota_instances, - 'cores': FLAGS.quota_cores, - 'volumes': FLAGS.quota_volumes, - 'gigabytes': FLAGS.quota_gigabytes, - 'floating_ips': FLAGS.quota_floating_ips, - 'metadata_items': FLAGS.quota_metadata_items, - } + rval = {'instances': FLAGS.quota_instances, + 'cores': FLAGS.quota_cores, + 'volumes': FLAGS.quota_volumes, + 'gigabytes': FLAGS.quota_gigabytes, + 'floating_ips': FLAGS.quota_floating_ips, + 'metadata_items': FLAGS.quota_metadata_items} try: quota = db.quota_get(context, project_id) diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index 89d58a664..cf4bedaa9 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -316,7 +316,7 @@ class VMOps(object): def inject_file(self, instance, path, contents): """Write a file to the VM instance. The path to which it is to be - written and the contents of the file need to be supplied; both should + written and the contents of the file need to be supplied; both will be base64-encoded to prevent errors with non-ASCII characters being transmitted. If the agent does not support file injection, or the user has disabled it, a NotImplementedError will be raised. -- cgit From aa4b8a557505108341d603659a5456d10d5f9632 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Sun, 6 Mar 2011 12:06:39 -0500 Subject: avoid possible string/int comparison problems --- nova/quota.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nova/quota.py b/nova/quota.py index 1d79f9ef2..fdef42bc5 100644 --- a/nova/quota.py +++ b/nova/quota.py @@ -115,17 +115,17 @@ def allowed_metadata_items(context, num_metadata_items): def allowed_personality_files(context): """Return the number of personality files allowed""" - return FLAGS.quota_personality_max_files + return int(FLAGS.quota_personality_max_files) def allowed_personality_content_bytes(context): """Return the number of bytes allowed per personality content""" - return FLAGS.quota_personality_max_content_bytes + return int(FLAGS.quota_personality_max_content_bytes) def allowed_personality_path_bytes(context): """Return the number of bytes allowed in a personality file path""" - return FLAGS.quota_personality_max_path_bytes + return int(FLAGS.quota_personality_max_path_bytes) class QuotaError(exception.ApiError): -- cgit From f1dea606a64c9144fb723be0e5b86806891380f8 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 9 Mar 2011 11:54:21 -0500 Subject: add override to handle xml deserialization for server instance creation --- nova/api/openstack/servers.py | 68 +++++++- nova/tests/api/openstack/fakes.py | 2 - nova/tests/api/openstack/test_servers.py | 277 +++++++++++++++++++++++++++++++ 3 files changed, 344 insertions(+), 3 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 93f504f91..4a6282204 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -17,6 +17,7 @@ import base64 import hashlib import json import traceback +from xml.dom import minidom from webob import exc @@ -166,9 +167,16 @@ class Controller(wsgi.Controller): personality_files.append((path, contents)) return personality_files + def _deserialize_create(self, request): + if request.content_type == "application/xml": + deserializer = ServerCreateRequestXMLDeserializer() + return deserializer.deserialize(request.body) + else: + return self._deserialize(request.body, request) + def create(self, req): """ Creates a new server for a given user """ - env = self._deserialize(req.body, req) + env = self._deserialize_create(req) if not env: return faults.Fault(exc.HTTPUnprocessableEntity()) @@ -448,3 +456,61 @@ class Controller(wsgi.Controller): _("Ramdisk not found for image %(image_id)s") % locals()) return kernel_id, ramdisk_id + + +class ServerCreateRequestXMLDeserializer(object): + + def deserialize(self, string): + dom = minidom.parseString(string) + server = self._extract_server(dom) + return {'server': server} + + def _extract_server(self, node): + server = {} + server_node = self._find_first_child_named(node, 'server') + for attr in ["name", "imageId", "flavorId"]: + server[attr] = server_node.getAttribute(attr) + metadata = self._extract_metadata(server_node) + if metadata is not None: + server["metadata"] = metadata + personality = self._extract_personality(server_node) + if personality is not None: + server["personality"] = personality + return server + + def _extract_metadata(self, server_node): + metadata_node = self._find_first_child_named(server_node, "metadata") + if metadata_node is None: + return None + metadata = {} + for meta_node in metadata_node.childNodes: + key = meta_node.getAttribute("key") + metadata[key] = self._extract_text(meta_node) + return metadata + + def _extract_personality(self, server_node): + personality_node = \ + self._find_first_child_named(server_node, "personality") + if personality_node is None: + return None + personality = [] + for file_node in personality_node.childNodes: + item = {} + if file_node.hasAttribute("path"): + item["path"] = file_node.getAttribute("path") + item["contents"] = self._extract_text(file_node) + personality.append(item) + return personality + + def _find_first_child_named(self, parent, name): + for node in parent.childNodes: + if node.nodeName == name: + return node + return None + + def _extract_text(self, node): + if len(node.childNodes) == 1: + child = node.childNodes[0] + if child.nodeType == child.TEXT_NODE: + return child.nodeValue + return "" diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index 49ce8c1b5..1d8692528 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -68,8 +68,6 @@ def fake_auth_init(self, application): @webob.dec.wsgify def fake_wsgi(self, req): req.environ['nova.context'] = context.RequestContext(1, 1) - if req.body: - req.environ['inst_dict'] = json.loads(req.body) return self.application diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 8fb5a9aec..5132d1ad3 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -18,6 +18,7 @@ import base64 import datetime import json +import unittest import stubout import webob @@ -284,6 +285,37 @@ class ServersTest(test.TestCase): response = request.get_response(fakes.wsgi_app()) return compute_api, response + def _format_xml_request_body(self, body_dict): + server = body_dict['server'] + body_parts = [] + body_parts.extend([ + '', + '' % ( + server['name'], server['imageId'], server['flavorId'])]) + if 'metadata' in server: + metadata = server['metadata'] + body_parts.append('') + for item in metadata.iteritems(): + body_parts.append('%s' % item) + body_parts.append('') + if 'personality' in server: + personalities = server['personality'] + body_parts.append('') + for file in personalities: + item = (file['path'], file['contents']) + body_parts.append('%s' % item) + body_parts.append('') + body_parts.append('') + return ''.join(body_parts) + + def _get_create_request_xml(self, body_dict): + req = webob.Request.blank('/v1.0/servers') + req.content_type = 'application/xml' + req.method = 'POST' + req.body = self._format_xml_request_body(body_dict) + return req + def _create_instance_with_personality_json(self, personality): body_dict = self._create_personality_request_dict(personality) request = self._get_create_request_json(body_dict) @@ -291,12 +323,25 @@ class ServersTest(test.TestCase): self._run_create_instance_with_mock_compute_api(request) return request, response, compute_api.personality_files + def _create_instance_with_personality_xml(self, personality): + body_dict = self._create_personality_request_dict(personality) + request = self._get_create_request_xml(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + return request, response, compute_api.personality_files + def test_create_instance_with_no_personality(self): request, response, personality_files = \ self._create_instance_with_personality_json(personality=None) self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, []) + def test_create_instance_with_no_personality_xml(self): + request, response, personality_files = \ + self._create_instance_with_personality_xml(personality=None) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, []) + def test_create_instance_with_personality(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' @@ -307,6 +352,16 @@ class ServersTest(test.TestCase): self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, [(path, contents)]) + def test_create_instance_with_personality_xml(self): + path = '/my/file/path' + contents = '#!/bin/bash\necho "Hello, World!"\n' + b64contents = base64.b64encode(contents) + personality = [(path, b64contents)] + request, response, personality_files = \ + self._create_instance_with_personality_xml(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, [(path, contents)]) + def test_create_instance_with_personality_no_path(self): personality = [('/remove/this/path', base64.b64encode('my\n\file\ncontents'))] @@ -318,6 +373,17 @@ class ServersTest(test.TestCase): self.assertEquals(response.status_int, 400) self.assertEquals(compute_api.personality_files, None) + def _test_create_instance_with_personality_no_path_xml(self): + personality = [('/remove/this/path', + base64.b64encode('my\n\file\ncontents'))] + body_dict = self._create_personality_request_dict(personality) + request = self._get_create_request_xml(body_dict) + request.body = request.body.replace(' path="/remove/this/path"', '') + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + def test_create_instance_with_personality_no_contents(self): personality = [('/test/path', base64.b64encode('remove\nthese\ncontents'))] @@ -605,3 +671,214 @@ class ServersTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status, '202 Accepted') self.assertEqual(self.server_delete_called, True) + + +class TestServerCreateRequestXMLDeserializer(unittest.TestCase): + + def setUp(self): + self.deserializer = servers.ServerCreateRequestXMLDeserializer() + + def test_minimal_request(self): + serial_request = """ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"server": { + "name": "new-server-test", + "imageId": "1", + "flavorId": "1", + }} + self.assertEquals(request, expected) + + def test_request_with_empty_metadata(self): + serial_request = """ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"server": { + "name": "new-server-test", + "imageId": "1", + "flavorId": "1", + "metadata": {}, + }} + self.assertEquals(request, expected) + + def test_request_with_empty_personality(self): + serial_request = """ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"server": { + "name": "new-server-test", + "imageId": "1", + "flavorId": "1", + "personality": [], + }} + self.assertEquals(request, expected) + + def test_request_with_empty_metadata_and_personality(self): + serial_request = """ +\ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"server": { + "name": "new-server-test", + "imageId": "1", + "flavorId": "1", + "metadata": {}, + "personality": [], + }} + self.assertEquals(request, expected) + + def test_request_with_empty_metadata_and_personality_reversed(self): + serial_request = """ +\ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"server": { + "name": "new-server-test", + "imageId": "1", + "flavorId": "1", + "metadata": {}, + "personality": [], + }} + self.assertEquals(request, expected) + + def test_request_with_one_personality(self): + serial_request = """ +\ +aabbccdd""" + request = self.deserializer.deserialize(serial_request) + expected = [{"path": "/etc/conf", "contents": "aabbccdd"}] + self.assertEquals(request["server"]["personality"], expected) + + def test_request_with_two_personalities(self): + serial_request = """ +\ +aabbccdd\ +abcd""" + request = self.deserializer.deserialize(serial_request) + expected = [{"path": "/etc/conf", "contents": "aabbccdd"}, + {"path": "/etc/sudoers", "contents": "abcd"}] + self.assertEquals(request["server"]["personality"], expected) + + def test_request_with_one_personality_missing_path(self): + serial_request = """ +\ +aabbccdd""" + request = self.deserializer.deserialize(serial_request) + expected = [{"contents": "aabbccdd"}] + self.assertEquals(request["server"]["personality"], expected) + + def test_request_with_one_personality_empty_contents(self): + serial_request = """ +\ +""" + request = self.deserializer.deserialize(serial_request) + expected = [{"path": "/etc/conf", "contents": ""}] + self.assertEquals(request["server"]["personality"], expected) + + def test_request_with_one_personality_empty_contents_variation(self): + serial_request = """ +\ +""" + request = self.deserializer.deserialize(serial_request) + expected = [{"path": "/etc/conf", "contents": ""}] + self.assertEquals(request["server"]["personality"], expected) + + def test_request_with_one_metadata(self): + serial_request = """ +\ +beta""" + request = self.deserializer.deserialize(serial_request) + expected = {"alpha": "beta"} + self.assertEquals(request["server"]["metadata"], expected) + + def test_request_with_two_metadata(self): + serial_request = """ +\ +betabar\ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"alpha": "beta", "foo": "bar"} + self.assertEquals(request["server"]["metadata"], expected) + + def test_request_with_metadata_missing_value(self): + serial_request = """ +\ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"alpha": ""} + self.assertEquals(request["server"]["metadata"], expected) + + def test_request_with_metadata_missing_key(self): + serial_request = """ +\ +beta""" + request = self.deserializer.deserialize(serial_request) + expected = {"": "beta"} + self.assertEquals(request["server"]["metadata"], expected) + + def test_request_with_metadata_duplicate_key(self): + serial_request = """ +\ +barbaz\ +""" + request = self.deserializer.deserialize(serial_request) + expected = {"foo": "baz"} + self.assertEquals(request["server"]["metadata"], expected) + + def test_canonical_request_from_docs(self): + serial_request = """ +\ +Apache1\ +\ +ICAgICAgDQoiQSBjbG91ZCBkb2VzIG5vdCBrbm93IHdoeSBp\ +dCBtb3ZlcyBpbiBqdXN0IHN1Y2ggYSBkaXJlY3Rpb24gYW5k\ +IGF0IHN1Y2ggYSBzcGVlZC4uLkl0IGZlZWxzIGFuIGltcHVs\ +c2lvbi4uLnRoaXMgaXMgdGhlIHBsYWNlIHRvIGdvIG5vdy4g\ +QnV0IHRoZSBza3kga25vd3MgdGhlIHJlYXNvbnMgYW5kIHRo\ +ZSBwYXR0ZXJucyBiZWhpbmQgYWxsIGNsb3VkcywgYW5kIHlv\ +dSB3aWxsIGtub3csIHRvbywgd2hlbiB5b3UgbGlmdCB5b3Vy\ +c2VsZiBoaWdoIGVub3VnaCB0byBzZWUgYmV5b25kIGhvcml6\ +b25zLiINCg0KLVJpY2hhcmQgQmFjaA==\ +""" + expected = {"server": { + "name": "new-server-test", + "imageId": "1", + "flavorId": "1", + "metadata": { + "My Server Name": "Apache1", + }, + "personality": [ + { + "path": "/etc/banner.txt", + "contents": """\ +ICAgICAgDQoiQSBjbG91ZCBkb2VzIG5vdCBrbm93IHdoeSBp\ +dCBtb3ZlcyBpbiBqdXN0IHN1Y2ggYSBkaXJlY3Rpb24gYW5k\ +IGF0IHN1Y2ggYSBzcGVlZC4uLkl0IGZlZWxzIGFuIGltcHVs\ +c2lvbi4uLnRoaXMgaXMgdGhlIHBsYWNlIHRvIGdvIG5vdy4g\ +QnV0IHRoZSBza3kga25vd3MgdGhlIHJlYXNvbnMgYW5kIHRo\ +ZSBwYXR0ZXJucyBiZWhpbmQgYWxsIGNsb3VkcywgYW5kIHlv\ +dSB3aWxsIGtub3csIHRvbywgd2hlbiB5b3UgbGlmdCB5b3Vy\ +c2VsZiBoaWdoIGVub3VnaCB0byBzZWUgYmV5b25kIGhvcml6\ +b25zLiINCg0KLVJpY2hhcmQgQmFjaA==""", + } + ], + }} + request = self.deserializer.deserialize(serial_request) + self.assertEqual(request, expected) + -- cgit From 1d74816bf705cb672d9d323398b03142297d8bec Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 9 Mar 2011 12:03:17 -0500 Subject: remove superfluous trailing blank line --- nova/tests/api/openstack/test_servers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 5132d1ad3..51fb09b18 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -881,4 +881,3 @@ b25zLiINCg0KLVJpY2hhcmQgQmFjaA==""", }} request = self.deserializer.deserialize(serial_request) self.assertEqual(request, expected) - -- cgit From 1166e16d08769222189e31e6de1c6019495fc743 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 9 Mar 2011 13:54:30 -0500 Subject: move my tests into their own testcase --- nova/tests/api/openstack/test_servers.py | 447 +++++++++++++++++-------------- 1 file changed, 243 insertions(+), 204 deletions(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index f2fbb04af..b0f888766 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -238,210 +238,6 @@ class ServersTest(test.TestCase): self.assertEqual(res.status_int, 200) - def _setup_mock_compute_api_for_personality(self): - - class MockComputeAPI(object): - - def __init__(self): - self.personality_files = None - - def create(self, *args, **kwargs): - if 'personality_files' in kwargs: - self.personality_files = kwargs['personality_files'] - else: - self.personality_files = None - return [{'id': '1234', 'display_name': 'fakeinstance'}] - - def make_stub_method(canned_return): - def stub_method(*args, **kwargs): - return canned_return - return stub_method - - compute_api = MockComputeAPI() - self.stubs.Set(nova.compute, 'API', make_stub_method(compute_api)) - self.stubs.Set(nova.api.openstack.servers.Controller, - '_get_kernel_ramdisk_from_image', make_stub_method((1, 1))) - self.stubs.Set(nova.api.openstack.common, - 'get_image_id_from_image_hash', make_stub_method(2)) - return compute_api - - def _create_personality_request_dict(self, personality_files): - server = {} - server['name'] = 'new-server-test' - server['imageId'] = 1 - server['flavorId'] = 1 - if personality_files is not None: - personalities = [] - for path, contents in personality_files: - personalities.append({'path': path, 'contents': contents}) - server['personality'] = personalities - return {'server': server} - - def _get_create_request_json(self, body_dict): - req = webob.Request.blank('/v1.0/servers') - req.content_type = 'application/json' - req.method = 'POST' - req.body = json.dumps(body_dict) - return req - - def _run_create_instance_with_mock_compute_api(self, request): - compute_api = self._setup_mock_compute_api_for_personality() - response = request.get_response(fakes.wsgi_app()) - return compute_api, response - - def _format_xml_request_body(self, body_dict): - server = body_dict['server'] - body_parts = [] - body_parts.extend([ - '', - '' % ( - server['name'], server['imageId'], server['flavorId'])]) - if 'metadata' in server: - metadata = server['metadata'] - body_parts.append('') - for item in metadata.iteritems(): - body_parts.append('%s' % item) - body_parts.append('') - if 'personality' in server: - personalities = server['personality'] - body_parts.append('') - for file in personalities: - item = (file['path'], file['contents']) - body_parts.append('%s' % item) - body_parts.append('') - body_parts.append('') - return ''.join(body_parts) - - def _get_create_request_xml(self, body_dict): - req = webob.Request.blank('/v1.0/servers') - req.content_type = 'application/xml' - req.method = 'POST' - req.body = self._format_xml_request_body(body_dict) - return req - - def _create_instance_with_personality_json(self, personality): - body_dict = self._create_personality_request_dict(personality) - request = self._get_create_request_json(body_dict) - compute_api, response = \ - self._run_create_instance_with_mock_compute_api(request) - return request, response, compute_api.personality_files - - def _create_instance_with_personality_xml(self, personality): - body_dict = self._create_personality_request_dict(personality) - request = self._get_create_request_xml(body_dict) - compute_api, response = \ - self._run_create_instance_with_mock_compute_api(request) - return request, response, compute_api.personality_files - - def test_create_instance_with_no_personality(self): - request, response, personality_files = \ - self._create_instance_with_personality_json(personality=None) - self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, []) - - def test_create_instance_with_no_personality_xml(self): - request, response, personality_files = \ - self._create_instance_with_personality_xml(personality=None) - self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, []) - - def test_create_instance_with_personality(self): - path = '/my/file/path' - contents = '#!/bin/bash\necho "Hello, World!"\n' - b64contents = base64.b64encode(contents) - personality = [(path, b64contents)] - request, response, personality_files = \ - self._create_instance_with_personality_json(personality) - self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, [(path, contents)]) - - def test_create_instance_with_personality_xml(self): - path = '/my/file/path' - contents = '#!/bin/bash\necho "Hello, World!"\n' - b64contents = base64.b64encode(contents) - personality = [(path, b64contents)] - request, response, personality_files = \ - self._create_instance_with_personality_xml(personality) - self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, [(path, contents)]) - - def test_create_instance_with_personality_no_path(self): - personality = [('/remove/this/path', - base64.b64encode('my\n\file\ncontents'))] - body_dict = self._create_personality_request_dict(personality) - del body_dict['server']['personality'][0]['path'] - request = self._get_create_request_json(body_dict) - compute_api, response = \ - self._run_create_instance_with_mock_compute_api(request) - self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) - - def _test_create_instance_with_personality_no_path_xml(self): - personality = [('/remove/this/path', - base64.b64encode('my\n\file\ncontents'))] - body_dict = self._create_personality_request_dict(personality) - request = self._get_create_request_xml(body_dict) - request.body = request.body.replace(' path="/remove/this/path"', '') - compute_api, response = \ - self._run_create_instance_with_mock_compute_api(request) - self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) - - def test_create_instance_with_personality_no_contents(self): - personality = [('/test/path', - base64.b64encode('remove\nthese\ncontents'))] - body_dict = self._create_personality_request_dict(personality) - del body_dict['server']['personality'][0]['contents'] - request = self._get_create_request_json(body_dict) - compute_api, response = \ - self._run_create_instance_with_mock_compute_api(request) - self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) - - def test_create_instance_with_personality_not_a_list(self): - personality = [('/test/path', base64.b64encode('test\ncontents\n'))] - body_dict = self._create_personality_request_dict(personality) - body_dict['server']['personality'] = \ - body_dict['server']['personality'][0] - request = self._get_create_request_json(body_dict) - compute_api, response = \ - self._run_create_instance_with_mock_compute_api(request) - self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) - - def test_create_instance_with_personality_with_non_b64_content(self): - path = '/my/file/path' - contents = '#!/bin/bash\necho "Oh no!"\n' - personality = [(path, contents)] - request, response, personality_files = \ - self._create_instance_with_personality_json(personality) - self.assertEquals(response.status_int, 400) - self.assertEquals(personality_files, None) - - def test_create_instance_with_three_personalities(self): - files = [ - ('/etc/sudoers', 'ALL ALL=NOPASSWD: ALL\n'), - ('/etc/motd', 'Enjoy your root access!\n'), - ('/etc/dovecot.conf', 'dovecot\nconfig\nstuff\n'), - ] - personality = [] - for path, content in files: - personality.append((path, base64.b64encode(content))) - request, response, personality_files = \ - self._create_instance_with_personality_json(personality) - self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, files) - - def test_create_instance_personality_empty_content(self): - path = '/my/file/path' - contents = '' - personality = [(path, contents)] - request, response, personality_files = \ - self._create_instance_with_personality_json(personality) - self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, [(path, contents)]) - def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') req.method = 'PUT' @@ -980,5 +776,248 @@ b25zLiINCg0KLVJpY2hhcmQgQmFjaA==""", self.assertEqual(request, expected) +class TestServerInstanceCreation(test.TestCase): + + def setUp(self): + super(TestServerInstanceCreation, self).setUp() + self.stubs = stubout.StubOutForTesting() + fakes.FakeAuthManager.auth_data = {} + fakes.FakeAuthDatabase.data = {} + fakes.stub_out_networking(self.stubs) + fakes.stub_out_rate_limiting(self.stubs) + fakes.stub_out_auth(self.stubs) + fakes.stub_out_key_pair_funcs(self.stubs) + fakes.stub_out_image_service(self.stubs) + self.stubs.Set(nova.db.api, 'instance_get_all', return_servers) + self.stubs.Set(nova.db.api, 'instance_get', return_server) + self.stubs.Set(nova.db.api, 'instance_get_all_by_user', + return_servers) + self.stubs.Set(nova.db.api, 'instance_add_security_group', + return_security_group) + self.stubs.Set(nova.db.api, 'instance_update', instance_update) + self.stubs.Set(nova.db.api, 'instance_get_fixed_address', + instance_address) + self.stubs.Set(nova.db.api, 'instance_get_floating_address', + instance_address) + self.stubs.Set(nova.compute.API, 'pause', fake_compute_api) + self.stubs.Set(nova.compute.API, 'unpause', fake_compute_api) + self.stubs.Set(nova.compute.API, 'suspend', fake_compute_api) + self.stubs.Set(nova.compute.API, 'resume', fake_compute_api) + self.stubs.Set(nova.compute.API, "get_diagnostics", fake_compute_api) + self.stubs.Set(nova.compute.API, "get_actions", fake_compute_api) + self.allow_admin = FLAGS.allow_admin_api + + self.webreq = common.webob_factory('/v1.0/servers') + + def tearDown(self): + self.stubs.UnsetAll() + FLAGS.allow_admin_api = self.allow_admin + super(TestServerInstanceCreation, self).tearDown() + + def _setup_mock_compute_api_for_personality(self): + + class MockComputeAPI(object): + + def __init__(self): + self.personality_files = None + + def create(self, *args, **kwargs): + if 'personality_files' in kwargs: + self.personality_files = kwargs['personality_files'] + else: + self.personality_files = None + return [{'id': '1234', 'display_name': 'fakeinstance'}] + + def make_stub_method(canned_return): + def stub_method(*args, **kwargs): + return canned_return + return stub_method + + compute_api = MockComputeAPI() + self.stubs.Set(nova.compute, 'API', make_stub_method(compute_api)) + self.stubs.Set(nova.api.openstack.servers.Controller, + '_get_kernel_ramdisk_from_image', make_stub_method((1, 1))) + self.stubs.Set(nova.api.openstack.common, + 'get_image_id_from_image_hash', make_stub_method(2)) + return compute_api + + def _create_personality_request_dict(self, personality_files): + server = {} + server['name'] = 'new-server-test' + server['imageId'] = 1 + server['flavorId'] = 1 + if personality_files is not None: + personalities = [] + for path, contents in personality_files: + personalities.append({'path': path, 'contents': contents}) + server['personality'] = personalities + return {'server': server} + + def _get_create_request_json(self, body_dict): + req = webob.Request.blank('/v1.0/servers') + req.content_type = 'application/json' + req.method = 'POST' + req.body = json.dumps(body_dict) + return req + + def _run_create_instance_with_mock_compute_api(self, request): + compute_api = self._setup_mock_compute_api_for_personality() + response = request.get_response(fakes.wsgi_app()) + return compute_api, response + + def _format_xml_request_body(self, body_dict): + server = body_dict['server'] + body_parts = [] + body_parts.extend([ + '', + '' % ( + server['name'], server['imageId'], server['flavorId'])]) + if 'metadata' in server: + metadata = server['metadata'] + body_parts.append('') + for item in metadata.iteritems(): + body_parts.append('%s' % item) + body_parts.append('') + if 'personality' in server: + personalities = server['personality'] + body_parts.append('') + for file in personalities: + item = (file['path'], file['contents']) + body_parts.append('%s' % item) + body_parts.append('') + body_parts.append('') + return ''.join(body_parts) + + def _get_create_request_xml(self, body_dict): + req = webob.Request.blank('/v1.0/servers') + req.content_type = 'application/xml' + req.method = 'POST' + req.body = self._format_xml_request_body(body_dict) + return req + + def _create_instance_with_personality_json(self, personality): + body_dict = self._create_personality_request_dict(personality) + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + return request, response, compute_api.personality_files + + def _create_instance_with_personality_xml(self, personality): + body_dict = self._create_personality_request_dict(personality) + request = self._get_create_request_xml(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + return request, response, compute_api.personality_files + + def test_create_instance_with_no_personality(self): + request, response, personality_files = \ + self._create_instance_with_personality_json(personality=None) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, []) + + def test_create_instance_with_no_personality_xml(self): + request, response, personality_files = \ + self._create_instance_with_personality_xml(personality=None) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, []) + + def test_create_instance_with_personality(self): + path = '/my/file/path' + contents = '#!/bin/bash\necho "Hello, World!"\n' + b64contents = base64.b64encode(contents) + personality = [(path, b64contents)] + request, response, personality_files = \ + self._create_instance_with_personality_json(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, [(path, contents)]) + + def test_create_instance_with_personality_xml(self): + path = '/my/file/path' + contents = '#!/bin/bash\necho "Hello, World!"\n' + b64contents = base64.b64encode(contents) + personality = [(path, b64contents)] + request, response, personality_files = \ + self._create_instance_with_personality_xml(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, [(path, contents)]) + + def test_create_instance_with_personality_no_path(self): + personality = [('/remove/this/path', + base64.b64encode('my\n\file\ncontents'))] + body_dict = self._create_personality_request_dict(personality) + del body_dict['server']['personality'][0]['path'] + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + + def _test_create_instance_with_personality_no_path_xml(self): + personality = [('/remove/this/path', + base64.b64encode('my\n\file\ncontents'))] + body_dict = self._create_personality_request_dict(personality) + request = self._get_create_request_xml(body_dict) + request.body = request.body.replace(' path="/remove/this/path"', '') + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + + def test_create_instance_with_personality_no_contents(self): + personality = [('/test/path', + base64.b64encode('remove\nthese\ncontents'))] + body_dict = self._create_personality_request_dict(personality) + del body_dict['server']['personality'][0]['contents'] + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + + def test_create_instance_with_personality_not_a_list(self): + personality = [('/test/path', base64.b64encode('test\ncontents\n'))] + body_dict = self._create_personality_request_dict(personality) + body_dict['server']['personality'] = \ + body_dict['server']['personality'][0] + request = self._get_create_request_json(body_dict) + compute_api, response = \ + self._run_create_instance_with_mock_compute_api(request) + self.assertEquals(response.status_int, 400) + self.assertEquals(compute_api.personality_files, None) + + def test_create_instance_with_personality_with_non_b64_content(self): + path = '/my/file/path' + contents = '#!/bin/bash\necho "Oh no!"\n' + personality = [(path, contents)] + request, response, personality_files = \ + self._create_instance_with_personality_json(personality) + self.assertEquals(response.status_int, 400) + self.assertEquals(personality_files, None) + + def test_create_instance_with_three_personalities(self): + files = [ + ('/etc/sudoers', 'ALL ALL=NOPASSWD: ALL\n'), + ('/etc/motd', 'Enjoy your root access!\n'), + ('/etc/dovecot.conf', 'dovecot\nconfig\nstuff\n'), + ] + personality = [] + for path, content in files: + personality.append((path, base64.b64encode(content))) + request, response, personality_files = \ + self._create_instance_with_personality_json(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, files) + + def test_create_instance_personality_empty_content(self): + path = '/my/file/path' + contents = '' + personality = [(path, contents)] + request, response, personality_files = \ + self._create_instance_with_personality_json(personality) + self.assertEquals(response.status_int, 200) + self.assertEquals(personality_files, [(path, contents)]) + + if __name__ == "__main__": unittest.main() -- cgit From 3999bb363501c6587f75255333094c9e61bf1828 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 9 Mar 2011 14:07:33 -0500 Subject: remove unneeded stubs --- nova/tests/api/openstack/test_servers.py | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index b0f888766..c67ecdaae 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -783,32 +783,10 @@ class TestServerInstanceCreation(test.TestCase): self.stubs = stubout.StubOutForTesting() fakes.FakeAuthManager.auth_data = {} fakes.FakeAuthDatabase.data = {} - fakes.stub_out_networking(self.stubs) - fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_auth(self.stubs) fakes.stub_out_key_pair_funcs(self.stubs) - fakes.stub_out_image_service(self.stubs) - self.stubs.Set(nova.db.api, 'instance_get_all', return_servers) - self.stubs.Set(nova.db.api, 'instance_get', return_server) - self.stubs.Set(nova.db.api, 'instance_get_all_by_user', - return_servers) - self.stubs.Set(nova.db.api, 'instance_add_security_group', - return_security_group) - self.stubs.Set(nova.db.api, 'instance_update', instance_update) - self.stubs.Set(nova.db.api, 'instance_get_fixed_address', - instance_address) - self.stubs.Set(nova.db.api, 'instance_get_floating_address', - instance_address) - self.stubs.Set(nova.compute.API, 'pause', fake_compute_api) - self.stubs.Set(nova.compute.API, 'unpause', fake_compute_api) - self.stubs.Set(nova.compute.API, 'suspend', fake_compute_api) - self.stubs.Set(nova.compute.API, 'resume', fake_compute_api) - self.stubs.Set(nova.compute.API, "get_diagnostics", fake_compute_api) - self.stubs.Set(nova.compute.API, "get_actions", fake_compute_api) self.allow_admin = FLAGS.allow_admin_api - self.webreq = common.webob_factory('/v1.0/servers') - def tearDown(self): self.stubs.UnsetAll() FLAGS.allow_admin_api = self.allow_admin -- cgit From 7854cf996608c09e753c70c3914746dab22c4e1a Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 9 Mar 2011 14:21:18 -0500 Subject: update authors file --- Authors | 1 + 1 file changed, 1 insertion(+) diff --git a/Authors b/Authors index 7993955e2..583cf8e75 100644 --- a/Authors +++ b/Authors @@ -39,6 +39,7 @@ Ken Pepple Kevin L. Mitchell Koji Iida Lorin Hochstein +Mark Washenberger Masanori Itoh Matt Dietz Michael Gundlach -- cgit From 2c733d5365b753989b506d82d376d980cd701547 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Wed, 9 Mar 2011 14:38:34 -0500 Subject: rearrange functions and add docstrings --- nova/api/openstack/servers.py | 83 ++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 4a18d870c..419a001fb 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -141,39 +141,6 @@ class Controller(wsgi.Controller): return faults.Fault(exc.HTTPNotFound()) return exc.HTTPAccepted() - def _get_personality_files(self, personality): - """ - Create a list of personality files from the personality attribute - - At this time, personality_files must be formatted as a list of - (file_path, file_content) pairs for compatibility with the - underlying compute service. - """ - personality_files = [] - for item in personality: - try: - path = item['path'] - contents = item['contents'] - except KeyError as key: - expl = 'Bad personality format: missing %s' % key - raise exc.HTTPBadRequest(explanation=expl) - except TypeError: - raise exc.HTTPBadRequest(explanation='Bad personality format') - try: - contents = base64.b64decode(contents) - except TypeError: - msg = 'Personality content for %s cannot be decoded' % path - raise exc.HTTPBadRequest(explanation=msg) - personality_files.append((path, contents)) - return personality_files - - def _deserialize_create(self, request): - if request.content_type == "application/xml": - deserializer = ServerCreateRequestXMLDeserializer() - return deserializer.deserialize(request.body) - else: - return self._deserialize(request.body, request) - def create(self, req): """ Creates a new server for a given user """ env = self._deserialize_create(req) @@ -218,6 +185,44 @@ class Controller(wsgi.Controller): personality_files=personality_files) return _translate_keys(instances[0]) + def _deserialize_create(self, request): + """ + Deserialize a create request + + Overrides normal behavior in the case of xml content + """ + if request.content_type == "application/xml": + deserializer = ServerCreateRequestXMLDeserializer() + return deserializer.deserialize(request.body) + else: + return self._deserialize(request.body, request) + + def _get_personality_files(self, personality): + """ + Create a list of personality files from the personality attribute + + At this time, personality_files must be formatted as a list of + (file_path, file_content) pairs for compatibility with the + underlying compute service. + """ + personality_files = [] + for item in personality: + try: + path = item['path'] + contents = item['contents'] + except KeyError as key: + expl = 'Bad personality format: missing %s' % key + raise exc.HTTPBadRequest(explanation=expl) + except TypeError: + raise exc.HTTPBadRequest(explanation='Bad personality format') + try: + contents = base64.b64decode(contents) + except TypeError: + msg = 'Personality content for %s cannot be decoded' % path + raise exc.HTTPBadRequest(explanation=msg) + personality_files.append((path, contents)) + return personality_files + def update(self, req, id): """ Updates the server name or password """ inst_dict = self._deserialize(req.body, req) @@ -507,13 +512,21 @@ class Controller(wsgi.Controller): class ServerCreateRequestXMLDeserializer(object): + """ + Deserializer to handle xml-formatted server create requests. + + Handles standard server attributes as well as optional metadata + and personality attributes + """ def deserialize(self, string): + """Deserialize an xml-formatted server create request""" dom = minidom.parseString(string) server = self._extract_server(dom) return {'server': server} def _extract_server(self, node): + """Marshal the server attribute of a parsed request""" server = {} server_node = self._find_first_child_named(node, 'server') for attr in ["name", "imageId", "flavorId"]: @@ -527,6 +540,7 @@ class ServerCreateRequestXMLDeserializer(object): return server def _extract_metadata(self, server_node): + """Marshal the metadata attribute of a parsed request""" metadata_node = self._find_first_child_named(server_node, "metadata") if metadata_node is None: return None @@ -537,6 +551,7 @@ class ServerCreateRequestXMLDeserializer(object): return metadata def _extract_personality(self, server_node): + """Marshal the personality attribute of a parsed request""" personality_node = \ self._find_first_child_named(server_node, "personality") if personality_node is None: @@ -551,12 +566,14 @@ class ServerCreateRequestXMLDeserializer(object): return personality def _find_first_child_named(self, parent, name): + """Search a nodes children for the first child with a given name""" for node in parent.childNodes: if node.nodeName == name: return node return None def _extract_text(self, node): + """Get the text field contained by the given node""" if len(node.childNodes) == 1: child = node.childNodes[0] if child.nodeType == child.TEXT_NODE: -- cgit From 90f38451e5df4f0ca862401cf898f01ffede6174 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 10 Mar 2011 00:26:25 -0500 Subject: add tests to verify the serialization of adminPass in server creation response --- nova/tests/api/openstack/test_servers.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 2fc28fe67..0561ad499 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -19,6 +19,7 @@ import base64 import datetime import json import unittest +from xml.dom import minidom import stubout import webob @@ -908,6 +909,7 @@ class TestServerInstanceCreation(test.TestCase): def _get_create_request_xml(self, body_dict): req = webob.Request.blank('/v1.0/servers') req.content_type = 'application/xml' + req.accept = 'application/xml' req.method = 'POST' req.body = self._format_xml_request_body(body_dict) return req @@ -1034,6 +1036,23 @@ class TestServerInstanceCreation(test.TestCase): self.assertEquals(response.status_int, 200) self.assertEquals(personality_files, [(path, contents)]) + def test_create_instance_admin_pass_json(self): + request, response, dummy = \ + self._create_instance_with_personality_json(None) + self.assertEquals(response.status_int, 200) + response = json.loads(response.body) + self.assertTrue('adminPass' in response['server']) + self.assertTrue(response['server']['adminPass'].startswith('fake')) + + def test_create_instance_admin_pass_xml(self): + request, response, dummy = \ + self._create_instance_with_personality_xml(None) + self.assertEquals(response.status_int, 200) + dom = minidom.parseString(response.body) + server = dom.childNodes[0] + self.assertEquals(server.nodeName, 'server') + self.assertTrue(server.getAttribute('adminPass').startswith('fake')) + if __name__ == "__main__": unittest.main() -- cgit From 616723fe4e7d52b0b8ddafda10fcfe07a87609c8 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 10 Mar 2011 14:53:13 -0500 Subject: add docstring --- nova/compute/api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nova/compute/api.py b/nova/compute/api.py index 2766ddc9c..efa051d10 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -81,6 +81,11 @@ class API(base.Base): {"method": "get_network_topic", "args": {'fake': 1}}) def _check_personality_file_quota(self, context, personality_files): + """ + Enforce quota limits on personality files + + Raises a QuotaError if any limit is exceeded + """ limit = quota.allowed_personality_files(context) if len(personality_files) > limit: raise quota.QuotaError(_("Personality limit exceeded. You can " -- cgit From c967679fa8144af57d79d89666ee29a0241d38a9 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Thu, 10 Mar 2011 17:36:41 -0500 Subject: switch to a more consistent usage of onset_files variable names --- nova/api/openstack/servers.py | 59 ++++++++++++++++++++----------- nova/compute/api.py | 38 ++++++++------------ nova/compute/manager.py | 2 +- nova/quota.py | 30 ++++++++-------- nova/tests/api/openstack/test_servers.py | 48 ++++++++++++------------- nova/tests/test_quota.py | 60 ++++++++++++++++---------------- 6 files changed, 124 insertions(+), 113 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 7bef8eb32..adb5c5f99 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -169,20 +169,23 @@ class Controller(wsgi.Controller): metadata.append({'key': k, 'value': v}) personality = env['server'].get('personality', []) - personality_files = self._get_personality_files(personality) - - instances = self.compute_api.create( - context, - instance_types.get_by_flavor_id(env['server']['flavorId']), - image_id, - kernel_id=kernel_id, - ramdisk_id=ramdisk_id, - display_name=env['server']['name'], - display_description=env['server']['name'], - key_name=key_pair['name'], - key_data=key_pair['public_key'], - metadata=metadata, - personality_files=personality_files) + onset_files = self._get_onset_files(personality) + + try: + instances = self.compute_api.create( + context, + instance_types.get_by_flavor_id(env['server']['flavorId']), + image_id, + kernel_id=kernel_id, + ramdisk_id=ramdisk_id, + display_name=env['server']['name'], + display_description=env['server']['name'], + key_name=key_pair['name'], + key_data=key_pair['public_key'], + metadata=metadata, + onset_files=onset_files) + except QuotaError as error: + self._handle_quota_error(error) server = _translate_keys(instances[0]) password = "%s%s" % (server['server']['name'][:4], @@ -204,15 +207,15 @@ class Controller(wsgi.Controller): else: return self._deserialize(request.body, request.get_content_type()) - def _get_personality_files(self, personality): + def _get_onset_files(self, personality): """ - Create a list of personality files from the personality attribute + Create a list of onset files from the personality attribute - At this time, personality_files must be formatted as a list of + At this time, onset_files must be formatted as a list of (file_path, file_content) pairs for compatibility with the underlying compute service. """ - personality_files = [] + onset_files = [] for item in personality: try: path = item['path'] @@ -227,8 +230,24 @@ class Controller(wsgi.Controller): except TypeError: msg = 'Personality content for %s cannot be decoded' % path raise exc.HTTPBadRequest(explanation=msg) - personality_files.append((path, contents)) - return personality_files + onset_files.append((path, contents)) + return onset_files + + def _handle_quota_errors(self, error): + """ + Reraise quota errors as api-specific http exceptions + """ + if error.code == "OnsetFileLimitExceeded": + expl = "Personality file limit exceeded" + raise exc.HTTPBadRequest(explanation=expl) + if error.code == "OnsetFilePathLimitExceeded": + expl = "Personality file path too long" + raise exc.HTTPBadRequest(explanation=expl) + if error.code == "OnsetFileContentLimitExceeded": + expl = "Personality file content too long" + raise exc.HTTPBadRequest(explanation=expl) + # if the original error is okay, just reraise it + raise error def update(self, req, id): """ Updates the server name or password """ diff --git a/nova/compute/api.py b/nova/compute/api.py index b97cadf61..140bbb3aa 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -80,30 +80,23 @@ class API(base.Base): topic, {"method": "get_network_topic", "args": {'fake': 1}}) - def _check_personality_file_quota(self, context, personality_files): + def _check_onset_file_quota(self, context, onset_files): """ - Enforce quota limits on personality files + Enforce quota limits on onset files Raises a QuotaError if any limit is exceeded """ - limit = quota.allowed_personality_files(context) - if len(personality_files) > limit: - raise quota.QuotaError(_("Personality limit exceeded. You can " - "only have %d personalities when " - "creating an instance.") % limit, - "PersonalityLimitExceeded") - path_limit = quota.allowed_personality_path_bytes(context) - content_limit = quota.allowed_personality_content_bytes(context) - for path, content in personality_files: + limit = quota.allowed_onset_files(context) + if len(onset_files) > limit: + raise quota.QuotaError(code="OnsetFileLimitExceeded") + path_limit = quota.allowed_onset_file_path_bytes(context) + content_limit = quota.allowed_onset_file_content_bytes(context) + for path, content in onset_files: if len(path) > path_limit: - raise quota.QuotaError( - _("Personality file path limit exceeded."), - "PersonalityLimitExceeded") + raise quota.QuotaError(code="OnsetFilePathLimitExceeded") if len(content) > content_limit: - raise quota.QuotaError( - _("Personality file content limit exceeded."), - "PersonalityLimitExceeded") - return personality_files + raise quota.QuotaError(code="OnsetFileContentLimitExceeded") + return onset_files def create(self, context, instance_type, image_id, kernel_id=None, ramdisk_id=None, @@ -111,7 +104,7 @@ class API(base.Base): display_name='', display_description='', key_name=None, key_data=None, security_group='default', availability_zone=None, user_data=None, metadata=[], - personality_files=None): + onset_files=None): """Create the number of instances requested if quota and other arguments check out ok.""" @@ -149,9 +142,8 @@ class API(base.Base): LOG.warn(msg) raise quota.QuotaError(msg, "MetadataLimitExceeded") - if personality_files is not None: - personality_files = \ - self._check_personality_file_quota(context, personality_files) + if onset_files is not None: + onset_files = self._check_onset_file_quota(context, onset_files) image = self.image_service.show(context, image_id) if kernel_id is None: @@ -248,7 +240,7 @@ class API(base.Base): "args": {"topic": FLAGS.compute_topic, "instance_id": instance_id, "availability_zone": availability_zone, - "personality_files": personality_files}}) + "onset_files": onset_files}}) for group_id in security_groups: self.trigger_security_group_members_refresh(elevated, group_id) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index d87290aae..601bb3084 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -173,7 +173,7 @@ class ComputeManager(manager.Manager): """Launch a new instance with specified options.""" context = context.elevated() instance_ref = self.db.instance_get(context, instance_id) - instance_ref.onset_files = kwargs.get('personality_files', []) + instance_ref.onset_files = kwargs.get('onset_files', []) if instance_ref['name'] in self.driver.list_instances(): raise exception.Error(_("Instance has already been created")) LOG.audit(_("instance %s: starting..."), instance_id, diff --git a/nova/quota.py b/nova/quota.py index fdef42bc5..e0fb97542 100644 --- a/nova/quota.py +++ b/nova/quota.py @@ -37,12 +37,12 @@ flags.DEFINE_integer('quota_floating_ips', 10, 'number of floating ips allowed per project') flags.DEFINE_integer('quota_metadata_items', 128, 'number of metadata items allowed per instance') -flags.DEFINE_integer('quota_personality_max_files', 5, - 'number of personality files allowed') -flags.DEFINE_integer('quota_personality_max_content_bytes', 10 * 1024, - 'number of bytes allowed per personality file') -flags.DEFINE_integer('quota_personality_max_path_bytes', 255, - 'number of bytes allowed per personality file path') +flags.DEFINE_integer('quota_max_onset_files', 5, + 'number of onset files allowed') +flags.DEFINE_integer('quota_max_onset_file_content_bytes', 10 * 1024, + 'number of bytes allowed per onset file') +flags.DEFINE_integer('quota_max_onset_file_path_bytes', 255, + 'number of bytes allowed per onset file path') def get_quota(context, project_id): @@ -113,19 +113,19 @@ def allowed_metadata_items(context, num_metadata_items): return min(num_metadata_items, num_allowed_metadata_items) -def allowed_personality_files(context): - """Return the number of personality files allowed""" - return int(FLAGS.quota_personality_max_files) +def allowed_onset_files(context): + """Return the number of onset files allowed""" + return int(FLAGS.quota_max_onset_files) -def allowed_personality_content_bytes(context): - """Return the number of bytes allowed per personality content""" - return int(FLAGS.quota_personality_max_content_bytes) +def allowed_onset_file_content_bytes(context): + """Return the number of bytes allowed per onset file content""" + return int(FLAGS.quota_max_onset_file_content_bytes) -def allowed_personality_path_bytes(context): - """Return the number of bytes allowed in a personality file path""" - return int(FLAGS.quota_personality_max_path_bytes) +def allowed_onset_file_path_bytes(context): + """Return the number of bytes allowed in an onset file path""" + return int(FLAGS.quota_max_onset_file_path_bytes) class QuotaError(exception.ApiError): diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 0561ad499..b6d88d833 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -833,13 +833,13 @@ class TestServerInstanceCreation(test.TestCase): class MockComputeAPI(object): def __init__(self): - self.personality_files = None + self.onset_files = None def create(self, *args, **kwargs): - if 'personality_files' in kwargs: - self.personality_files = kwargs['personality_files'] + if 'onset_files' in kwargs: + self.onset_files = kwargs['onset_files'] else: - self.personality_files = None + self.onset_files = None return [{'id': '1234', 'display_name': 'fakeinstance'}] def set_admin_password(self, *args, **kwargs): @@ -919,46 +919,46 @@ class TestServerInstanceCreation(test.TestCase): request = self._get_create_request_json(body_dict) compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) - return request, response, compute_api.personality_files + return request, response, compute_api.onset_files def _create_instance_with_personality_xml(self, personality): body_dict = self._create_personality_request_dict(personality) request = self._get_create_request_xml(body_dict) compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) - return request, response, compute_api.personality_files + return request, response, compute_api.onset_files def test_create_instance_with_no_personality(self): - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_json(personality=None) self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, []) + self.assertEquals(onset_files, []) def test_create_instance_with_no_personality_xml(self): - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_xml(personality=None) self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, []) + self.assertEquals(onset_files, []) def test_create_instance_with_personality(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' b64contents = base64.b64encode(contents) personality = [(path, b64contents)] - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, [(path, contents)]) + self.assertEquals(onset_files, [(path, contents)]) def test_create_instance_with_personality_xml(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' b64contents = base64.b64encode(contents) personality = [(path, b64contents)] - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_xml(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, [(path, contents)]) + self.assertEquals(onset_files, [(path, contents)]) def test_create_instance_with_personality_no_path(self): personality = [('/remove/this/path', @@ -969,7 +969,7 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) + self.assertEquals(compute_api.onset_files, None) def _test_create_instance_with_personality_no_path_xml(self): personality = [('/remove/this/path', @@ -980,7 +980,7 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) + self.assertEquals(compute_api.onset_files, None) def test_create_instance_with_personality_no_contents(self): personality = [('/test/path', @@ -991,7 +991,7 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) + self.assertEquals(compute_api.onset_files, None) def test_create_instance_with_personality_not_a_list(self): personality = [('/test/path', base64.b64encode('test\ncontents\n'))] @@ -1002,16 +1002,16 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.personality_files, None) + self.assertEquals(compute_api.onset_files, None) def test_create_instance_with_personality_with_non_b64_content(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Oh no!"\n' personality = [(path, contents)] - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 400) - self.assertEquals(personality_files, None) + self.assertEquals(onset_files, None) def test_create_instance_with_three_personalities(self): files = [ @@ -1022,19 +1022,19 @@ class TestServerInstanceCreation(test.TestCase): personality = [] for path, content in files: personality.append((path, base64.b64encode(content))) - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, files) + self.assertEquals(onset_files, files) def test_create_instance_personality_empty_content(self): path = '/my/file/path' contents = '' personality = [(path, contents)] - request, response, personality_files = \ + request, response, onset_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(personality_files, [(path, contents)]) + self.assertEquals(onset_files, [(path, contents)]) def test_create_instance_admin_pass_json(self): request, response, dummy = \ diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py index 86b1d40fb..d94381aa2 100644 --- a/nova/tests/test_quota.py +++ b/nova/tests/test_quota.py @@ -200,66 +200,66 @@ class QuotaTestCase(test.TestCase): image_id='fake', metadata=metadata) - def test_allowed_personality_files(self): + def test_allowed_onset_files(self): self.assertEqual( - quota.allowed_personality_files(self.context), - FLAGS.quota_personality_max_files) + quota.allowed_onset_files(self.context), + FLAGS.quota_max_onset_files) - def _create_with_personality(self, files): + def _create_with_onset_files(self, files): api = compute.API(image_service=self.StubImageService()) api.create(self.context, min_count=1, max_count=1, instance_type='m1.small', image_id='fake', - personality_files=files) + onset_files=files) - def test_no_personality_files(self): + def test_no_onset_files(self): api = compute.API(image_service=self.StubImageService()) api.create(self.context, instance_type='m1.small', image_id='fake') - def test_max_personality_files(self): + def test_max_onset_files(self): files = [] - for i in xrange(FLAGS.quota_personality_max_files): + for i in xrange(FLAGS.quota_max_onset_files): files.append(('/my/path%d' % i, 'config = test\n')) - self._create_with_personality(files) # no QuotaError + self._create_with_onset_files(files) # no QuotaError - def test_too_many_personality_files(self): + def test_too_many_onset_files(self): files = [] - for i in xrange(FLAGS.quota_personality_max_files + 1): + for i in xrange(FLAGS.quota_max_onset_files + 1): files.append(('/my/path%d' % i, 'my\ncontent%d\n' % i)) self.assertRaises(quota.QuotaError, - self._create_with_personality, files) + self._create_with_onset_files, files) - def test_allowed_personality_content_bytes(self): + def test_allowed_onset_file_content_bytes(self): self.assertEqual( - quota.allowed_personality_content_bytes(self.context), - FLAGS.quota_personality_max_content_bytes) + quota.allowed_onset_file_content_bytes(self.context), + FLAGS.quota_max_onset_file_content_bytes) - def test_max_personality_content_bytes(self): - max = FLAGS.quota_personality_max_content_bytes + def test_max_onset_file_content_bytes(self): + max = FLAGS.quota_max_onset_file_content_bytes content = ''.join(['a' for i in xrange(max)]) files = [('/test/path', content)] - self._create_with_personality(files) # no QuotaError + self._create_with_onset_files(files) # no QuotaError - def test_too_many_personality_content_bytes(self): - max = FLAGS.quota_personality_max_content_bytes + def test_too_many_onset_file_content_bytes(self): + max = FLAGS.quota_max_onset_file_content_bytes content = ''.join(['a' for i in xrange(max + 1)]) files = [('/test/path', content)] self.assertRaises(quota.QuotaError, - self._create_with_personality, files) + self._create_with_onset_files, files) - def test_allowed_personality_path_bytes(self): + def test_allowed_onset_file_path_bytes(self): self.assertEqual( - quota.allowed_personality_path_bytes(self.context), - FLAGS.quota_personality_max_path_bytes) + quota.allowed_onset_file_path_bytes(self.context), + FLAGS.quota_max_onset_file_path_bytes) - def test_max_personality_path_bytes(self): - max = FLAGS.quota_personality_max_path_bytes + def test_max_onset_file_path_bytes(self): + max = FLAGS.quota_max_onset_file_path_bytes path = ''.join(['a' for i in xrange(max)]) files = [(path, 'config = quotatest')] - self._create_with_personality(files) # no QuotaError + self._create_with_onset_files(files) # no QuotaError - def test_too_many_personality_path_bytes(self): - max = FLAGS.quota_personality_max_path_bytes + def test_too_many_onset_file_path_bytes(self): + max = FLAGS.quota_max_onset_file_path_bytes path = ''.join(['a' for i in xrange(max + 1)]) files = [(path, 'config = quotatest')] self.assertRaises(quota.QuotaError, - self._create_with_personality, files) + self._create_with_onset_files, files) -- cgit From be9734b03bce871d32e21da2ba341dfa42aa020a Mon Sep 17 00:00:00 2001 From: Brian Lamar Date: Fri, 11 Mar 2011 17:19:14 -0500 Subject: Fixed lp732866 by catching relevant `exception.NotFound` exception. Tests did not uncover this vulnerability due to "incorrect" FakeAuthManager. I say "incorrect" because potentially different implementations (LDAP or Database driven) of AuthManager might return different errors from `get_user_from_access_key`. Also, removed all references to 'bacon', 'ham', 'herp', and 'derp' and replaced them with hopefully more helpful terms. --- nova/api/openstack/auth.py | 6 +++- nova/tests/api/openstack/fakes.py | 5 +++- nova/tests/api/openstack/test_auth.py | 56 +++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/nova/api/openstack/auth.py b/nova/api/openstack/auth.py index 4c6b58eff..f3a9bdeca 100644 --- a/nova/api/openstack/auth.py +++ b/nova/api/openstack/auth.py @@ -135,7 +135,11 @@ class AuthMiddleware(wsgi.Middleware): req - wsgi.Request object """ ctxt = context.get_admin_context() - user = self.auth.get_user_from_access_key(key) + + try: + user = self.auth.get_user_from_access_key(key) + except exception.NotFound: + user = None if user and user.name == username: token_hash = hashlib.sha1('%s%s%f' % (username, key, diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index e50d11a3d..7cb974bb2 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -321,7 +321,10 @@ class FakeAuthManager(object): (user.id == p.project_manager_id)] def get_user_from_access_key(self, key): - return FakeAuthManager.auth_data.get(key, None) + try: + return FakeAuthManager.auth_data[key] + except KeyError: + raise exc.NotFound class FakeRateLimiter(object): diff --git a/nova/tests/api/openstack/test_auth.py b/nova/tests/api/openstack/test_auth.py index aaaa4e415..d1b3c0e10 100644 --- a/nova/tests/api/openstack/test_auth.py +++ b/nova/tests/api/openstack/test_auth.py @@ -51,11 +51,12 @@ class Test(test.TestCase): def test_authorize_user(self): f = fakes.FakeAuthManager() - f.add_user('derp', nova.auth.manager.User(1, 'herp', None, None, None)) + f.add_user('user1_key', + nova.auth.manager.User(1, 'user1', None, None, None)) req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-User'] = 'herp' - req.headers['X-Auth-Key'] = 'derp' + req.headers['X-Auth-User'] = 'user1' + req.headers['X-Auth-Key'] = 'user1_key' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '204 No Content') self.assertEqual(len(result.headers['X-Auth-Token']), 40) @@ -65,13 +66,13 @@ class Test(test.TestCase): def test_authorize_token(self): f = fakes.FakeAuthManager() - u = nova.auth.manager.User(1, 'herp', None, None, None) - f.add_user('derp', u) - f.create_project('test', u) + u = nova.auth.manager.User(1, 'user1', None, None, None) + f.add_user('user1_key', u) + f.create_project('user1_project', u) req = webob.Request.blank('/v1.0/', {'HTTP_HOST': 'foo'}) - req.headers['X-Auth-User'] = 'herp' - req.headers['X-Auth-Key'] = 'derp' + req.headers['X-Auth-User'] = 'user1' + req.headers['X-Auth-Key'] = 'user1_key' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '204 No Content') self.assertEqual(len(result.headers['X-Auth-Token']), 40) @@ -92,7 +93,7 @@ class Test(test.TestCase): def test_token_expiry(self): self.destroy_called = False - token_hash = 'bacon' + token_hash = 'token_hash' def destroy_token_mock(meh, context, token): self.destroy_called = True @@ -109,15 +110,26 @@ class Test(test.TestCase): bad_token) req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-Token'] = 'bacon' + req.headers['X-Auth-Token'] = 'token_hash' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '401 Unauthorized') self.assertEqual(self.destroy_called, True) - def test_bad_user(self): + def test_bad_user_bad_key(self): + req = webob.Request.blank('/v1.0/') + req.headers['X-Auth-User'] = 'unknown_user' + req.headers['X-Auth-Key'] = 'unknown_user_key' + result = req.get_response(fakes.wsgi_app()) + self.assertEqual(result.status, '401 Unauthorized') + + def test_bad_user_good_key(self): + f = fakes.FakeAuthManager() + u = nova.auth.manager.User(1, 'user1', None, None, None) + f.add_user('user1_key', u) + req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-User'] = 'herp' - req.headers['X-Auth-Key'] = 'derp' + req.headers['X-Auth-User'] = 'unknown_user' + req.headers['X-Auth-Key'] = 'user1_key' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '401 Unauthorized') @@ -128,7 +140,7 @@ class Test(test.TestCase): def test_bad_token(self): req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-Token'] = 'baconbaconbacon' + req.headers['X-Auth-Token'] = 'unknown_token' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '401 Unauthorized') @@ -137,11 +149,11 @@ class TestFunctional(test.TestCase): def test_token_expiry(self): ctx = context.get_admin_context() tok = db.auth_token_create(ctx, dict( - token_hash='bacon', + token_hash='test_token_hash', cdn_management_url='', server_management_url='', storage_url='', - user_id='ham', + user_id='user1', )) db.auth_token_update(ctx, tok.token_hash, dict( @@ -149,13 +161,13 @@ class TestFunctional(test.TestCase): )) req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-Token'] = 'bacon' + req.headers['X-Auth-Token'] = 'test_token_hash' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '401 Unauthorized') def test_token_doesnotexist(self): req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-Token'] = 'ham' + req.headers['X-Auth-Token'] = 'nonexistant_token_hash' result = req.get_response(fakes.wsgi_app()) self.assertEqual(result.status, '401 Unauthorized') @@ -178,13 +190,13 @@ class TestLimiter(test.TestCase): def test_authorize_token(self): f = fakes.FakeAuthManager() - u = nova.auth.manager.User(1, 'herp', None, None, None) - f.add_user('derp', u) + u = nova.auth.manager.User(1, 'user1', None, None, None) + f.add_user('user1_key', u) f.create_project('test', u) req = webob.Request.blank('/v1.0/') - req.headers['X-Auth-User'] = 'herp' - req.headers['X-Auth-Key'] = 'derp' + req.headers['X-Auth-User'] = 'user1' + req.headers['X-Auth-Key'] = 'user1_key' result = req.get_response(fakes.wsgi_app()) self.assertEqual(len(result.headers['X-Auth-Token']), 40) -- cgit From 909b72faa77ba0a2bc787309b95fdfae9bb9ca01 Mon Sep 17 00:00:00 2001 From: Brian Lamar Date: Fri, 11 Mar 2011 17:41:10 -0500 Subject: Removed EOL whitespace in accordance with PEP-8. --- nova/tests/api/openstack/test_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/tests/api/openstack/test_auth.py b/nova/tests/api/openstack/test_auth.py index d1b3c0e10..0448ed701 100644 --- a/nova/tests/api/openstack/test_auth.py +++ b/nova/tests/api/openstack/test_auth.py @@ -51,7 +51,7 @@ class Test(test.TestCase): def test_authorize_user(self): f = fakes.FakeAuthManager() - f.add_user('user1_key', + f.add_user('user1_key', nova.auth.manager.User(1, 'user1', None, None, None)) req = webob.Request.blank('/v1.0/') -- cgit From 337bda95a9e12d395f838e81e279c875b056aba9 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Mon, 14 Mar 2011 22:17:14 +0100 Subject: Add missing fallback chain for ipv6. --- nova/virt/libvirt_conn.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nova/virt/libvirt_conn.py b/nova/virt/libvirt_conn.py index 0b306c950..03f046cbd 100644 --- a/nova/virt/libvirt_conn.py +++ b/nova/virt/libvirt_conn.py @@ -1597,6 +1597,9 @@ class IptablesFirewallDriver(FirewallDriver): self.iptables.ipv4['filter'].add_chain('sg-fallback') self.iptables.ipv4['filter'].add_rule('sg-fallback', '-j DROP') + if FLAGS.use_ipv6: + self.iptables.ipv6['filter'].add_chain('sg-fallback') + self.iptables.ipv6['filter'].add_rule('sg-fallback', '-j DROP') def setup_basic_filtering(self, instance): """Use NWFilter from libvirt for this.""" -- cgit -- cgit From a4e94971b696681a5ced189d8f4263c8f77cc531 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 14 Mar 2011 19:57:30 -0700 Subject: Make key_pair optional with OpenStack API --- nova/api/openstack/servers.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index dc28a0782..47ed254ec 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -146,10 +146,14 @@ class Controller(wsgi.Controller): return faults.Fault(exc.HTTPUnprocessableEntity()) context = req.environ['nova.context'] + + key_name = None + key_data = None key_pairs = auth_manager.AuthManager.get_key_pairs(context) - if not key_pairs: - raise exception.NotFound(_("No keypairs defined")) - key_pair = key_pairs[0] + if key_pairs: + key_pair = key_pairs[0] + key_name = key_pair['name'] + key_data = key_pair['public_key'] image_id = common.get_image_id_from_image_hash(self._image_service, context, env['server']['imageId']) @@ -174,8 +178,8 @@ class Controller(wsgi.Controller): ramdisk_id=ramdisk_id, display_name=env['server']['name'], display_description=env['server']['name'], - key_name=key_pair['name'], - key_data=key_pair['public_key'], + key_name=key_name, + key_data=key_data, metadata=metadata, onset_files=env.get('onset_files', [])) -- cgit From da605eb84f7d5de741225ff936447db01690a04f Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 14 Mar 2011 20:48:33 -0700 Subject: Don't generate insecure passwords where it's easy to use urandom instead --- nova/console/manager.py | 2 +- nova/console/xvp.py | 4 ---- nova/utils.py | 15 ++++++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/nova/console/manager.py b/nova/console/manager.py index 57c75cf4f..bfa571ea9 100644 --- a/nova/console/manager.py +++ b/nova/console/manager.py @@ -69,7 +69,7 @@ class ConsoleProxyManager(manager.Manager): except exception.NotFound: logging.debug(_("Adding console")) if not password: - password = self.driver.generate_password() + password = utils.generate_password(8) if not port: port = self.driver.get_port(context) console_data = {'instance_name': name, diff --git a/nova/console/xvp.py b/nova/console/xvp.py index 68d8c8565..0cedfbb13 100644 --- a/nova/console/xvp.py +++ b/nova/console/xvp.py @@ -91,10 +91,6 @@ class XVPConsoleProxy(object): """Trim password to length, and encode""" return self._xvp_encrypt(password) - def generate_password(self, length=8): - """Returns random console password""" - return os.urandom(length * 2).encode('base64')[:length] - def _rebuild_xvp_conf(self, context): logging.debug(_("Rebuilding xvp conf")) pools = [pool for pool in diff --git a/nova/utils.py b/nova/utils.py index 87e726394..9c8b27d56 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -263,12 +263,17 @@ def generate_mac(): def generate_password(length=20): - """Generate a random sequence of letters and digits - to be used as a password. Note that this is not intended - to represent the ultimate in security. + """Generate a random alphanumeric password, avoiding 'confusing' O,0,I,1. + + Believed to be reasonably secure (with a reasonable password length!) """ - chrs = string.letters + string.digits - return "".join([random.choice(chrs) for i in xrange(length)]) + # 26 letters, 10 digits = 36 + # Remove O, 0, I, 1 => 32 digits + # 32 digits means we're just using the low 5 bit of each byte + chrs = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" + + random_bytes = os.urandom(length) + return "".join([chrs[ord(random_bytes[i]) % 32] for i in xrange(length)]) def last_octet(address): -- cgit From 3d0cde272e3227978c5875c811c93e1e3df692ed Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 14 Mar 2011 21:01:48 -0700 Subject: Clarify the logic in using 32 symbols --- nova/utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nova/utils.py b/nova/utils.py index 9c8b27d56..0510c3cbe 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -267,9 +267,10 @@ def generate_password(length=20): Believed to be reasonably secure (with a reasonable password length!) """ - # 26 letters, 10 digits = 36 - # Remove O, 0, I, 1 => 32 digits - # 32 digits means we're just using the low 5 bit of each byte + # 26 letters, 10 digits = 36 choices + # Remove O, 0, I, 1 => 32 choices + # 32 choices means we're just using the low 5 bit of each byte, + # so there's no bias introduced by using a modulo chrs = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" random_bytes = os.urandom(length) -- cgit From 8a41046dc7cafb19afb6719866b11681daaa9082 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Tue, 15 Mar 2011 09:48:21 +0100 Subject: Always put the ipv6 fallback in place. FLAGS.use_ipv6 does not exist yet when the firewall driver is instantiated and the iptables manager takes care not to fiddle with ipv6 if not enabled. --- nova/virt/libvirt_conn.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nova/virt/libvirt_conn.py b/nova/virt/libvirt_conn.py index 03f046cbd..f87decaa0 100644 --- a/nova/virt/libvirt_conn.py +++ b/nova/virt/libvirt_conn.py @@ -1597,9 +1597,8 @@ class IptablesFirewallDriver(FirewallDriver): self.iptables.ipv4['filter'].add_chain('sg-fallback') self.iptables.ipv4['filter'].add_rule('sg-fallback', '-j DROP') - if FLAGS.use_ipv6: - self.iptables.ipv6['filter'].add_chain('sg-fallback') - self.iptables.ipv6['filter'].add_rule('sg-fallback', '-j DROP') + self.iptables.ipv6['filter'].add_chain('sg-fallback') + self.iptables.ipv6['filter'].add_rule('sg-fallback', '-j DROP') def setup_basic_filtering(self, instance): """Use NWFilter from libvirt for this.""" -- cgit From 56ff68056254610c4f0eb5cd5c5432a68ed30b2f Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Tue, 15 Mar 2011 10:42:32 -0700 Subject: Support testing the OpenStack API without key_pairs --- nova/tests/api/openstack/fakes.py | 11 +++++++++-- nova/tests/api/openstack/test_servers.py | 9 ++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index e50d11a3d..ccc853360 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -85,10 +85,17 @@ def wsgi_app(inner_application=None): return mapper -def stub_out_key_pair_funcs(stubs): +def stub_out_key_pair_funcs(stubs, have_key_pair=True): def key_pair(context, user_id): return [dict(name='key', public_key='public_key')] - stubs.Set(nova.db, 'key_pair_get_all_by_user', key_pair) + + def no_key_pair(context, user_id): + return [] + + if have_key_pair: + stubs.Set(nova.db, 'key_pair_get_all_by_user', key_pair) + else: + stubs.Set(nova.db, 'key_pair_get_all_by_user', no_key_pair) def stub_out_image_service(stubs): diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 5d7a208e9..40026a615 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -216,7 +216,7 @@ class ServersTest(test.TestCase): servers = json.loads(res.body)['servers'] self.assertEqual([s['id'] for s in servers], [1, 2]) - def test_create_instance(self): + def _test_create_instance_helper(self, with_key_pair): def instance_create(context, inst): return {'id': '1', 'display_name': 'server_test'} @@ -271,6 +271,13 @@ class ServersTest(test.TestCase): self.assertEqual(res.status_int, 200) + def test_create_instance(self): + self._test_create_instance_helper(True) + + def test_create_instance_no_key_pair(self): + fakes.stub_out_key_pair_funcs(self.stubs, False) + self._test_create_instance_helper(False) + def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') req.method = 'PUT' -- cgit From 22aad6700124411aceed0b2bd3953cbbc48b6130 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Tue, 15 Mar 2011 11:24:07 -0700 Subject: Use random.SystemRandom for easy secure randoms, configurable symbol set by default including mixed-case --- nova/utils.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/nova/utils.py b/nova/utils.py index 0510c3cbe..199ee8701 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -262,19 +262,25 @@ def generate_mac(): return ':'.join(map(lambda x: "%02x" % x, mac)) -def generate_password(length=20): - """Generate a random alphanumeric password, avoiding 'confusing' O,0,I,1. +# Default symbols to use for passwords. Avoids visually confusing characters. +# ~6 bits per symbol +DEFAULT_PASSWORD_SYMBOLS = ("23456789" # Removed: 0,1 + "ABCDEFGHJKLMNPQRSTUVWXYZ" # Removed: I, O + "abcdefghijkmnopqrstuvwxyz") # Removed: l + + +# ~5 bits per symbol +EASIER_PASSWORD_SYMBOLS = ("23456789" # Removed: 0, 1 + "ABCDEFGHJKLMNPQRSTUVWXYZ") # Removed: I, O + + +def generate_password(length=20, symbols=DEFAULT_PASSWORD_SYMBOLS): + """Generate a random password from the supplied symbols. Believed to be reasonably secure (with a reasonable password length!) """ - # 26 letters, 10 digits = 36 choices - # Remove O, 0, I, 1 => 32 choices - # 32 choices means we're just using the low 5 bit of each byte, - # so there's no bias introduced by using a modulo - chrs = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" - - random_bytes = os.urandom(length) - return "".join([chrs[ord(random_bytes[i]) % 32] for i in xrange(length)]) + r = random.SystemRandom() + return "".join([r.choice(symbols) for _i in xrange(length)]) def last_octet(address): -- cgit From 1d69d499124317aa1a9cf7d4bc54db2ff0bc3be9 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 15 Mar 2011 14:33:45 -0400 Subject: refactor onset_files quota checking --- nova/compute/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index b6ef889f6..c11059a28 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -86,6 +86,8 @@ class API(base.Base): Raises a QuotaError if any limit is exceeded """ + if onset_files is None: + return limit = quota.allowed_onset_files(context) if len(onset_files) > limit: raise quota.QuotaError(code="OnsetFileLimitExceeded") @@ -96,7 +98,6 @@ class API(base.Base): raise quota.QuotaError(code="OnsetFilePathLimitExceeded") if len(content) > content_limit: raise quota.QuotaError(code="OnsetFileContentLimitExceeded") - return onset_files def create(self, context, instance_type, image_id, kernel_id=None, ramdisk_id=None, @@ -142,8 +143,7 @@ class API(base.Base): LOG.warn(msg) raise quota.QuotaError(msg, "MetadataLimitExceeded") - if onset_files is not None: - onset_files = self._check_onset_file_quota(context, onset_files) + self._check_onset_file_quota(context, onset_files) image = self.image_service.show(context, image_id) -- cgit From 0eaf02efd5fef3f77fced9c1a71c32a6f14f293f Mon Sep 17 00:00:00 2001 From: Josh Kearney Date: Tue, 15 Mar 2011 16:21:22 -0500 Subject: Add logging to lock check --- nova/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nova/utils.py b/nova/utils.py index 87e726394..d6f9ba829 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -518,6 +518,9 @@ def synchronized(name): def wrap(f): @functools.wraps(f) def inner(*args, **kwargs): + LOG.debug(_("Attempting to grab %(lock)s for method " + "%(method)s..." % {"lock": name, + "method": f.__name__})) lock = lockfile.FileLock(os.path.join(FLAGS.lock_path, 'nova-%s.lock' % name)) with lock: -- cgit From e9ef6e04786a40d20f8022bec5d23d2e4503ce3a Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 15 Mar 2011 17:56:00 -0400 Subject: s/onset_files/injected_files/g --- nova/api/openstack/servers.py | 16 ++++----- nova/compute/api.py | 22 ++++++------ nova/compute/manager.py | 2 +- nova/db/sqlalchemy/models.py | 2 +- nova/quota.py | 30 ++++++++-------- nova/tests/api/openstack/test_servers.py | 48 ++++++++++++------------- nova/tests/test_quota.py | 60 ++++++++++++++++---------------- nova/virt/xenapi/vmops.py | 21 +++++------ 8 files changed, 101 insertions(+), 100 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index adb5c5f99..42fe13619 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -169,7 +169,7 @@ class Controller(wsgi.Controller): metadata.append({'key': k, 'value': v}) personality = env['server'].get('personality', []) - onset_files = self._get_onset_files(personality) + injected_files = self._get_injected_files(personality) try: instances = self.compute_api.create( @@ -183,7 +183,7 @@ class Controller(wsgi.Controller): key_name=key_pair['name'], key_data=key_pair['public_key'], metadata=metadata, - onset_files=onset_files) + injected_files=injected_files) except QuotaError as error: self._handle_quota_error(error) @@ -207,15 +207,15 @@ class Controller(wsgi.Controller): else: return self._deserialize(request.body, request.get_content_type()) - def _get_onset_files(self, personality): + def _get_injected_files(self, personality): """ - Create a list of onset files from the personality attribute + Create a list of injected files from the personality attribute - At this time, onset_files must be formatted as a list of + At this time, injected_files must be formatted as a list of (file_path, file_content) pairs for compatibility with the underlying compute service. """ - onset_files = [] + injected_files = [] for item in personality: try: path = item['path'] @@ -230,8 +230,8 @@ class Controller(wsgi.Controller): except TypeError: msg = 'Personality content for %s cannot be decoded' % path raise exc.HTTPBadRequest(explanation=msg) - onset_files.append((path, contents)) - return onset_files + injected_files.append((path, contents)) + return injected_files def _handle_quota_errors(self, error): """ diff --git a/nova/compute/api.py b/nova/compute/api.py index c11059a28..32577af82 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -80,20 +80,20 @@ class API(base.Base): topic, {"method": "get_network_topic", "args": {'fake': 1}}) - def _check_onset_file_quota(self, context, onset_files): + def _check_injected_file_quota(self, context, injected_files): """ - Enforce quota limits on onset files + Enforce quota limits on injected files Raises a QuotaError if any limit is exceeded """ - if onset_files is None: + if injected_files is None: return - limit = quota.allowed_onset_files(context) - if len(onset_files) > limit: + limit = quota.allowed_injected_files(context) + if len(injected_files) > limit: raise quota.QuotaError(code="OnsetFileLimitExceeded") - path_limit = quota.allowed_onset_file_path_bytes(context) - content_limit = quota.allowed_onset_file_content_bytes(context) - for path, content in onset_files: + path_limit = quota.allowed_injected_file_path_bytes(context) + content_limit = quota.allowed_injected_file_content_bytes(context) + for path, content in injected_files: if len(path) > path_limit: raise quota.QuotaError(code="OnsetFilePathLimitExceeded") if len(content) > content_limit: @@ -105,7 +105,7 @@ class API(base.Base): display_name='', display_description='', key_name=None, key_data=None, security_group='default', availability_zone=None, user_data=None, metadata=[], - onset_files=None): + injected_files=None): """Create the number of instances requested if quota and other arguments check out ok.""" @@ -143,7 +143,7 @@ class API(base.Base): LOG.warn(msg) raise quota.QuotaError(msg, "MetadataLimitExceeded") - self._check_onset_file_quota(context, onset_files) + self._check_injected_file_quota(context, injected_files) image = self.image_service.show(context, image_id) @@ -246,7 +246,7 @@ class API(base.Base): "args": {"topic": FLAGS.compute_topic, "instance_id": instance_id, "availability_zone": availability_zone, - "onset_files": onset_files}}) + "injected_files": injected_files}}) for group_id in security_groups: self.trigger_security_group_members_refresh(elevated, group_id) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 6bb169fa5..92deca813 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -179,7 +179,7 @@ class ComputeManager(manager.Manager): """Launch a new instance with specified options.""" context = context.elevated() instance_ref = self.db.instance_get(context, instance_id) - instance_ref.onset_files = kwargs.get('onset_files', []) + instance_ref.injected_files = kwargs.get('injected_files', []) if instance_ref['name'] in self.driver.list_instances(): raise exception.Error(_("Instance has already been created")) LOG.audit(_("instance %s: starting..."), instance_id, diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 162f6fded..1845e85eb 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -161,7 +161,7 @@ class Certificate(BASE, NovaBase): class Instance(BASE, NovaBase): """Represents a guest vm.""" __tablename__ = 'instances' - onset_files = [] + injected_files = [] id = Column(Integer, primary_key=True, autoincrement=True) diff --git a/nova/quota.py b/nova/quota.py index e0fb97542..2b24c0b5b 100644 --- a/nova/quota.py +++ b/nova/quota.py @@ -37,12 +37,12 @@ flags.DEFINE_integer('quota_floating_ips', 10, 'number of floating ips allowed per project') flags.DEFINE_integer('quota_metadata_items', 128, 'number of metadata items allowed per instance') -flags.DEFINE_integer('quota_max_onset_files', 5, - 'number of onset files allowed') -flags.DEFINE_integer('quota_max_onset_file_content_bytes', 10 * 1024, - 'number of bytes allowed per onset file') -flags.DEFINE_integer('quota_max_onset_file_path_bytes', 255, - 'number of bytes allowed per onset file path') +flags.DEFINE_integer('quota_max_injected_files', 5, + 'number of injected files allowed') +flags.DEFINE_integer('quota_max_injected_file_content_bytes', 10 * 1024, + 'number of bytes allowed per injected file') +flags.DEFINE_integer('quota_max_injected_file_path_bytes', 255, + 'number of bytes allowed per injected file path') def get_quota(context, project_id): @@ -113,19 +113,19 @@ def allowed_metadata_items(context, num_metadata_items): return min(num_metadata_items, num_allowed_metadata_items) -def allowed_onset_files(context): - """Return the number of onset files allowed""" - return int(FLAGS.quota_max_onset_files) +def allowed_injected_files(context): + """Return the number of injected files allowed""" + return FLAGS.quota_max_injected_files -def allowed_onset_file_content_bytes(context): - """Return the number of bytes allowed per onset file content""" - return int(FLAGS.quota_max_onset_file_content_bytes) +def allowed_injected_file_content_bytes(context): + """Return the number of bytes allowed per injected file content""" + return FLAGS.quota_max_injected_file_content_bytes -def allowed_onset_file_path_bytes(context): - """Return the number of bytes allowed in an onset file path""" - return int(FLAGS.quota_max_onset_file_path_bytes) +def allowed_injected_file_path_bytes(context): + """Return the number of bytes allowed in an injected file path""" + return FLAGS.quota_max_injected_file_path_bytes class QuotaError(exception.ApiError): diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 7027c7ea3..253b84be9 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -834,13 +834,13 @@ class TestServerInstanceCreation(test.TestCase): class MockComputeAPI(object): def __init__(self): - self.onset_files = None + self.injected_files = None def create(self, *args, **kwargs): - if 'onset_files' in kwargs: - self.onset_files = kwargs['onset_files'] + if 'injected_files' in kwargs: + self.injected_files = kwargs['injected_files'] else: - self.onset_files = None + self.injected_files = None return [{'id': '1234', 'display_name': 'fakeinstance'}] def set_admin_password(self, *args, **kwargs): @@ -920,46 +920,46 @@ class TestServerInstanceCreation(test.TestCase): request = self._get_create_request_json(body_dict) compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) - return request, response, compute_api.onset_files + return request, response, compute_api.injected_files def _create_instance_with_personality_xml(self, personality): body_dict = self._create_personality_request_dict(personality) request = self._get_create_request_xml(body_dict) compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) - return request, response, compute_api.onset_files + return request, response, compute_api.injected_files def test_create_instance_with_no_personality(self): - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_json(personality=None) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, []) + self.assertEquals(injected_files, []) def test_create_instance_with_no_personality_xml(self): - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_xml(personality=None) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, []) + self.assertEquals(injected_files, []) def test_create_instance_with_personality(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' b64contents = base64.b64encode(contents) personality = [(path, b64contents)] - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) + self.assertEquals(injected_files, [(path, contents)]) def test_create_instance_with_personality_xml(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Hello, World!"\n' b64contents = base64.b64encode(contents) personality = [(path, b64contents)] - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_xml(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) + self.assertEquals(injected_files, [(path, contents)]) def test_create_instance_with_personality_no_path(self): personality = [('/remove/this/path', @@ -970,7 +970,7 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.onset_files, None) + self.assertEquals(compute_api.injected_files, None) def _test_create_instance_with_personality_no_path_xml(self): personality = [('/remove/this/path', @@ -981,7 +981,7 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.onset_files, None) + self.assertEquals(compute_api.injected_files, None) def test_create_instance_with_personality_no_contents(self): personality = [('/test/path', @@ -992,7 +992,7 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.onset_files, None) + self.assertEquals(compute_api.injected_files, None) def test_create_instance_with_personality_not_a_list(self): personality = [('/test/path', base64.b64encode('test\ncontents\n'))] @@ -1003,16 +1003,16 @@ class TestServerInstanceCreation(test.TestCase): compute_api, response = \ self._run_create_instance_with_mock_compute_api(request) self.assertEquals(response.status_int, 400) - self.assertEquals(compute_api.onset_files, None) + self.assertEquals(compute_api.injected_files, None) def test_create_instance_with_personality_with_non_b64_content(self): path = '/my/file/path' contents = '#!/bin/bash\necho "Oh no!"\n' personality = [(path, contents)] - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 400) - self.assertEquals(onset_files, None) + self.assertEquals(injected_files, None) def test_create_instance_with_three_personalities(self): files = [ @@ -1023,19 +1023,19 @@ class TestServerInstanceCreation(test.TestCase): personality = [] for path, content in files: personality.append((path, base64.b64encode(content))) - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, files) + self.assertEquals(injected_files, files) def test_create_instance_personality_empty_content(self): path = '/my/file/path' contents = '' personality = [(path, contents)] - request, response, onset_files = \ + request, response, injected_files = \ self._create_instance_with_personality_json(personality) self.assertEquals(response.status_int, 200) - self.assertEquals(onset_files, [(path, contents)]) + self.assertEquals(injected_files, [(path, contents)]) def test_create_instance_admin_pass_json(self): request, response, dummy = \ diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py index d94381aa2..c65bc459d 100644 --- a/nova/tests/test_quota.py +++ b/nova/tests/test_quota.py @@ -200,66 +200,66 @@ class QuotaTestCase(test.TestCase): image_id='fake', metadata=metadata) - def test_allowed_onset_files(self): + def test_allowed_injected_files(self): self.assertEqual( - quota.allowed_onset_files(self.context), - FLAGS.quota_max_onset_files) + quota.allowed_injected_files(self.context), + FLAGS.quota_max_injected_files) - def _create_with_onset_files(self, files): + def _create_with_injected_files(self, files): api = compute.API(image_service=self.StubImageService()) api.create(self.context, min_count=1, max_count=1, instance_type='m1.small', image_id='fake', - onset_files=files) + injected_files=files) - def test_no_onset_files(self): + def test_no_injected_files(self): api = compute.API(image_service=self.StubImageService()) api.create(self.context, instance_type='m1.small', image_id='fake') - def test_max_onset_files(self): + def test_max_injected_files(self): files = [] - for i in xrange(FLAGS.quota_max_onset_files): + for i in xrange(FLAGS.quota_max_injected_files): files.append(('/my/path%d' % i, 'config = test\n')) - self._create_with_onset_files(files) # no QuotaError + self._create_with_injected_files(files) # no QuotaError - def test_too_many_onset_files(self): + def test_too_many_injected_files(self): files = [] - for i in xrange(FLAGS.quota_max_onset_files + 1): + for i in xrange(FLAGS.quota_max_injected_files + 1): files.append(('/my/path%d' % i, 'my\ncontent%d\n' % i)) self.assertRaises(quota.QuotaError, - self._create_with_onset_files, files) + self._create_with_injected_files, files) - def test_allowed_onset_file_content_bytes(self): + def test_allowed_injected_file_content_bytes(self): self.assertEqual( - quota.allowed_onset_file_content_bytes(self.context), - FLAGS.quota_max_onset_file_content_bytes) + quota.allowed_injected_file_content_bytes(self.context), + FLAGS.quota_max_injected_file_content_bytes) - def test_max_onset_file_content_bytes(self): - max = FLAGS.quota_max_onset_file_content_bytes + def test_max_injected_file_content_bytes(self): + max = FLAGS.quota_max_injected_file_content_bytes content = ''.join(['a' for i in xrange(max)]) files = [('/test/path', content)] - self._create_with_onset_files(files) # no QuotaError + self._create_with_injected_files(files) # no QuotaError - def test_too_many_onset_file_content_bytes(self): - max = FLAGS.quota_max_onset_file_content_bytes + def test_too_many_injected_file_content_bytes(self): + max = FLAGS.quota_max_injected_file_content_bytes content = ''.join(['a' for i in xrange(max + 1)]) files = [('/test/path', content)] self.assertRaises(quota.QuotaError, - self._create_with_onset_files, files) + self._create_with_injected_files, files) - def test_allowed_onset_file_path_bytes(self): + def test_allowed_injected_file_path_bytes(self): self.assertEqual( - quota.allowed_onset_file_path_bytes(self.context), - FLAGS.quota_max_onset_file_path_bytes) + quota.allowed_injected_file_path_bytes(self.context), + FLAGS.quota_max_injected_file_path_bytes) - def test_max_onset_file_path_bytes(self): - max = FLAGS.quota_max_onset_file_path_bytes + def test_max_injected_file_path_bytes(self): + max = FLAGS.quota_max_injected_file_path_bytes path = ''.join(['a' for i in xrange(max)]) files = [(path, 'config = quotatest')] - self._create_with_onset_files(files) # no QuotaError + self._create_with_injected_files(files) # no QuotaError - def test_too_many_onset_file_path_bytes(self): - max = FLAGS.quota_max_onset_file_path_bytes + def test_too_many_injected_file_path_bytes(self): + max = FLAGS.quota_max_injected_file_path_bytes path = ''.join(['a' for i in xrange(max + 1)]) files = [(path, 'config = quotatest')] self.assertRaises(quota.QuotaError, - self._create_with_onset_files, files) + self._create_with_injected_files, files) diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index d3fc335fe..488a61e8e 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -137,19 +137,20 @@ class VMOps(object): LOG.info(_('Spawning VM %(instance_name)s created %(vm_ref)s.') % locals()) - def _inject_onset_files(): - onset_files = instance.onset_files - if onset_files: + def _inject_files(): + injected_files = instance.injected_files + if injected_files: # Check if this is a JSON-encoded string and convert if needed. - if isinstance(onset_files, basestring): + if isinstance(injected_files, basestring): try: - onset_files = json.loads(onset_files) + injected_files = json.loads(injected_files) except ValueError: - LOG.exception(_("Invalid value for onset_files: '%s'") - % onset_files) - onset_files = [] + LOG.exception( + _("Invalid value for injected_files: '%s'") + % injected_files) + injected_files = [] # Inject any files, if specified - for path, contents in instance.onset_files: + for path, contents in instance.injected_files: LOG.debug(_("Injecting file path: '%s'") % path) self.inject_file(instance, path, contents) # NOTE(armando): Do we really need to do this in virt? @@ -165,7 +166,7 @@ class VMOps(object): if state == power_state.RUNNING: LOG.debug(_('Instance %s: booted'), instance_name) timer.stop() - _inject_onset_files() + _inject_files() return True except Exception, exc: LOG.warn(exc) -- cgit From 3cc78174e023b3f848b9c4b30468d356ee575ea6 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 15 Mar 2011 18:11:54 -0400 Subject: internationalization --- nova/api/openstack/servers.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 42fe13619..f618c31a0 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -221,15 +221,16 @@ class Controller(wsgi.Controller): path = item['path'] contents = item['contents'] except KeyError as key: - expl = 'Bad personality format: missing %s' % key + expl = _('Bad personality format: missing %s') % key raise exc.HTTPBadRequest(explanation=expl) except TypeError: - raise exc.HTTPBadRequest(explanation='Bad personality format') + expl = _('Bad personality format') + raise exc.HTTPBadRequest(explanation=expl) try: contents = base64.b64decode(contents) except TypeError: - msg = 'Personality content for %s cannot be decoded' % path - raise exc.HTTPBadRequest(explanation=msg) + expl = _('Personality content for %s cannot be decoded') % path + raise exc.HTTPBadRequest(explanation=expl) injected_files.append((path, contents)) return injected_files @@ -238,13 +239,13 @@ class Controller(wsgi.Controller): Reraise quota errors as api-specific http exceptions """ if error.code == "OnsetFileLimitExceeded": - expl = "Personality file limit exceeded" + expl = _("Personality file limit exceeded") raise exc.HTTPBadRequest(explanation=expl) if error.code == "OnsetFilePathLimitExceeded": - expl = "Personality file path too long" + expl = _("Personality file path too long") raise exc.HTTPBadRequest(explanation=expl) if error.code == "OnsetFileContentLimitExceeded": - expl = "Personality file content too long" + expl = _("Personality file content too long") raise exc.HTTPBadRequest(explanation=expl) # if the original error is okay, just reraise it raise error -- cgit From 70769dbe239c979d97154b88a33cb34d377d1196 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 15 Mar 2011 18:12:46 -0400 Subject: pep8 --- nova/tests/api/openstack/test_servers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 253b84be9..a92c0f590 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -806,7 +806,7 @@ ZSBwYXR0ZXJucyBiZWhpbmQgYWxsIGNsb3VkcywgYW5kIHlv\ dSB3aWxsIGtub3csIHRvbywgd2hlbiB5b3UgbGlmdCB5b3Vy\ c2VsZiBoaWdoIGVub3VnaCB0byBzZWUgYmV5b25kIGhvcml6\ b25zLiINCg0KLVJpY2hhcmQgQmFjaA==""", - } + }, ], }} request = self.deserializer.deserialize(serial_request) -- cgit From e237b4a5653384688b16f7fd2c0708eaec4b9ec7 Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 15 Mar 2011 19:11:21 -0400 Subject: ignore differently-named nodes in personality and metadata parsing --- nova/api/openstack/servers.py | 10 +- nova/tests/api/openstack/test_servers.py | 164 ++++++++++++++++++++++--------- 2 files changed, 126 insertions(+), 48 deletions(-) diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index f618c31a0..ea88f1fdc 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -575,7 +575,7 @@ class ServerCreateRequestXMLDeserializer(object): if metadata_node is None: return None metadata = {} - for meta_node in metadata_node.childNodes: + for meta_node in self._find_children_named(metadata_node, "meta"): key = meta_node.getAttribute("key") metadata[key] = self._extract_text(meta_node) return metadata @@ -587,7 +587,7 @@ class ServerCreateRequestXMLDeserializer(object): if personality_node is None: return None personality = [] - for file_node in personality_node.childNodes: + for file_node in self._find_children_named(personality_node, "file"): item = {} if file_node.hasAttribute("path"): item["path"] = file_node.getAttribute("path") @@ -602,6 +602,12 @@ class ServerCreateRequestXMLDeserializer(object): return node return None + def _find_children_named(self, parent, name): + """Return all of a nodes children who have the given name""" + for node in parent.childNodes: + if node.nodeName == name: + yield node + def _extract_text(self, node): """Get the text field contained by the given node""" if len(node.childNodes) == 1: diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index a92c0f590..ed37cb705 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -610,7 +610,7 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_minimal_request(self): serial_request = """ -""" request = self.deserializer.deserialize(serial_request) expected = {"server": { @@ -622,8 +622,10 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_empty_metadata(self): serial_request = """ -""" + + +""" request = self.deserializer.deserialize(serial_request) expected = {"server": { "name": "new-server-test", @@ -635,8 +637,10 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_empty_personality(self): serial_request = """ -""" + + +""" request = self.deserializer.deserialize(serial_request) expected = {"server": { "name": "new-server-test", @@ -648,9 +652,11 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_empty_metadata_and_personality(self): serial_request = """ -\ -""" + + + +""" request = self.deserializer.deserialize(serial_request) expected = {"server": { "name": "new-server-test", @@ -663,9 +669,11 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_empty_metadata_and_personality_reversed(self): serial_request = """ -\ -""" + + + +""" request = self.deserializer.deserialize(serial_request) expected = {"server": { "name": "new-server-test", @@ -678,28 +686,47 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_one_personality(self): serial_request = """ -\ -aabbccdd""" + + + aabbccdd + +""" request = self.deserializer.deserialize(serial_request) expected = [{"path": "/etc/conf", "contents": "aabbccdd"}] self.assertEquals(request["server"]["personality"], expected) def test_request_with_two_personalities(self): serial_request = """ -\ -aabbccdd\ + +aabbccdd abcd""" request = self.deserializer.deserialize(serial_request) expected = [{"path": "/etc/conf", "contents": "aabbccdd"}, {"path": "/etc/sudoers", "contents": "abcd"}] self.assertEquals(request["server"]["personality"], expected) + def test_request_second_personality_node_ignored(self): + serial_request = """ + + + aabbccdd + + + anything + +""" + request = self.deserializer.deserialize(serial_request) + expected = [{"path": "/etc/conf", "contents": "aabbccdd"}] + self.assertEquals(request["server"]["personality"], expected) + + def test_request_with_one_personality_missing_path(self): serial_request = """ -\ + aabbccdd""" request = self.deserializer.deserialize(serial_request) expected = [{"contents": "aabbccdd"}] @@ -707,8 +734,8 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_one_personality_empty_contents(self): serial_request = """ -\ + """ request = self.deserializer.deserialize(serial_request) expected = [{"path": "/etc/conf", "contents": ""}] @@ -716,8 +743,8 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_one_personality_empty_contents_variation(self): serial_request = """ -\ + """ request = self.deserializer.deserialize(serial_request) expected = [{"path": "/etc/conf", "contents": ""}] @@ -725,57 +752,101 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): def test_request_with_one_metadata(self): serial_request = """ -\ -beta""" + + + beta + +""" request = self.deserializer.deserialize(serial_request) expected = {"alpha": "beta"} self.assertEquals(request["server"]["metadata"], expected) def test_request_with_two_metadata(self): serial_request = """ -\ -betabar\ -""" + + + beta + bar + +""" request = self.deserializer.deserialize(serial_request) expected = {"alpha": "beta", "foo": "bar"} self.assertEquals(request["server"]["metadata"], expected) def test_request_with_metadata_missing_value(self): serial_request = """ -\ -""" + + + + +""" request = self.deserializer.deserialize(serial_request) expected = {"alpha": ""} self.assertEquals(request["server"]["metadata"], expected) + def test_request_with_two_metadata_missing_value(self): + serial_request = """ + + + + + +""" + request = self.deserializer.deserialize(serial_request) + expected = {"alpha": "", "delta": ""} + self.assertEquals(request["server"]["metadata"], expected) + def test_request_with_metadata_missing_key(self): serial_request = """ -\ -beta""" + + + beta + +""" request = self.deserializer.deserialize(serial_request) expected = {"": "beta"} self.assertEquals(request["server"]["metadata"], expected) + def test_request_with_two_metadata_missing_key(self): + serial_request = """ + + + beta + gamma + +""" + request = self.deserializer.deserialize(serial_request) + expected = {"":"gamma"} + self.assertEquals(request["server"]["metadata"], expected) + def test_request_with_metadata_duplicate_key(self): serial_request = """ -\ -barbaz\ -""" + + + bar + baz + +""" request = self.deserializer.deserialize(serial_request) expected = {"foo": "baz"} self.assertEquals(request["server"]["metadata"], expected) def test_canonical_request_from_docs(self): serial_request = """ -\ -Apache1\ -\ + + + Apache1 + + + \ ICAgICAgDQoiQSBjbG91ZCBkb2VzIG5vdCBrbm93IHdoeSBp\ dCBtb3ZlcyBpbiBqdXN0IHN1Y2ggYSBkaXJlY3Rpb24gYW5k\ IGF0IHN1Y2ggYSBzcGVlZC4uLkl0IGZlZWxzIGFuIGltcHVs\ @@ -784,8 +855,9 @@ QnV0IHRoZSBza3kga25vd3MgdGhlIHJlYXNvbnMgYW5kIHRo\ ZSBwYXR0ZXJucyBiZWhpbmQgYWxsIGNsb3VkcywgYW5kIHlv\ dSB3aWxsIGtub3csIHRvbywgd2hlbiB5b3UgbGlmdCB5b3Vy\ c2VsZiBoaWdoIGVub3VnaCB0byBzZWUgYmV5b25kIGhvcml6\ -b25zLiINCg0KLVJpY2hhcmQgQmFjaA==\ -""" +b25zLiINCg0KLVJpY2hhcmQgQmFjaA== + +""" expected = {"server": { "name": "new-server-test", "imageId": "1", -- cgit From fc07caece79e379b6d6f2a3220806af9271e349b Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Tue, 15 Mar 2011 19:23:46 -0400 Subject: pep8 --- nova/tests/api/openstack/test_servers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index ed37cb705..9a6f2c052 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -722,7 +722,6 @@ class TestServerCreateRequestXMLDeserializer(unittest.TestCase): expected = [{"path": "/etc/conf", "contents": "aabbccdd"}] self.assertEquals(request["server"]["personality"], expected) - def test_request_with_one_personality_missing_path(self): serial_request = """ """ request = self.deserializer.deserialize(serial_request) - expected = {"":"gamma"} + expected = {"": "gamma"} self.assertEquals(request["server"]["metadata"], expected) def test_request_with_metadata_duplicate_key(self): -- cgit From 8b3e35b157c688fd38d5aa0eb10ddef33653003d Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 16 Mar 2011 10:29:04 +0100 Subject: fixed pep8 errors (with version 0.5.0) --- bin/nova-manage | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index 44b1d9ac6..c38e25d6b 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -739,8 +739,7 @@ class InstanceCommands(object): _('project'), _('user'), _('zone'), - _('index') - ) + _('index')) if host == None: instances = db.instance_get_all(context.get_admin_context()) @@ -762,8 +761,7 @@ class InstanceCommands(object): instance['project_id'], instance['user_id'], instance['availability_zone'], - instance['launch_index'] - ) + instance['launch_index']) class VolumeCommands(object): @@ -1053,8 +1051,7 @@ CATEGORIES = [ ('instance_type', InstanceTypeCommands), ('image', ImageCommands), ('flavor', InstanceTypeCommands), - ('instance', InstanceCommands) -] + ('instance', InstanceCommands)] def lazy_match(name, key_value_tuples): -- cgit From 016669543a1f6d4ffc281637ba98c6b6fe30be82 Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Wed, 16 Mar 2011 10:38:48 +0100 Subject: Fix unknown exception error in euca-get-ajax-console --- nova/virt/libvirt_conn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/virt/libvirt_conn.py b/nova/virt/libvirt_conn.py index 7994e9547..d7bdc3faa 100644 --- a/nova/virt/libvirt_conn.py +++ b/nova/virt/libvirt_conn.py @@ -502,7 +502,7 @@ class LibvirtConnection(object): cmd = 'netcat', '0.0.0.0', port, '-w', '1' try: stdout, stderr = utils.execute(*cmd, process_input='') - except ProcessExecutionError: + except exception.ProcessExecutionError: return port raise Exception(_('Unable to find an open port')) -- cgit From ba35831c1f66c424e9495642ba23e9d2742a339e Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 16 Mar 2011 10:58:02 +0100 Subject: added correct path to cpu information (tested on a system with 1 installed cpu package) --- nova/virt/libvirt_conn.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nova/virt/libvirt_conn.py b/nova/virt/libvirt_conn.py index 7994e9547..9943b742a 100644 --- a/nova/virt/libvirt_conn.py +++ b/nova/virt/libvirt_conn.py @@ -984,18 +984,18 @@ class LibvirtConnection(object): xml = self._conn.getCapabilities() xml = libxml2.parseDoc(xml) - nodes = xml.xpathEval('//cpu') + nodes = xml.xpathEval('//host/cpu') if len(nodes) != 1: raise exception.Invalid(_("Invalid xml. '' must be 1," "but %d\n") % len(nodes) + xml.serialize()) cpu_info = dict() - cpu_info['arch'] = xml.xpathEval('//cpu/arch')[0].getContent() - cpu_info['model'] = xml.xpathEval('//cpu/model')[0].getContent() - cpu_info['vendor'] = xml.xpathEval('//cpu/vendor')[0].getContent() + cpu_info['arch'] = xml.xpathEval('//host/cpu/arch')[0].getContent() + cpu_info['model'] = xml.xpathEval('//host/cpu/model')[0].getContent() + cpu_info['vendor'] = xml.xpathEval('//host/cpu/vendor')[0].getContent() - topology_node = xml.xpathEval('//cpu/topology')[0].get_properties() + topology_node = xml.xpathEval('//host/cpu/topology')[0].get_properties() topology = dict() while topology_node != None: name = topology_node.get_name() @@ -1009,7 +1009,7 @@ class LibvirtConnection(object): raise exception.Invalid(_("Invalid xml: topology(%(topology)s) " "must have %(ks)s") % locals()) - feature_nodes = xml.xpathEval('//cpu/feature') + feature_nodes = xml.xpathEval('//host/cpu/feature') features = list() for nodes in feature_nodes: features.append(nodes.get_properties().getContent()) -- cgit From 7fa96f6292ff7d63621fe024b1ef45b1a1996121 Mon Sep 17 00:00:00 2001 From: "jaypipes@gmail.com" <> Date: Wed, 16 Mar 2011 14:49:18 -0400 Subject: Re-commit r804 --- nova/tests/api/openstack/test_servers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 40026a615..cde2fc036 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -216,7 +216,7 @@ class ServersTest(test.TestCase): servers = json.loads(res.body)['servers'] self.assertEqual([s['id'] for s in servers], [1, 2]) - def _test_create_instance_helper(self, with_key_pair): + def _test_create_instance_helper(self): def instance_create(context, inst): return {'id': '1', 'display_name': 'server_test'} @@ -272,11 +272,11 @@ class ServersTest(test.TestCase): self.assertEqual(res.status_int, 200) def test_create_instance(self): - self._test_create_instance_helper(True) + self._test_create_instance_helper() def test_create_instance_no_key_pair(self): - fakes.stub_out_key_pair_funcs(self.stubs, False) - self._test_create_instance_helper(False) + fakes.stub_out_key_pair_funcs(self.stubs, have_key_pair=False) + self._test_create_instance_helper() def test_update_no_body(self): req = webob.Request.blank('/v1.0/servers/1') -- cgit From 663c1726d9a96540b8fd729223fcb34d7cf3cdf7 Mon Sep 17 00:00:00 2001 From: "jaypipes@gmail.com" <> Date: Wed, 16 Mar 2011 14:49:25 -0400 Subject: Re-commit r805 --- nova/tests/api/openstack/test_servers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index cde2fc036..ad36fa551 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -217,6 +217,7 @@ class ServersTest(test.TestCase): self.assertEqual([s['id'] for s in servers], [1, 2]) def _test_create_instance_helper(self): + """Shared implementation for tests below that create instance""" def instance_create(context, inst): return {'id': '1', 'display_name': 'server_test'} -- cgit From 157ea09c03148ff4615bae27ca3f276a05620825 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 16 Mar 2011 19:54:15 +0100 Subject: fixed pep8 issue --- nova/virt/libvirt_conn.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nova/virt/libvirt_conn.py b/nova/virt/libvirt_conn.py index 9943b742a..4e17555f4 100644 --- a/nova/virt/libvirt_conn.py +++ b/nova/virt/libvirt_conn.py @@ -995,7 +995,8 @@ class LibvirtConnection(object): cpu_info['model'] = xml.xpathEval('//host/cpu/model')[0].getContent() cpu_info['vendor'] = xml.xpathEval('//host/cpu/vendor')[0].getContent() - topology_node = xml.xpathEval('//host/cpu/topology')[0].get_properties() + topology_node = xml.xpathEval('//host/cpu/topology')[0]\ + .get_properties() topology = dict() while topology_node != None: name = topology_node.get_name() -- cgit From 85bae497aa803914d329f2872d343a9982dc370e Mon Sep 17 00:00:00 2001 From: Cerberus Date: Wed, 16 Mar 2011 14:35:04 -0500 Subject: Changes --- nova/api/openstack/flavors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/flavors.py b/nova/api/openstack/flavors.py index f3d040ba3..b9e40371d 100644 --- a/nova/api/openstack/flavors.py +++ b/nova/api/openstack/flavors.py @@ -36,7 +36,7 @@ class Controller(wsgi.Controller): def index(self, req): """Return all flavors in brief.""" - return dict(flavors=[dict(id=flavor['id'], name=flavor['name']) + return dict(flavors=[dict(id=flavor['flavorid'], name=flavor['name']) for flavor in self.detail(req)['flavors']]) def detail(self, req): @@ -48,6 +48,7 @@ class Controller(wsgi.Controller): """Return data about the given flavor id.""" ctxt = req.environ['nova.context'] values = db.instance_type_get_by_flavor_id(ctxt, id) + values.update({'id': values['flavorid']}) return dict(flavor=values) raise faults.Fault(exc.HTTPNotFound()) -- cgit From 227957e31d75b24bb8afa078c8d3f2bc447a8215 Mon Sep 17 00:00:00 2001 From: "matt.dietz@rackspace.com" <> Date: Wed, 16 Mar 2011 19:40:16 +0000 Subject: Unit test update --- nova/tests/api/openstack/test_flavors.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index 8280a505f..30326dc50 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import json import stubout import webob @@ -50,3 +51,5 @@ class FlavorsTest(test.TestCase): req = webob.Request.blank('/v1.0/flavors/1') res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) + body = json.loads(res.body) + self.assertEqual(body['flavor']['id'], 1) -- cgit From f17fb9370d4af42267837a36c937f213669b0291 Mon Sep 17 00:00:00 2001 From: Cerberus Date: Wed, 16 Mar 2011 14:43:57 -0500 Subject: Dumb --- nova/api/openstack/flavors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/api/openstack/flavors.py b/nova/api/openstack/flavors.py index b9e40371d..1c440b3a9 100644 --- a/nova/api/openstack/flavors.py +++ b/nova/api/openstack/flavors.py @@ -48,7 +48,7 @@ class Controller(wsgi.Controller): """Return data about the given flavor id.""" ctxt = req.environ['nova.context'] values = db.instance_type_get_by_flavor_id(ctxt, id) - values.update({'id': values['flavorid']}) + values['id'] = values['flavorid'] return dict(flavor=values) raise faults.Fault(exc.HTTPNotFound()) -- cgit From 41619f49ce72d8e85f013c5a5dd248faa8490555 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Thu, 17 Mar 2011 11:44:27 +0100 Subject: Comparisons to None should not use == or !=. Stop converting sets to lists before comparing them. They might be in different order after being list()ified. --- nova/virt/libvirt_conn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/virt/libvirt_conn.py b/nova/virt/libvirt_conn.py index 2559c2b81..0a85da541 100644 --- a/nova/virt/libvirt_conn.py +++ b/nova/virt/libvirt_conn.py @@ -998,14 +998,14 @@ class LibvirtConnection(object): topology_node = xml.xpathEval('//host/cpu/topology')[0]\ .get_properties() topology = dict() - while topology_node != None: + while topology_node: name = topology_node.get_name() topology[name] = topology_node.getContent() topology_node = topology_node.get_next() keys = ['cores', 'sockets', 'threads'] tkeys = topology.keys() - if list(set(tkeys)) != list(set(keys)): + if set(tkeys) != set(keys): ks = ', '.join(keys) raise exception.Invalid(_("Invalid xml: topology(%(topology)s) " "must have %(ks)s") % locals()) -- cgit From 1d64d0a3d7f25448361ce54e32bba3de68c7afd1 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Thu, 17 Mar 2011 16:14:59 +0100 Subject: Remove unconditional raise, probably left over from debugging. --- nova/api/ec2/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 40a9da0e7..e257e44e7 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -959,7 +959,7 @@ class CloudController(object): raise exception.NotFound(_('Image %s not found') % image_id) internal_id = image['id'] del(image['id']) - raise Exception(image) + image['properties']['is_public'] = (operation_type == 'add') return self.image_service.update(context, internal_id, image) -- cgit