File: subnet_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 (196 lines) | stat: -rw-r--r-- 7,832 bytes parent folder | download | duplicates (2)
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
"""
    SoftLayer.tests.CLI.modules.subnet_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :license: MIT, see LICENSE for more details.
"""
import json
from unittest import mock as mock

import SoftLayer
from SoftLayer.fixtures import SoftLayer_Product_Order
from SoftLayer.fixtures import SoftLayer_Product_Package
from SoftLayer import testing


class SubnetTests(testing.TestCase):

    def test_detail(self):
        result = self.run_command(['subnet', 'detail', '1234'])
        subnet = json.loads(result.output)
        self.assert_no_fail(result)
        self.assertEqual(subnet.get('id'), 1234)
        self.assertEqual(subnet.get('identifier'), '1.2.3.4/26')

    def test_list(self):
        result = self.run_command(['subnet', 'list'])
        self.assert_no_fail(result)

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_create_subnet_ipv4(self, confirm_mock):
        confirm_mock.return_value = True

        item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
        item_mock.return_value = SoftLayer_Product_Package.getItems

        place_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
        place_mock.return_value = SoftLayer_Product_Order.placeOrder

        result = self.run_command(['subnet', 'create', 'private', '8', '12346'])
        self.assert_no_fail(result)

        output = [
            {'Item': 'Total monthly cost', 'cost': '0.00'}
        ]

        self.assertEqual(output, json.loads(result.output))

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_create_subnet_ipv6(self, confirm_mock):
        confirm_mock.return_value = True

        item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
        item_mock.return_value = SoftLayer_Product_Package.getItems

        place_mock = self.set_mock('SoftLayer_Product_Order', 'verifyOrder')
        place_mock.return_value = SoftLayer_Product_Order.verifyOrder

        result = self.run_command(['subnet', 'create', '--v6', 'public', '64', '12346', '--test'])
        self.assert_no_fail(result)

        output = [
            {'Item': 'this is a thing', 'cost': '2.00'},
            {'Item': 'Total monthly cost', 'cost': '2.00'}
        ]

        self.assertEqual(output, json.loads(result.output))

    def test_create_subnet_no_prices_found(self):
        item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
        item_mock.return_value = SoftLayer_Product_Package.getItems

        verify_mock = self.set_mock('SoftLayer_Product_Order', 'verifyOrder')
        verify_mock.side_effect = SoftLayer.SoftLayerAPIError('SoftLayer_Exception', 'Price not found')

        result = self.run_command(['subnet', 'create', '--v6', 'public', '32', '12346', '--test'])

        self.assertRaises(SoftLayer.SoftLayerAPIError, verify_mock)
        self.assertIn('Unable to order 32 public ipv6', result.exception.message, )

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_create_subnet_static(self, confirm_mock):
        confirm_mock.return_value = True

        item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
        item_mock.return_value = SoftLayer_Product_Package.getItems

        place_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
        place_mock.return_value = SoftLayer_Product_Order.placeOrder

        result = self.run_command(['subnet', 'create', 'static', '2', '12346'])
        self.assert_no_fail(result)

        output = [
            {'Item': 'Total monthly cost', 'cost': '0.00'}
        ]

        self.assertEqual(output, json.loads(result.output))

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_create_subnet_static_ipv6(self, confirm_mock):
        confirm_mock.return_value = True

        item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
        item_mock.return_value = SoftLayer_Product_Package.getItems

        place_mock = self.set_mock('SoftLayer_Product_Order', 'verifyOrder')
        place_mock.return_value = SoftLayer_Product_Order.verifyOrder

        result = self.run_command(['subnet', 'create', '--v6', 'static', '64', '12346', '--test'])
        self.assert_no_fail(result)

        output = [
            {'Item': 'this is a thing', 'cost': '2.00'},
            {'Item': 'Total monthly cost', 'cost': '2.00'}
        ]

        self.assertEqual(output, json.loads(result.output))

    @mock.patch('SoftLayer.CLI.subnet.edit.click')
    def test_subnet_set_tags(self, click):
        result = self.run_command(['subnet', 'edit', '1234', '--tags=tag1,tag2'])
        click.secho.assert_called_with('Set tags successfully', fg='green')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', identifier=1234, args=("tag1,tag2",))

    @mock.patch('SoftLayer.CLI.subnet.edit.click')
    def test_subnet_edit_note(self, click):
        result = self.run_command(['subnet', 'edit', '1234', '--note=test'])
        click.secho.assert_called_with('Edit note successfully', fg='green')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', identifier=1234, args=("test",))

    @mock.patch('SoftLayer.CLI.subnet.edit.click')
    def test_subnet_set_tags_failure(self, click):
        mock = self.set_mock('SoftLayer_Network_Subnet', 'setTags')
        mock.return_value = False
        result = self.run_command(['subnet', 'edit', '1234', '--tags=tag1,tag2'])
        click.secho.assert_called_with('Failed to set tags', fg='red')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', identifier=1234, args=("tag1,tag2",))

    @mock.patch('SoftLayer.CLI.subnet.edit.click')
    def test_edit_note_failure(self, click):
        mock = self.set_mock('SoftLayer_Network_Subnet', 'editNote')
        mock.return_value = False
        result = self.run_command(['subnet', 'edit', '1234', '--note=test'])
        click.secho.assert_called_with('Failed to edit note', fg='red')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', identifier=1234, args=("test",))

    def test_editrou_Ip(self):
        result = self.run_command(['subnet', 'edit-ip', '16.26.26.26', '--note=test'])
        self.assert_no_fail(result)
        self.assertTrue(result)

    def test_editrou_Id(self):
        result = self.run_command(['subnet', 'edit-ip', '123456', '--note=test'])
        self.assert_no_fail(result)
        self.assertTrue(result)

    def test_lookup(self):
        result = self.run_command(['subnet', 'lookup', '1.2.3.10'])
        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), {'device': {
            'id': 12856,
            'name': 'unit.test.com',
            'type': 'server'},
            "id": 12345,
            "ip": "10.0.1.37",
            "subnet": {
                "id": 258369,
                "identifier": "10.0.1.38/26",
                "netmask": "255.255.255.192",
                "gateway": "10.47.16.129",
                "type": "PRIMARY"
        }})

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_cancel(self, confirm_mock):
        confirm_mock.return_value = True
        result = self.run_command(['subnet', 'cancel', '1234'])
        self.assert_no_fail(result)

    def test_cancel_fail(self):
        result = self.run_command(['subnet', 'cancel', '1234'])
        self.assertEqual(result.exit_code, 2)

    def test_route(self):
        result = self.run_command(['subnet', 'route', '1'])

        self.assert_no_fail(result)
        self.assertEqual(result.exit_code, 0)

    def test_clear_route(self):
        result = self.run_command(['subnet', 'clear-route', '123456'])
        self.assert_no_fail(result)