Skip to content

Commit 2fe5c71

Browse files
More vlan-trunking related unit tests
1 parent b2847f4 commit 2fe5c71

File tree

6 files changed

+156
-63
lines changed

6 files changed

+156
-63
lines changed

.secrets.baseline

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2023-10-12T20:19:44Z",
6+
"generated_at": "2023-10-12T22:45:35Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -529,23 +529,22 @@
529529
"verified_result": null
530530
}
531531
],
532-
"tests/CLI/modules/securitygroup_tests.py": [
532+
"tests/CLI/modules/hardware/hardware_basic_tests.py": [
533533
{
534-
"hashed_secret": "bc553d847e40dd6f3f63638f16f57b28ce1425cc",
535-
"is_secret": false,
534+
"hashed_secret": "6367c48dd193d56ea7b0baad25b19455e529f5ee",
536535
"is_verified": false,
537-
"line_number": 339,
538-
"type": "Hex High Entropy String",
536+
"line_number": 58,
537+
"type": "Secret Keyword",
539538
"verified_result": null
540539
}
541540
],
542-
"tests/CLI/modules/server_tests.py": [
541+
"tests/CLI/modules/securitygroup_tests.py": [
543542
{
544-
"hashed_secret": "6367c48dd193d56ea7b0baad25b19455e529f5ee",
543+
"hashed_secret": "bc553d847e40dd6f3f63638f16f57b28ce1425cc",
545544
"is_secret": false,
546545
"is_verified": false,
547-
"line_number": 58,
548-
"type": "Secret Keyword",
546+
"line_number": 339,
547+
"type": "Hex High Entropy String",
549548
"verified_result": null
550549
}
551550
],

SoftLayer/fixtures/SoftLayer_Hardware_Server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@
173173
}
174174
]
175175
# This splits out the network components into 2 sections so they are different enough for tests
176-
getFrontendNetworkComponents = getNetworkComponents[0::1]
177-
getBackendNetworkComponents = getNetworkComponents[2::3]
176+
getFrontendNetworkComponents = getNetworkComponents[:2]
177+
getBackendNetworkComponents = getNetworkComponents[3:]
178178

