Skip to content

Commit 042e2b7

Browse files
committed
[compute] Add unit test for keypair
keypair do not have unit test, this patch adds it. Change-Id: Id702ccaad239b916340bb17014d1ede0a28aaec9
1 parent b5b5fdd commit 042e2b7

3 files changed

Lines changed: 318 additions & 1 deletion

File tree

openstackclient/compute/v2/keypair.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
"""Keypair action implementations"""
1717

18+
import io
1819
import os
1920
import six
2021
import sys
@@ -47,7 +48,8 @@ def take_action(self, parsed_args):
4748
public_key = parsed_args.public_key
4849
if public_key:
4950
try:
50-
with open(os.path.expanduser(parsed_args.public_key)) as p:
51+
with io.open(os.path.expanduser(parsed_args.public_key),
52+
"rb") as p:
5153
public_key = p.read()
5254
except IOError as e:
5355
msg = "Key file %s not found: %s"

openstackclient/tests/compute/v2/fakes.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ def __init__(self, **kwargs):
137137
self.networks = mock.Mock()
138138
self.networks.resource_class = fakes.FakeResource(None, {})
139139

140+
self.keypairs = mock.Mock()
141+
self.keypairs.resource_class = fakes.FakeResource(None, {})
142+
140143
self.auth_token = kwargs['token']
141144

142145
self.management_url = kwargs['endpoint']
@@ -534,6 +537,58 @@ def get_flavors(flavors=None, count=2):
534537
return mock.MagicMock(side_effect=flavors)
535538

536539

