Skip to content

Commit e0b6cab

Browse files
author
ting.wang
committed
Add some test cases for "server list" command
Add some test cases that test 'server list' command when specifying flavor or image. Because I add some attribution to fake.py, I have to change some code in create server test. Despite all this, I think it's good for testing. Change-Id: I714deac1f6f940b790a3c20af5f7ffa724ac44d1
1 parent e5fe9a7 commit e0b6cab

2 files changed

Lines changed: 78 additions & 27 deletions

File tree

openstackclient/tests/compute/v2/fakes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ def create_one_server(attrs={}, methods={}):
377377
'id': 'server-id-' + uuid.uuid4().hex,
378378
'name': 'server-name-' + uuid.uuid4().hex,
379379
'metadata': {},
380+
'image': {
381+
'id': 'image-id-' + uuid.uuid4().hex,
382+
},
383+
'flavor': {
384+
'id': 'flavor-id-' + uuid.uuid4().hex,
385+
}
380386
}
381387

382388
# Overwrite default attributes.

openstackclient/tests/compute/v2/test_server.py

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
#
15-
1615
import mock
1716

1817
from mock import call
@@ -92,6 +91,7 @@ class TestServerCreate(TestServer):
9291
'addresses',
9392
'flavor',
9493
'id',
94+
'image',
9595
'name',
9696
'networks',
9797
'properties',
@@ -100,8 +100,9 @@ class TestServerCreate(TestServer):
100100
def datalist(self):
101101
datalist = (
102102
'',
103-
self.flavor.name + ' ()',
103+
self.flavor.name + ' (' + self.new_server.flavor.get('id') + ')',
104104
self.new_server.id,
105+
self.image.name + ' (' + self.new_server.image.get('id') + ')',
105106
self.new_server.name,
106107
self.new_server.networks,
107108
'',
@@ -617,33 +618,32 @@ class TestServerList(TestServer):
617618
'Properties',
618619
)
619620

620-
# Default search options, in the case of no commandline option specified.
621-
search_opts = {
622-
'reservation_id': None,
623-
'ip': None,
624-
'ip6': None,
625-
'name': None,
626-
'instance_name': None,
627-
'status': None,
628-
'flavor': None,
629-
'image': None,
630-
'host': None,
631-
'tenant_id': None,
632-
'all_tenants': False,
633-
'user_id': None,
634-
}
635-
636-
# Default params of the core function of the command in the case of no
637-
# commandline option specified.
638-
kwargs = {
639-
'search_opts': search_opts,
640-
'marker': None,
641-
'limit': None,
642-
}
643-
644621
def setUp(self):
645622
super(TestServerList, self).setUp()
646623

624+
self.search_opts = {
625+
'reservation_id': None,
626+
'ip': None,
627+
'ip6': None,
628+
'name': None,
629+
'instance_name': None,
630+
'status': None,
631+
'flavor': None,
632+
'image': None,
633+
'host': None,
634+
'tenant_id': None,
635+
'all_tenants': False,
636+
'user_id': None,
637+
}
638+
639+
# Default params of the core function of the command in the case of no
640+
# commandline option specified.
641+
self.kwargs = {
642+
'search_opts': self.search_opts,
643+
'marker': None,
644+
'limit': None,
645+
}
646+
647647
# The fake servers' attributes. Use the original attributes names in
648648
# nova, not the ones printed by "server list" command.
649649
self.attrs = {
@@ -660,9 +660,14 @@ def setUp(self):
660660

661661
# The servers to be listed.
662662
self.servers = self.setup_servers_mock(3)
663-
664663
self.servers_mock.list.return_value = self.servers
665664

665+
self.image = image_fakes.FakeImage.create_one_image()
666+
self.cimages_mock.get.return_value = self.image
667+
668+
self.flavor = compute_fakes.FakeFlavor.create_one_flavor()
669+
self.flavors_mock.get.return_value = self.flavor
670+
666671
# Get the command object to test
667672
self.cmd = server.ListServer(self.app, None)
668673

@@ -721,6 +726,46 @@ def test_server_list_long_option(self):
721726
self.assertEqual(self.columns_long, columns)
722727
self.assertEqual(tuple(self.data_long), tuple(data))
723728

729+
def test_server_list_with_image(self):
730+
731+
arglist = [
732+
'--image', self.image.id
733+
]
734+
verifylist = [
735+
('image', self.image.id)
736+
]
737+
738+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
739+
columns, data = self.cmd.take_action(parsed_args)
740+
741+
self.cimages_mock.get.assert_called_with(self.image.id)
742+
743+
self.search_opts['image'] = self.image.id
744+
self.servers_mock.list.assert_called_with(**self.kwargs)
745+
746+
self.assertEqual(self.columns, columns)
747+
self.assertEqual(tuple(self.data), tuple(data))
748+
749+
def test_server_list_with_flavor(self):
750+
751+
arglist = [
752+
'--flavor', self.flavor.id
753+
]
754+
verifylist = [
755+
('flavor', self.flavor.id)
756+
]
757+
758+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
759+
columns, data = self.cmd.take_action(parsed_args)
760+
761+
self.flavors_mock.get.assert_called_with(self.flavor.id)
762+
763+
self.search_opts['flavor'] = self.flavor.id
764+
self.servers_mock.list.assert_called_with(**self.kwargs)
765+
766+
self.assertEqual(self.columns, columns)
767+
self.assertEqual(tuple(self.data), tuple(data))
768+
724769

725770
class TestServerLock(TestServer):
726771

0 commit comments

Comments
 (0)