Debian Codegraph @

tests/test_mock.py

raw
1 # -*- coding: utf-8 -*-
2 # coding=utf-8
3 # --------------------------------------------------------------------------
4 # Copyright (c) Microsoft Corporation. All rights reserved.
5 # Licensed under the MIT License. See License.txt in the project root for
6 # license information.
7 # --------------------------------------------------------------------------
8
9 """
10 Tests to be executed with mock framework (HTTPretty) rather than actual server.
11
12 Do not use mock framework for functionality tests like end to end operation calls.
13 Only use it for specific internal logic which can't be tested on server.
14 Example: How will the logic behave in case of specific error from server side.
15
16 This was introduced to test the Retry Policy but can be carefully used for other tests as well.
17 """
18 import pytest
19 import responses
20 from requests import ConnectionError, ConnectTimeout, ReadTimeout, Timeout, HTTPError
21
22 from tests import settings
23 from azure.datalake.store.exceptions import DatalakeRESTException
24 from tests.testing import azure, azure_teardown, posix, working_dir
25 from tests.settings import TENANT_ID, CLIENT_ID
26 test_dir = working_dir()
27
28 a = posix(test_dir / 'a')
29 mock_url = 'https://%s.azuredatalakestore.net/webhdfs/v1/' % settings.STORE_NAME
30
31
32 def test_retry_read_timeout(azure):
33 __test_retry_error(azure, 200, 2, body=ReadTimeout())
34
35
36 def test_retry_timeout(azure):
37 __test_retry_error(azure, 200, 2, body=Timeout())
38
39
40 def test_retry_connection_error(azure):
41 __test_retry_error(azure, 200, 2, body=ConnectionError())
42
43
44 def test_retry_connection_timeout(azure):
45 __test_retry_error(azure, 200, 2, body=ConnectTimeout())
46
47
48 def test_retry_500(azure):
49 __test_retry_error(azure, 500, 2)
50
51
52 def test_retry_401(azure):
53 __test_retry_error(azure, 401, 3)
54
55
56 def test_retry_408(azure):
57 __test_retry_error(azure, 408, 4)
58
59
60 def test_retry_429(azure):
61 __test_retry_error(azure, 429, 2)
62
63
64 def test_retry_500_5retry(azure):
65 __test_retry_error(azure, 500, 5)
66
67
68 def test_retry_500_6retry(azure):
69 # exceeded max tries
70 __test_retry_error(azure, 500, 6, is_exception_expected=True)
71
72
73 def test_retry_400(azure):
74 __test_retry_error(azure, 400, 2, is_exception_expected=True)
75
76
77 def test_retry_501(azure):
78 __test_retry_error(azure, 501, 2, is_exception_expected=True)
79
80
81 def test_retry_505(azure):
82 __test_retry_error(azure, 505, 2, is_exception_expected=True)
83
84
85 def test_retry_200(azure):
86 __test_retry_error(azure, 200, 1)
87
88 @responses.activate
89 def __test_retry_error(azure,
90 error_status,
91 total_tries,
92 is_exception_expected=False,
93 last_try_status=200,
94 body=""):
95 mock_url_a = mock_url + a
96 while total_tries>1:
97 responses.add(responses.PUT, mock_url_a,
98 body=body, status=error_status)
99 total_tries -= 1
100 responses.add(responses.PUT, mock_url_a,
101 body="", status=last_try_status)
102
103 # teardown not required in mock tests
104 try:
105 azure.mkdir(a)
106 assert not is_exception_expected
107 except DatalakeRESTException:
108 assert is_exception_expected
109
110