From 6a5be8c4c9abf25d7fa6a952ae466aedd056d353 Mon Sep 17 00:00:00 2001 From: Tang Chen <tangchen@cn.fujitsu.com> Date: Wed, 9 Dec 2015 19:42:52 +0800 Subject: [PATCH] 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 --- .../tests/compute/v2/test_server.py | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/openstackclient/tests/compute/v2/test_server.py b/openstackclient/tests/compute/v2/test_server.py index ce2dcdf7e8..ec916b9fdc 100644 --- a/openstackclient/tests/compute/v2/test_server.py +++ b/openstackclient/tests/compute/v2/test_server.py @@ -620,10 +620,17 @@ class TestServerList(TestServer): 'Status', 'Networks', ) - - # Data returned by corresponding Nova API. The elements in this list are - # tuples filled with server attributes. - data = [] + columns_long = ( + 'ID', + 'Name', + 'Status', + 'Task State', + 'Power State', + 'Networks', + 'Availability Zone', + 'Host', + 'Properties', + ) # Default search options, in the case of no commandline option specified. search_opts = { @@ -652,12 +659,18 @@ class TestServerList(TestServer): def setUp(self): super(TestServerList, self).setUp() - # The fake servers' attributes. + # The fake servers' attributes. Use the original attributes names in + # nova, not the ones printed by "server list" command. self.attrs = { 'status': 'ACTIVE', + 'OS-EXT-STS:task_state': 'None', + 'OS-EXT-STS:power_state': 0x01, # Running 'networks': { u'public': [u'10.20.30.40', u'2001:db8::5'] }, + 'OS-EXT-AZ:availability_zone': 'availability-zone-xxx', + 'OS-EXT-SRV-ATTR:host': 'host-name-xxx', + 'Metadata': '', } # The servers to be listed. @@ -669,6 +682,9 @@ class TestServerList(TestServer): self.cmd = server.ListServer(self.app, None) # Prepare data returned by fake Nova API. + self.data = [] + self.data_long = [] + for s in self.servers: self.data.append(( s.id, @@ -676,6 +692,19 @@ class TestServerList(TestServer): s.status, server._format_servers_list_networks(s.networks), )) + self.data_long.append(( + s.id, + s.name, + s.status, + getattr(s, 'OS-EXT-STS:task_state'), + server._format_servers_list_power_state( + getattr(s, 'OS-EXT-STS:power_state') + ), + server._format_servers_list_networks(s.networks), + getattr(s, 'OS-EXT-AZ:availability_zone'), + getattr(s, 'OS-EXT-SRV-ATTR:host'), + s.Metadata, + )) def test_server_list_no_option(self): arglist = [] @@ -691,6 +720,22 @@ class TestServerList(TestServer): self.assertEqual(self.columns, columns) self.assertEqual(tuple(self.data), tuple(data)) + def test_server_list_long_option(self): + arglist = [ + '--long', + ] + verifylist = [ + ('all_projects', False), + ('long', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.servers_mock.list.assert_called_with(**self.kwargs) + self.assertEqual(self.columns_long, columns) + self.assertEqual(tuple(self.data_long), tuple(data)) + class TestServerLock(TestServer):