179179
getBandwidthAllotmentDetail = {
180180
'allocationId': 25465663,

SoftLayer/managers/hardware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,10 +1223,10 @@ def clear_vlan(self, hardware_id):
12231223
# We only want to call this API on components with actual trunks.
12241224
# Calling this on the primary and redundant components might cause exceptions.
12251225
for c in components.get('backendNetworkComponent', []):
1226-
if len(c.get('networkVlanTrunks')):
1226+
if len(c.get('networkVlanTrunks', [])):
12271227
self.client.call('SoftLayer_Network_Component', 'clearNetworkVlanTrunks', id=c.get('id'))
12281228
for c in components.get('frontendNetworkComponent', []):
1229-
if len(c.get('networkVlanTrunks')):
1229+
if len(c.get('networkVlanTrunks', [])):
12301230
self.client.call('SoftLayer_Network_Component', 'clearNetworkVlanTrunks', id=c.get('id'))
12311231

12321232
def get_sensors(self, hardware_id):

tests/CLI/helper_tests.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -450,41 +450,4 @@ def test_export_to_template(self):
450450
self.assertIn('disk=disk1,disk2\n', data)
451451

452452

453-
class IterToTableTests(testing.TestCase):
454453

455-
def test_format_api_dict(self):
456-
result = formatting._format_dict({'key': 'value'})
457-
458-
self.assertIsInstance(result, formatting.Table)
459-
self.assertEqual(result.columns, ['name', 'value'])
460-
self.assertEqual(result.rows, [['key', 'value']])
461-
462-
def test_format_api_list(self):
463-
result = formatting._format_list([{'key': 'value'}])
464-
465-
self.assertIsInstance(result, formatting.Table)
466-
self.assertEqual(result.columns, ['key'])
467-
self.assertEqual(result.rows, [['value']])
468-
469-
def test_format_api_list_non_objects(self):
470-
result = formatting._format_list(['a', 'b', 'c'])
471-
472-
self.assertIsInstance(result, formatting.Table)
473-
self.assertEqual(result.columns, ['value'])
474-
self.assertEqual(result.rows, [['a'], ['b'], ['c']])
475-
476-
def test_format_api_list_with_none_value(self):
477-
result = formatting._format_list([{'key': [None, 'value']}, None])
478-
479-
self.assertIsInstance(result, formatting.Table)
480-
self.assertEqual(result.columns, ['key'])
481-
482-
def test_format_api_list_with_empty_array(self):
483-
result = formatting.iter_to_table([{'id': 130224450, 'activeTickets': []}])
484-
self.assertIsInstance(result, formatting.Table)
485-
self.assertIn('id', result.columns)
486-
self.assertIn('activeTickets', result.columns)
487-
formatted = formatting.format_output(result, "table")
488-
# No good ways to test whats actually in a Rich.Table without going through the hassel of
489-
# printing it out. As long as this didn't throw and exception it should be fine.
490-
self.assertEqual(formatted.row_count, 1)

tests/CLI/modules/server_tests.py renamed to tests/CLI/modules/hardware/hardware_basic_tests.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from SoftLayer import utils
2222

2323

24-
class ServerCLITests(testing.TestCase):
24+
class HardwareCLITests(testing.TestCase):
2525

2626
def test_server_cancel_reasons(self):
2727
result = self.run_command(['server', 'cancel-reasons'])
@@ -1032,14 +1032,3 @@ def test_hardware_cancel_no_force(self, confirm_mock):
10321032
self.assertEqual(2, result.exit_code)
10331033
self.assertEqual('Aborted', result.exception.message)
10341034

1035-
def test_hardware_vlan_add(self):
1036-
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
1037-
mock.return_value = SoftLayer_Search.advancedSearchVlan
1038-
result = self.run_command(['hardware', 'vlan-add', '12345', '5555'])
1039-
self.assert_no_fail(result)
1040-
search_args = '_objectType:SoftLayer_Network_Vlan "5555"'
1041-
self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
1042-
self.assert_called_with('SoftLayer_Hardware_Server', 'getFrontendNetworkComponents', identifier=12345)
1043-
self.assert_called_with('SoftLayer_Hardware_Server', 'getBackendNetworkComponents', identifier=12345)
1044-
self.assert_called_with('SoftLayer_Network_Component', 'addNetworkVlanTrunks', identifier=998877)
1045-
self.assert_called_with('SoftLayer_Network_Component', 'addNetworkVlanTrunks', identifier=123456)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""
2+
SoftLayer.tests.CLI.modules.hardware.hardware_vlan_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
These tests are for any commands that work with the vlans on hardware objects.
6+
7+
:license: MIT, see LICENSE for more details.
8+
"""
9+
10+
import json
11+
import sys
12+
import tempfile
13+
from unittest import mock as mock
14+
15+
from SoftLayer.CLI import exceptions
16+
from SoftLayer.fixtures import SoftLayer_Hardware_Server
17+
from SoftLayer.fixtures import SoftLayer_Search
18+
from SoftLayer import SoftLayerError
19+
from SoftLayer import testing
20+
from SoftLayer import utils
21+
22+
23+
class HardwareVlanCLITests(testing.TestCase):
24+
25+
# slcli hardware vlan-trunkable
26+
def test_hardware_vlan_trunkable_no_hardware(self):
27+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
28+
mock.return_value = []
29+
result = self.run_command(['hardware', 'vlan-trunkable'])
30+
self.assertEqual(2, result.exit_code)
31+
self.assertIn("Missing argument 'HARDWARE'.", result.output)
32+
33+
def test_hardware_vlan_trunkable_happypath(self):
34+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
35+
mock.return_value = [
36+
{
37+
'maxSpeed': 1000,
38+
'networkVlansTrunkable': [{
39+
'id':5555,
40+
'fullyQualifiedName': 'test01.ibm99.1234',
41+
'name': 'IBMTEst',
42+
'networkSpace': 'PUBLIC'
43+
}],
44+
'primaryIpAddress': '192.168.1.1',
45+
'id': 998877,
46+
}
47+
]
48+
result = self.run_command(['hardware', 'vlan-trunkable', '12345'])
49+
self.assert_no_fail(result)
50+
output = json.loads(result.output)
51+
self.assertEqual(len(output), 1)
52+
self.assertEqual(output[0]['ID'], 5555)
53+
54+
def test_hardware_vlan_trunkable_no_vlans(self):
55+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
56+
mock.return_value = []
57+
result = self.run_command(['--format=table', 'hardware', 'vlan-trunkable', '12345'])
58+
print(result.output)
59+
self.assert_no_fail(result)
60+
self.assertIn("No trunkable vlans found.", result.output)
61+
62+
def test_hardware_vlan_trunkable_no_vlans_json(self):
63+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
64+
mock.return_value = []
65+
result = self.run_command(['hardware', 'vlan-trunkable', '12345'])
66+
output = json.loads(result.output)
67+
self.assert_no_fail(result)
68+
self.assertEqual([], output)
69+
70+
71+
# slcli hardware vlan-remove
72+
def test_hardware_vlan_remove(self):
73+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
74+
mock.return_value = SoftLayer_Search.advancedSearchVlan
75+
result = self.run_command(['hardware', 'vlan-remove', '12345', '5555'])
76+
self.assert_no_fail(result)
77+
search_args = '_objectType:SoftLayer_Network_Vlan "5555"'
78+
self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
79+
self.assert_called_with('SoftLayer_Hardware_Server', 'getFrontendNetworkComponents', identifier=12345)
80+
self.assert_called_with('SoftLayer_Hardware_Server', 'getBackendNetworkComponents', identifier=12345)
81+
self.assert_called_with('SoftLayer_Network_Component', 'removeNetworkVlanTrunks', identifier=998877)
82+
self.assert_called_with('SoftLayer_Network_Component', 'removeNetworkVlanTrunks', identifier=123456)
83+
84+
def test_hardware_vlan_remove_two_vlans(self):
85+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
86+
mock.return_value = SoftLayer_Search.advancedSearchVlan
87+
result = self.run_command(['hardware', 'vlan-remove', '12345', '5555', 'testVlan'])
88+
self.assert_no_fail(result)
89+
search_args = '_objectType:SoftLayer_Network_Vlan "5555" "testVlan"'
90+
self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
91+
92+
def test_hardware_vlan_remove_no_vlans(self):
93+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
94+
mock.return_value = SoftLayer_Search.advancedSearchVlan
95+
result = self.run_command(['hardware', 'vlan-remove', '12345'])
96+
self.assertEqual(2, result.exit_code)
97+
self.assertEqual("Argument Error: Error: Missing argument 'VLANS'.", result.exception.message)
98+
99+
def test_hardware_vlan_remove_all_vlans(self):
100+
from pprint import pprint as pp
101+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
102+
mock.return_value = SoftLayer_Search.advancedSearchVlan
103+
hardware_mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject')
104+
hardware_return = {
105+
'backendNetworkComponent': SoftLayer_Hardware_Server.getBackendNetworkComponents,
106+
'frontendNetworkComponent': SoftLayer_Hardware_Server.getFrontendNetworkComponents
107+
}
108+
hardware_return['backendNetworkComponent'][1]['networkVlanTrunks'] = [{'id': 99}]
109+
hardware_return['frontendNetworkComponent'][1]['networkVlanTrunks'] = [{'id': 11}]
110+
hardware_mock.return_value = hardware_return
111+
result = self.run_command(['hardware', 'vlan-remove', '12345', '--all'])
112+
self.assert_no_fail(result)
113+
self.assert_called_with('SoftLayer_Network_Component', 'clearNetworkVlanTrunks', identifier=998877)
114+
self.assert_called_with('SoftLayer_Network_Component', 'clearNetworkVlanTrunks', identifier=123456)
115+
116+
# slcli hardware vlan-add
117+
def test_hardware_vlan_add(self):
118+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
119+
mock.return_value = SoftLayer_Search.advancedSearchVlan
120+
result = self.run_command(['hardware', 'vlan-add', '12345', '5555'])
121+
self.assert_no_fail(result)
122+
search_args = '_objectType:SoftLayer_Network_Vlan "5555"'
123+
self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
124+
self.assert_called_with('SoftLayer_Hardware_Server', 'getFrontendNetworkComponents', identifier=12345)
125+
self.assert_called_with('SoftLayer_Hardware_Server', 'getBackendNetworkComponents', identifier=12345)
126+
self.assert_called_with('SoftLayer_Network_Component', 'addNetworkVlanTrunks', identifier=998877)
127+
self.assert_called_with('SoftLayer_Network_Component', 'addNetworkVlanTrunks', identifier=123456)
128+
129+
def test_hardware_vlan_add_two_vlans(self):
130+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
131+
mock.return_value = SoftLayer_Search.advancedSearchVlan
132+
result = self.run_command(['hardware', 'vlan-add', '12345', '5555', 'testVlan'])
133+
self.assert_no_fail(result)
134+
search_args = '_objectType:SoftLayer_Network_Vlan "5555" "testVlan"'
135+
self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
136+
137+
def test_hardware_vlan_add_no_vlans(self):
138+
mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
139+
mock.return_value = SoftLayer_Search.advancedSearchVlan
140+
result = self.run_command(['hardware', 'vlan-add', '12345'])
141+
self.assertEqual(2, result.exit_code)
142+
self.assertEqual("Argument Error: Error: Missing argument 'VLANS'.", result.exception.message)

0 commit comments

Comments
 (0)