Skip to content

Commit c9cc8b0

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Format aggregate command fields and de-race functional tests"
2 parents 6c818c4 + 03a2acc commit c9cc8b0

3 files changed

Lines changed: 117 additions & 29 deletions

File tree

openstackclient/compute/v2/aggregate.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import logging
2020

21+
from osc_lib.cli import format_columns
2122
from osc_lib.cli import parseractions
2223
from osc_lib.command import command
2324
from osc_lib import exceptions
@@ -58,6 +59,15 @@ def take_action(self, parsed_args):
5859

5960
info = {}
6061
info.update(data._info)
62+
63+
# Special mapping for columns to make the output easier to read:
64+
# 'metadata' --> 'properties'
65+
info.update(
66+
{
67+
'hosts': format_columns.ListColumn(info.pop('hosts')),
68+
'properties': format_columns.DictColumn(info.pop('metadata')),
69+
},
70+
)
6171
return zip(*sorted(six.iteritems(info)))
6272

6373

@@ -101,8 +111,20 @@ def take_action(self, parsed_args):
101111
parsed_args.property,
102112
)._info)
103113

104-
# TODO(dtroyer): re-format metadata field to properites as
105-
# in the set command
114+
# Special mapping for columns to make the output easier to read:
115+
# 'metadata' --> 'properties'
116+
hosts = None
117+
properties = None
118+
if 'hosts' in info.keys():
119+
hosts = format_columns.ListColumn(info.pop('hosts'))
120+
if 'metadata' in info.keys():
121+
properties = format_columns.DictColumn(info.pop('metadata'))
122+
info.update(
123+
{
124+
'hosts': hosts,
125+
'properties': properties,
126+
},
127+
)
106128
return zip(*sorted(six.iteritems(info)))
107129

108130

@@ -186,6 +208,10 @@ def take_action(self, parsed_args):
186208
return (column_headers,
187209
(utils.get_item_properties(
188210
s, columns,
211+
formatters={
212+
'Hosts': format_columns.ListColumn,
213+
'Metadata': format_columns.DictColumn,
214+
},
189215
) for s in data))
190216

191217

@@ -220,6 +246,15 @@ def take_action(self, parsed_args):
220246

221247
info = {}
222248
info.update(data._info)
249+
250+
# Special mapping for columns to make the output easier to read:
251+
# 'metadata' --> 'properties'
252+
info.update(
253+
{
254+
'hosts': format_columns.ListColumn(info.pop('hosts')),
255+
'properties': format_columns.DictColumn(info.pop('metadata')),
256+
},
257+
)
223258
return zip(*sorted(six.iteritems(info)))
224259

225260

@@ -326,7 +361,12 @@ def take_action(self, parsed_args):
326361
# 'metadata' --> 'properties'
327362
data._info.update(
328363
{
329-
'properties': utils.format_dict(data._info.pop('metadata')),
364+
'hosts': format_columns.ListColumn(
365+
data._info.pop('hosts')
366+
),
367+
'properties': format_columns.DictColumn(
368+
data._info.pop('metadata')
369+
),
330370
},
331371
)
332372

openstackclient/tests/functional/compute/v2/test_aggregate.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# under the License.
1212

1313
import json
14+
import time
1415
import uuid
1516

1617
from openstackclient.tests.functional import base
@@ -19,6 +20,32 @@
1920
class AggregateTests(base.TestCase):
2021
"""Functional tests for aggregate"""
2122

