Skip to content

Commit 4a03ab1

Browse files
softlayer#1093 properly send in hostId when creating a dedicated host VSI
1 parent 2bbbab1 commit 4a03ab1

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

CONTRIBUTING.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,70 @@ Docs are generated with [Sphinx](https://docs.readthedocs.io/en/latest/intro/get
3535
`make html` in the softlayer-python/docs directory, which should generate the HTML in softlayer-python/docs/_build/html for testing.
3636

3737

38+
## Unit Tests
3839

40+
All new features should be 100% code covered, and your pull request should at the very least increase total code overage.
3941

42+
### Mocks
43+
To tests results from the API, we keep mock results in SoftLayer/fixtures/<SoftLayer_Service>/ with the method name matching the variable name.
44+
45+
Any call to a service that doesn't have a fixture will result in a TransportError
46+
47+
### Overriding Fixtures
48+
49+
Adding your expected output in the fixtures file with a unique name is a good way to define a fixture that gets used frequently in a test.
50+
51+
```python
52+
from SoftLayer.fixtures import SoftLayer_Product_Package
53+
54+
def test_test(self):
55+
amock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
56+
amock.return_value = fixtures.SoftLayer_Product_Package.RESERVED_CAPACITY
57+
```
58+
59+
Otherwise defining it on the spot works too.
60+
```python
61+
def test_test(self):
62+
mock = self.set_mock('SoftLayer_Network_Storage', 'getObject')
63+
mock.return_value = {
64+
'billingItem': {'hourlyFlag': True, 'id': 449},
65+
}
66+
```
67+
68+
69+
### Call testing
70+
Testing your code to make sure it makes the correct API call is also very important.
71+
72+
The testing.TestCase class has a method call `assert_called_with` which is pretty handy here.
73+
74+
```python
75+
self.assert_called_with(
76+
'SoftLayer_Billing_Item', # Service
77+
'cancelItem', # Method
78+
args=(True, True, ''), # Args
79+
identifier=449, # Id
80+
mask=mock.ANY, # object Mask,
81+
filter=mock.ANY, # object Filter
82+
limit=0, # result Limit
83+
offset=0 # result Offset
84+
)
85+
```
86+
87+
Making sure a API was NOT called
88+
89+
```python
90+
self.assertEqual([], self.calls('SoftLayer_Account', 'getObject'))
91+
```
92+
93+
Making sure an API call has a specific arg, but you don't want to list out the entire API call (like with a place order test)
94+
95+
```python
96+
# Get the API Call signature
97+
order_call = self.calls('SoftLayer_Product_Order', 'placeOrder')
98+
99+
# Get the args property of that API call, which is a tuple, with the first entry being our data.
100+
order_args = getattr(order_call[0], 'args')[0]
101+
102+
# Test our specific argument value
103+
self.assertEqual(123, order_args['hostId'])
104+
```

SoftLayer/managers/vs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,11 @@ def order_guest(self, guest_object, test=False):
910910
if guest_object.get('userdata'):
911911
# SL_Virtual_Guest::generateOrderTemplate() doesn't respect userData, so we need to add it ourself
912912
template['virtualGuests'][0]['userData'] = [{"value": guest_object.get('userdata')}]
913-
913+
if guest_object.get('host_id'):
914+
template['hostId'] = guest_object.get('host_id')
914915
if guest_object.get('placement_id'):
915916
template['virtualGuests'][0]['placementGroupId'] = guest_object.get('placement_id')
917+
916918
if test:
917919
result = self.client.call('Product_Order', 'verifyOrder', template)
918920
else:

tests/CLI/modules/vs/vs_create_tests.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,13 @@ def test_create_with_host_id(self, confirm_mock):
294294

295295
self.assert_no_fail(result)
296296
self.assertIn('"guid": "1a2b3c-1701"', result.output)
297+
# Argument testing Example
298+
order_call = self.calls('SoftLayer_Product_Order', 'placeOrder')
299+
order_args = getattr(order_call[0], 'args')[0]
300+
self.assertEqual(123, order_args['hostId'])
301+
297302
self.assert_called_with('SoftLayer_Product_Order', 'placeOrder')
298-
args = ({
303+
template_args = ({
299304
'startCpus': 2,
300305
'maxMemory': 1024,
301306
'hostname': 'host',
@@ -319,7 +324,7 @@ def test_create_with_host_id(self, confirm_mock):
319324
]
320325
},)
321326

322-
self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args)
327+
self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=template_args)
323328

324329
@mock.patch('SoftLayer.CLI.formatting.confirm')
325330
def test_create_like(self, confirm_mock):

0 commit comments

Comments
 (0)