540+
class FakeKeypair(object):
541+
"""Fake one or more keypairs."""
542+
543+
@staticmethod
544+
def create_one_keypair(attrs=None, no_pri=False):
545+
"""Create a fake keypair
546+
547+
:param Dictionary attrs:
548+
A dictionary with all attributes
549+
:return:
550+
A FakeResource
551+
"""
552+
# Set default attributes.
553+
if attrs is None:
554+
attrs = {}
555+
556+
keypair_info = {
557+
'name': 'keypair-name-' + uuid.uuid4().hex,
558+
'fingerprint': 'dummy',
559+
'public_key': 'dummy',
560+
'user_id': 'user'
561+
}
562+
if not no_pri:
563+
keypair_info['private_key'] = 'private_key'
564+
565+
# Overwrite default attributes.
566+
keypair_info.update(attrs)
567+
568+
keypair = fakes.FakeResource(info=copy.deepcopy(keypair_info),
569+
loaded=True)
570+
571+
return keypair
572+
573+
@staticmethod
574+
def create_keypairs(attrs=None, count=2):
575+
"""Create multiple fake flavors.
576+
577+
:param Dictionary attrs:
578+
A dictionary with all attributes
579+
:param int count:
580+
The number of flavors to fake
581+
:return:
582+
A list of FakeFlavorResource objects faking the flavors
583+
"""
584+
585+
keypairs = []
586+
for i in range(0, count):
587+
keypairs.append(FakeKeypair.create_one_keypair(attrs))
588+
589+
return keypairs
590+
591+
537592
class FakeAvailabilityZone(object):
538593
"""Fake one or more compute availability zones (AZs)."""
539594

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
# Copyright 2016 IBM
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
import mock
17+
18+
from openstackclient.compute.v2 import keypair
19+
from openstackclient.tests.compute.v2 import fakes as compute_fakes
20+
from openstackclient.tests import utils as tests_utils
21+
22+
23+
class TestKeypair(compute_fakes.TestComputev2):
24+
25+
def setUp(self):
26+
super(TestKeypair, self).setUp()
27+
28+
# Get a shortcut to the KeypairManager Mock
29+
self.keypairs_mock = self.app.client_manager.compute.keypairs
30+
self.keypairs_mock.reset_mock()
31+
32+
33+
class TestKeypairCreate(TestKeypair):
34+
35+
keypair = compute_fakes.FakeKeypair.create_one_keypair()
36+
37+
def setUp(self):
38+
super(TestKeypairCreate, self).setUp()
39+
40+
self.columns = (
41+
'fingerprint',
42+
'name',
43+
'user_id'
44+
)
45+
self.data = (
46+
self.keypair.fingerprint,
47+
self.keypair.name,
48+
self.keypair.user_id
49+
)
50+
51+
# Get the command object to test
52+
self.cmd = keypair.CreateKeypair(self.app, None)
53+
54+
self.keypairs_mock.create.return_value = self.keypair
55+
56+
def test_key_pair_create_no_options(self):
57+
58+
arglist = [
59+
self.keypair.name,
60+
]
61+
verifylist = [
62+
('name', self.keypair.name),
63+
]
64+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
65+
66+
columns, data = self.cmd.take_action(parsed_args)
67+
68+
self.keypairs_mock.create.assert_called_with(
69+
self.keypair.name,
70+
public_key=None
71+
)
72+
73+
self.assertEqual({}, columns)
74+
self.assertEqual({}, data)
75+
76+
def test_keypair_create_public_key(self):
77+
# overwrite the setup one because we want to omit private_key
78+
self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
79+
no_pri=True)
80+
self.keypairs_mock.create.return_value = self.keypair
81+
82+
self.data = (
83+
self.keypair.fingerprint,
84+
self.keypair.name,
85+
self.keypair.user_id
86+
)
87+
88+
arglist = [
89+
'--public-key', self.keypair.public_key,
90+
self.keypair.name,
91+
]
92+
verifylist = [
93+
('public_key', self.keypair.public_key),
94+
('name', self.keypair.name)
95+
]
96+
97+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
98+
99+
with mock.patch('io.open') as mock_open:
100+
mock_open.return_value = mock.MagicMock()
101+
m_file = mock_open.return_value.__enter__.return_value
102+
m_file.read.return_value = 'dummy'
103+
104+
columns, data = self.cmd.take_action(parsed_args)
105+
106+
self.keypairs_mock.create.assert_called_with(
107+
self.keypair.name,
108+
public_key=self.keypair.public_key
109+
)
110+
111+
self.assertEqual(self.columns, columns)
112+
self.assertEqual(self.data, data)
113+
114+
115+
class TestKeypairDelete(TestKeypair):
116+
117+
keypair = compute_fakes.FakeKeypair.create_one_keypair()
118+
119+
def setUp(self):
120+
super(TestKeypairDelete, self).setUp()
121+
122+
self.keypairs_mock.get.return_value = self.keypair
123+
self.keypairs_mock.delete.return_value = None
124+
125+
self.cmd = keypair.DeleteKeypair(self.app, None)
126+
127+
def test_keypair_delete(self):
128+
arglist = [
129+
self.keypair.name
130+
]
131+
verifylist = [
132+
('name', self.keypair.name),
133+
]
134+
135+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
136+
137+
ret = self.cmd.take_action(parsed_args)
138+
139+
self.assertIsNone(ret)
140+
self.keypairs_mock.delete.assert_called_with(self.keypair.name)
141+
142+
143+
class TestKeypairList(TestKeypair):
144+
145+
# Return value of self.keypairs_mock.list().
146+
keypairs = compute_fakes.FakeKeypair.create_keypairs(count=1)
147+
148+
columns = (
149+
"Name",
150+
"Fingerprint"
151+
)
152+
153+
data = ((
154+
keypairs[0].name,
155+
keypairs[0].fingerprint
156+
), )
157+
158+
def setUp(self):
159+
super(TestKeypairList, self).setUp()
160+
161+
self.keypairs_mock.list.return_value = self.keypairs
162+
163+
# Get the command object to test
164+
self.cmd = keypair.ListKeypair(self.app, None)
165+
166+
def test_keypair_list_no_options(self):
167+
arglist = []
168+
verifylist = [
169+
]
170+
171+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
172+
173+
# In base command class Lister in cliff, abstract method take_action()
174+
# returns a tuple containing the column names and an iterable
175+
# containing the data to be listed.
176+
columns, data = self.cmd.take_action(parsed_args)
177+
178+
# Set expected values
179+
180+
self.keypairs_mock.list.assert_called_with()
181+
182+
self.assertEqual(self.columns, columns)
183+
self.assertEqual(tuple(self.data), tuple(data))
184+
185+
186+
class TestKeypairShow(TestKeypair):
187+
188+
keypair = compute_fakes.FakeKeypair.create_one_keypair()
189+
190+
def setUp(self):
191+
super(TestKeypairShow, self).setUp()
192+
193+
self.keypairs_mock.get.return_value = self.keypair
194+
195+
self.cmd = keypair.ShowKeypair(self.app, None)
196+
197+
self.columns = (
198+
"fingerprint",
199+
"name",
200+
"user_id"
201+
)
202+
203+
self.data = (
204+
self.keypair.fingerprint,
205+
self.keypair.name,
206+
self.keypair.user_id
207+
)
208+
209+
def test_show_no_options(self):
210+
211+
arglist = []
212+
verifylist = []
213+
214+
# Missing required args should boil here
215+
self.assertRaises(tests_utils.ParserException, self.check_parser,
216+
self.cmd, arglist, verifylist)
217+
218+
def test_keypair_show(self):
219+
# overwrite the setup one because we want to omit private_key
220+
self.keypair = compute_fakes.FakeKeypair.create_one_keypair(
221+
no_pri=True)
222+
self.keypairs_mock.get.return_value = self.keypair
223+
224+
self.data = (
225+
self.keypair.fingerprint,
226+
self.keypair.name,
227+
self.keypair.user_id
228+
)
229+
230+
arglist = [
231+
self.keypair.name
232+
]
233+
verifylist = [
234+
('name', self.keypair.name)
235+
]
236+
237+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
238+
239+
columns, data = self.cmd.take_action(parsed_args)
240+
241+
self.assertEqual(self.columns, columns)
242+
self.assertEqual(self.data, data)
243+
244+
def test_keypair_show_public(self):
245+
246+
arglist = [
247+
'--public-key',
248+
self.keypair.name
249+
]
250+
verifylist = [
251+
('public_key', True),
252+
('name', self.keypair.name)
253+
]
254+
255+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
256+
257+
columns, data = self.cmd.take_action(parsed_args)
258+
259+
self.assertEqual({}, columns)
260+
self.assertEqual({}, data)

0 commit comments

Comments
 (0)