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
|
# Author: Federico Ceratto <federico.ceratto@hpe.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock
import dns
import dns.rdataclass
import dns.rdatatype
import designate.backend.agent as agent
import designate.backend.private_codes as pcodes
from designate import exceptions
from designate.mdns import rpcapi as mdns_api
from designate import objects
from designate import tests
from designate.tests.unit import RoObject
class AgentBackendTestCase(tests.TestCase):
def setUp(self):
super(AgentBackendTestCase, self).setUp()
self.CONF.set_override('poll_timeout', 1, 'service:worker')
self.CONF.set_override('poll_retry_interval', 4,
'service:worker')
self.CONF.set_override('poll_max_retries', 5, 'service:worker')
self.CONF.set_override('poll_delay', 6, 'service:worker')
self.context = self.get_context()
self.zone = objects.Zone(
id='e2bed4dc-9d01-11e4-89d3-123b93f75cba',
name='example.com.',
email='example@example.com',
)
self.target = {
'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
'type': 'agent',
'masters': [],
'options': [
{'key': 'host', 'value': 2},
{'key': 'port', 'value': 3},
],
}
self.backend = agent.AgentPoolBackend(
objects.PoolTarget.from_dict(self.target)
)
@mock.patch.object(mdns_api.MdnsAPI, 'get_instance')
def test_mdns_api(self, mock_get_instance):
self.assertIsInstance(self.backend.mdns_api, mock.Mock)
@mock.patch.object(mdns_api.MdnsAPI, 'get_instance')
def test_create_zone(self, mock_get_instance):
self.backend._make_and_send_dns_message = mock.Mock(
return_value=(1, 2))
out = self.backend.create_zone(self.context, self.zone)
self.backend._make_and_send_dns_message.assert_called_with(
self.zone.name, 1, 14, pcodes.CREATE, pcodes.SUCCESS, 2, 3)
self.assertIsNone(out)
@mock.patch.object(mdns_api.MdnsAPI, 'get_instance')
def test_create_zone_exception(self, mock_get_instance):
self.backend._make_and_send_dns_message = mock.Mock(
return_value=(None, 2))
self.assertRaisesRegex(
exceptions.Backend, 'create_zone.* failed',
self.backend.create_zone, self.context, self.zone,
)
self.backend._make_and_send_dns_message.assert_called_with(
self.zone.name, 1, 14, pcodes.CREATE, pcodes.SUCCESS, 2, 3)
@mock.patch.object(mdns_api.MdnsAPI, 'get_instance')
def test_update_zone(self, mock_get_instance):
self.backend.mdns_api.notify_zone_changed = mock.Mock()
out = self.backend.update_zone(self.context, self.zone)
self.backend.mdns_api.notify_zone_changed.assert_called_with(
self.context, self.zone, 2, 3, 1, 4, 5, 6)
self.assertIsNone(out)
@mock.patch.object(mdns_api.MdnsAPI, 'get_instance')
def test_delete_zone(self, mock_get_instance):
self.backend._make_and_send_dns_message = mock.Mock(
return_value=(1, 2))
out = self.backend.delete_zone(self.context, self.zone)
self.backend._make_and_send_dns_message.assert_called_with(
self.zone.name, 1, 14, pcodes.DELETE, pcodes.SUCCESS, 2, 3)
self.assertIsNone(out)
@mock.patch.object(mdns_api.MdnsAPI, 'get_instance')
def test_delete_zone_exception(self, mock_get_instance):
self.backend._make_and_send_dns_message = mock.Mock(
return_value=(None, 2))
self.assertRaisesRegex(
exceptions.Backend, 'failed delete_zone',
self.backend.delete_zone, self.context, self.zone,
)
self.backend._make_and_send_dns_message.assert_called_with(
self.zone.name, 1, 14, pcodes.DELETE, pcodes.SUCCESS, 2, 3)
def test_make_and_send_dns_message_timeout(self):
self.backend._make_dns_message = mock.Mock(return_value='')
self.backend._send_dns_message = mock.Mock(
return_value=dns.exception.Timeout())
out = self.backend._make_and_send_dns_message('h', 123, 1, 2, 3, 4, 5)
self.assertEqual((None, 0), out)
def test_make_and_send_dns_message_bad_response(self):
self.backend._make_dns_message = mock.Mock(return_value='')
self.backend._send_dns_message = mock.Mock(
return_value=agent.dns_query.BadResponse())
out = self.backend._make_and_send_dns_message('h', 123, 1, 2, 3, 4, 5)
self.assertEqual((None, 0), out)
def test_make_and_send_dns_message_missing_AA_flags(self):
self.backend._make_dns_message = mock.Mock(return_value='')
response = RoObject(
rcode=mock.Mock(return_value=dns.rcode.NOERROR),
# rcode is NOERROR but (flags & dns.flags.AA) gives 0
flags=0,
)
self.backend._send_dns_message = mock.Mock(return_value=response)
out = self.backend._make_and_send_dns_message('h', 123, 1, 2, 3, 4, 5)
self.assertEqual((None, 0), out)
def test_make_and_send_dns_message_error_flags(self):
self.backend._make_dns_message = mock.Mock(return_value='')
response = RoObject(
rcode=mock.Mock(return_value=dns.rcode.NOERROR),
# rcode is NOERROR but flags are not NOERROR
flags=123,
ednsflags=321
)
self.backend._send_dns_message = mock.Mock(return_value=response)
out = self.backend._make_and_send_dns_message('h', 123, 1, 2, 3, 4, 5)
self.assertEqual((None, 0), out)
def test_make_and_send_dns_message(self):
self.backend._make_dns_message = mock.Mock(return_value='')
response = RoObject(
rcode=mock.Mock(return_value=dns.rcode.NOERROR),
flags=agent.dns.flags.AA,
ednsflags=321
)
self.backend._send_dns_message = mock.Mock(return_value=response)
out = self.backend._make_and_send_dns_message('h', 123, 1, 2, 3, 4, 5)
self.assertEqual((response, 0), out)
@mock.patch.object(agent.dns_query, 'tcp')
@mock.patch.object(agent.dns_query, 'udp')
def test_send_dns_message(self, mock_udp, mock_tcp):
mock_udp.return_value = 'mock udp resp'
out = self.backend._send_dns_message('msg', 'host', 123, 1)
self.assertFalse(agent.dns_query.tcp.called)
agent.dns_query.udp.assert_called_with('msg', 'host', port=123,
timeout=1)
self.assertEqual('mock udp resp', out)
@mock.patch.object(agent.dns_query, 'tcp')
@mock.patch.object(agent.dns_query, 'udp')
def test_send_dns_message_timeout(self, mock_udp, mock_tcp):
mock_udp.side_effect = dns.exception.Timeout
out = self.backend._send_dns_message('msg', 'host', 123, 1)
agent.dns_query.udp.assert_called_with('msg', 'host', port=123,
timeout=1)
self.assertIsInstance(out, dns.exception.Timeout)
@mock.patch.object(agent.dns_query, 'tcp')
@mock.patch.object(agent.dns_query, 'udp')
def test_send_dns_message_bad_response(self, mock_udp, mock_tcp):
mock_udp.side_effect = agent.dns_query.BadResponse
out = self.backend._send_dns_message('msg', 'host', 123, 1)
agent.dns_query.udp.assert_called_with('msg', 'host', port=123,
timeout=1)
self.assertIsInstance(out, agent.dns_query.BadResponse)
@mock.patch.object(agent.dns_query, 'tcp')
@mock.patch.object(agent.dns_query, 'udp')
def test_send_dns_message_tcp(self, mock_udp, mock_tcp):
self.CONF.set_override('all_tcp', True, 'service:mdns')
mock_tcp.return_value = 'mock tcp resp'
out = self.backend._send_dns_message('msg', 'host', 123, 1)
self.assertFalse(agent.dns_query.udp.called)
agent.dns_query.tcp.assert_called_with('msg', 'host', port=123,
timeout=1)
self.assertEqual('mock tcp resp', out)
|