-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathauth_test.py
More file actions
136 lines (107 loc) · 4.44 KB
/
auth_test.py
File metadata and controls
136 lines (107 loc) · 4.44 KB
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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
import logging
import unittest
import mock
from apache_beam.internal.gcp import auth
from apache_beam.options.pipeline_options import GoogleCloudOptions
from apache_beam.options.pipeline_options import PipelineOptions
try:
import google.auth as gauth
import google_auth_httplib2 # pylint: disable=unused-import
except ImportError:
gauth = None # type: ignore
class MockLoggingHandler(logging.Handler):
"""Mock logging handler to check for expected logs."""
def __init__(self, *args, **kwargs):
self.reset()
logging.Handler.__init__(self, *args, **kwargs)
def emit(self, record):
self.messages[record.levelname.lower()].append(record.getMessage())
def reset(self):
self.messages = {
'debug': [],
'info': [],
'warning': [],
'error': [],
'critical': [],
}
@unittest.skipIf(gauth is None, 'Google Auth dependencies are not installed')
class AuthTest(unittest.TestCase):
@mock.patch('google.auth.default')
def test_auth_with_retrys(self, unused_mock_arg):
pipeline_options = PipelineOptions()
pipeline_options.view_as(
GoogleCloudOptions).impersonate_service_account = False
credentials = ('creds', 1)
self.is_called = False
def side_effect(scopes=None):
if self.is_called:
return credentials
else:
self.is_called = True
raise IOError('Failed')
google_auth_mock = mock.MagicMock()
gauth.default = google_auth_mock
google_auth_mock.side_effect = side_effect
# _Credentials caches the actual credentials.
# This resets it for idempotent tests.
if auth._Credentials._credentials_init:
auth._Credentials._credentials_init = False
auth._Credentials._credentials = None
returned_credentials = auth.get_service_credentials(pipeline_options)
# _Credentials caches the actual credentials.
# This resets it for idempotent tests.
if auth._Credentials._credentials_init:
auth._Credentials._credentials_init = False
auth._Credentials._credentials = None
self.assertEqual('creds', returned_credentials._google_auth_credentials)
@mock.patch(
'apache_beam.internal.gcp.auth._Credentials._get_credentials_with_retrys')
def test_auth_with_retrys_always_fail(self, unused_mock_arg):
pipeline_options = PipelineOptions()
pipeline_options.view_as(
GoogleCloudOptions).impersonate_service_account = False
loggerHandler = MockLoggingHandler()
auth._LOGGER.addHandler(loggerHandler)
#Remove call to retrying method, as otherwise test takes ~10 minutes to run
def raise_(scopes=None):
raise IOError('Failed')
retry_auth_mock = mock.MagicMock()
auth._Credentials._get_credentials_with_retrys = retry_auth_mock
retry_auth_mock.side_effect = raise_
# _Credentials caches the actual credentials.
# This resets it for idempotent tests.
if auth._Credentials._credentials_init:
auth._Credentials._credentials_init = False
auth._Credentials._credentials = None
returned_credentials = auth.get_service_credentials(pipeline_options)
self.assertEqual(None, returned_credentials)
self.assertEqual([
'Unable to find default credentials to use: Failed\n'
'Connecting anonymously. This is expected if no credentials are '
'needed to access GCP resources.'
],
loggerHandler.messages.get('warning'))
# _Credentials caches the actual credentials.
# This resets it for idempotent tests.
if auth._Credentials._credentials_init:
auth._Credentials._credentials_init = False
auth._Credentials._credentials = None
auth._LOGGER.removeHandler(loggerHandler)
if __name__ == '__main__':
unittest.main()