Skip to content

Commit 553ea9b

Browse files
committed
Moves the existing mock objects into fixtures.
There's a couple implementation details with this. I believe due to merging hardware and bmc categories, something has changed with one of the hardware tests. Literally one line isn't being covered, which probably means there's other things that are incorrect as well.
1 parent b81961b commit 553ea9b

17 files changed

Lines changed: 580 additions & 796 deletions

SoftLayer/managers/hardware.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ def cancel_hardware(self, hardware_id, reason='unneeded', comment=''):
5353
# content - Comment about the cancellation
5454
# cancelAssociatedItems
5555
# attachmentType - Only option is HARDWARE
56-
ticket_obj = self.client['Ticket']
57-
return ticket_obj.createCancelServerTicket(hardware_id, cancel_reason,
58-
comment, True, 'HARDWARE')
56+
return self.client['Ticket'].createCancelServerTicket(hardware_id,
57+
cancel_reason,
58+
comment,
59+
True,
60+
'HARDWARE')
5961

6062
def cancel_metal(self, hardware_id, immediate=False):
6163
""" Cancels the specified bare metal instance.
@@ -653,7 +655,6 @@ def _parse_package_data(self, package_id):
653655
'labor_fee': price.get('laborFee', 0),
654656
'capacity': float(price['item'].get('capacity', 0)),
655657
})
656-
657658
results['categories'][code]['items'] = items
658659

659660
return results

SoftLayer/tests/CLI/modules/bmc_tests.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
:copyright: (c) 2013, SoftLayer Technologies, Inc. All rights reserved.
99
:license: MIT, see LICENSE for more details.
1010
"""
11-
from SoftLayer.tests import unittest
12-
from mock import MagicMock, patch
11+
from SoftLayer.tests import unittest, FixtureClient
12+
from mock import patch
1313

1414
from SoftLayer.CLI.helpers import format_output, CLIAbort, ArgumentError
1515
from SoftLayer.CLI.modules import bmc
16-
from SoftLayer.tests.mocks import product_package_mock
1716

1817

1918
class BMCCLITests(unittest.TestCase):
2019
def setUp(self):
21-
self.client = self._setup_package_mocks(MagicMock())
20+
self.client = FixtureClient()
2221

