def test_transmission_206_500(self): mixin = TransportMixin() mixin.options = Options() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3, 4, 5]) with mock.patch('requests.post') as post: post.return_value = MockResponse( 206, json.dumps({ 'itemsReceived': 5, 'itemsAccepted': 3, 'errors': [ { 'index': 0, 'statusCode': 400, 'message': '', }, { 'index': 2, 'statusCode': 500, 'message': 'Internal Server Error', }, ], })) mixin._transmit_from_storage() self.assertEqual(len(os.listdir(mixin.storage.path)), 1) self.assertEqual(mixin.storage.get().get(), (3, ))
def test_transmission_auth(self): mixin = TransportMixin() mixin.options = Options() url = 'https://dc.services.visualstudio.com' mixin.options.endpoint = url credential = mock.Mock() mixin.options.credential = credential token_mock = mock.Mock() token_mock.token = "test_token" credential.get_token.return_value = token_mock data = '[1, 2, 3]' headers = { 'Accept': 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': 'Bearer test_token', } with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3]) with mock.patch('requests.post') as post: post.return_value = MockResponse(200, 'unknown') mixin._transmit_from_storage() post.assert_called_with(url=url + '/v2.1/track', data=data, headers=headers, timeout=10.0, proxies={}) credential.get_token.assert_called_with(_MONITOR_OAUTH_SCOPE) self.assertIsNone(mixin.storage.get()) self.assertEqual(len(os.listdir(mixin.storage.path)), 0) credential.get_token.assert_called_once()
def test_statsbeat_206_no_retry(self): _requests_map.clear() mixin = TransportMixin() mixin.options = Options() with mock.patch('requests.post') as post: post.return_value = MockResponse( 206, json.dumps({ 'itemsReceived': 3, 'itemsAccepted': 2, 'errors': [ { 'index': 0, 'statusCode': 400, 'message': '', }, ], })) result = mixin._transmit([1, 2, 3]) self.assertEqual(len(_requests_map), 3) self.assertIsNotNone(_requests_map['duration']) self.assertEqual(_requests_map['count'], 1) self.assertEqual(_requests_map['failure'], 1) self.assertEqual(result, -206) _requests_map.clear()
def test_transmission_pre_exception(self): mixin = TransportMixin() mixin.options = Options() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3]) with mock.patch('requests.post', throw(Exception)): mixin._transmit_from_storage() self.assertIsNone(mixin.storage.get()) self.assertEqual(len(os.listdir(mixin.storage.path)), 1)
def test_transmission_400(self): mixin = TransportMixin() mixin.options = Options() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3]) with mock.patch('requests.post') as post: post.return_value = MockResponse(400, '{}') mixin._transmit_from_storage() self.assertEqual(len(os.listdir(mixin.storage.path)), 0)
def test_transmission_cred_exception(self): mixin = TransportMixin() mixin.options = Options() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3]) with mock.patch('requests.post', throw(CredentialUnavailableError)): # noqa: E501 mixin._transmit_from_storage() self.assertIsNone(mixin.storage.get()) self.assertEqual(len(os.listdir(mixin.storage.path)), 0)
def test_check_stats_collection(self): mixin = TransportMixin() mixin.options = Options() mixin.options.enable_stats_metrics = True self.assertTrue(mixin._check_stats_collection()) mixin._is_stats = False self.assertTrue(mixin._check_stats_collection()) mixin._is_stats = True self.assertFalse(mixin._check_stats_collection()) mixin.options.enable_stats_metrics = False self.assertFalse(mixin._check_stats_collection())
def test_transmission_307_circular_reference(self): mixin = TransportMixin() mixin.options = Options() mixin._consecutive_redirects = 0 mixin.options.endpoint = "https://example.com" with mock.patch('requests.post') as post: post.return_value = MockResponse( 307, '{}', {"location": "https://example.com"}) # noqa: E501 result = mixin._transmit([1, 2, 3]) self.assertEqual(result, -307) self.assertEqual(post.call_count, _MAX_CONSECUTIVE_REDIRECTS) self.assertEqual(mixin.options.endpoint, "https://example.com")
def test_statsbeat_timeout(self): _requests_map.clear() mixin = TransportMixin() mixin.options = Options() with mock.patch('requests.post', throw(requests.Timeout)): result = mixin._transmit([1, 2, 3]) self.assertEqual(len(_requests_map), 3) self.assertIsNotNone(_requests_map['duration']) self.assertEqual(_requests_map['count'], 1) self.assertEqual(_requests_map['retry'], 1) self.assertEqual(result, mixin.options.minimum_retry_interval) _requests_map.clear()
def test_statsbeat_exception(self): _requests_map.clear() mixin = TransportMixin() mixin.options = Options() with mock.patch('requests.post', throw(Exception)): result = mixin._transmit([1, 2, 3]) self.assertEqual(len(_requests_map), 3) self.assertIsNotNone(_requests_map['duration']) self.assertEqual(_requests_map['exception'], 1) self.assertEqual(_requests_map['count'], 1) self.assertEqual(result, -1) _requests_map.clear()
def test_transmission_lease_failure(self, requests_mock): requests_mock.return_value = MockResponse(200, 'unknown') mixin = TransportMixin() mixin.options = Options() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3]) with mock.patch( 'opencensus.ext.azure.common.storage.LocalFileBlob.lease' ) as lease: # noqa: E501 lease.return_value = False mixin._transmit_from_storage() self.assertTrue(mixin.storage.get())
def test_statsbeat_439(self): _requests_map.clear() mixin = TransportMixin() mixin.options = Options() with mock.patch('requests.post') as post: post.return_value = MockResponse(439, 'unknown') result = mixin._transmit([1, 2, 3]) self.assertEqual(len(_requests_map), 3) self.assertIsNotNone(_requests_map['duration']) self.assertEqual(_requests_map['throttle'], 1) self.assertEqual(_requests_map['count'], 1) self.assertEqual(result, -439) _requests_map.clear()
def test_statsbeat_403(self): _requests_map.clear() mixin = TransportMixin() mixin.options = Options() with mock.patch('requests.post') as post: post.return_value = MockResponse(403, 'unknown') result = mixin._transmit([1, 2, 3]) self.assertEqual(len(_requests_map), 4) self.assertIsNotNone(_requests_map['duration']) self.assertEqual(_requests_map['retry'], 1) self.assertEqual(_requests_map['count'], 1) self.assertEqual(_requests_map['failure'], 1) self.assertEqual(result, mixin.options.minimum_retry_interval) _requests_map.clear()
def test_transmission_307(self): mixin = TransportMixin() mixin.options = Options() mixin._consecutive_redirects = 0 mixin.options.endpoint = "test.endpoint" with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3]) with mock.patch('requests.post') as post: post.return_value = MockResponse( 307, '{}', {"location": "https://example.com"}) # noqa: E501 mixin._transmit_from_storage() self.assertEqual(post.call_count, _MAX_CONSECUTIVE_REDIRECTS) self.assertEqual(len(os.listdir(mixin.storage.path)), 0) self.assertEqual(mixin.options.endpoint, "https://example.com")
def test_statsbeat_307(self): _requests_map.clear() mixin = TransportMixin() mixin.options = Options() mixin._consecutive_redirects = 0 mixin.options.endpoint = "test.endpoint" with mock.patch('requests.post') as post: post.return_value = MockResponse( 307, '{}', {"location": "https://example.com"}) # noqa: E501 result = mixin._transmit([1, 2, 3]) self.assertEqual(len(_requests_map), 4) self.assertIsNotNone(_requests_map['duration']) self.assertEqual(_requests_map['exception'], 1) self.assertEqual(_requests_map['count'], 10) self.assertEqual(_requests_map['failure'], 10) self.assertEqual(result, -307) _requests_map.clear()
def test_transmission_206_bogus(self): mixin = TransportMixin() mixin.options = Options() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor mixin.storage.put([1, 2, 3, 4, 5]) with mock.patch('requests.post') as post: post.return_value = MockResponse( 206, json.dumps({ 'itemsReceived': 5, 'itemsAccepted': 3, 'errors': [ { 'foo': 0, 'bar': 1, }, ], })) mixin._transmit_from_storage() self.assertIsNone(mixin.storage.get()) self.assertEqual(len(os.listdir(mixin.storage.path)), 0)