File: dns_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 (302 lines) | stat: -rw-r--r-- 11,932 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
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
"""
    SoftLayer.tests.CLI.modules.dns_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :license: MIT, see LICENSE for more details.
"""
import json
import os.path
import sys

from unittest import mock as mock

from SoftLayer.CLI.dns import zone_import
from SoftLayer.CLI import exceptions
from SoftLayer import testing


class DnsTests(testing.TestCase):

    def test_zone_print(self):
        result = self.run_command(['dns', 'zone-print', '1234'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), "lots of text")

    def test_create_zone(self):
        result = self.run_command(['dns', 'zone-create', 'example.com'])

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

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_delete_zone(self, no_going_back_mock):
        no_going_back_mock.return_value = True
        result = self.run_command(['dns', 'zone-delete', '1234'])

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

        no_going_back_mock.return_value = False
        result = self.run_command(['--really', 'dns', 'zone-delete', '1234'])

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

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_delete_zone_abort(self, no_going_back_mock):
        no_going_back_mock.return_value = False
        result = self.run_command(['dns', 'zone-delete', '1234'])

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

    def test_list_zones(self):
        result = self.run_command(['dns', 'zone-list'])

        self.assert_no_fail(result)
        actual_output = json.loads(result.output)
        self.assertEqual(actual_output[0]['zone'], 'example.com')

    def test_list_records(self):
        result = self.run_command(['dns', 'record-list', '1234'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output)[0],
                         {'record': 'a',
                          'type': 'CNAME',
                          'id': 1,
                          'data': 'd',
                          'ttl': 7200})

    def test_add_record(self):
        result = self.run_command(['dns', 'record-add', 'hostname', 'A',
                                   'data', '--zone=1234', '--ttl=100'])

        self.assert_no_fail(result)
        self.assertEqual(str(result.output), 'A record added successfully\n')

    def test_add_record_mx(self):
        result = self.run_command(['dns', 'record-add', 'hostname', 'MX',
                                   'data', '--zone=1234', '--ttl=100', '--priority=25'])

        self.assert_no_fail(result)
        self.assertEqual(str(result.output), 'MX record added successfully\n')

    def test_add_record_srv(self):
        result = self.run_command(['dns', 'record-add', 'hostname', 'SRV',
                                   'data', '--zone=1234', '--protocol=udp',
                                   '--port=88', '--ttl=100', '--weight=5'])

        self.assert_no_fail(result)
        self.assertEqual(str(result.output), 'SRV record added successfully\n')

    def test_add_record_ptr(self):
        result = self.run_command(['dns', 'record-add', '192.168.1.1', 'PTR',
                                   'hostname', '--ttl=100'])

        self.assert_no_fail(result)
        self.assertEqual(str(result.output), 'PTR record added successfully\n')

    def test_add_record_abort(self):
        result = self.run_command(['dns', 'record-add', 'hostname', 'A',
                                   'data', '--ttl=100'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.CLIAbort)
        self.assertEqual(result.exception.message, "A isn't a valid record type or zone is missing")

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_delete_record(self, no_going_back_mock):
        no_going_back_mock.return_value = True
        result = self.run_command(['dns', 'record-remove', '1234'])

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

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_delete_record_abort(self, no_going_back_mock):
        no_going_back_mock.return_value = False
        result = self.run_command(['dns', 'record-remove', '1234'])

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

    def test_parse_zone_file(self):
        zone_file = """$ORIGIN realtest.com.
$TTL 86400
@ IN SOA ns1.softlayer.com. support.softlayer.com. (
                       2014052300        ; Serial
                       7200              ; Refresh
                       600               ; Retry
                       1728000           ; Expire
                       43200)            ; Minimum

@                      86400    IN NS    ns1.softlayer.com.
@                      86400    IN NS    ns2.softlayer.com.

                        IN MX 10 test.realtest.com.
testing                86400    IN A     127.0.0.1
testing1               86400    IN A     12.12.0.1
server2      IN   A  1.0.3.4
ftp                             IN  CNAME server2
dev.realtest.com    IN  TXT "This is just a test of the txt record"
    IN  AAAA  2001:db8:10::1
spf  IN TXT "v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a"
*.testing              86400    IN A     127.0.0.2
*                      86400    IN A     127.0.0.3

"""
        expected = [{'data': 'ns1.softlayer.com.',
                     'record': '@',
                     'type': 'NS',
                     'ttl': '86400'},
                    {'data': 'ns2.softlayer.com.',
                     'record': '@',
                     'type': 'NS',
                     'ttl': '86400'},
                    {'data': '127.0.0.1',
                     'record': 'testing',
                     'type': 'A',
                     'ttl': '86400'},
                    {'data': '12.12.0.1',
                     'record': 'testing1',
                     'type': 'A',
                     'ttl': '86400'},
                    {'data': '1.0.3.4',
                     'record': 'server2',
                     'type': 'A',
                     'ttl': None},
                    {'data': 'server2',
                     'record': 'ftp',
                     'type': 'CNAME',
                     'ttl': None},
                    {'data': '"This is just a test of the txt record"',
                     'record': 'dev.realtest.com',
                     'type': 'TXT',
                     'ttl': None},
                    {'data': '"v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a"',
                     'record': 'spf',
                     'type': 'TXT',
                     'ttl': None},
                    {'data': '127.0.0.2',
                     'record': '*.testing',
                     'type': 'A',
                     'ttl': '86400'},
                    {'data': '127.0.0.3',
                     'record': '*',
                     'type': 'A',
                     'ttl': '86400'}]
        zone, records, bad_lines = zone_import.parse_zone_details(zone_file)
        self.assertEqual(zone, 'realtest.com')
        self.assertEqual(records, expected)
        self.assertEqual(len(bad_lines), 13)

    def test_import_zone_dry_run(self):
        path = os.path.join(testing.FIXTURE_PATH, 'realtest.com')
        result = self.run_command(['dns', 'import', path, '--dry-run'])

        self.assertIn("Parsed: zone=realtest.com", result.output)
        self.assertIn(
            "Parsed: type=NS, record=@, data=ns1.softlayer.com., ttl=86400",
            result.output)
        self.assertIn("Unparsed: $TTL 86400", result.output)

    def test_import_zone(self):
        path = os.path.join(testing.FIXTURE_PATH, 'realtest.com')
        result = self.run_command(['dns', 'import', path])

        self.assertEqual(self.calls('SoftLayer_Dns_Domain', 'createObject'),
                         [])

        calls = self.calls('SoftLayer_Dns_Domain_ResourceRecord',
                           'createObject')
        expected_calls = [{'data': 'ns1.softlayer.com.',
                           'host': '@',
                           'domainId': 12345,
                           'type': 'NS',
                           'ttl': '86400'},
                          {'data': 'ns2.softlayer.com.',
                           'host': '@',
                           'domainId': 12345,
                           'type': 'NS',
                           'ttl': '86400'},
                          {'data': '127.0.0.1',
                           'host': 'testing',
                           'domainId': 12345,
                           'type': 'A',
                           'ttl': '86400'},
                          {'data': '12.12.0.1',
                           'host': 'testing1',
                           'domainId': 12345,
                           'type': 'A',
                           'ttl': '86400'},
                          {'data': '1.0.3.4',
                           'host': 'server2',
                           'domainId': 12345,
                           'type': 'A',
                           'ttl': None},
                          {'data': 'server2',
                           'host': 'ftp',
                           'domainId': 12345,
                           'type': 'CNAME',
                           'ttl': None},
                          {'data':
                           '"This is just a test of the txt record"',
                           'host': 'dev.realtest.com',
                           'domainId': 12345,
                           'type': 'TXT',
                           'ttl': None},
                          {'data': '"v=spf1 ip4:192.0.2.0/24 '
                                   'ip4:198.51.100.123 a -all"',
                           'host': 'spf',
                           'domainId': 12345,
                           'type': 'TXT',
                           'ttl': None}]

        self.assertEqual(len(calls), len(expected_calls))
        for call, expected_call in zip(calls, expected_calls):
            self.assertEqual(call.args[0], expected_call)

        self.assertIn("Finished", result.output)

    def test_issues_999(self):
        """Makes sure certain zones with a None host record are pritable"""

        # SRV records can have a None `host` record, or just a plain missing one.
        fake_records = [
            {
                'data': '1.2.3.4',
                'id': 137416416,
                'ttl': 900,
                'type': 'srv'
            }
        ]
        record_mock = self.set_mock('SoftLayer_Dns_Domain', 'getResourceRecords')
        record_mock.return_value = fake_records
        result = self.run_command(['dns', 'record-list', '1234'])

        self.assert_no_fail(result)
        actual_output = json.loads(result.output)[0]
        self.assertEqual(actual_output['id'], 137416416)
        self.assertEqual(actual_output['record'], '')

    def test_list_zones_no_update(self):
        pyversion = sys.version_info
        fake_zones = [
            {
                'name': 'example.com',
                'id': 12345,
                'serial': 2014030728,
                'updateDate': None}
        ]
        domains_mock = self.set_mock('SoftLayer_Account', 'getDomains')
        domains_mock.return_value = fake_zones
        result = self.run_command(['dns', 'zone-list'])

        self.assert_no_fail(result)
        actual_output = json.loads(result.output)
        if pyversion.major >= 3 and pyversion.minor >= 7:
            self.assertEqual(actual_output[0]['updated'], '2014-03-07 00:00')
        else:
            self.assertEqual(actual_output[0]['updated'], '2014-03-07T00:00:00-06:00')