2322
def test_BMCCreateOptions(self):
2423
args = {
@@ -277,12 +276,3 @@ def test_get_default_value_returns_none_for_unknown_category(self):
277276
runnable = bmc.CreateBMCInstance(client=self.client)
278277
output = runnable._get_default_value(option_mock, 'nope')
279278
self.assertEqual(None, output)
280-
281-
def _setup_package_mocks(self, client):
282-
p = client['Product_Package']
283-
p.getAllObjects = product_package_mock.getAllObjects_Mock()
284-
p.getConfiguration = product_package_mock.getConfiguration_Mock(True)
285-
p.getCategories = product_package_mock.getCategories_Mock(True)
286-
p.getRegions = product_package_mock.getRegions_Mock()
287-
288-
return client

SoftLayer/tests/CLI/modules/server_tests.py

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
:copyright: (c) 2013, SoftLayer Technologies, Inc. All rights reserved.
99
:license: MIT, see LICENSE for more details.
1010
"""
11-
from SoftLayer.tests import unittest
12-
from mock import Mock, MagicMock, patch
11+
from SoftLayer.tests import unittest, FixtureClient
12+
from mock import Mock, patch
1313
try:
1414
# Python 3.x compatibility
1515
import builtins # NOQA
@@ -19,13 +19,11 @@
1919

2020
from SoftLayer.CLI.helpers import format_output, CLIAbort, ArgumentError
2121
from SoftLayer.CLI.modules import server
22-
from SoftLayer.tests.mocks import (
23-
account_mock, hardware_mock, product_package_mock)
2422

2523

2624
class ServerCLITests(unittest.TestCase):
2725
def setUp(self):
28-
self.client = MagicMock()
26+
self.client = FixtureClient()
2927

3028
def test_ServerCancelReasons(self):
3129
runnable = server.ServerCancelReasons(client=self.client)
@@ -68,31 +66,31 @@ def test_ServerCreateOptions(self):
6866
'--controller': False,
6967
}
7068

71-
client = self._setup_package_mocks(self.client)
72-
runnable = server.ServerCreateOptions(client=client)
69+
runnable = server.ServerCreateOptions(client=self.client)
7370

7471
output = runnable.execute(args)
7572

7673
expected = {
77-
'datacenter': ['RANDOM_LOCATION'],
78-
'dual nic': ['100_DUAL', '10_DUAL'],
79-
'disk_controllers': ['None', 'RAID0'],
80-
'os (CENTOS)': ['CENTOS_6_64'],
81-
'os (DEBIAN)': ['DEBIAN_6_32'],
82-
'os (REDHAT)': ['REDHAT_6_64_6'],
83-
'os (UBUNTU)': ['UBUNTU_12_64', 'UBUNTU_12_64_MINIMAL'],
84-
'os (WIN)': ['WIN_2003-STD-R2_64', 'WIN_2008-DC-HYPERV_64',
85-
'WIN_2008-ENT_64', 'WIN_2008-STD_64'],
86-
'memory': [4, 6],
87-
'disk': ['1000_DRIVE'],
88-
'single nic': ['100', '1000'],
8974
'cpu': [
9075
{'description': 'Dual Quad Core Pancake 200 - 1.60GHz',
9176
'id': 723},
9277
{'description': 'Dual Quad Core Pancake 200 - 1.80GHz',
93-
'id': 724}
94-
],
95-
}
78+
'id': 724}],
79+
'datacenter': ['RANDOM_LOCATION'],
80+
'disk': ['250_SATA_II', '500_SATA_II'],
81+
'disk_controllers': ['None', 'RAID0'],
82+
'dual nic': ['1000_DUAL', '100_DUAL', '10_DUAL'],
83+
'memory': [4, 6],
84+
'os (CENTOS)': ['CENTOS_6_64_LAMP', 'CENTOS_6_64_MINIMAL'],
85+
'os (REDHAT)': ['REDHAT_6_64_LAMP', 'REDHAT_6_64_MINIMAL'],
86+
'os (UBUNTU)': ['UBUNTU_12_64_LAMP', 'UBUNTU_12_64_MINIMAL'],
87+
'os (WIN)': [
88+
'WIN_2008-DC_64',
89+
'WIN_2008-ENT_64',
90+
'WIN_2008-STD-R2_64',
91+
'WIN_2008-STD_64',
92+
'WIN_2012-DC-HYPERV_64'],
93+
'single nic': ['100', '1000']}
9694

9795
self.assertEqual(expected, format_output(output, 'python'))
9896

@@ -109,8 +107,7 @@ def test_ServerCreateOptions_with_cpu_only(self):
109107
'--controller': False,
110108
}
111109

112-
client = self._setup_package_mocks(self.client)
113-
runnable = server.ServerCreateOptions(client=client)
110+
runnable = server.ServerCreateOptions(client=self.client)
114111

115112
output = runnable.execute(args)
116113

@@ -128,13 +125,7 @@ def test_ServerCreateOptions_with_cpu_only(self):
128125
def test_ServerDetails(self):
129126
hw_id = 1234
130127

131-
client = Mock()
132-
client.__getitem__ = Mock()
133-
service = client['Hardware_Server']
134-
service.getObject = hardware_mock.getObject_Mock(1000)
135-
dns_mock = hardware_mock.getReverseDomainRecords_Mock(1000)
136-
service.getReverseDomainRecords = dns_mock
137-
runnable = server.ServerDetails(client=client)
128+
runnable = server.ServerDetails(client=self.client)
138129

139130
args = {'<identifier>': hw_id, '--passwords': True, '--price': True}
140131
output = runnable.execute(args)
@@ -163,7 +154,6 @@ def test_ServerDetails(self):
163154
self.assertEqual(expected, format_output(output, 'python'))
164155

165156
def test_ListServers(self):
166-
self.client['Account'].getHardware = account_mock.getHardware_Mock()
167157
runnable = server.ListServers(client=self.client)
168158

169159
output = runnable.execute({'--tags': 'openstack'})
@@ -375,7 +365,7 @@ def test_CreateServer(self):
375365
'--datacenter': 'TEST00',
376366
'--cpu': False,
377367
'--network': '100',
378-
'--disk': ['1000_DRIVE', '1000_DRIVE'],
368+
'--disk': ['250_SATA_II', '250_SATA_II'],
379369
'--os': 'UBUNTU_12_64_MINIMAL',
380370
'--memory': False,
381371
'--controller': False,
@@ -387,8 +377,7 @@ def test_CreateServer(self):
387377
'--vlan_private': 20468,
388378
}
389379

390-
client = self._setup_package_mocks(self.client)
391-
runnable = server.CreateServer(client=client)
380+
runnable = server.CreateServer(client=self.client)
392381

393382
# First, test the --test flag
394383
with patch('SoftLayer.HardwareManager.verify_order') as verify_mock:
@@ -466,7 +455,6 @@ def test_CreateServer(self):
466455
self.assertRaises(CLIAbort, runnable.execute, args)
467456

468457
def test_CreateServer_failures(self):
469-
client = self._setup_package_mocks(self.client)
470458

471459
# This is missing a required argument
472460
args = {
@@ -483,7 +471,7 @@ def test_CreateServer_failures(self):
483471
'--template': None,
484472
}
485473

486-
runnable = server.CreateServer(client=client)
474+
runnable = server.CreateServer(client=self.client)
487475

488476
# Verify that ArgumentError is properly raised on error
489477
self.assertRaises(ArgumentError, runnable.execute, args)
@@ -521,8 +509,7 @@ def test_CreateServer_with_export(self, export_to_template):
521509
'--export': 'test_file.txt',
522510
}
523511

524-
client = self._setup_package_mocks(self.client)
525-
runnable = server.CreateServer(client=client)
512+
runnable = server.CreateServer(client=self.client)
526513

527514
expected = args.copy()
528515
del(expected['--export'])
@@ -605,12 +592,3 @@ def test_get_default_value_returns_none_for_unknown_category(self):
605592
runnable = server.CreateServer()
606593
output = runnable._get_default_value(option_mock, 'nope')
607594
self.assertEqual(None, output)
608-
609-
def _setup_package_mocks(self, client):
610-
package = client['Product_Package']
611-
package.getAllObjects = product_package_mock.getAllObjects_Mock()
612-
package.getConfiguration = product_package_mock.getConfiguration_Mock()
613-
package.getCategories = product_package_mock.getCategories_Mock()
614-
package.getRegions = product_package_mock.getRegions_Mock()
615-
616-
return client

SoftLayer/tests/fixture_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def reset_mock(self):
3131
class FixtureService(object):
3232

3333
def __init__(self, name):
34+
self.name = name
3435
try:
3536
self.module = import_module('SoftLayer.tests.fixtures.%s' % name)
3637
except ImportError:
@@ -40,12 +41,12 @@ def __init__(self, name):
4041
self.loaded_methods = {}
4142

4243
def __getattr__(self, name):
43-
if self.loaded_methods.get(name):
44+
if name in self.loaded_methods:
4445
return self.loaded_methods[name]
4546

4647
call_handler = MagicMock()
4748
fixture = getattr(self.module, name, None)
48-
if fixture:
49+
if fixture is not None:
4950
call_handler.return_value = fixture
5051

5152
self.loaded_methods[name] = call_handler

SoftLayer/tests/fixtures/Account.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,128 @@
1616
'name': 'private_image2',
1717
'parentId': ''
1818
}]
19+
20+
getVirtualGuests = [{
21+
'id': 100,
22+
'hostname': 'cci-test1',
23+
'domain': 'test.sftlyr.ws',
24+
'fullyQualifiedDomainName': 'cci-test1.test.sftlyr.ws',
25+
'status': {'keyName': 'ACTIVE', 'name': 'Active'},
26+
# TODO - This needs to come from wherever data centers come from
27+
'datacenter': {'id': 50, 'name': 'TEST00',
28+
'description': 'Test Data Center'},
29+
'powerState': {'keyName': 'RUNNING', 'name': 'Running'},
30+
'maxCpu': 2,
31+
'maxMemory': 1024,
32+
'primaryIpAddress': '172.16.240.2',
33+
'globalIdentifier': '1a2b3c-1701',
34+
'primaryBackendIpAddress': '10.45.19.37',
35+
'hourlyBillingFlag': False,
36+
}, {
37+
'id': 104,
38+
'hostname': 'cci-test2',
39+
'domain': 'test.sftlyr.ws',
40+
'fullyQualifiedDomainName': 'cci-test2.test.sftlyr.ws',
41+
'status': {'keyName': 'ACTIVE', 'name': 'Active'},
42+
# TODO - This needs to come from wherever data centers come from
43+
'datacenter': {'id': 50, 'name': 'TEST00',
44+
'description': 'Test Data Center'},
45+
'powerState': {'keyName': 'RUNNING', 'name': 'Running'},
46+
'maxCpu': 4,
47+
'maxMemory': 4096,
48+
'primaryIpAddress': '172.16.240.7',
49+
'globalIdentifier': '05a8ac-6abf0',
50+
'primaryBackendIpAddress': '10.45.19.35',
51+
'hourlyBillingFlag': True,
52+
}]
53+
54+
getMonthlyVirtualGuests = [cci for cci in getVirtualGuests
55+
if not cci['hourlyBillingFlag']]
56+
getHourlyVirtualGuests = [cci for cci in getVirtualGuests
57+
if cci['hourlyBillingFlag']]
58+
59+
60+
getHardware = [{
61+
'id': 1000,
62+
'datacenter': {'id': 50, 'name': 'TEST00',
63+
'description': 'Test Data Center'},
64+
'billingItem': {'id': 6327, 'recurringFee': 1.54},
65+
'primaryIpAddress': '172.16.1.100',
66+
'hostname': 'hardware-test1',
67+
'domain': 'test.sftlyr.ws',
68+
'fullyQualifiedDomainName': 'hardware-test1.test.sftlyr.ws',
69+
'processorPhysicalCoreAmount': 2,
70+
'memoryCapacity': 2,
71+
'primaryBackendIpAddress': '10.1.0.2',
72+
'networkManagementIpAddress': '10.1.0.3',
73+
'hardwareStatus': {'status': 'ACTIVE'},
74+
'provisionDate': '2013-08-01 15:23:45',
75+
'notes': 'These are test notes.',
76+
'operatingSystem': {
77+
'softwareLicense': {
78+
'softwareDescription': {
79+
'referenceCode': 'Ubuntu',
80+
'name': 'Ubuntu 12.04 LTS',
81+
}
82+
},
83+
'passwords': [
84+
{'username': 'root', 'password': 'abc123'}
85+
],
86+
},
87+
'networkVlans': [
88+
{
89+
'networkSpace': 'PRIVATE',
90+
'vlanNumber': 1800,
91+
'id': 9653
92+
},
93+
{
94+
'networkSpace': 'PUBLIC',
95+
'vlanNumber': 3672,
96+
'id': 19082
97+
},
98+
],
99+
'tagReferences': [
100+
{'tag': {'name': 'test_tag'}}
101+
],
102+
'activeTransaction': {
103+
'transactionStatus': {
104+
'name': 'TXN_NAME',
105+
'friendlyName': 'Friendly Transaction Name',
106+
'id': 6660
107+
}
108+
}
109+
}, {
110+
'id': 1001,
111+
'datacenter': {'name': 'TEST00',
112+
'description': 'Test Data Center'},
113+
'billingItem': {'id': 7112},
114+
'primaryIpAddress': '172.16.4.94',
115+
'hostname': 'hardware-test2',
116+
'domain': 'test.sftlyr.ws',
117+
'fullyQualifiedDomainName': 'hardware-test2.test.sftlyr.ws',
118+
'processorPhysicalCoreAmount': 4,
119+
'memoryCapacity': 4,
120+
'primaryBackendIpAddress': '10.1.0.3',
121+
'hardwareStatus': {'status': 'ACTIVE'},
122+
'provisionDate': '2013-08-03 07:15:22',
123+
'operatingSystem': {
124+
'softwareLicense': {
125+
'softwareDescription': {
126+
'referenceCode': 'Ubuntu',
127+
'name': 'Ubuntu 12.04 LTS',
128+
}
129+
}
130+
},
131+
'networkVlans': [
132+
{
133+
'networkSpace': 'PRIVATE',
134+
'vlanNumber': 1800,
135+
'id': 9653
136+
},
137+
{
138+
'networkSpace': 'PUBLIC',
139+
'vlanNumber': 3672,
140+
'id': 19082
141+
},
142+
]
143+
}]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cancelService = {}
2+
cancelServiceOnAnniversaryDate = {}

0 commit comments

Comments
 (0)