Skip to content

Commit 6ccaa27

Browse files
finishing up unit tests
1 parent 861ffab commit 6ccaa27

File tree

6 files changed

+281
-38
lines changed

6 files changed

+281
-38
lines changed

SoftLayer/CLI/hardware/bandwidth.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,3 @@ def cli(env, identifier, start_date, end_date, summary_period, quite_summary):
4242
env.fout(sum_table)
4343
if not quite_summary:
4444
env.fout(table)
45-
46-
47-
def mb_to_gb(mbytes):
48-
"""Converts a MegaByte int to GigaByte. mbytes/2^10"""
49-
return round(mbytes / 2 ** 10, 4)

tests/CLI/modules/server_tests.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,6 @@ def test_bandwidth_hw(self):
591591

592592
# Since this is 2 tables, it gets returned as invalid json like "[{}][{}]"" instead of "[[{}],[{}]]"
593593
# so we just do some hacky string substitution to pull out the respective arrays that can be jsonifyied
594-
from pprint import pprint as pp
595-
pp(result.output)
596-
print("FUCK")
597-
pp(result.output[0:-157])
598594
output_summary = json.loads(result.output[0:-157])
599595
output_list = json.loads(result.output[-158:])
600596

tests/CLI/modules/vs/vs_tests.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -689,12 +689,6 @@ def test_bandwidth_vs(self):
689689

690690
# Since this is 2 tables, it gets returned as invalid json like "[{}][{}]"" instead of "[[{}],[{}]]"
691691
# so we just do some hacky string substitution to pull out the respective arrays that can be jsonifyied
692-
693-
from pprint import pprint as pp
694-
pp(result.output)
695-
print("FUCK")
696-
pp(result.output[0:-157])
697-
698692
output_summary = json.loads(result.output[0:-157])
699693
output_list = json.loads(result.output[-158:])
700694

tests/managers/hardware_tests.py

Lines changed: 161 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
import SoftLayer
13+
1314
from SoftLayer import fixtures
1415
from SoftLayer import managers
1516
from SoftLayer import testing
@@ -320,6 +321,14 @@ def test_cancel_hardware_monthly_whenever(self):
320321
self.assert_called_with('SoftLayer_Billing_Item', 'cancelItem',
321322
identifier=6327, args=(False, False, 'No longer needed', ''))
322323

324+
def test_cancel_running_transaction(self):
325+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject')
326+
mock.return_value = {'id': 987, 'billingItem': {'id': 6327},
327+
'activeTransaction': {'id': 4567}}
328+
self.assertRaises(SoftLayer.SoftLayerError,
329+
self.hardware.cancel_hardware,
330+
12345)
331+
323332
def test_change_port_speed_public(self):
324333
self.hardware.change_port_speed(2, True, 100)
325334

@@ -410,14 +419,102 @@ def test_reflash_firmware_selective(self):
410419
'createFirmwareReflashTransaction',
411420
identifier=100, args=(1, 0, 0))
412421

422+
def test_get_tracking_id(self):
423+
result = self.hardware.get_tracking_id(1234)
424+
self.assert_called_with('SoftLayer_Hardware_Server', 'getMetricTrackingObjectId')
425+
self.assertEqual(result, 1000)
426+
427+
def test_get_bandwidth_data(self):
428+
result = self.hardware.get_bandwidth_data(1234, '2019-01-01', '2019-02-01', 'public', 1000)
429+
self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getBandwidthData', args=('2019-01-01', '2019-02-01',
430+
'public', 1000), identifier=1000)
431+
self.assertEqual(result[0]['type'], 'cpu0')
432+
433+
def test_get_bandwidth_allocation(self):
434+
result = self.hardware.get_bandwidth_allocation(1234)
435+
self.assert_called_with('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail', identifier=1234)
436+
self.assert_called_with('SoftLayer_Hardware_Server', 'getBillingCycleBandwidthUsage', identifier=1234)
437+
self.assertEqual(result['allotment']['amount'], '250')
438+
self.assertEqual(result['useage'][0]['amountIn'], '.448')
439+
413440