23+
@classmethod
24+
def wait_for_status(cls, check_type, check_name, desired_status,
25+
wait=120, interval=5, failures=None):
26+
current_status = "notset"
27+
if failures is None:
28+
failures = ['error']
29+
total_sleep = 0
30+
while total_sleep < wait:
31+
output = json.loads(cls.openstack(
32+
check_type + ' show -f json ' + check_name))
33+
current_status = output['name']
34+
if (current_status == desired_status):
35+
print('{} {} now has status {}'
36+
.format(check_type, check_name, current_status))
37+
return
38+
print('Checking {} {} Waiting for {} current status: {}'
39+
.format(check_type, check_name,
40+
desired_status, current_status))
41+
if current_status in failures:
42+
raise Exception(
43+
'Current status {} of {} {} is one of failures {}'
44+
.format(current_status, check_type, check_name, failures))
45+
time.sleep(interval)
46+
total_sleep += interval
47+
cls.assertOutput(desired_status, current_status)
48+
2249
def test_aggregate_crud(self):
2350
"""Test create, delete multiple"""
2451
name1 = uuid.uuid4().hex
@@ -36,12 +63,16 @@ def test_aggregate_crud(self):
3663
'nova',
3764
cmd_output['availability_zone']
3865
)
39-
# TODO(dtroyer): enable the following once the properties field
40-
# is correctly formatted in the create output
41-
# self.assertIn(
42-
# "a='b'",
43-
# cmd_output['properties']
44-
# )
66+
self.assertIn(
67+
'a',
68+
cmd_output['properties']
69+
)
70+
self.wait_for_status('aggregate', name1, name1)
71+
self.addCleanup(
72+
self.openstack,
73+
'aggregate delete ' + name1,
74+
fail_ok=True,
75+
)
4576

4677
name2 = uuid.uuid4().hex
4778
cmd_output = json.loads(self.openstack(
@@ -57,6 +88,12 @@ def test_aggregate_crud(self):
5788
'external',
5889
cmd_output['availability_zone']
5990
)
91+
self.wait_for_status('aggregate', name2, name2)
92+
self.addCleanup(
93+
self.openstack,
94+
'aggregate delete ' + name2,
95+
fail_ok=True,
96+
)
6097

6198
# Test aggregate set
6299
name3 = uuid.uuid4().hex
@@ -69,6 +106,12 @@ def test_aggregate_crud(self):
69106
name1
70107
)
71108
self.assertOutput('', raw_output)
109+
self.wait_for_status('aggregate', name3, name3)
110+
self.addCleanup(
111+
self.openstack,
112+
'aggregate delete ' + name3,
113+
fail_ok=True,
114+
)
72115

73116
cmd_output = json.loads(self.openstack(
74117
'aggregate show -f json ' +
@@ -83,11 +126,11 @@ def test_aggregate_crud(self):
83126
cmd_output['availability_zone']
84127
)
85128
self.assertIn(
86-
"c='d'",
129+
'c',
87130
cmd_output['properties']
88131
)
89132
self.assertNotIn(
90-
"a='b'",
133+
'a',
91134
cmd_output['properties']
92135
)
93136

openstackclient/tests/unit/compute/v2/test_aggregate.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mock
1717
from mock import call
1818

19+
from osc_lib.cli import format_columns
1920
from osc_lib import exceptions
2021
from osc_lib import utils
2122

@@ -31,16 +32,16 @@ class TestAggregate(compute_fakes.TestComputev2):
3132
'availability_zone',
3233
'hosts',
3334
'id',
34-
'metadata',
3535
'name',
36+
'properties',
3637
)
3738

3839
data = (
3940
fake_ag.availability_zone,
40-
fake_ag.hosts,
41+
format_columns.ListColumn(fake_ag.hosts),
4142
fake_ag.id,
42-
fake_ag.metadata,
4343
fake_ag.name,
44+
format_columns.DictColumn(fake_ag.metadata),
4445
)
4546

4647
def setUp(self):
@@ -75,7 +76,7 @@ def test_aggregate_add_host(self):
7576
self.aggregate_mock.add_host.assert_called_once_with(self.fake_ag,
7677
parsed_args.host)
7778
self.assertEqual(self.columns, columns)
78-
self.assertEqual(self.data, data)
79+
self.assertItemEqual(self.data, data)
7980

8081

8182
class TestAggregateCreate(TestAggregate):
@@ -99,7 +100,7 @@ def test_aggregate_create(self):
99100
self.aggregate_mock.create.assert_called_once_with(parsed_args.name,
100101
None)
101102
self.assertEqual(self.columns, columns)
102-
self.assertEqual(self.data, data)
103+
self.assertItemEqual(self.data, data)
103104

