File: globalip_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 (129 lines) | stat: -rw-r--r-- 5,112 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
129
"""
    SoftLayer.tests.CLI.modules.globalip_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from SoftLayer.CLI import exceptions
from SoftLayer import testing

import json


class GlobalIpTests(testing.TestCase):

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_ip_cancel(self, no_going_back_mock):
        # Test using --really flag
        result = self.run_command(['--really', 'globalip', 'cancel', '1'])

        self.assert_no_fail(result)
        self.assertEqual(result.output, "")

        # Test with confirmation
        no_going_back_mock.return_value = True
        result = self.run_command(['globalip', 'cancel', '1'])

        self.assert_no_fail(result)
        self.assertEqual(result.output, "")

        # Test with confirmation and responding negatively
        no_going_back_mock.return_value = False
        result = self.run_command(['globalip', 'cancel', '1'])

        self.assertEqual(result.exit_code, 2)

    def test_ip_list(self):
        result = self.run_command(['globalip', 'list', '--ip-version=v4'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output),
                         [{'assigned': 'Yes',
                           'id': '200',
                           'ip': '127.0.0.1',
                           'target': '127.0.0.1 (example.com)'},
                          {'assigned': 'Yes',
                           'id': '201',
                           'ip': '127.0.0.1',
                           'target': '127.0.0.1 (example.com)'}])

        result = self.run_command(['globalip', 'list', '--ip-version=v6'])
        self.assertEqual(json.loads(result.output),
                         [{'assigned': 'Yes',
                           'id': '200',
                           'ip': '127.0.0.1',
                           'target': '127.0.0.1 (example.com)'},
                          {'assigned': 'Yes',
                           'id': '201',
                           'ip': '127.0.0.1',
                           'target': '127.0.0.1 (example.com)'}])

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_create(self, confirm_mock):
        confirm_mock.return_value = True
        result = self.run_command(['globalip', 'create', '-v6'])
        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), [{
            "item": "this is a thing",
            "cost": "2.00"
        },
            {
                "item": "Total monthly cost",
                "cost": "2.00"
        }])

    def test_ip_unassign(self):
        result = self.run_command(['globalip', 'unassign', '1'])
        self.assert_no_fail(result)
        self.assertEqual(result.output, "")
        self.assert_called_with('SoftLayer_Network_Subnet', 'clearRoute', identifier=9988)

    def test_ip_assign(self):
        result = self.run_command(['globalip', 'assign', '1', '999'])
        self.assert_no_fail(result)
        self.assertEqual(result.output, "")
        service = 'SoftLayer_Network_Subnet_IpAddress'
        self.assert_called_with('SoftLayer_Network_Subnet', 'route', identifier=9988, args=(service, '999'))

    def test_ip_assign_target(self):
        result = self.run_command(['globalip', 'assign', '1', '--target-id=8123'])
        self.assert_no_fail(result)
        self.assertEqual(result.output, "")
        service = 'SoftLayer_Network_Subnet_IpAddress'
        self.assert_called_with('SoftLayer_Network_Subnet', 'route', identifier=9988, args=(service, '8123'))

    def test_ip_assign_ip(self):
        mock_api = self.set_mock('SoftLayer_Account', 'getGlobalIpRecords')
        mock_api.return_value = [{"id": 112233}]
        result = self.run_command(['globalip', 'assign', '192.168.1.1', '1.2.3.4'])
        self.assert_no_fail(result)
        self.assertEqual(result.output, "")
        service = 'SoftLayer_Network_Subnet_IpAddress'
        self.assert_called_with(f"{service}_Global", "getObject", identifier=112233)
        self.assert_called_with('SoftLayer_Network_Subnet', 'route', identifier=9988, args=(service, '1.2.3.4'))

    def test_ip_cancel_force(self):
        result = self.run_command(['globalip', 'cancel', '1', '--force'])

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

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_ip_cancel_no_abort(self, confirm_mock):
        # Test with confirmation and responding negatively
        confirm_mock.return_value = True
        result = self.run_command(['globalip', 'cancel', '1'])

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

    @mock.patch('SoftLayer.CLI.formatting.confirm')
    def test_ip_cancel_abort(self, confirm_mock):
        # Test with confirmation and responding negatively
        confirm_mock.return_value = False
        result = self.run_command(['globalip', 'cancel', '1'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.CLIAbort)