File: test_tools.py

package info (click to toggle)
python-msrestazure 0.6.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: python: 3,942; makefile: 8
file content (501 lines) | stat: -rw-r--r-- 22,592 bytes parent folder | download | duplicates (3)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#--------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved. 
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#--------------------------------------------------------------------------

import unittest
import json
try:
    from unittest import mock
except ImportError:
    import mock

import requests
import httpretty

from msrestazure.azure_configuration import AzureConfiguration
from msrestazure.tools import parse_resource_id, is_valid_resource_id, resource_id, is_valid_resource_name

class TestTools(unittest.TestCase):

    @httpretty.activate
    @mock.patch('time.sleep', return_value=None)
    def test_register_rp_hook(self, time_sleep):
        """Protocol:
        - We call the provider and get a 409 provider error
        - Now we POST register provider and get "Registering"
        - Now we GET register provider and get "Registered"
        - We call again the first endpoint and this time this succeed
        """

        provider_url = ("https://management.azure.com/"
                        "subscriptions/12345678-9abc-def0-0000-000000000000/"
                        "resourceGroups/clitest.rg000001/"
                        "providers/Microsoft.Sql/servers/ygserver123?api-version=2014-04-01")

        provider_error = ('{"error":{"code":"MissingSubscriptionRegistration", '
                          '"message":"The subscription registration is in \'Unregistered\' state. '
                          'The subscription must be registered to use namespace \'Microsoft.Sql\'. '
                          'See https://aka.ms/rps-not-found for how to register subscriptions."}}')

        provider_success = '{"success": true}'

        httpretty.register_uri(httpretty.PUT,
                               provider_url,
                               responses=[
                                   httpretty.Response(body=provider_error, status=409),
                                   httpretty.Response(body=provider_success),
                               ],
                               content_type="application/json")

        register_post_url = ("https://management.azure.com/"
                             "subscriptions/12345678-9abc-def0-0000-000000000000/"
                             "providers/Microsoft.Sql/register?api-version=2016-02-01")

        register_post_result = {
            "registrationState":"Registering"
        }

        register_get_url = ("https://management.azure.com/"
                            "subscriptions/12345678-9abc-def0-0000-000000000000/"
                            "providers/Microsoft.Sql?api-version=2016-02-01")

        register_get_result = {
            "registrationState":"Registered"
        }

        httpretty.register_uri(httpretty.POST,
                               register_post_url,
                               body=json.dumps(register_post_result),
                               content_type="application/json")

        httpretty.register_uri(httpretty.GET,
                               register_get_url,
                               body=json.dumps(register_get_result),
                               content_type="application/json")

        configuration = AzureConfiguration(None)
        register_rp_hook = configuration.hooks[0]

        session = requests.Session()
        def rp_cb(r, *args, **kwargs):
            kwargs.setdefault("msrest", {})["session"] = session
            return register_rp_hook(r, *args, **kwargs)
        session.hooks['response'].append(rp_cb)

        response = session.put(provider_url)
        self.assertTrue(response.json()['success'])

    @httpretty.activate
    @mock.patch('time.sleep', return_value=None)
    def test_register_failed(self, time_sleep):
        """Protocol:
        - We call the provider and get a 409 provider error
        - Now we POST register provider and get "Registering"
        - This POST failed
        """

        provider_url = ("https://management.azure.com/"
                        "subscriptions/12345678-9abc-def0-0000-000000000000/"
                        "resourceGroups/clitest.rg000001/"
                        "providers/Microsoft.Sql/servers/ygserver123?api-version=2014-04-01")

        provider_error = ('{"error":{"code":"MissingSubscriptionRegistration", '
                          '"message":"The subscription registration is in \'Unregistered\' state. '
                          'The subscription must be registered to use namespace \'Microsoft.Sql\'. '
                          'See https://aka.ms/rps-not-found for how to register subscriptions."}}')

        provider_success = '{"success": true}'

        httpretty.register_uri(httpretty.PUT,
                               provider_url,
                               responses=[
                                   httpretty.Response(body=provider_error, status=409),
                                   httpretty.Response(body=provider_success),
                               ],
                               content_type="application/json")

        register_post_url = ("https://management.azure.com/"
                             "subscriptions/12345678-9abc-def0-0000-000000000000/"
                             "providers/Microsoft.Sql/register?api-version=2016-02-01")

        httpretty.register_uri(httpretty.POST,
                               register_post_url,
                               status=409,
                               content_type="application/json")

        configuration = AzureConfiguration(None)
        register_rp_hook = configuration.hooks[0]

        session = requests.Session()
        def rp_cb(r, *args, **kwargs):
            kwargs.setdefault("msrest", {})["session"] = session
            return register_rp_hook(r, *args, **kwargs)
        session.hooks['response'].append(rp_cb)

        response = session.put(provider_url)
        self.assertEqual(409, response.status_code)

    def test_resource_parse(self):
        """
        Tests resource id parsing, reforming, and validation.
        """
        tests = [
            {
                'resource_id': '/subscriptions/fakesub/resourcegroups/testgroup/providers'
                               '/Microsoft.Storage/storageAccounts/foo/providers'
                               '/Microsoft.Authorization/locks/bar',
                'expected': {
                    'name': 'foo',
                    'type': 'storageAccounts',
                    'namespace': 'Microsoft.Storage',
                    'child_name_1': 'bar',
                    'child_namespace_1': 'Microsoft.Authorization',
                    'child_type_1': 'locks',
                    'child_parent_1': 'storageAccounts/foo/providers/Microsoft.Authorization/',
                    'resource_group': 'testgroup',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/fakesub/resourcegroups/testgroup/providers'
                               '/Microsoft.Storage/storageAccounts/foo'
                               '/locks/bar',
                'expected': {
                    'name': 'foo',
                    'type': 'storageAccounts',
                    'namespace': 'Microsoft.Storage',
                    'child_name_1': 'bar',
                    'child_type_1': 'locks',
                    'child_parent_1': 'storageAccounts/foo/',
                    'resource_group': 'testgroup',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/fakesub/resourcegroups/testgroup/providers'
                               '/Microsoft.Storage/storageAccounts/foo/providers'
                               '/Microsoft.Authorization/locks/bar/providers/Microsoft.Network/'
                               'nets/gc',
                'expected': {
                    'name': 'foo',
                    'type': 'storageAccounts',
                    'namespace': 'Microsoft.Storage',
                    'child_name_1': 'bar',
                    'child_namespace_1': 'Microsoft.Authorization',
                    'child_type_1': 'locks',
                    'child_parent_1': 'storageAccounts/foo/providers/Microsoft.Authorization/',
                    'child_name_2': 'gc',
                    'child_namespace_2': 'Microsoft.Network',
                    'child_type_2': 'nets',
                    'child_parent_2': 'storageAccounts/foo/providers/Microsoft.Authorization/'
                                      'locks/bar/providers/Microsoft.Network/',
                    'resource_group': 'testgroup',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/fakesub/resourcegroups/testgroup/providers'
                               '/Microsoft.Storage/storageAccounts/foo'
                               '/locks/bar/nets/gc',
                'expected': {
                    'name': 'foo',
                    'type': 'storageAccounts',
                    'namespace': 'Microsoft.Storage',
                    'child_name_1': 'bar',
                    'child_type_1': 'locks',
                    'child_parent_1': 'storageAccounts/foo/',
                    'child_name_2': 'gc',
                    'child_type_2': 'nets',
                    'child_parent_2': 'storageAccounts/foo/locks/bar/',
                    'resource_group': 'testgroup',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/providers/'
                               'Microsoft.Provider1/resourceType1/name1',
                'expected': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1',
                    'type': 'resourceType1',
                    'name': 'name1',
                    'resource_parent': '',
                    'resource_namespace': 'Microsoft.Provider1',
                    'resource_type': 'resourceType1',
                    'resource_name': 'name1'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/providers/'
                               'Microsoft.Provider1/resourceType1/name1/resourceType2/name2',
                'expected': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1',
                    'type': 'resourceType1',
                    'name': 'name1',
                    'child_namespace_1': None,
                    'child_type_1': 'resourceType2',
                    'child_name_1': 'name2',
                    'child_parent_1': 'resourceType1/name1/',
                    'resource_parent': 'resourceType1/name1/',
                    'resource_namespace': 'Microsoft.Provider1',
                    'resource_type': 'resourceType2',
                    'resource_name': 'name2'
                }
            },
            {
                'resource_id': '/subscriptions/00000/resourceGroups/myRg/providers/'
                               'Microsoft.RecoveryServices/vaults/vault_name/backupFabrics/'
                               'fabric_name/protectionContainers/container_name/'
                               'protectedItems/item_name/recoveryPoint/recovery_point_guid',
                'expected': {
                    'subscription': '00000',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.RecoveryServices',
                    'type': 'vaults',
                    'name': 'vault_name',
                    'child_type_1': 'backupFabrics',
                    'child_name_1': 'fabric_name',
                    'child_parent_1': 'vaults/vault_name/',
                    'child_type_2': 'protectionContainers',
                    'child_name_2': 'container_name',
                    'child_parent_2': 'vaults/vault_name/backupFabrics/fabric_name/',
                    'child_type_3': 'protectedItems',
                    'child_name_3': 'item_name',
                    'child_parent_3': 'vaults/vault_name/backupFabrics/fabric_name/'
                                      'protectionContainers/container_name/',
                    'child_type_4': 'recoveryPoint',
                    'child_name_4': 'recovery_point_guid',
                    'child_parent_4': 'vaults/vault_name/backupFabrics/fabric_name/'
                                      'protectionContainers/container_name/protectedItems/'
                                      'item_name/',
                    'resource_parent': 'vaults/vault_name/backupFabrics/fabric_name/'
                                       'protectionContainers/container_name/protectedItems/'
                                       'item_name/',
                    'resource_namespace': 'Microsoft.RecoveryServices',
                    'resource_type': 'recoveryPoint',
                    'resource_name': 'recovery_point_guid'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/providers/'
                               'Microsoft.Provider1/resourceType1/name1/resourceType2/name2/'
                               'providers/Microsoft.Provider3/resourceType3/name3',
                'expected': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1',
                    'type': 'resourceType1',
                    'name': 'name1',
                    'child_namespace_1': None,
                    'child_type_1': 'resourceType2',
                    'child_name_1': 'name2',
                    'child_parent_1' : 'resourceType1/name1/',
                    'child_namespace_2': 'Microsoft.Provider3',
                    'child_type_2': 'resourceType3',
                    'child_name_2': 'name3',
                    'child_parent_2': 'resourceType1/name1/resourceType2/name2/'
                                      'providers/Microsoft.Provider3/',
                    'resource_parent': 'resourceType1/name1/resourceType2/name2/'
                                       'providers/Microsoft.Provider3/',
                    'resource_namespace': 'Microsoft.Provider1',
                    'resource_type': 'resourceType3',
                    'resource_name': 'name3'
                }
            },
            {
                'resource_id': '/subscriptions/fakesub/providers/Microsoft.Authorization'
                               '/locks/foo',
                'expected': {
                    'name': 'foo',
                    'type': 'locks',
                    'namespace': 'Microsoft.Authorization',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/Subscriptions/fakesub/providers/Microsoft.Authorization'
                               '/locks/foo',
                'expected': {
                    'name': 'foo',
                    'type': 'locks',
                    'namespace': 'Microsoft.Authorization',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg',
                'expected': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg'
                }
            }
        ]
        for test in tests:
            self.assertTrue(is_valid_resource_id(test['resource_id']))
            kwargs = parse_resource_id(test['resource_id'])
            for key in test['expected']:
                try:
                    self.assertEqual(kwargs[key], test['expected'][key])
                except KeyError:
                    self.assertTrue(key not in kwargs and test['expected'][key] is None)

        invalid_ids = [
            '/subscriptions/fakesub/resourceGroups/myRg/type1/name1',
            '/subscriptions/fakesub/resourceGroups/myRg/providers/Microsoft.Provider/foo',
            '/subscriptions/fakesub/resourceGroups/myRg/providers/namespace/type/name/type1'
        ]
        for invalid_id in invalid_ids:
            self.assertFalse(is_valid_resource_id(invalid_id))

        tests = [
            {
                'resource_id': '/subscriptions/fakesub/resourcegroups/testgroup/providers'
                               '/Microsoft.Storage/storageAccounts/foo/providers'
                               '/Microsoft.Authorization/locks/bar',
                'id_args': {
                    'name': 'foo',
                    'type': 'storageAccounts',
                    'namespace': 'Microsoft.Storage',
                    'child_name_1': 'bar',
                    'child_namespace_1': 'Microsoft.Authorization',
                    'child_type_1': 'locks',
                    'resource_group': 'testgroup',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/fakesub/resourcegroups/testgroup/providers'
                               '/Microsoft.Storage/storageAccounts/foo'
                               '/locks/bar',
                'id_args': {
                    'name': 'foo',
                    'type': 'storageAccounts',
                    'namespace': 'Microsoft.Storage',
                    'child_name_1': 'bar',
                    'child_type_1': 'locks',
                    'resource_group': 'testgroup',
                    'subscription': 'fakesub',
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/providers/'
                               'Microsoft.Provider1/resourceType1/name1/resourceType2/name2/'
                               'providers/Microsoft.Provider3/resourceType3/name3',
                'id_args': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1',
                    'type': 'resourceType1',
                    'name': 'name1',
                    'child_type_1': 'resourceType2',
                    'child_name_1': 'name2',
                    'child_namespace_2': 'Microsoft.Provider3',
                    'child_type_2': 'resourceType3',
                    'child_name_2': 'name3'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/'
                               'providers/Microsoft.Provider1',
                'id_args': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg',
                'id_args': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/'
                               'providers/Microsoft.Provider1/resourceType1/name1/resourceType2/'
                               'name2/providers/Microsoft.Provider3',
                'id_args': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1',
                    'type': 'resourceType1',
                    'name': 'name1',
                    'child_type_1': 'resourceType2',
                    'child_name_1': 'name2',
                    'child_namespace_2': 'Microsoft.Provider3'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg/'
                               'providers/Microsoft.Provider1/resourceType1/name1',
                'id_args': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg',
                    'namespace': 'Microsoft.Provider1',
                    'type': 'resourceType1',
                    'name': 'name1',
                    'child_type_1': None,
                    'child_name_1': 'name2',
                    'child_namespace_2': 'Microsoft.Provider3'
                }
            },
            {
                'resource_id': '/subscriptions/mySub/resourceGroups/myRg',
                'id_args': {
                    'subscription': 'mySub',
                    'resource_group': 'myRg'
                }
            }
        ]
        for test in tests:
            rsrc_id = resource_id(**test['id_args'])
            self.assertEqual(rsrc_id.lower(), test['resource_id'].lower())

    def test_is_resource_name(self):
        invalid_names = [
            '',
            'knights/ni',
            'spam&eggs',
            'i<3you',
            'a' * 261,
        ]

        for test in invalid_names:
            assert not is_valid_resource_name(test)

        valid_names = [
            'abc-123',
            ' ',  # no one said it had to be a good resource name.
            'a' * 260,
        ]

        for test in valid_names:
            assert is_valid_resource_name(test)


if __name__ == "__main__":
    unittest.main()