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
|
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Author: Endre Karlson <endre.karlson@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.
"""
Unit test Producer tasks
"""
import datetime
from unittest import mock
import fixtures
import oslotest.base
from oslo_config import cfg
from oslo_config import fixture as cfg_fixture
from oslo_utils import timeutils
from designate import context
from designate import rpc
from designate.central import rpcapi as central_api
from designate.producer import tasks
from designate.tests.unit import RoObject
from designate.utils import generate_uuid
DUMMY_TASK_GROUP = cfg.OptGroup(
name='producer_task:dummy',
title='Configuration for Producer Task: Dummy Task'
)
DUMMY_TASK_OPTS = [
cfg.IntOpt('per_page', default=100,
help='Default amount of results returned per page'),
]
CONF = cfg.CONF
CONF.register_group(DUMMY_TASK_GROUP)
CONF.register_opts(DUMMY_TASK_OPTS, group=DUMMY_TASK_GROUP)
class DummyTask(tasks.PeriodicTask):
"""Dummy task used to test helper functions"""
__plugin_name__ = 'dummy'
class PeriodicTest(oslotest.base.BaseTestCase):
def setUp(self):
super(PeriodicTest, self).setUp()
self.useFixture(cfg_fixture.Config(CONF))
self.task = DummyTask()
self.task.my_partitions = range(0, 10)
@mock.patch.object(central_api.CentralAPI, 'get_instance')
def test_iter_zones_no_items(self, get_central):
# Test that the iteration code is working properly.
central = mock.Mock()
get_central.return_value = central
ctxt = mock.Mock()
iterer = self.task._iter_zones(ctxt)
central.find_zones.return_value = []
self.assertRaises(StopIteration, next, iterer)
@mock.patch.object(central_api.CentralAPI, 'get_instance')
def test_iter_zones(self, get_central):
# Test that the iteration code is working properly.
central = mock.Mock()
get_central.return_value = central
ctxt = mock.Mock()
iterer = self.task._iter_zones(ctxt)
items = [RoObject(id=generate_uuid()) for i in range(0, 5)]
central.find_zones.return_value = items
# Iterate through the items causing the "paging" to be done.
list(map(lambda i: next(iterer), items))
central.find_zones.assert_called_once_with(
ctxt, {"shard": "BETWEEN 0,9"}, limit=100)
central.find_zones.reset_mock()
# Call next on the iterator and see it trying to load a new page.
# Also this will raise a StopIteration since there are no more items.
central.find_zones.return_value = []
self.assertRaises(StopIteration, next, iterer)
central.find_zones.assert_called_once_with(
ctxt,
{"shard": "BETWEEN 0,9"},
marker=items[-1].id,
limit=100
)
def test_my_range(self):
self.assertEqual((0, 9), self.task._my_range())
class PeriodicExistsTest(oslotest.base.BaseTestCase):
def setUp(self):
super(PeriodicExistsTest, self).setUp()
self.useFixture(cfg_fixture.Config(CONF))
CONF.set_override('interval', 5, 'producer_task:periodic_exists')
# Mock a ctxt...
self.ctxt = mock.Mock()
self.useFixture(fixtures.MockPatchObject(
context.DesignateContext, 'get_admin_context',
return_value=self.ctxt
))
# Patch get_notifier so that it returns a mock..
self.mock_notifier = mock.Mock()
self.useFixture(fixtures.MockPatchObject(
rpc, 'get_notifier',
return_value=self.mock_notifier
))
self.task = tasks.PeriodicExistsTask()
self.task.my_partitions = range(0, 10)
# Install our own period results to verify that the end / start is
# correct below
self.period = tasks.PeriodicExistsTask._get_period(2)
self.period_data = {
"audit_period_beginning": self.period[0],
"audit_period_ending": self.period[1]
}
self.useFixture(fixtures.MockPatchObject(
tasks.PeriodicExistsTask, '_get_period',
return_value=self.period
))
def test_emit_exists(self):
zone = RoObject(id=generate_uuid())
with mock.patch.object(self.task, '_iter_zones') as iter_:
iter_.return_value = [zone]
self.task()
data = dict(zone)
data.update(self.period_data)
# Ensure both the old (domain) and new (zone) events are fired
# until the old is fully deprecated.
self.mock_notifier.info.assert_any_call(
self.ctxt, "dns.domain.exists", data)
self.mock_notifier.info.assert_any_call(
self.ctxt, "dns.zone.exists", data)
def test_emit_exists_no_zones(self):
with mock.patch.object(self.task, '_iter_zones') as iter_:
iter_.return_value = []
self.task()
self.assertFalse(self.mock_notifier.info.called)
def test_emit_exists_multiple_zones(self):
zones = [RoObject()] * 10
with mock.patch.object(self.task, '_iter_zones') as iter_:
iter_.return_value = zones
self.task()
for zone in zones:
data = dict(zone)
data.update(self.period_data)
# Ensure both the old (domain) and new (zone) events are fired
# until the old is fully deprecated.
self.mock_notifier.info.assert_any_call(
self.ctxt, "dns.domain.exists", data)
self.mock_notifier.info.assert_any_call(
self.ctxt, "dns.zone.exists", data)
class PeriodicSecondaryRefreshTest(oslotest.base.BaseTestCase):
def setUp(self):
super(PeriodicSecondaryRefreshTest, self).setUp()
self.useFixture(cfg_fixture.Config(CONF))
# Mock a ctxt...
self.ctxt = mock.Mock()
self.useFixture(fixtures.MockPatchObject(
context.DesignateContext, 'get_admin_context',
return_value=self.ctxt
))
# Mock a central...
self.central = mock.Mock()
self.useFixture(fixtures.MockPatchObject(
central_api.CentralAPI, 'get_instance',
return_value=self.central
))
self.task = tasks.PeriodicSecondaryRefreshTask()
self.task.my_partitions = 0, 9
def test_refresh_no_zone(self):
with mock.patch.object(self.task, '_iter') as _iter:
_iter.return_value = []
self.task()
self.assertFalse(self.central.xfr_zone.called)
def test_refresh_zone(self):
transferred = timeutils.utcnow(True) - datetime.timedelta(minutes=62)
zone = RoObject(
id=generate_uuid(),
transferred_at=datetime.datetime.isoformat(transferred),
refresh=3600)
with mock.patch.object(self.task, '_iter') as _iter:
_iter.return_value = [zone]
self.task()
self.central.xfr_zone.assert_called_once_with(self.ctxt, zone.id)
def test_refresh_zone_not_expired(self):
# Dummy zone object
transferred = timeutils.utcnow(True) - datetime.timedelta(minutes=50)
zone = RoObject(
id=generate_uuid(),
transferred_at=datetime.datetime.isoformat(transferred),
refresh=3600
)
with mock.patch.object(self.task, '_iter') as _iter:
_iter.return_value = [zone]
self.task()
self.assertFalse(self.central.xfr_zone.called)
|