Skip to content

Commit eba33fd

Browse files
author
Fernando Ojeda
committed
Refactor hardware detail bandwidth allocation and add more unit test.
1 parent 3e0faad commit eba33fd

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

SoftLayer/CLI/hardware/detail.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ def _bw_table(bw_data):
9999
allotment = 'N/A'
100100
if bw_point['type']['alias'] == 'PUBLIC_SERVER_BW':
101101
bw_type = 'Public'
102-
if bw_data.get('allotment') is None:
102+
if not bw_data.get('allotment'):
103103
allotment = '-'
104104
else:
105105
allotment = utils.lookup(bw_data, 'allotment', 'amount')
106-
if allotment is None:
107-
allotment = '-'
108106

109107
table.add_row([bw_type, bw_point['amountIn'], bw_point['amountOut'], allotment])
110108
return table

SoftLayer/managers/hardware.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,9 @@ def get_bandwidth_allocation(self, instance_id):
698698
allotment = self.client.call('Hardware_Server', 'getBandwidthAllotmentDetail', id=instance_id, mask=a_mask)
699699
u_mask = "mask[amountIn,amountOut,type]"
700700
usage = self.client.call('Hardware_Server', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
701-
allotment_bandwidth = None
702-
if allotment is not "":
703-
allotment_bandwidth = allotment.get('allocation')
704-
return {'allotment': allotment_bandwidth, 'usage': usage}
701+
if allotment:
702+
return {'allotment': allotment.get('allocation'), 'usage': usage}
703+
return {'allotment': allotment, 'usage': usage}
705704

706705

707706
def _get_extra_price_id(items, key_name, hourly, location):

tests/CLI/modules/server_tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ def test_detail_vs_empty_tag(self):
127127
['example-tag'],
128128
)
129129

130+
def test_detail_empty_allotment(self):
131+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
132+
mock.return_value = None
133+
result = self.run_command(['server', 'detail', '100'])
134+
135+
self.assert_no_fail(result)
136+
self.assertEqual(
137+
json.loads(result.output)['Bandwidth'][0]['Allotment'],
138+
'-',
139+
)
140+
130141
def test_list_servers(self):
131142
result = self.run_command(['server', 'list', '--tag=openstack'])
132143

tests/managers/hardware_tests.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88

99
import mock
1010

11-
1211
import SoftLayer
13-
1412
from SoftLayer import fixtures
1513
from SoftLayer import managers
1614
from SoftLayer import testing
1715

18-
1916
MINIMAL_TEST_CREATE_ARGS = {
2017
'size': 'S1270_8GB_2X1TBSATA_NORAID',
2118
'hostname': 'unicorn',
@@ -448,17 +445,23 @@ def test_get_bandwidth_allocation(self):
448445
self.assertEqual(result['allotment']['amount'], '250')
449446
self.assertEqual(result['usage'][0]['amountIn'], '.448')
450447

451-
def test_get_bandwidth_allocation_no_allotment(self):
448+
def test_get_bandwidth_allocation_with_allotment(self):
452449
mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
453-
mock.return_value = {}
450+
mock.return_value = {
451+
"allocationId": 11111,
452+
"id": 22222,
453+
"allocation": {
454+
"amount": "2000"
455+
}
456+
}
454457

455458
result = self.hardware.get_bandwidth_allocation(1234)
456459

457-
self.assertEqual(None, result['allotment'])
460+
self.assertEqual(2000, int(result['allotment']['amount']))
458461

459-
def test_get_bandwidth_allocation_empty_allotment(self):
462+
def test_get_bandwidth_allocation_no_allotment(self):
460463
mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
461-
mock.return_value = ""
464+
mock.return_value = None
462465

463466
result = self.hardware.get_bandwidth_allocation(1234)
464467

0 commit comments

Comments
 (0)