forked from asheiduk/dyndns53
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_get_ips.py
133 lines (106 loc) · 4.11 KB
/
test_get_ips.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from ipaddress import IPv4Address, IPv6Address
import json
import logging
import unittest
from unittest.case import skip
from unittest.mock import MagicMock
import dyndns53
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
# logging.basicConfig()
class GetIPsTest(unittest.TestCase):
zone_id = 'Z1234567890ABC'
host = "test1.example.com."
host_next = "test2.example.com."
ipv4 = IPv4Address('172.16.0.1')
ipv6 = IPv6Address('2001::1')
def setUp(self):
dyndns53.client53 = MagicMock()
self.list_mock = dyndns53.client53.list_resource_record_sets
#------------------------------------------------------------------------------
def test_ipv4_and_ipv6(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'A', 3600, self.ipv4),
self._rrs(self.host, 'AAAA', 300, self.ipv6),
self._rrs(self.host, 'CAA', 300, '0 issue ";"'),
)
self._do(self.ipv4, self.ipv6)
#------------------------------------------------------------------------------
def test_no_address_with_followup_other_type(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'CAA', 300, '0 issue ";"'),
)
self._do(None, None)
def test_no_host_with_followup_other_host(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host_next, 'A', 3600, self.ipv4),
)
self._do(None, None)
def test_no_host_with_no_followup(self):
self.list_mock.return_value = self._rrss(
)
self._do(None, None)
#------------------------------------------------------------------------------
def test_ipv4_with_followup_same_host(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'A', 3600, self.ipv4),
self._rrs(self.host, 'CAA', 300, '0 issue ";"'),
)
self._do(self.ipv4, None)
def test_ipv4_with_followup_other_host(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'A', 3600, self.ipv4),
self._rrs(self.host_next, 'AAAA', 3600, self.ipv6),
)
self._do(self.ipv4, None)
def test_ipv4_with_no_followup(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'A', 3600, self.ipv4),
)
self._do(self.ipv4, None)
#------------------------------------------------------------------------------
def test_ipv6_with_followup_same_host(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'AAAA', 3600, self.ipv6),
self._rrs(self.host, 'CAA', 300, '0 issue ";"'),
)
self._do(None, self.ipv6)
def test_ipv6_with_followup_other_host(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'AAAA', 3600, self.ipv6),
self._rrs(self.host_next, 'A', 3600, self.ipv4),
)
self._do(None, self.ipv6)
def test_ipv6_with_no_followup(self):
self.list_mock.return_value = self._rrss(
self._rrs(self.host, 'AAAA', 3600, self.ipv6),
)
self._do(None, self.ipv6)
#------------------------------------------------------------------------------
def _do(self, expected_ipv4, expected_ipv6):
result = dyndns53.get_ips(self.host, self.zone_id)
dyndns53.client53.list_resource_record_sets.assert_called_once_with(
HostedZoneId = self.zone_id,
StartRecordName = self.host,
StartRecordType = 'A',
MaxItems = '2'
)
self.assertEquals(result, (expected_ipv4, expected_ipv6))
def _rrss(self, *rrs):
return {
'ResourceRecordSets': rrs
}
def _rrs(self, name: str, type: str, ttl: int, *values: str):
return {
'Name': name,
'Type': type,
'TTL': ttl,
'ResourceRecords': [
{
'Value': str(value)
}
for value in values
]
}