forked from octodns/octodns-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
114 lines (92 loc) · 3.14 KB
/
test.py
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
#
#
#
from mock import call, patch
from octodns.zone import Zone
from unittest import TestCase
from octodns_ddns import DdnsSource
class MockResponse(object):
def __init__(self, content=None, raise_for_status=False):
self.content = content
self._raise_for_status = raise_for_status
def raise_for_status(self):
if self._raise_for_status:
raise Exception(self._raise_for_status)
class TestDdnsSource(TestCase):
@patch('requests.Session.get')
def test_defaults(self, mock):
a_value = '1.2.3.4'
aaaa_value = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
mock.side_effect = [
MockResponse(a_value),
MockResponse(aaaa_value)
]
zone = Zone('example.com.', [])
DdnsSource('dynamic').populate(zone)
records = sorted(list(zone.records))
self.assertEquals(2, len(records))
a = records[0]
self.assertEquals([a_value], a.values)
aaaa = records[1]
self.assertEquals([aaaa_value], aaaa.values)
mock.assert_has_calls([
call('https://v4.ident.me/'),
call('https://v6.ident.me/')
])
@patch('requests.Session.get')
def test_error(self, mock):
mock.side_effect = [
MockResponse(raise_for_status='boom'),
]
zone = Zone('example.com.', [])
with self.assertRaises(Exception):
DdnsSource('dynamic').populate(zone)
@patch('requests.Session.get')
def test_types_a(self, mock):
a_value = '1.2.3.4'
mock.side_effect = [
MockResponse(a_value),
]
zone = Zone('example.com.', [])
DdnsSource('dynamic', types=('A',)).populate(zone)
self.assertEquals(1, len(zone.records))
mock.assert_has_calls([
call('https://v4.ident.me/'),
])
mock.assert_called_once()
@patch('requests.Session.get')
def test_types_aaaa(self, mock):
aaaa_value = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
mock.side_effect = [
MockResponse(aaaa_value),
]
zone = Zone('example.com.', [])
DdnsSource('dynamic', types=('AAAA',)).populate(zone)
self.assertEquals(1, len(zone.records))
mock.assert_has_calls([
call('https://v6.ident.me/'),
])
mock.assert_called_once()
@patch('requests.Session.get')
def test_urls(self, mock):
a_value = '1.2.3.4'
aaaa_value = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
mock.side_effect = [
MockResponse(a_value),
MockResponse(aaaa_value)
]
zone = Zone('example.com.', [])
DdnsSource('dynamic', urls={
'A': 'https://foo.bar/v4',
'AAAA': 'https://foo.bar/v6',
}).populate(zone)
records = sorted(list(zone.records))
self.assertEquals(2, len(records))
a = records[0]
self.assertEquals([a_value], a.values)
aaaa = records[1]
self.assertEquals([aaaa_value], aaaa.values)
mock.assert_has_calls([
call('https://foo.bar/v4'),
call('https://foo.bar/v6')
])