1919
2020import mock
2121import requests
22+ from azure .core .exceptions import ClientAuthenticationError
23+ from azure .identity ._exceptions import CredentialUnavailableError
2224
2325from opencensus .ext .azure .common import Options
2426from opencensus .ext .azure .common .storage import LocalFileStorage
25- from opencensus .ext .azure .common .transport import TransportMixin
27+ from opencensus .ext .azure .common .transport import (
28+ _MONITOR_OAUTH_SCOPE ,
29+ TransportMixin ,
30+ )
2631
27- TEST_FOLDER = os .path .abspath ('.test.storage ' )
32+ TEST_FOLDER = os .path .abspath ('.test.transport ' )
2833
2934
3035def setUpModule ():
@@ -68,6 +73,39 @@ def test_transmission_pre_timeout(self):
6873 self .assertIsNone (mixin .storage .get ())
6974 self .assertEqual (len (os .listdir (mixin .storage .path )), 1 )
7075
76+ def test_transmission_pre_req_exception (self ):
77+ mixin = TransportMixin ()
78+ mixin .options = Options ()
79+ with LocalFileStorage (os .path .join (TEST_FOLDER , self .id ())) as stor :
80+ mixin .storage = stor
81+ mixin .storage .put ([1 , 2 , 3 ])
82+ with mock .patch ('requests.post' , throw (requests .RequestException )):
83+ mixin ._transmit_from_storage ()
84+ self .assertIsNone (mixin .storage .get ())
85+ self .assertEqual (len (os .listdir (mixin .storage .path )), 1 )
86+
87+ def test_transmission_cred_exception (self ):
88+ mixin = TransportMixin ()
89+ mixin .options = Options ()
90+ with LocalFileStorage (os .path .join (TEST_FOLDER , self .id ())) as stor :
91+ mixin .storage = stor
92+ mixin .storage .put ([1 , 2 , 3 ])
93+ with mock .patch ('requests.post' , throw (CredentialUnavailableError )): # noqa: E501
94+ mixin ._transmit_from_storage ()
95+ self .assertIsNone (mixin .storage .get ())
96+ self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
97+
98+ def test_transmission_client_exception (self ):
99+ mixin = TransportMixin ()
100+ mixin .options = Options ()
101+ with LocalFileStorage (os .path .join (TEST_FOLDER , self .id ())) as stor :
102+ mixin .storage = stor
103+ mixin .storage .put ([1 , 2 , 3 ])
104+ with mock .patch ('requests.post' , throw (ClientAuthenticationError )):
105+ mixin ._transmit_from_storage ()
106+ self .assertIsNone (mixin .storage .get ())
107+ self .assertEqual (len (os .listdir (mixin .storage .path )), 1 )
108+
71109 def test_transmission_pre_exception (self ):
72110 mixin = TransportMixin ()
73111 mixin .options = Options ()
@@ -77,7 +115,7 @@ def test_transmission_pre_exception(self):
77115 with mock .patch ('requests.post' , throw (Exception )):
78116 mixin ._transmit_from_storage ()
79117 self .assertIsNone (mixin .storage .get ())
80- self .assertEqual (len (os .listdir (mixin .storage .path )), 1 )
118+ self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
81119
82120 @mock .patch ('requests.post' , return_value = mock .Mock ())
83121 def test_transmission_lease_failure (self , requests_mock ):
@@ -120,6 +158,40 @@ def test_transmission_200(self):
120158 self .assertIsNone (mixin .storage .get ())
121159 self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
122160
161+ def test_transmission_auth (self ):
162+ mixin = TransportMixin ()
163+ mixin .options = Options ()
164+ url = 'https://dc.services.visualstudio.com'
165+ mixin .options .endpoint = url
166+ credential = mock .Mock ()
167+ mixin .options .credential = credential
168+ token_mock = mock .Mock ()
169+ token_mock .token = "test_token"
170+ credential .get_token .return_value = token_mock
171+ data = '[1, 2, 3]'
172+ headers = {
173+ 'Accept' : 'application/json' ,
174+ 'Content-Type' : 'application/json; charset=utf-8' ,
175+ 'Authorization' : 'Bearer test_token' ,
176+ }
177+ with LocalFileStorage (os .path .join (TEST_FOLDER , self .id ())) as stor :
178+ mixin .storage = stor
179+ mixin .storage .put ([1 , 2 , 3 ])
180+ with mock .patch ('requests.post' ) as post :
181+ post .return_value = MockResponse (200 , 'unknown' )
182+ mixin ._transmit_from_storage ()
183+ post .assert_called_with (
184+ url = url + '/v2.1/track' ,
185+ data = data ,
186+ headers = headers ,
187+ timeout = 10.0 ,
188+ proxies = {}
189+ )
190+ credential .get_token .assert_called_with (_MONITOR_OAUTH_SCOPE )
191+ self .assertIsNone (mixin .storage .get ())
192+ self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
193+ credential .get_token .assert_called_once ()
194+
123195 def test_transmission_206 (self ):
124196 mixin = TransportMixin ()
125197 mixin .options = Options ()
@@ -201,16 +273,16 @@ def test_transmission_206_bogus(self):
201273 self .assertIsNone (mixin .storage .get ())
202274 self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
203275
204- def test_transmission_400 (self ):
276+ def test_transmission_401 (self ):
205277 mixin = TransportMixin ()
206278 mixin .options = Options ()
207279 with LocalFileStorage (os .path .join (TEST_FOLDER , self .id ())) as stor :
208280 mixin .storage = stor
209281 mixin .storage .put ([1 , 2 , 3 ])
210282 with mock .patch ('requests.post' ) as post :
211- post .return_value = MockResponse (400 , '{}' )
283+ post .return_value = MockResponse (401 , '{}' )
212284 mixin ._transmit_from_storage ()
213- self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
285+ self .assertEqual (len (os .listdir (mixin .storage .path )), 1 )
214286
215287 def test_transmission_500 (self ):
216288 mixin = TransportMixin ()
@@ -223,3 +295,14 @@ def test_transmission_500(self):
223295 mixin ._transmit_from_storage ()
224296 self .assertIsNone (mixin .storage .get ())
225297 self .assertEqual (len (os .listdir (mixin .storage .path )), 1 )
298+
299+ def test_transmission_400 (self ):
300+ mixin = TransportMixin ()
301+ mixin .options = Options ()
302+ with LocalFileStorage (os .path .join (TEST_FOLDER , self .id ())) as stor :
303+ mixin .storage = stor
304+ mixin .storage .put ([1 , 2 , 3 ])
305+ with mock .patch ('requests.post' ) as post :
306+ post .return_value = MockResponse (400 , '{}' )
307+ mixin ._transmit_from_storage ()
308+ self .assertEqual (len (os .listdir (mixin .storage .path )), 0 )
0 commit comments