Skip to content

Commit 6a5be8c

Browse files
committed
Add unit test for TestServerList to test --long option.
In two steps: 1. Setup all necessary attributes of a server in setUp(), including the ones that are not faked in FaseServer by default. 2. Run a similar process with no option test case. The future plan is to move all these attributes to FakeServer. But it will cause some other changes which has nothing to do with this patch. So leave this job to do later. Change-Id: I1134812a0ea146ef737b0f0ffbef8ca23684accd Implements: blueprint osc-unit-test-framework-improvement
1 parent 85d6aee commit 6a5be8c

1 file changed

Lines changed: 50 additions & 5 deletions

File tree

openstackclient/tests/compute/v2/test_server.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,17 @@ class TestServerList(TestServer):
620620
'Status',
621621
'Networks',
622622
)
623-
624-
# Data returned by corresponding Nova API. The elements in this list are
625-
# tuples filled with server attributes.
626-
data = []
623+
columns_long = (
624+
'ID',
625+
'Name',
626+
'Status',
627+
'Task State',
628+
'Power State',
629+
'Networks',
630+
'Availability Zone',
631+
'Host',
632+
'Properties',
633+
)
627634

628635
# Default search options, in the case of no commandline option specified.
629636
search_opts = {
@@ -652,12 +659,18 @@ class TestServerList(TestServer):
652659
def setUp(self):
653660
super(TestServerList, self).setUp()
654661

655-
# The fake servers' attributes.
662+
# The fake servers' attributes. Use the original attributes names in
663+
# nova, not the ones printed by "server list" command.
656664
self.attrs = {
657665
'status': 'ACTIVE',
666+
'OS-EXT-STS:task_state': 'None',
667+
'OS-EXT-STS:power_state': 0x01, # Running
658668
'networks': {
659669
u'public': [u'10.20.30.40', u'2001:db8::5']
660670
},
671+
'OS-EXT-AZ:availability_zone': 'availability-zone-xxx',
672+
'OS-EXT-SRV-ATTR:host': 'host-name-xxx',
673+
'Metadata': '',
661674
}
662675

663676
# The servers to be listed.
@@ -669,13 +682,29 @@ def setUp(self):
669682
self.cmd = server.ListServer(self.app, None)
670683

671684
# Prepare data returned by fake Nova API.
685+
self.data = []
686+
self.data_long = []
687+
672688
for s in self.servers:
673689
self.data.append((
674690
s.id,
675691
s.name,
676692
s.status,
677693
server._format_servers_list_networks(s.networks),
678694
))
695+
self.data_long.append((
696+
s.id,
697+
s.name,
698+
s.status,
699+
getattr(s, 'OS-EXT-STS:task_state'),
700+
server._format_servers_list_power_state(
701+
getattr(s, 'OS-EXT-STS:power_state')
702+
),
703+
server._format_servers_list_networks(s.networks),
704+
getattr(s, 'OS-EXT-AZ:availability_zone'),
705+
getattr(s, 'OS-EXT-SRV-ATTR:host'),
706+
s.Metadata,
707+
))
679708

680709
def test_server_list_no_option(self):
681710
arglist = []
@@ -691,6 +720,22 @@ def test_server_list_no_option(self):
691720
self.assertEqual(self.columns, columns)
692721
self.assertEqual(tuple(self.data), tuple(data))
693722

723+
def test_server_list_long_option(self):
724+
arglist = [
725+
'--long',
726+
]
727+
verifylist = [
728+
('all_projects', False),
729+
('long', True),
730+
]
731+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
732+
733+
columns, data = self.cmd.take_action(parsed_args)
734+
735+
self.servers_mock.list.assert_called_with(**self.kwargs)
736+
self.assertEqual(self.columns_long, columns)
737+
self.assertEqual(tuple(self.data_long), tuple(data))
738+
694739

695740
class TestServerLock(TestServer):
696741

0 commit comments

Comments
 (0)