File: hardware_vlan_tests.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (128 lines) | stat: -rw-r--r-- 6,659 bytes parent folder | download
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
"""
    SoftLayer.tests.CLI.modules.hardware.hardware_vlan_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    These tests are for any commands that work with the vlans on hardware objects.

    :license: MIT, see LICENSE for more details.
"""

import json

from SoftLayer.fixtures import SoftLayer_Hardware_Server
from SoftLayer.fixtures import SoftLayer_Search
from SoftLayer import testing


class HardwareVlanCLITests(testing.TestCase):

    # slcli hardware vlan-trunkable
    def test_hardware_vlan_trunkable_no_hardware(self):
        mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
        mock.return_value = []
        result = self.run_command(['hardware', 'vlan-trunkable'])
        self.assertEqual(2, result.exit_code)
        self.assertIn("Missing argument 'HARDWARE'.", result.output)

    def test_hardware_vlan_trunkable_happypath(self):
        mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
        mock.return_value = [
            {
                'maxSpeed': 1000,
                'networkVlansTrunkable': [{
                    'id': 5555,
                    'fullyQualifiedName': 'test01.ibm99.1234',
                    'name': 'IBMTEst',
                    'networkSpace': 'PUBLIC'
                }],
                'primaryIpAddress': '192.168.1.1',
                'id': 998877,
            }
        ]
        result = self.run_command(['hardware', 'vlan-trunkable', '12345'])
        self.assert_no_fail(result)
        output = json.loads(result.output)
        self.assertEqual(len(output), 1)
        self.assertEqual(output[0]['ID'], 5555)

    def test_hardware_vlan_trunkable_no_vlans(self):
        mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
        mock.return_value = []
        result = self.run_command(['--format=table', 'hardware', 'vlan-trunkable', '12345'])
        print(result.output)
        self.assert_no_fail(result)
        self.assertIn("No trunkable vlans found.", result.output)

    def test_hardware_vlan_trunkable_no_vlans_json(self):
        mock = self.set_mock('SoftLayer_Hardware_Server', 'getNetworkComponents')
        mock.return_value = []
        result = self.run_command(['hardware', 'vlan-trunkable', '12345'])
        output = json.loads(result.output)
        self.assert_no_fail(result)
        self.assertEqual([], output)

    # slcli hardware vlan-remove
    def test_hardware_vlan_remove(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        result = self.run_command(['hardware', 'vlan-remove', '12345', '5555'])
        self.assert_no_fail(result)
        search_args = '_objectType:SoftLayer_Network_Vlan "5555"'
        self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
        self.assert_called_with('SoftLayer_Hardware_Server', 'getFrontendNetworkComponents', identifier=12345)
        self.assert_called_with('SoftLayer_Hardware_Server', 'getBackendNetworkComponents', identifier=12345)
        self.assert_called_with('SoftLayer_Network_Component', 'removeNetworkVlanTrunks', identifier=998877)
        self.assert_called_with('SoftLayer_Network_Component', 'removeNetworkVlanTrunks', identifier=123456)

    def test_hardware_vlan_remove_two_vlans(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        result = self.run_command(['hardware', 'vlan-remove', '12345', '5555', 'testVlan'])
        self.assert_no_fail(result)
        search_args = '_objectType:SoftLayer_Network_Vlan "5555" "testVlan"'
        self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))

    def test_hardware_vlan_remove_no_vlans(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        result = self.run_command(['hardware', 'vlan-remove', '12345'])
        self.assertEqual(2, result.exit_code)
        self.assertEqual("Argument Error: Error: Missing argument 'VLANS'.", result.exception.message)

    def test_hardware_vlan_remove_all_vlans(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        hardware_mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject')
        hardware_mock.return_value = SoftLayer_Hardware_Server.getObjectVlanClear
        result = self.run_command(['hardware', 'vlan-remove', '12345', '--all'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Network_Component', 'clearNetworkVlanTrunks', identifier=998877)
        self.assert_called_with('SoftLayer_Network_Component', 'clearNetworkVlanTrunks', identifier=123456)

    # slcli hardware vlan-add
    def test_hardware_vlan_add(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        result = self.run_command(['hardware', 'vlan-add', '12345', '5555'])
        self.assert_no_fail(result)
        search_args = '_objectType:SoftLayer_Network_Vlan "5555"'
        self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))
        self.assert_called_with('SoftLayer_Hardware_Server', 'getFrontendNetworkComponents', identifier=12345)
        self.assert_called_with('SoftLayer_Hardware_Server', 'getBackendNetworkComponents', identifier=12345)
        self.assert_called_with('SoftLayer_Network_Component', 'addNetworkVlanTrunks', identifier=998877)
        self.assert_called_with('SoftLayer_Network_Component', 'addNetworkVlanTrunks', identifier=123456)

    def test_hardware_vlan_add_two_vlans(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        result = self.run_command(['hardware', 'vlan-add', '12345', '5555', 'testVlan'])
        self.assert_no_fail(result)
        search_args = '_objectType:SoftLayer_Network_Vlan "5555" "testVlan"'
        self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=(search_args,))

    def test_hardware_vlan_add_no_vlans(self):
        mock = self.set_mock('SoftLayer_Search', 'advancedSearch')
        mock.return_value = SoftLayer_Search.advancedSearchVlan
        result = self.run_command(['hardware', 'vlan-add', '12345'])
        self.assertEqual(2, result.exit_code)
        self.assertEqual("Argument Error: Error: Missing argument 'VLANS'.", result.exception.message)