From 8b7765276951f4a2a6a80df3bce7c12e64cee44f Mon Sep 17 00:00:00 2001 From: Alvaro Lopez Garcia Date: Thu, 19 Jul 2012 14:49:41 +0200 Subject: Fix cloudpipe keypair creation. Add pipelib tests Commit ec0a65d81fd11d26be77b45827a4dd7c77711675 introduced a new internal key pair API, removing the _gen_key() method from the EC2 API. pipelib.py used this method so as to generate the keypair of the cloudpipe instance. This commit also adds unittests for pipelib.py, thus fixes bug 828588 Change-Id: I0884289bac01ac5109dde4bdee5af55e7bfd5aa2 --- nova/cloudpipe/pipelib.py | 7 +++-- nova/tests/test_pipelib.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 nova/tests/test_pipelib.py diff --git a/nova/cloudpipe/pipelib.py b/nova/cloudpipe/pipelib.py index 6926978fc..6a772eb63 100644 --- a/nova/cloudpipe/pipelib.py +++ b/nova/cloudpipe/pipelib.py @@ -26,8 +26,6 @@ import os import string import zipfile -# NOTE(vish): cloud is only for the _gen_key functionality -from nova.api.ec2 import cloud from nova import compute from nova.compute import instance_types from nova import crypto @@ -146,7 +144,10 @@ class CloudPipe(object): def setup_key_pair(self, context): key_name = '%s%s' % (context.project_id, FLAGS.vpn_key_suffix) try: - result = cloud._gen_key(context, context.user_id, key_name) + keypair_api = compute.api.KeypairAPI() + result = keypair_api.create_key_pair(context, + context.user_id, + key_name) private_key = result['private_key'] key_dir = os.path.join(FLAGS.keys_path, context.user_id) if not os.path.exists(key_dir): diff --git a/nova/tests/test_pipelib.py b/nova/tests/test_pipelib.py new file mode 100644 index 000000000..26ab82ffd --- /dev/null +++ b/nova/tests/test_pipelib.py @@ -0,0 +1,75 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova.cloudpipe import pipelib +from nova import context +from nova import crypto +from nova import db +from nova import flags +from nova import test +from nova import utils + + +FLAGS = flags.FLAGS + + +class PipelibTest(test.TestCase): + def setUp(self): + super(PipelibTest, self).setUp() + self.cloudpipe = pipelib.CloudPipe() + self.project = "222" + self.user = "111" + self.context = context.RequestContext(self.user, self.project) + + def test_get_encoded_zip(self): + with utils.tempdir() as tmpdir: + self.flags(ca_path=tmpdir) + crypto.ensure_ca_filesystem() + + ret = self.cloudpipe.get_encoded_zip(self.project) + self.assertTrue(ret) + + def test_launch_vpn_instance(self): + self.stubs.Set(self.cloudpipe.compute_api, + "create", + lambda *a, **kw: (None, "r-fakeres")) + with utils.tempdir() as tmpdir: + self.flags(ca_path=tmpdir, keys_path=tmpdir) + crypto.ensure_ca_filesystem() + self.cloudpipe.launch_vpn_instance(self.context) + + def test_setup_security_group(self): + group_name = "%s%s" % (self.project, FLAGS.vpn_key_suffix) + + # First attemp, does not exist (thus its created) + res1_group = self.cloudpipe.setup_security_group(self.context) + self.assertEqual(res1_group, group_name) + + # Second attem, it exists in the DB + res2_group = self.cloudpipe.setup_security_group(self.context) + self.assertEqual(res1_group, res2_group) + + def test_setup_key_pair(self): + key_name = "%s%s" % (self.project, FLAGS.vpn_key_suffix) + with utils.tempdir() as tmpdir: + self.flags(keys_path=tmpdir) + + # First attemp, key does not exist (thus it is generated) + res1_key = self.cloudpipe.setup_key_pair(self.context) + self.assertEqual(res1_key, key_name) + + # Second attem, it exists in the DB + res2_key = self.cloudpipe.setup_key_pair(self.context) + self.assertEqual(res2_key, res1_key) -- cgit