1717
1818from openstackclient .compute .v2 import host
1919from openstackclient .tests .unit .compute .v2 import fakes as compute_fakes
20+ from openstackclient .tests .unit import fakes
2021from openstackclient .tests .unit import utils as tests_utils
2122
2223
@@ -26,60 +27,65 @@ def setUp(self):
2627 super (TestHost , self ).setUp ()
2728
2829 # Get a shortcut to the compute client
29- self .compute = self .app .client_manager .compute
30+ self .app .client_manager .sdk_connection = mock .Mock ()
31+ self .app .client_manager .sdk_connection .compute = mock .Mock ()
32+ self .sdk_client = self .app .client_manager .sdk_connection .compute
33+ self .sdk_client .get = mock .Mock ()
3034
3135
3236@mock .patch (
3337 'openstackclient.api.compute_v2.APIv2.host_list'
3438)
3539class TestHostList (TestHost ):
3640
37- host = compute_fakes .FakeHost .create_one_host ()
38-
39- columns = (
40- 'Host Name' ,
41- 'Service' ,
42- 'Zone' ,
43- )
44-
45- data = [(
46- host ['host_name' ],
47- host ['service' ],
48- host ['zone' ],
49- )]
41+ _host = compute_fakes .FakeHost .create_one_host ()
5042
5143 def setUp (self ):
5244 super (TestHostList , self ).setUp ()
5345
46+ self .sdk_client .get .return_value = fakes .FakeResponse (
47+ data = {'hosts' : [self ._host ]}
48+ )
49+
50+ self .columns = (
51+ 'Host Name' , 'Service' , 'Zone'
52+ )
53+
54+ self .data = [(
55+ self ._host ['host_name' ],
56+ self ._host ['service' ],
57+ self ._host ['zone' ],
58+ )]
59+
5460 self .cmd = host .ListHost (self .app , None )
5561
5662 def test_host_list_no_option (self , h_mock ):
57- h_mock .return_value = [self .host ]
63+ h_mock .return_value = [self ._host ]
5864 arglist = []
5965 verifylist = []
6066
6167 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
6268
6369 columns , data = self .cmd .take_action (parsed_args )
6470
65- h_mock . assert_called_with (None )
71+ self . sdk_client . get . assert_called_with ('/os-hosts' , microversion = '2.1' )
6672 self .assertEqual (self .columns , columns )
6773 self .assertEqual (self .data , list (data ))
6874
6975 def test_host_list_with_option (self , h_mock ):
70- h_mock .return_value = [self .host ]
76+ h_mock .return_value = [self ._host ]
7177 arglist = [
72- '--zone' , self .host ['zone' ],
78+ '--zone' , self ._host ['zone' ],
7379 ]
7480 verifylist = [
75- ('zone' , self .host ['zone' ]),
81+ ('zone' , self ._host ['zone' ]),
7682 ]
7783
7884 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
7985
8086 columns , data = self .cmd .take_action (parsed_args )
8187
82- h_mock . assert_called_with (self . host [ 'zone' ] )
88+ self . sdk_client . get . assert_called_with ('/os-hosts' , microversion = '2.1' )
8389 self .assertEqual (self .columns , columns )
8490 self .assertEqual (self .data , list (data ))
8591
@@ -141,31 +147,43 @@ def test_host_set(self, h_mock):
141147)
142148class TestHostShow (TestHost ):
143149
144- host = compute_fakes .FakeHost .create_one_host ()
145-
146- columns = (
147- 'Host' ,
148- 'Project' ,
149- 'CPU' ,
150- 'Memory MB' ,
151- 'Disk GB' ,
152- )
153-
154- data = [(
155- host ['host' ],
156- host ['project' ],
157- host ['cpu' ],
158- host ['memory_mb' ],
159- host ['disk_gb' ],
160- )]
150+ _host = compute_fakes .FakeHost .create_one_host ()
161151
162152 def setUp (self ):
163153 super (TestHostShow , self ).setUp ()
164154
155+ output_data = {"resource" : {
156+ "host" : self ._host ['host' ],
157+ "project" : self ._host ['project' ],
158+ "cpu" : self ._host ['cpu' ],
159+ "memory_mb" : self ._host ['memory_mb' ],
160+ "disk_gb" : self ._host ['disk_gb' ]
161+ }}
162+
163+ self .sdk_client .get .return_value = fakes .FakeResponse (
164+ data = {'host' : [output_data ]}
165+ )
166+
167+ self .columns = (
168+ 'Host' ,
169+ 'Project' ,
170+ 'CPU' ,
171+ 'Memory MB' ,
172+ 'Disk GB' ,
173+ )
174+
175+ self .data = [(
176+ self ._host ['host' ],
177+ self ._host ['project' ],
178+ self ._host ['cpu' ],
179+ self ._host ['memory_mb' ],
180+ self ._host ['disk_gb' ],
181+ )]
182+
165183 self .cmd = host .ShowHost (self .app , None )
166184
167185 def test_host_show_no_option (self , h_mock ):
168- h_mock .host_show .return_value = [self .host ]
186+ h_mock .host_show .return_value = [self ._host ]
169187 arglist = []
170188 verifylist = []
171189
@@ -174,18 +192,21 @@ def test_host_show_no_option(self, h_mock):
174192 self .cmd , arglist , verifylist )
175193
176194 def test_host_show_with_option (self , h_mock ):
177- h_mock .return_value = [self .host ]
195+ h_mock .return_value = [self ._host ]
178196 arglist = [
179- self .host ['host_name' ],
197+ self ._host ['host_name' ],
180198 ]
181199 verifylist = [
182- ('host' , self .host ['host_name' ]),
200+ ('host' , self ._host ['host_name' ]),
183201 ]
184202
185203 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
186204
187205 columns , data = self .cmd .take_action (parsed_args )
188206
189- h_mock .assert_called_with (self .host ['host_name' ])
207+ self .sdk_client .get .assert_called_with (
208+ '/os-hosts/' + self ._host ['host_name' ],
209+ microversion = '2.1'
210+ )
190211 self .assertEqual (self .columns , columns )
191212 self .assertEqual (self .data , list (data ))
0 commit comments