104105
def test_aggregate_create_with_zone(self):
105106
arglist = [
@@ -116,7 +117,7 @@ def test_aggregate_create_with_zone(self):
116117
self.aggregate_mock.create.assert_called_once_with(parsed_args.name,
117118
parsed_args.zone)
118119
self.assertEqual(self.columns, columns)
119-
self.assertEqual(self.data, data)
120+
self.assertItemEqual(self.data, data)
120121

121122
def test_aggregate_create_with_property(self):
122123
arglist = [
@@ -135,7 +136,7 @@ def test_aggregate_create_with_property(self):
135136
self.aggregate_mock.set_metadata.assert_called_once_with(
136137
self.fake_ag, parsed_args.property)
137138
self.assertEqual(self.columns, columns)
138-
self.assertEqual(self.data, data)
139+
self.assertItemEqual(self.data, data)
139140

140141

141142
class TestAggregateDelete(TestAggregate):
@@ -234,8 +235,11 @@ class TestAggregateList(TestAggregate):
234235
TestAggregate.fake_ag.id,
235236
TestAggregate.fake_ag.name,
236237
TestAggregate.fake_ag.availability_zone,
237-
{key: value for key, value in TestAggregate.fake_ag.metadata.items()
238-
if key != 'availability_zone'},
238+
format_columns.DictColumn({
239+
key: value
240+
for key, value in TestAggregate.fake_ag.metadata.items()
241+
if key != 'availability_zone'
242+
}),
239243
), )
240244

241245
def setUp(self):
@@ -250,7 +254,7 @@ def test_aggregate_list(self):
250254
columns, data = self.cmd.take_action(parsed_args)
251255

252256
self.assertEqual(self.list_columns, columns)
253-
self.assertEqual(self.list_data, tuple(data))
257+
self.assertItemEqual(self.list_data, tuple(data))
254258

255259
def test_aggregate_list_with_long(self):
256260
arglist = [
@@ -263,7 +267,7 @@ def test_aggregate_list_with_long(self):
263267
columns, data = self.cmd.take_action(parsed_args)
264268

265269
self.assertEqual(self.list_columns_long, columns)
266-
self.assertEqual(self.list_data_long, tuple(data))
270+
self.assertListItemEqual(self.list_data_long, tuple(data))
267271

268272

269273
class TestAggregateRemoveHost(TestAggregate):
@@ -290,7 +294,7 @@ def test_aggregate_add_host(self):
290294
self.aggregate_mock.remove_host.assert_called_once_with(
291295
self.fake_ag, parsed_args.host)
292296
self.assertEqual(self.columns, columns)
293-
self.assertEqual(self.data, data)
297+
self.assertItemEqual(self.data, data)
294298

295299

296300
class TestAggregateSet(TestAggregate):
@@ -440,13 +444,14 @@ class TestAggregateShow(TestAggregate):
440444

441445
data = (
442446
TestAggregate.fake_ag.availability_zone,
443-
TestAggregate.fake_ag.hosts,
447+
format_columns.ListColumn(TestAggregate.fake_ag.hosts),
444448
TestAggregate.fake_ag.id,
445449
TestAggregate.fake_ag.name,
446-
utils.format_dict(
447-
{key: value
448-
for key, value in TestAggregate.fake_ag.metadata.items()
449-
if key != 'availability_zone'}),
450+
format_columns.DictColumn({
451+
key: value
452+
for key, value in TestAggregate.fake_ag.metadata.items()
453+
if key != 'availability_zone'
454+
}),
450455
)
451456

452457
def setUp(self):
@@ -467,7 +472,7 @@ def test_aggregate_show(self):
467472
self.aggregate_mock.get.assert_called_once_with(parsed_args.aggregate)
468473

469474
self.assertEqual(self.columns, columns)
470-
self.assertEqual(self.data, data)
475+
self.assertItemEqual(self.data, tuple(data))
471476

472477

473478
class TestAggregateUnset(TestAggregate):

0 commit comments

Comments
 (0)