Skip to content

Commit ff62c83

Browse files
committed
Update keypair tests
Make testcases separate instances of test class. Use setUp with addCleanup methods for every test case. Add negative test cases. Closes-Bug: #1564480 Change-Id: I5d8fd2a238e6cf0584777eb0d24dfcaed4133ee1
1 parent 8eade18 commit ff62c83

1 file changed

Lines changed: 131 additions & 31 deletions

File tree

functional/tests/compute/v2/test_keypair.py

Lines changed: 131 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,84 @@
1111
# under the License.
1212

1313
import tempfile
14-
import uuid
1514

1615
from functional.common import test
1716

17+
from tempest_lib.common.utils import data_utils
18+
from tempest_lib import exceptions
1819

19-
PUBLIC_KEY = (
20-
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWNGczJxNaFUrJJVhta4dWsZY6bU'
21-
'5HUMPbyfSMu713ca3mYtG848W4dfDCB98KmSQx2Bl0D6Q2nrOszOXEQWAXNdfMadnW'
22-
'c4mNwhZcPBVohIFoC1KZJC8kcBTvFZcoz3mdIijxJtywZNpGNh34VRJlZeHyYjg8/D'
23-
'esHzdoBVd5c/4R36emQSIV9ukY6PHeZ3scAH4B3K9PxItJBwiFtouSRphQG0bJgOv/'
24-
'gjAjMElAvg5oku98cb4QiHZ8T8WY68id804raHR6pJxpVVJN4TYJmlUs+NOVM+pPKb'
25-
'KJttqrIBTkawGK9pLHNfn7z6v1syvUo/4enc1l0Q/Qn2kWiz67 fake@openstack'
26-
)
2720

21+
class KeypairBase(test.TestCase):
22+
"""Methods for functional tests."""
2823

29-
class KeypairTests(test.TestCase):
30-
"""Functional tests for compute keypairs. """
31-
NAME = uuid.uuid4().hex
32-
HEADERS = ['Name']
33-
FIELDS = ['name']
24+
def keypair_create(self, name=data_utils.rand_uuid()):
25+
"""Create keypair and add cleanup."""
26+
raw_output = self.openstack('keypair create ' + name)
27+
self.addCleanup(self.keypair_delete, name, True)
28+
if not raw_output:
29+
self.fail('Keypair has not been created!')
3430

35-
@classmethod
36-
def setUpClass(cls):
37-
private_key = cls.openstack('keypair create ' + cls.NAME)
38-
cls.assertInOutput('-----BEGIN RSA PRIVATE KEY-----', private_key)
39-
cls.assertInOutput('-----END RSA PRIVATE KEY-----', private_key)
31+
def keypair_list(self, params=''):
32+
"""Return dictionary with list of keypairs."""
33+
raw_output = self.openstack('keypair list')
34+
keypairs = self.parse_show_as_object(raw_output)
35+
return keypairs
4036

41-
@classmethod
42-
def tearDownClass(cls):
43-
raw_output = cls.openstack('keypair delete ' + cls.NAME)
44-
cls.assertOutput('', raw_output)
37+
def keypair_delete(self, name, ignore_exceptions=False):
38+
"""Try to delete keypair by name."""
39+
try:
40+
self.openstack('keypair delete ' + name)
41+
except exceptions.CommandFailed:
42+
if not ignore_exceptions:
43+
raise
4544