414441
class HardwareHelperTests(testing.TestCase):
415442
def test_get_extra_price_id_no_items(self):
416443
ex = self.assertRaises(SoftLayer.SoftLayerError,
417444
managers.hardware._get_extra_price_id,
418445
[], 'test', True, None)
419-
self.assertEqual("Could not find valid price for extra option, 'test'",
420-
str(ex))
446+
self.assertEqual("Could not find valid price for extra option, 'test'", str(ex))
447+
448+
def test_get_extra_price_mismatched(self):
449+
items = [
450+
{'keyName': 'TEST', 'prices':[{'id':1, 'locationGroupId': None, 'recurringFee':99}]},
451+
{'keyName': 'TEST', 'prices':[{'id':2, 'locationGroupId': 55, 'hourlyRecurringFee':99}]},
452+
{'keyName': 'TEST', 'prices':[{'id':3, 'locationGroupId': None, 'hourlyRecurringFee':99}]},
453+
]
454+
location = {
455+
'location': {
456+
'location': {
457+
'priceGroups': [
458+
{'id': 50},
459+
{'id': 51}
460+
]
461+
}
462+
}
463+
}
464+
result = managers.hardware._get_extra_price_id(items, 'TEST', True, location)
465+
self.assertEqual(3, result)
466+
467+
def test_get_bandwidth_price_mismatched(self):
468+
items = [
469+
{'itemCategory': {'categoryCode':'bandwidth'},
470+
'capacity': 100,
471+
'prices':[{'id':1, 'locationGroupId': None, 'hourlyRecurringFee':99}]
472+
},
473+
{'itemCategory': {'categoryCode':'bandwidth'},
474+
'capacity': 100,
475+
'prices':[{'id':2, 'locationGroupId': 55, 'recurringFee':99}]
476+
},
477+
{'itemCategory': {'categoryCode':'bandwidth'},
478+
'capacity': 100,
479+
'prices':[{'id':3, 'locationGroupId': None, 'recurringFee':99}]
480+
},
481+
]
482+
location = {
483+
'location': {
484+
'location': {
485+
'priceGroups': [
486+
{'id': 50},
487+
{'id': 51}
488+
]
489+
}
490+
}
491+
}
492+
result = managers.hardware._get_bandwidth_price_id(items, False, False, location)
493+
self.assertEqual(3, result)
494+
495+
def test_get_os_price_mismatched(self):
496+
items = [
497+
{'itemCategory': {'categoryCode':'os'},
498+
'softwareDescription': {'referenceCode': 'TEST_OS'},
499+
'prices':[{'id':2, 'locationGroupId': 55, 'recurringFee':99}]
500+
},
501+
{'itemCategory': {'categoryCode':'os'},
502+
'softwareDescription': {'referenceCode': 'TEST_OS'},
503+
'prices':[{'id':3, 'locationGroupId': None, 'recurringFee':99}]
504+
},
505+
]
506+
location = {
507+
'location': {
508+
'location': {
509+
'priceGroups': [
510+
{'id': 50},
511+
{'id': 51}
512+
]
513+
}
514+
}
515+
}
516+
result = managers.hardware._get_os_price_id(items, 'TEST_OS', location)
517+
self.assertEqual(3, result)
421518

