|
| 1 | +""" |
| 2 | + SoftLayer.tests.CLI.modules.bmc_tests |
| 3 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | + This is a series of integration tests designed to test the complete |
| 6 | + command line interface. |
| 7 | +
|
| 8 | + :copyright: (c) 2013, SoftLayer Technologies, Inc. All rights reserved. |
| 9 | + :license: BSD, see LICENSE for more details. |
| 10 | +""" |
| 11 | +from SoftLayer.tests import unittest |
| 12 | +from mock import MagicMock, patch |
| 13 | + |
| 14 | +from SoftLayer.CLI.helpers import format_output, CLIAbort, ArgumentError |
| 15 | +from SoftLayer.CLI.modules import bmc |
| 16 | +from SoftLayer.tests.mocks import product_package_mock |
| 17 | + |
| 18 | + |
| 19 | +class BMCCLITests(unittest.TestCase): |
| 20 | + def setUp(self): |
| 21 | + self.client = MagicMock() |
| 22 | + |
| 23 | + def test_BMCCreateOptions(self): |
| 24 | + args = { |
| 25 | + '--all': True, |
| 26 | + '--datacenter': False, |
| 27 | + '--cpu': False, |
| 28 | + '--nic': False, |
| 29 | + '--disk': False, |
| 30 | + '--os': False, |
| 31 | + '--memory': False, |
| 32 | + '--controller': False, |
| 33 | + } |
| 34 | + |
| 35 | + client = self._setup_package_mocks(self.client) |
| 36 | + |
| 37 | + output = bmc.BMCCreateOptions.execute(client, args) |
| 38 | + |
| 39 | + expected = { |
| 40 | + 'datacenter': ['RANDOM_LOCATION'], |
| 41 | + 'dual nic': ['1000_DUAL', '100_DUAL', '10_DUAL'], |
| 42 | + 'os (CENTOS)': ['CENTOS_6_64_LAMP', 'CENTOS_6_64_MINIMAL'], |
| 43 | + 'os (REDHAT)': ['REDHAT_6_64_LAMP', 'REDHAT_6_64_MINIMAL'], |
| 44 | + 'os (UBUNTU)': ['UBUNTU_12_64_LAMP', 'UBUNTU_12_64_MINIMAL'], |
| 45 | + 'os (WIN)': ['WIN_2008-DC_64', 'WIN_2008-ENT_64', |
| 46 | + 'WIN_2008-STD-R2_64', 'WIN_2008-STD_64', |
| 47 | + 'WIN_2012-DC-HYPERV_64'], |
| 48 | + 'disks': [250, 500], |
| 49 | + 'single nic': [100, 1000], |
| 50 | + 'memory/cpu': [ |
| 51 | + {'cpu': ['2'], 'memory': '2'}, |
| 52 | + {'cpu': ['4'], 'memory': '4'} |
| 53 | + ], |
| 54 | + } |
| 55 | + |
| 56 | + self.assertEqual(expected, format_output(output, 'python')) |
| 57 | + |
| 58 | + # Check get_create_options() with invalid input |
| 59 | + self.assertEqual([], bmc.BMCCreateOptions.get_create_options([], |
| 60 | + 'nope')) |
| 61 | + |
| 62 | + def test_BMCCreateOptions_with_cpu_only(self): |
| 63 | + args = { |
| 64 | + '--all': False, |
| 65 | + '--datacenter': False, |
| 66 | + '--cpu': True, |
| 67 | + '--nic': False, |
| 68 | + '--disk': False, |
| 69 | + '--os': False, |
| 70 | + '--memory': False, |
| 71 | + '--controller': False, |
| 72 | + } |
| 73 | + |
| 74 | + client = self._setup_package_mocks(self.client) |
| 75 | + |
| 76 | + output = bmc.BMCCreateOptions.execute(client, args) |
| 77 | + |
| 78 | + expected = { |
| 79 | + 'memory/cpu': [ |
| 80 | + {'cpu': ['2'], 'memory': '2'}, |
| 81 | + {'cpu': ['4'], 'memory': '4'} |
| 82 | + ], |
| 83 | + } |
| 84 | + |
| 85 | + self.assertEqual(expected, format_output(output, 'python')) |
| 86 | + |
| 87 | + def test_CreateBMCInstance(self): |
| 88 | + args = { |
| 89 | + '--hostname': 'test', |
| 90 | + '--domain': 'example.com', |
| 91 | + '--datacenter': 'TEST00', |
| 92 | + '--cpu': '2', |
| 93 | + '--network': 100, |
| 94 | + '--disk': [250, 250], |
| 95 | + '--os': 'UBUNTU_12_64_MINIMAL', |
| 96 | + '--memory': '2', |
| 97 | + '--controller': False, |
| 98 | + '--test': True, |
| 99 | + '--export': None, |
| 100 | + '--template': None, |
| 101 | + '--hourly': True, |
| 102 | + '--monthly': False, |
| 103 | + } |
| 104 | + |
| 105 | + client = self._setup_package_mocks(self.client) |
| 106 | + |
| 107 | + # First, test the --test flag |
| 108 | + with patch('SoftLayer.HardwareManager.verify_order') as verify_mock: |
| 109 | + verify_mock.return_value = { |
| 110 | + 'prices': [ |
| 111 | + { |
| 112 | + 'hourlyRecurringFee': 0.0, |
| 113 | + 'recurringFee': 0.0, |
| 114 | + 'setupFee': 0.0, |
| 115 | + 'item': {'description': 'First Item'}, |
| 116 | + }, |
| 117 | + { |
| 118 | + 'hourlyRecurringFee': 0.50, |
| 119 | + 'recurringFee': 25.0, |
| 120 | + 'setupFee': 0.0, |
| 121 | + 'item': {'description': 'Second Item'}, |
| 122 | + } |
| 123 | + ] |
| 124 | + } |
| 125 | + output = bmc.CreateBMCInstance.execute(client, args) |
| 126 | + |
| 127 | + expected = """:...................:......: |
| 128 | +: Item : cost : |
| 129 | +:...................:......: |
| 130 | +: First Item : 0.00 : |
| 131 | +: Second Item : 0.50 : |
| 132 | +: Total hourly cost : 0.50 : |
| 133 | +:...................:......: |
| 134 | + -- ! Prices reflected here are retail and do not take account level discounts and are not guaranteed.""" |
| 135 | + |
| 136 | + self.assertEqual(expected, format_output(output, 'table')) |
| 137 | + |
| 138 | + args['--hourly'] = False |
| 139 | + args['--monthly'] = True |
| 140 | + |
| 141 | + output = bmc.CreateBMCInstance.execute(client, args) |
| 142 | + |
| 143 | + expected = """:....................:.......: |
| 144 | +: Item : cost : |
| 145 | +:....................:.......: |
| 146 | +: First Item : 0.00 : |
| 147 | +: Second Item : 25.00 : |
| 148 | +: Total monthly cost : 25.00 : |
| 149 | +:....................:.......: |
| 150 | + -- ! Prices reflected here are retail and do not take account level discounts and are not guaranteed.""" |
| 151 | + |
| 152 | + self.assertEqual(expected, format_output(output, 'table')) |
| 153 | + |
| 154 | + # Make sure we can order without specifying the disk as well |
| 155 | + args['--disk'] = [] |
| 156 | + |
| 157 | + output = bmc.CreateBMCInstance.execute(client, args) |
| 158 | + |
| 159 | + self.assertEqual(expected, format_output(output, 'table')) |
| 160 | + |
| 161 | + # And make sure we can pass in disk as a comma separated string, |
| 162 | + # which is what templates do |
| 163 | + args['--disk'] = '1000_DRIVE,1000_DRIVE' |
| 164 | + |
| 165 | + output = bmc.CreateBMCInstance.execute(client, args) |
| 166 | + |
| 167 | + self.assertEqual(expected, format_output(output, 'table')) |
| 168 | + |
| 169 | + # Test explicitly setting a RAID configuration |
| 170 | + args['--controller'] = 'RAID0' |
| 171 | + |
| 172 | + output = bmc.CreateBMCInstance.execute(client, args) |
| 173 | + |
| 174 | + self.assertEqual(expected, format_output(output, 'table')) |
| 175 | + |
| 176 | + # Now test ordering |
| 177 | + with patch('SoftLayer.HardwareManager.place_order') as order_mock: |
| 178 | + order_mock.return_value = { |
| 179 | + 'orderId': 98765, |
| 180 | + 'orderDate': '2013-08-02 15:23:47' |
| 181 | + } |
| 182 | + |
| 183 | + args['--test'] = False |
| 184 | + args['--really'] = True |
| 185 | + |
| 186 | + output = bmc.CreateBMCInstance.execute(self.client, args) |
| 187 | + |
| 188 | + expected = {'id': 98765, 'created': '2013-08-02 15:23:47'} |
| 189 | + self.assertEqual(expected, format_output(output, 'python')) |
| 190 | + |
| 191 | + # Finally, test cancelling the process |
| 192 | + with patch('SoftLayer.CLI.modules.bmc.confirm') as confirm: |
| 193 | + confirm.return_value = False |
| 194 | + |
| 195 | + args['--really'] = False |
| 196 | + |
| 197 | + self.assertRaises(CLIAbort, |
| 198 | + bmc.CreateBMCInstance.execute, self.client, args) |
| 199 | + |
| 200 | + def test_CreateBMCInstance_failures(self): |
| 201 | + client = self._setup_package_mocks(self.client) |
| 202 | + |
| 203 | + # This is missing a required argument |
| 204 | + args = { |
| 205 | + '--domain': 'example.com', |
| 206 | + '--datacenter': 'TEST00', |
| 207 | + '--cpu': '2', |
| 208 | + '--network': 100, |
| 209 | + '--disk': [250, 250], |
| 210 | + '--os': 'UBUNTU_12_64_MINIMAL', |
| 211 | + '--memory': '2', |
| 212 | + '--controller': False, |
| 213 | + '--test': True, |
| 214 | + '--export': None, |
| 215 | + '--template': None, |
| 216 | + '--hourly': '0', |
| 217 | + '--monthly': '0', |
| 218 | + } |
| 219 | + |
| 220 | + # Verify that ArgumentError is properly raised on error |
| 221 | + self.assertRaises(ArgumentError, |
| 222 | + bmc.CreateBMCInstance.execute, client, args) |
| 223 | + |
| 224 | + # Sending strange values for hourly and monthly |
| 225 | + args['--hostname'] = 'bmc-test' |
| 226 | + self.assertRaises(ArgumentError, |
| 227 | + bmc.CreateBMCInstance.execute, client, args) |
| 228 | + |
| 229 | + # Send both hourly and monthly |
| 230 | + args['--hourly'] = True |
| 231 | + args['--monthly'] = True |
| 232 | + self.assertRaises(ArgumentError, |
| 233 | + bmc.CreateBMCInstance.execute, client, args) |
| 234 | + |
| 235 | + # Send neither hourly nor monthly |
| 236 | + args['--hourly'] = False |
| 237 | + args['--monthly'] = False |
| 238 | + self.assertRaises(ArgumentError, |
| 239 | + bmc.CreateBMCInstance.execute, client, args) |
| 240 | + |
| 241 | + # This is missing a server_core combo |
| 242 | + args['--monthly'] = True |
| 243 | + args['--cpu'] = 100 |
| 244 | + self.assertRaises(CLIAbort, |
| 245 | + bmc.CreateBMCInstance.execute, client, args) |
| 246 | + |
| 247 | + # This section is missing an OS code |
| 248 | + args['--cpu'] = '2' |
| 249 | + args['--os'] = 'nope' |
| 250 | + |
| 251 | + self.assertRaises(CLIAbort, |
| 252 | + bmc.CreateBMCInstance.execute, client, args) |
| 253 | + |
| 254 | + # This section is missing a NIC speed |
| 255 | + args['--os'] = 'UBUNTU_12_64_MINIMAL' |
| 256 | + args['--network'] = 'nope' |
| 257 | + |
| 258 | + self.assertRaises(CLIAbort, |
| 259 | + bmc.CreateBMCInstance.execute, client, args) |
| 260 | + |
| 261 | + @patch('SoftLayer.CLI.modules.bmc.CLIAbort') |
| 262 | + @patch('SoftLayer.CLI.modules.bmc.no_going_back') |
| 263 | + @patch('SoftLayer.HardwareManager.cancel_metal') |
| 264 | + @patch('SoftLayer.CLI.modules.bmc.resolve_id') |
| 265 | + def test_CancelInstance( |
| 266 | + self, resolve_mock, cancel_mock, ngb_mock, abort_mock): |
| 267 | + hw_id = 12345 |
| 268 | + resolve_mock.return_value = hw_id |
| 269 | + ngb_mock.return_value = False |
| 270 | + |
| 271 | + # Check the positive case |
| 272 | + args = {'--really': True, '--immediate': False} |
| 273 | + bmc.CancelInstance.execute(self.client, args) |
| 274 | + |
| 275 | + cancel_mock.assert_called_with(hw_id, False) |
| 276 | + |
| 277 | + # Now check to make sure we properly call CLIAbort in the negative case |
| 278 | + args['--really'] = False |
| 279 | + |
| 280 | + bmc.CancelInstance.execute(self.client, args) |
| 281 | + abort_mock.assert_called() |
| 282 | + |
| 283 | + def test_get_default_value_returns_none_for_unknown_category(self): |
| 284 | + option_mock = {'categories': {'cat1': []}} |
| 285 | + output = bmc.CreateBMCInstance._get_default_value(option_mock, 'nope') |
| 286 | + self.assertEqual(None, output) |
| 287 | + |
| 288 | + @staticmethod |
| 289 | + def _setup_package_mocks(client): |
| 290 | + p = client['Product_Package'] |
| 291 | + p.getAllObjects = product_package_mock.getAllObjects_Mock() |
| 292 | + p.getConfiguration = product_package_mock.getConfiguration_Mock(True) |
| 293 | + p.getCategories = product_package_mock.getCategories_Mock(True) |
| 294 | + p.getRegions = product_package_mock.getRegions_Mock() |
| 295 | + |
| 296 | + return client |
0 commit comments