46-
def test_keypair_create(self):
45+
46+
class KeypairTests(KeypairBase):
47+
"""Functional tests for compute keypairs."""
48+
49+
PUBLIC_KEY = (
50+
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWNGczJxNaFUrJJVhta4dWsZY6bU'
51+
'5HUMPbyfSMu713ca3mYtG848W4dfDCB98KmSQx2Bl0D6Q2nrOszOXEQWAXNdfMadnW'
52+
'c4mNwhZcPBVohIFoC1KZJC8kcBTvFZcoz3mdIijxJtywZNpGNh34VRJlZeHyYjg8/D'
53+
'esHzdoBVd5c/4R36emQSIV9ukY6PHeZ3scAH4B3K9PxItJBwiFtouSRphQG0bJgOv/'
54+
'gjAjMElAvg5oku98cb4QiHZ8T8WY68id804raHR6pJxpVVJN4TYJmlUs+NOVM+pPKb'
55+
'KJttqrIBTkawGK9pLHNfn7z6v1syvUo/4enc1l0Q/Qn2kWiz67 fake@openstack'
56+
)
57+
58+
def setUp(self):
59+
"""Create keypair with randomized name for tests."""
60+
super(KeypairTests, self).setUp()
61+
self.KPName = data_utils.rand_name('TestKeyPair')
62+
self.keypair = self.keypair_create(self.KPName)
63+
64+
def test_keypair_create_duplicate(self):
65+
"""Try to create duplicate name keypair.
66+
67+
Test steps:
68+
1) Create keypair in setUp
69+
2) Try to create duplicate keypair with the same name
70+
"""
71+
self.assertRaises(exceptions.CommandFailed,
72+
self.openstack, 'keypair create ' + self.KPName)
73+
74+
def test_keypair_create_noname(self):
75+
"""Try to create keypair without name.
76+
77+
Test steps:
78+
1) Try to create keypair without a name
79+
"""
80+
self.assertRaises(exceptions.CommandFailed,
81+
self.openstack, 'keypair create')
82+
83+
def test_keypair_create_public_key(self):
84+
"""Test for create keypair with --public-key option.
85+
86+
Test steps:
87+
1) Create keypair with given public key
88+
2) Delete keypair
89+
"""
4790
with tempfile.NamedTemporaryFile() as f:
48-
f.write(PUBLIC_KEY)
91+
f.write(self.PUBLIC_KEY)
4992
f.flush()
5093

5194
raw_output = self.openstack(
@@ -57,12 +100,69 @@ def test_keypair_create(self):
57100
)
58101
self.assertIn('tmpkey', raw_output)
59102

103+
def test_keypair_create(self):
104+
"""Test keypair create command.
105+
106+
Test steps:
107+
1) Create keypair in setUp
108+
2) Check RSA private key in output
109+
3) Check for new keypair in keypairs list
110+
"""
111+
NewName = data_utils.rand_name('TestKeyPairCreated')
112+
raw_output = self.openstack('keypair create ' + NewName)
113+
self.addCleanup(self.openstack, 'keypair delete ' + NewName)
114+
self.assertInOutput('-----BEGIN RSA PRIVATE KEY-----', raw_output)
115+
self.assertRegex(raw_output, "[0-9A-Za-z+/]+[=]{0,3}\n")
116+
self.assertInOutput('-----END RSA PRIVATE KEY-----', raw_output)
117+
self.assertIn(NewName, self.keypair_list())
118+
119+
def test_keypair_delete_not_existing(self):
120+
"""Try to delete keypair with not existing name.
121+
122+
Test steps:
123+
1) Create keypair in setUp
124+
2) Try to delete not existing keypair
125+
"""
126+
self.assertRaises(exceptions.CommandFailed,
127+
self.openstack, 'keypair delete not_existing')
128+
129+
def test_keypair_delete(self):
130+
"""Test keypair delete command.
131+
132+
Test steps:
133+
1) Create keypair in setUp
134+
2) Delete keypair
135+
3) Check that keypair not in keypairs list
136+
"""
137+
self.openstack('keypair delete ' + self.KPName)
138+
self.assertNotIn(self.KPName, self.keypair_list())
139+
60140
def test_keypair_list(self):
61-
opts = self.get_list_opts(self.HEADERS)
62-
raw_output = self.openstack('keypair list' + opts)
63-
self.assertIn(self.NAME, raw_output)
141+
"""Test keypair list command.
142+
143+
Test steps:
144+
1) Create keypair in setUp
145+
2) List keypairs
146+
3) Check output table structure
147+
4) Check keypair name in output
148+
"""
149+
HEADERS = ['Name', 'Fingerprint']
150+
raw_output = self.openstack('keypair list')
151+
items = self.parse_listing(raw_output)
152+
self.assert_table_structure(items, HEADERS)
153+
self.assertIn(self.KPName, raw_output)
64154

65155
def test_keypair_show(self):
66-
opts = self.get_show_opts(self.FIELDS)
67-
raw_output = self.openstack('keypair show ' + self.NAME + opts)
68-
self.assertEqual(self.NAME + "\n", raw_output)
156+
"""Test keypair show command.
157+
158+
Test steps:
159+
1) Create keypair in setUp
160+
2) Show keypair
161+
3) Check output table structure
162+
4) Check keypair name in output
163+
"""
164+
HEADERS = ['Field', 'Value']
165+
raw_output = self.openstack('keypair show ' + self.KPName)
166+
items = self.parse_listing(raw_output)
167+
self.assert_table_structure(items, HEADERS)
168+
self.assertInOutput(self.KPName, raw_output)

0 commit comments

Comments
 (0)