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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# ============LICENSE_START====================================================
# org.onap.ccsdk
# =============================================================================
# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
# =============================================================================
# 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.
# ============LICENSE_END======================================================
import pytest
import requests
import dnsdesig.dns_plugin
from cloudify.mocks import MockCloudifyContext
from cloudify.state import current_ctx
from cloudify.exceptions import NonRecoverableError
from cloudify.exceptions import RecoverableError
from cloudify import ctx
class _resp(object):
def __init__(self, code, body = None, rhdrs = None):
self.status_code = code
if rhdrs is not None:
self.headers = rhdrs
if body is not None:
self._json = body
def json(self):
return self._json
def rhdrs(self):
return self.headers
def _same(a, b):
t1 = type(a)
t2 = type(b)
if t1 != t2:
return False
if t1 == dict:
if len(a) != len(b):
return False
for k, v in a.items():
if k not in b or not _same(v, b[k]):
return False
return True
if t1 == list:
if len(a) != len(b):
return False
for i in range(len(a)):
if not _same(a[i], b[i]):
return False
return True
return a == b
class _req(object):
def __init__(self, op, url, headers, resp, json = None):
self.resp = resp
self.op = op
self.url = url
self.headers = headers
self.json = json
def check(self, op, url, headers, json):
if op != self.op or url != self.url:
return None
if self.headers is not None and not _same(self.headers, headers):
return None
if self.json is not None and not _same(self.json, json):
return None
return self.resp
_nf = _resp(404)
_ar = _resp(401)
_np = _resp(403)
_svcunavail = _resp(503)
_ok = _resp(200, { 'something': 'or-other' })
_tok = 'at'
_hdrs = { 'X-Auth-Token': _tok }
_goodos = {
'auth_url': 'https://example.com/identity/v3',
'password': 'pw',
'region': 'r',
'tenant_name': 'tn',
'username': 'un'
}
_bados = {
'auth_url': 'https://example.com/identity/v3',
'password': 'xx',
'region': 'r',
'tenant_name': 'tn',
'username': 'un'
}
_goodosv2 = {
'auth_url': 'https://example.com/identity/v2.0',
'password': 'pw',
'region': 'r',
'tenant_name': 'tn',
'username': 'un'
}
_badosv2 = {
'auth_url': 'https://example.com/identity/v2.0',
'password': 'xx',
'region': 'r',
'tenant_name': 'tn',
'username': 'un'
}
_answers = [
# Authenticate v3
_req('POST', 'https://example.com/identity/v3/auth/tokens', headers=None, resp=_resp(200, {
'token': {
'catalog': [
{
'type': 'dns',
'endpoints': [
{
'interface': 'public',
'region': 'r2',
'url': 'https://example.com/invalid2'
},
{
'interface': 'public',
'region': 'r3',
'url': 'https://example.com/invalid3'
},
{
'interface': 'public',
'url': 'https://example.com/dns'
}
]
}
]
}
}, rhdrs = {
'X-Subject-Token': _tok
}), json={
'auth': {
'identity': {
'methods': [
'password'
],
'password': {
'user': {
'name': 'un',
'domain': {
'id': 'default'
},
'password': 'pw'
}
}
},
'scope': {
'project': {
'name': 'tn',
'domain': {
'id': 'default'
}
}
}
}
}),
# Invalid authentication v3
_req('POST', 'https://example.com/identity/v3/auth/tokens', headers=None, resp=_np),
# Authenticate v2.0
_req('POST', 'https://example.com/identity/v2.0/tokens', headers=None, resp=_resp(200, {
'access': {
'token': {
'id': _tok
}, 'serviceCatalog': [
{
'type': 'dns',
'endpoints': [
{
'publicURL': 'https://example.com/dns',
'region': 'r'
},
{
'publicURL': 'https://example.com/otherregions'
}
]
}
]
}
}), json={
'auth': {
'tenantName': 'tn',
'passwordCredentials': {
'username': 'un',
'password': 'pw'
}
}
}),
# Invalid authentication v2.0
_req('POST', 'https://example.com/identity/v2.0/tokens', headers=None, resp=_np),
# Get zones
_req('GET', 'https://example.com/dns/v2/zones', headers=_hdrs, resp=_resp(200, {
'zones': [
{
'name': 'x.example.com.',
'id': 'z1'
}
]
})),
# Get recordsets
_req('GET', 'https://example.com/dns/v2/zones/z1/recordsets?limit=1000', headers=_hdrs, resp=_resp(200, {
'recordsets': [
{
'id': 'ar1',
'type': 'A',
'name': 'a.x.example.com.',
'ttl': 300,
'records': [
'87.65.43.21',
'98.76,54.32'
]
}, {
'id': 'cname1',
'type': 'CNAME',
'name': 'c.x.example.com.',
'ttl': 300,
'records': [
'a.x.example.com.'
]
}, {
'id': 'noservice',
'type': 'CNAME',
'name': 'noservice.x.example.com.',
'ttl': 300,
'records': [
'a.x.example.com.'
]
}
]
})),
# Bad auth
_req('GET', 'https://example.com/dns/v2/zones/z1/recordsets?limit=1000', headers=None, resp=_ar),
# Create A recordset
_req('POST', 'https://example.com/dns/v2/zones/z1/recordsets', headers=_hdrs, resp=_ok, json={
'type': 'A',
'name': 'b.x.example.com.',
'ttl': 300,
'records': [
'34.56.78.12'
]
}),
# Create CNAME recordset
_req('POST', 'https://example.com/dns/v2/zones/z1/recordsets', headers=_hdrs, resp=_ok, json={
'type': 'CNAME',
'name': 'd.x.example.com.',
'ttl': 300,
'records': [
'b.x.example.com.'
]
}),
# Update A recordset
_req('PUT', 'https://example.com/dns/v2/zones/z1/recordsets/ar1', headers=_hdrs, resp=_ok, json={
'ttl': 300,
'records': [
'34.56.78.12'
]
}),
# Update CNAME recordset
_req('PUT', 'https://example.com/dns/v2/zones/z1/recordsets/cname1', headers=_hdrs, resp=_ok, json={
'ttl': 300,
'records': [
'b.x.example.com.'
]
}),
# Delete A recordset
_req('DELETE', 'https://example.com/dns/v2/zones/z1/recordsets/ar1', headers=_hdrs, resp=_ok),
# Delete CNAME recordset
_req('DELETE', 'https://example.com/dns/v2/zones/z1/recordsets/cname1', headers=_hdrs, resp=_ok),
# service unavailable
_req('DELETE', 'https://example.com/dns/v2/zones/z1/recordsets/noservice', headers=_hdrs, resp=_svcunavail)
]
def _match(op, url, headers, json = None):
for choice in _answers:
ret = choice.check(op, url, headers, json)
if ret is not None:
return ret
return _nf
def _delete(url, headers):
return _match('DELETE', url, headers)
def _get(url, headers):
return _match('GET', url, headers)
def _post(url, json, headers = None):
return _match('POST', url, headers, json)
def _put(url, json, headers = None):
return _match('PUT', url, headers, json)
def _setup(os, fqdn, ttl=None):
def fcnbuilder(fcn):
def newfcn(monkeypatch):
monkeypatch.setattr(requests, 'delete', _delete)
monkeypatch.setattr(requests, 'get', _get)
monkeypatch.setattr(requests, 'post', _post)
monkeypatch.setattr(requests, 'put', _put)
properties = { 'fqdn': fqdn, 'openstack': os }
if ttl is not None:
properties['ttl'] = ttl
mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name', properties=properties)
try:
current_ctx.set(mock_ctx)
fcn()
finally:
current_ctx.clear()
return newfcn
return fcnbuilder
@_setup(_badosv2, 'a.x.example.com')
def test_dns_badauthv2():
with pytest.raises(NonRecoverableError):
dnsdesig.dns_plugin.anotneeded()
@_setup(_goodosv2, 'a.x.example.com')
def test_dns_goodauthv2():
dnsdesig.dns_plugin.anotneeded()
@_setup(_bados, 'a.x.example.com')
def test_dns_badauth():
with pytest.raises(NonRecoverableError):
dnsdesig.dns_plugin.anotneeded()
@_setup(_goodos, 'a.bad.example.com')
def test_dns_badzone():
with pytest.raises(NonRecoverableError):
dnsdesig.dns_plugin.anotneeded()
@_setup(_goodos, 'b.x.example.com', 300)
def test_dns_addarecord():
dnsdesig.dns_plugin.aneeded(args={'ip_addresses': [ '34.56.78.12' ]})
@_setup(_goodos, 'a.x.example.com', 300)
def test_dns_modarecord():
dnsdesig.dns_plugin.aneeded(args={'ip_addresses': [ '34.56.78.12' ]})
@_setup(_goodos, 'a.x.example.com')
def test_dns_delarecord():
dnsdesig.dns_plugin.anotneeded()
@_setup(_goodos, 'd.x.example.com', 300)
def test_dns_addcnamerecord():
dnsdesig.dns_plugin.cnameneeded(args={'cname': 'b.x.example.com' })
@_setup(_goodos, 'c.x.example.com', 300)
def test_dns_modcnamerecord():
dnsdesig.dns_plugin.cnameneeded(args={'cname': 'b.x.example.com' })
@_setup(_goodos, 'c.x.example.com')
def test_dns_delcname():
dnsdesig.dns_plugin.cnamenotneeded()
@_setup(_goodos, 'noservice.x.example.com')
def test_dns_delbadcname():
with pytest.raises(RecoverableError):
dnsdesig.dns_plugin.cnamenotneeded()
def test_module_logger():
dnsdesig.get_module_logger('dnsdesig')
|