From dee22d8faa0c8a0da1d6ff62c0997c2cc770b759 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Mon, 27 Feb 2017 14:35:05 +0800 Subject: Add "--private-key" option for "keypair create" Aim to specify the private key file to save when keypair is created. That is a convenient way to save private key in OSC interactive mode, avoid to copy CLI output, then paste it into file. Change-Id: I119d2f2a3323d17ecbe3de4e27f35e1ceef6e0a5 Closes-Bug: #1549410 --- .../tests/functional/compute/v2/test_keypair.py | 21 +++++++++++++++ .../tests/unit/compute/v2/test_keypair.py | 31 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) (limited to 'openstackclient/tests') diff --git a/openstackclient/tests/functional/compute/v2/test_keypair.py b/openstackclient/tests/functional/compute/v2/test_keypair.py index 01078c61..1e1a03d6 100644 --- a/openstackclient/tests/functional/compute/v2/test_keypair.py +++ b/openstackclient/tests/functional/compute/v2/test_keypair.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import json import tempfile from openstackclient.tests.functional import base @@ -100,6 +101,26 @@ class KeypairTests(KeypairBase): ) self.assertIn('tmpkey', raw_output) + def test_keypair_create_private_key(self): + """Test for create keypair with --private-key option. + + Test steps: + 1) Create keypair with private key file + 2) Delete keypair + """ + with tempfile.NamedTemporaryFile() as f: + cmd_output = json.loads(self.openstack( + 'keypair create -f json --private-key %s tmpkey' % f.name, + )) + self.addCleanup(self.openstack, 'keypair delete tmpkey') + self.assertEqual('tmpkey', cmd_output.get('name')) + self.assertIsNotNone(cmd_output.get('user_id')) + self.assertIsNotNone(cmd_output.get('fingerprint')) + pk_content = f.read() + self.assertInOutput('-----BEGIN RSA PRIVATE KEY-----', pk_content) + self.assertRegex(pk_content, "[0-9A-Za-z+/]+[=]{0,3}\n") + self.assertInOutput('-----END RSA PRIVATE KEY-----', pk_content) + def test_keypair_create(self): """Test keypair create command. diff --git a/openstackclient/tests/unit/compute/v2/test_keypair.py b/openstackclient/tests/unit/compute/v2/test_keypair.py index efc5463c..d6f5ecf4 100644 --- a/openstackclient/tests/unit/compute/v2/test_keypair.py +++ b/openstackclient/tests/unit/compute/v2/test_keypair.py @@ -15,6 +15,7 @@ import mock from mock import call +import uuid from osc_lib import exceptions from osc_lib import utils @@ -115,6 +116,36 @@ class TestKeypairCreate(TestKeypair): self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) + def test_keypair_create_private_key(self): + tmp_pk_file = '/tmp/kp-file-' + uuid.uuid4().hex + arglist = [ + '--private-key', tmp_pk_file, + self.keypair.name, + ] + verifylist = [ + ('private_key', tmp_pk_file), + ('name', self.keypair.name) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch('io.open') as mock_open: + mock_open.return_value = mock.MagicMock() + m_file = mock_open.return_value.__enter__.return_value + + columns, data = self.cmd.take_action(parsed_args) + + self.keypairs_mock.create.assert_called_with( + self.keypair.name, + public_key=None + ) + + mock_open.assert_called_once_with(tmp_pk_file, 'w+') + m_file.write.assert_called_once_with(self.keypair.private_key) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + class TestKeypairDelete(TestKeypair): -- cgit v1.2.1