1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
"""
SoftLayer.tests.CLI.modules.vlan_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:license: MIT, see LICENSE for more details.
"""
import json
from unittest import mock as mock
from SoftLayer.CLI import exceptions
from SoftLayer import exceptions as ex
from SoftLayer.fixtures import SoftLayer_Network_Vlan
from SoftLayer.fixtures import SoftLayer_Product_Order
from SoftLayer.fixtures import SoftLayer_Product_Package
from SoftLayer import testing
class VlanTests(testing.TestCase):
def test_detail(self):
result = self.run_command(['vlan', 'detail', '1234'])
self.assert_no_fail(result)
def test_create_options(self):
result = self.run_command(['vlan', 'create-options'])
self.assert_no_fail(result)
def test_detail_no_vs(self):
result = self.run_command(['vlan', 'detail', '1234', '--no-vs'])
self.assert_no_fail(result)
def test_detail_no_hardware(self):
result = self.run_command(['vlan', 'detail', '1234', '--no-hardware'])
self.assert_no_fail(result)
def test_subnet_list(self):
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
getObject = {'primaryRouter': {'datacenter': {'id': 1234, 'longName': 'TestDC'},
'fullyQualifiedDomainName': 'fcr01.TestDC'},
'id': 1234,
'vlanNumber': 4444,
'firewallInterfaces': None,
'subnets': [{'id': 99, 'networkIdentifier': 1111111, 'netmask': '255.255.255.0',
'gateway': '12.12.12.12', 'subnetType': 'TEST', 'usableIpAddressCount': 1}
]
}
vlan_mock.return_value = getObject
result = self.run_command(['vlan', 'detail', '1234'])
self.assert_no_fail(result)
def test_detail_hardware_without_hostname(self):
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
getObject = {
'primaryRouter': {
'datacenter': {'id': 1234, 'longName': 'TestDC'},
'fullyQualifiedDomainName': 'fcr01.TestDC'
},
'id': 1234,
'vlanNumber': 4444,
'firewallInterfaces': None,
'subnets': [],
'hardware': [
{'a_hardware': 'that_has_none_of_the_expected_attributes_provided'},
{'domain': 'example.com',
'networkManagementIpAddress': '10.171.202.131',
'hardwareStatus': {'status': 'ACTIVE', 'id': 5},
'notes': '',
'hostname': 'hw1', 'hardwareStatusId': 5,
'globalIdentifier': 'f6ea716a-41d8-4c52-bb2e-48d63105f4b0',
'primaryIpAddress': '169.60.169.169',
'primaryBackendIpAddress': '10.171.202.130', 'id': 826425,
'privateIpAddress': '10.171.202.130',
'fullyQualifiedDomainName': 'hw1.example.com'}
]
}
vlan_mock.return_value = getObject
result = self.run_command(['vlan', 'detail', '1234'])
self.assert_no_fail(result)
@mock.patch('SoftLayer.CLI.vlan.edit.click')
def test_vlan_edit(self, click):
result = self.run_command(['vlan', 'edit', '--name=nameTest', '--note=noteTest', '--tags=tag1,tag2', '100'])
click.secho.assert_called_with('Vlan edited successfully', fg='green')
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)
@mock.patch('SoftLayer.CLI.vlan.edit.click')
def test_vlan_edit_not_any(self, click):
result = self.run_command(['vlan', 'edit', '100'])
self.assertEqual(result.exit_code, 2)
self.assertEqual("At least one option is required", result.exception.message)
def test_vlan_create_not_data_pod(self):
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
_mock.return_value = SoftLayer_Product_Package.getItemsVLAN
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
result = self.run_command(['vlan', 'create', '--name', 'my_vlan', '--datacenter', '', '--pod', ''])
self.assertIsInstance(result.exception, exceptions.CLIAbort)
self.assertIn("--datacenter or --pod is required to", result.exception.message)
def test_vlan_create_network_extra_route_exception(self):
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
_mock.return_value = SoftLayer_Product_Package.getItemsVLAN
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
result = self.run_command(['vlan', 'create',
'--name', 'TEST001',
'--datacenter', 'dal09',
'--network', 'private',
'--pod', 'dal09.pod01'
])
self.assertIsInstance(result.exception, exceptions.CLIAbort)
self.assertIn("Unable to find pod name", result.exception.message)
@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_vlan_create_network_priv(self, confirm_mock):
confirm_mock.return_value = True
get_pods_mock = self.set_mock('SoftLayer_Network_Pod', 'getAllObjects')
get_pods_mock.return_value = [{
'name': 'ams03.pod01',
'frontendRouterId': 468136,
'backendRouterId': 468132}]
result = self.run_command(['vlan', 'create',
'--name', 'TEST001',
'--datacenter', 'ams03',
'--network', 'private',
'--pod', 'ams03.pod01'
])
self.assert_called_with('SoftLayer_Network_Pod', 'getAllObjects')
self.assertIsInstance(result.exception, ex.SoftLayerError)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_create_option(self, confirm_mock):
confirm_mock.return_value = True
datacenter_mock = self.set_mock('SoftLayer_Location_Datacenter', 'getDatacenters')
datacenter_mock.return_value = [{'id': 814994, 'name': 'ams03'}]
router_mock = self.set_mock('SoftLayer_Location_Datacenter', 'getHardwareRouters')
router_mock.return_value = [{'hostname': 'bcr01a.ams03'}]
result = self.run_command(['vlan', 'create-options'])
self.assert_no_fail(result)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_list_get_pod_with_closed_announcement(self, ngb_mock):
ngb_mock.return_value = True
vlan_mock = self.set_mock('SoftLayer_Account', 'getNetworkVlans')
gpods_mock = self.set_mock('SoftLayer_Network_Pod', 'getAllObjects')
vlan_mock.return_value = [
{
"primaryRouter": {
"fullyQualifiedDomainName": "bcr01a.fra02.softlayer.com",
"id": 462912
},
"tagReferences": ""
}
]
gpods_mock.return_value = [{'backendRouterId': 462912,
'backendRouterName': 'bcr01a.fra02',
'datacenterId': 449506,
'datacenterLongName': 'Frankfurt 2',
'frontendRouterId': 335916,
'frontendRouterName': 'fcr01a.fra02',
'name': 'fra02.pod01',
'capabilities': ['SUPPORTS_SECURITY_GROUP', 'CLOSURE_ANNOUNCED']}]
result = self.run_command(['vlan', 'list'])
self.assert_no_fail(result)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_list_get_pod_with_closed_announcement_no_closure(self, ngb_mock):
ngb_mock.return_value = True
vlan_mock = self.set_mock('SoftLayer_Account', 'getNetworkVlans')
gpods_mock = self.set_mock('SoftLayer_Network_Pod', 'getAllObjects')
vlan_mock.return_value = [
{
"primaryRouter": {
"fullyQualifiedDomainName": "bcr01a.fra02.softlayer.com",
"id": 462912
},
"tagReferences": ""
}
]
gpods_mock.return_value = [{'backendRouterId': 462912,
'backendRouterName': 'bcr01a.fra02',
'datacenterId': 449506,
'datacenterLongName': 'Frankfurt 2',
'frontendRouterId': 335916,
'frontendRouterName': 'fcr01a.fra02',
'name': 'fra02.pod01',
'capabilities': ['SUPPORTS_SECURITY_GROUP']}]
result = self.run_command(['vlan', 'list'])
self.assert_no_fail(result)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_details_no_vs_no_hard_no_trunk(self, ngb_mock):
ngb_mock.return_value = True
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
vlan_mock.return_value = SoftLayer_Network_Vlan.getObject_no_hard_no_vs_no_trunk
result = self.run_command(['vlan', 'detail', '3284824'])
self.assert_no_fail(result)
@mock.patch('SoftLayer.CLI.vlan.edit.click')
def test_vlan_edit_failure(self, click):
mock = self.set_mock('SoftLayer_Network_Vlan', 'editObject')
mock.return_value = False
result = self.run_command(['vlan', 'edit', '--name=nameTest', '--note=noteTest', '--tags=tag1,tag2', '100'])
click.secho.assert_called_with('Failed to edit the vlan', fg='red')
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)
def test_vlan_detail_firewall(self):
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
get_object = {
'primaryRouter': {
'datacenter': {'id': 1234, 'longName': 'TestDC'},
'fullyQualifiedDomainName': 'fcr01.TestDC'
},
'id': 1234,
'vlanNumber': 4444,
'networkVlanFirewall': {
'datacenter': {'id': 1234, 'longName': 'TestDC'},
'fullyQualifiedDomainName': 'fcr01.TestDC'
}
}
vlan_mock.return_value = get_object
result = self.run_command(['vlan', 'detail', '1234'])
self.assert_no_fail(result)
def test_vlan_detail_gateway(self):
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
get_object = {
'primaryRouter': {
'datacenter': {'id': 1234, 'longName': 'TestDC'},
'fullyQualifiedDomainName': 'fcr01.TestDC'
},
'id': 1234,
'vlanNumber': 4444,
'attachedNetworkGateway': {
'id': 54321,
"name": 'support'
}
}
vlan_mock.return_value = get_object
result = self.run_command(['vlan', 'detail', '1234'])
self.assert_no_fail(result)
def test_vlan_list(self):
result = self.run_command(['vlan', 'list'])
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Account', 'getNetworkVlans')
def test_vlan_list_space_empty(self):
result = self.run_command(['vlan', 'list'])
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Account', 'getNetworkVlans')
self.assertEqual(json.loads(result.output)[0]['Network'], '')
def test_create_vlan(self):
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
_mock.return_value = SoftLayer_Product_Package.getItemsVLAN
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
result = self.run_command(['vlan', 'create',
'--name', 'test',
'-d TEST00',
'--network', 'public',
'--billing', 'hourly'
])
self.assert_no_fail(result)
self.assertEqual(json.loads(result.output), {'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
def test_create_vlan_pod(self):
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
_mock.return_value = SoftLayer_Product_Package.getItemsVLAN
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
result = self.run_command(['vlan', 'create',
'--name', 'test',
'-p', 'TEST00.pod2',
'--network', 'public',
'--billing', 'hourly'
])
self.assert_no_fail(result)
self.assertEqual(json.loads(result.output), {'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_cancel(self, confirm_mock):
confirm_mock.return_value = True
mock = self.set_mock('SoftLayer_Network_Vlan', 'getCancelFailureReasons')
mock.return_value = []
result = self.run_command(['vlan', 'cancel', '1234'])
self.assert_no_fail(result)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_cancel_error(self, confirm_mock):
confirm_mock.return_value = True
result = self.run_command(['vlan', 'cancel', '1234'])
self.assertTrue(result.exit_code, 2)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_cancel_fail(self, confirm_mock):
confirm_mock.return_value = False
result = self.run_command(['vlan', 'cancel', '1234'])
self.assertTrue(result.exit_code, 2)
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
def test_vlan_cancel_exception(self, confirm_mock):
confirm_mock.return_value = True
cancel_failure_mock = self.set_mock('SoftLayer_Network_Vlan', 'getCancelFailureReasons')
cancel_failure_mock.return_value = []
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
vlan_mock.return_value = {}
result = self.run_command(['vlan', 'cancel', '1234'])
self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.CLIAbort)
self.assertIn("VLAN is an automatically assigned", result.exception.message)
|