422519
def test_get_default_price_id_item_not_first(self):
423520
items = [{
@@ -432,33 +529,85 @@ def test_get_default_price_id_item_not_first(self):
432529
ex = self.assertRaises(SoftLayer.SoftLayerError,
433530
managers.hardware._get_default_price_id,
434531
items, 'unknown', True, None)
435-
self.assertEqual("Could not find valid price for 'unknown' option",
436-
str(ex))
532+
self.assertEqual("Could not find valid price for 'unknown' option", str(ex))
437533

438534
def test_get_default_price_id_no_items(self):
439535
ex = self.assertRaises(SoftLayer.SoftLayerError,
440536
managers.hardware._get_default_price_id,
441537
[], 'test', True, None)
442-
self.assertEqual("Could not find valid price for 'test' option",
443-
str(ex))
538+
self.assertEqual("Could not find valid price for 'test' option", str(ex))
444539

445540
def test_get_bandwidth_price_id_no_items(self):
446541
ex = self.assertRaises(SoftLayer.SoftLayerError,
447542
managers.hardware._get_bandwidth_price_id,
448543
[], hourly=True, no_public=False)
449-
self.assertEqual("Could not find valid price for bandwidth option",
450-
str(ex))
544+
self.assertEqual("Could not find valid price for bandwidth option", str(ex))
451545

452546
def test_get_os_price_id_no_items(self):
453547
ex = self.assertRaises(SoftLayer.SoftLayerError,
454548
managers.hardware._get_os_price_id,
455549
[], 'UBUNTU_14_64', None)
456-
self.assertEqual("Could not find valid price for os: 'UBUNTU_14_64'",
457-
str(ex))
550+
self.assertEqual("Could not find valid price for os: 'UBUNTU_14_64'", str(ex))
458551

459552
def test_get_port_speed_price_id_no_items(self):
460553
ex = self.assertRaises(SoftLayer.SoftLayerError,
461554
managers.hardware._get_port_speed_price_id,
462555
[], 10, True, None)
463-
self.assertEqual("Could not find valid price for port speed: '10'",
464-
str(ex))
556+
self.assertEqual("Could not find valid price for port speed: '10'", str(ex))
557+
558+
def test_get_port_speed_price_id_mismatch(self):
559+
items = [
560+
{'itemCategory': {'categoryCode':'port_speed'},
561+
'capacity':101,
562+
'attributes':[{'attributeTypeKeyName': 'IS_PRIVATE_NETWORK_ONLY'}],
563+
'prices':[{'id':1, 'locationGroupId': None, 'recurringFee':99}]
564+
},
565+
{'itemCategory': {'categoryCode':'port_speed'},
566+
'capacity':100,
567+
'attributes':[{'attributeTypeKeyName': 'IS_NOT_PRIVATE_NETWORK_ONLY'}],
568+
'prices':[{'id':2, 'locationGroupId': 55, 'recurringFee':99}]
569+
},
570+
{'itemCategory': {'categoryCode':'port_speed'},
571+
'capacity':100,
572+
'attributes':[{'attributeTypeKeyName': 'IS_PRIVATE_NETWORK_ONLY'}, {'attributeTypeKeyName': 'NON_LACP'}],
573+
'prices':[{'id':3, 'locationGroupId': 55, 'recurringFee':99}]
574+
},
575+
{'itemCategory': {'categoryCode':'port_speed'},
576+
'capacity':100,
577+
'attributes':[{'attributeTypeKeyName': 'IS_PRIVATE_NETWORK_ONLY'}],
578+
'prices':[{'id':4, 'locationGroupId': 12, 'recurringFee':99}]
579+
},
580+
{'itemCategory': {'categoryCode':'port_speed'},
581+
'capacity':100,
582+
'attributes':[{'attributeTypeKeyName': 'IS_PRIVATE_NETWORK_ONLY'}],
583+
'prices':[{'id':5, 'locationGroupId': None, 'recurringFee':99}]
584+
},
585+
]
586+
location = {
587+
'location': {
588+
'location': {
589+
'priceGroups': [
590+
{'id': 50},
591+
{'id': 51}
592+
]
593+
}
594+
}
595+
}
596+
result = managers.hardware._get_port_speed_price_id(items, 100, True, location)
597+
self.assertEqual(5, result)
598+
599+
def test_matches_location(self):
600+
price = {'id':1, 'locationGroupId': 51, 'recurringFee':99}
601+
location = {
602+
'location': {
603+
'location': {
604+
'priceGroups': [
605+
{'id': 50},
606+
{'id': 51}
607+
]
608+
}
609+
}
610+
}
611+
result = managers.hardware._matches_location(price, location)
612+
self.assertTrue(result)
613+

tests/managers/vs/vs_order_tests.py

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mock
1010

1111
import SoftLayer
12+
from SoftLayer import exceptions
1213
from SoftLayer import fixtures
1314
from SoftLayer import testing
1415

@@ -45,16 +46,11 @@ def test_upgrade_blank(self):
4546
result = self.vs.upgrade(1)
4647

4748
self.assertEqual(result, False)
48-
self.assertEqual(self.calls('SoftLayer_Product_Order', 'placeOrder'),
49-
[])
49+
self.assertEqual(self.calls('SoftLayer_Product_Order', 'placeOrder'), [])
5050

5151
def test_upgrade_full(self):
5252
# Testing all parameters Upgrade
53-
result = self.vs.upgrade(1,
54-
cpus=4,
55-
memory=2,
56-
nic_speed=1000,
57-
public=True)
53+
result = self.vs.upgrade(1, cpus=4, memory=2, nic_speed=1000, public=True)
5854

5955
self.assertEqual(result, True)
6056
self.assert_called_with('SoftLayer_Product_Order', 'placeOrder')
@@ -67,10 +63,7 @@ def test_upgrade_full(self):
6763

6864
def test_upgrade_with_flavor(self):
6965
# Testing Upgrade with parameter preset
70-
result = self.vs.upgrade(1,
71-
preset="M1_64X512X100",
72-
nic_speed=1000,
73-
public=True)
66+
result = self.vs.upgrade(1, preset="M1_64X512X100", nic_speed=1000, public=True)
7467

7568
self.assertEqual(result, True)
7669
self.assert_called_with('SoftLayer_Product_Order', 'placeOrder')
@@ -141,6 +134,42 @@ def test_get_price_id_for_upgrade_finds_memory_price(self):
141134
value='1000')
142135
self.assertEqual(1122, price_id)
143136

137+
def test__get_price_id_for_upgrade_find_private_price(self):
138+
package_items = self.vs._get_package_items()
139+
price_id = self.vs._get_price_id_for_upgrade(package_items=package_items,
140+
option='cpus',
141+
value='4',
142+
public=False)
143+
self.assertEqual(1007, price_id)
144+
145+
def test_upgrade_mem_and_preset_exception(self):
146+
self.assertRaises(
147+
ValueError,
148+
self.vs.upgrade,
149+
1234,
150+
memory=10,
151+
preset="M1_64X512X100"
152+
)
153+
154+
def test_upgrade_cpu_and_preset_exception(self):
155+
self.assertRaises(
156+
ValueError,
157+
self.vs.upgrade,
158+
1234,
159+
cpus=10,
160+
preset="M1_64X512X100"
161+
)
162+
163+
@mock.patch('SoftLayer.managers.vs.VSManager._get_price_id_for_upgrade_option')
164+
def test_upgrade_no_price_exception(self, get_price):
165+
get_price.return_value = None
166+
self.assertRaises(
167+
exceptions.SoftLayerError,
168+
self.vs.upgrade,
169+
1234,
170+
memory=1,
171+
)
172+
144173
@mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict')
145174
def test_order_guest(self, create_dict):
146175
create_dict.return_value = {'test': 1, 'verify': 1}
@@ -173,3 +202,42 @@ def test_order_guest_ipv6(self, create_dict):
173202
self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate')
174203
self.assert_called_with('SoftLayer_Product_Package', 'getItems', identifier=200)
175204
self.assert_called_with('SoftLayer_Product_Order', 'verifyOrder')
205+
206+
@mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict')
207+
def test_order_guest_placement_group(self, create_dict):
208+
create_dict.return_value = {'test': 1, 'verify': 1}
209+
guest = {'test': 1, 'verify': 1, 'placement_id': 5}
210+
result = self.vs.order_guest(guest, test=True)
211+
212+
call = self.calls('SoftLayer_Product_Order', 'verifyOrder')[0]
213+
order_container = call.args[0]
214+
215+
self.assertEqual(1234, result['orderId'])
216+
self.assertEqual(5, order_container['virtualGuests'][0]['placementGroupId'])
217+
self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate')
218+
self.assert_called_with('SoftLayer_Product_Order', 'verifyOrder')
219+
220+
def test_get_price_id_empty(self):
221+
upgrade_prices = [
222+
{'categories': None, 'item': None},
223+
{'categories': [{'categoryCode': 'ram'}], 'item': None},
224+
{'categories': None, 'item': {'capacity':1}},
225+
]
226+
result = self.vs._get_price_id_for_upgrade_option(upgrade_prices,'memory',1)
227+
self.assertEqual(None,result)
228+
229+
def test_get_price_id_memory_capacity(self):
230+
upgrade_prices = [
231+
{'categories': [{'categoryCode': 'ram'}], 'item': {'capacity':1},'id':99}
232+
]
233+
result = self.vs._get_price_id_for_upgrade_option(upgrade_prices,'memory',1)
234+
self.assertEqual(99,result)
235+
236+
def test_get_price_id_mismatch_capacity(self):
237+
upgrade_prices = [
238+
{'categories': [{'categoryCode': 'ram1'}], 'item': {'capacity':1},'id':90},
239+
{'categories': [{'categoryCode': 'ram'}], 'item': {'capacity':2},'id':91},
240+
{'categories': [{'categoryCode': 'ram'}], 'item': {'capacity':1},'id':92},
241+
]
242+
result = self.vs._get_price_id_for_upgrade_option(upgrade_prices,'memory',1)
243+
self.assertEqual(92,result)

0 commit comments

Comments
 (0)