summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-03-17 23:02:51 +0000
committerGerrit Code Review <review@openstack.org>2013-03-17 23:02:51 +0000
commited1d6f2d0245cbe42a53f2d76f4ec196ba062e25 (patch)
tree0caac6e1fc4b3f0ff58c08c50c8d5081aa828347
parentdcc76e702b1d2310ae0644438baff45ae9b100ee (diff)
parentb883b259fc4c2b651fd27ac0a392f8918b77489a (diff)
downloadnova-ed1d6f2d0245cbe42a53f2d76f4ec196ba062e25.tar.gz
nova-ed1d6f2d0245cbe42a53f2d76f4ec196ba062e25.tar.xz
nova-ed1d6f2d0245cbe42a53f2d76f4ec196ba062e25.zip
Merge "Check keypair destroy result operation"
-rw-r--r--nova/db/sqlalchemy/api.py10
-rw-r--r--nova/tests/test_db_api.py45
2 files changed, 51 insertions, 4 deletions
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index d9fbb34b5..bf0845146 100644
--- a/nova/db/sqlalchemy/api.py
+++ b/nova/db/sqlalchemy/api.py
@@ -1950,10 +1950,12 @@ def key_pair_create(context, values):
@require_context
def key_pair_destroy(context, user_id, name):
nova.context.authorize_user_context(context, user_id)
- model_query(context, models.KeyPair).\
- filter_by(user_id=user_id).\
- filter_by(name=name).\
- delete()
+ result = model_query(context, models.KeyPair).\
+ filter_by(user_id=user_id).\
+ filter_by(name=name).\
+ delete()
+ if not result:
+ raise exception.KeypairNotFound(user_id=user_id, name=name)
@require_context
diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py
index 29112be1e..23fff5cc4 100644
--- a/nova/tests/test_db_api.py
+++ b/nova/tests/test_db_api.py
@@ -797,6 +797,51 @@ class DbApiTestCase(DbTestCase):
self.assertEqual('schedule', event['event'])
self.assertEqual(start_time, event['start_time'])
+ def test_add_key_pair(self, name=None):
+ """Check if keypair creation work as expected."""
+ keypair = {
+ 'user_id': self.user_id,
+ 'name': name or 'test-keypair',
+ 'fingerprint': '15:b0:f8:b3:f9:48:63:71:cf:7b:5b:38:6d:44:2d:4a',
+ 'private_key': 'private_key_value',
+ 'public_key': 'public_key_value'
+ }
+ result_key = db.key_pair_create(context.get_admin_context(), keypair)
+ for label in keypair:
+ self.assertEqual(keypair[label], result_key[label])
+
+ def test_key_pair_destroy(self):
+ """Check if key pair deletion works as expected."""
+ keypair_name = 'test-delete-keypair'
+ self.test_add_key_pair(name=keypair_name)
+ db.key_pair_destroy(context.get_admin_context(), self.user_id,
+ keypair_name)
+ self.assertRaises(exception.KeypairNotFound, db.key_pair_get,
+ context.get_admin_context(), self.user_id,
+ keypair_name)
+
+ def test_key_pair_get(self):
+ """Test if a previously created keypair can be found."""
+ keypair_name = 'test-get-keypair'
+ self.test_add_key_pair(name=keypair_name)
+ result = db.key_pair_get(context.get_admin_context(), self.user_id,
+ keypair_name)
+ self.assertEqual(result.name, keypair_name)
+
+ def test_key_pair_get_all_by_user(self):
+ self.assertTrue(isinstance(db.key_pair_get_all_by_user(
+ context.get_admin_context(), self.user_id), list))
+
+ def test_delete_non_existent_key_pair(self):
+ self.assertRaises(exception.KeypairNotFound, db.key_pair_destroy,
+ context.get_admin_context(), self.user_id,
+ 'non-existent-keypair')
+
+ def test_get_non_existent_key_pair(self):
+ self.assertRaises(exception.KeypairNotFound, db.key_pair_get,
+ context.get_admin_context(), self.user_id,
+ 'invalid-key')
+
def test_dns_registration(self):
domain1 = 'test.domain.one'
domain2 = 'test.domain.two'