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_nothing(self): mixin = TransportMixin() with LocalFileStorage(os.path.join(TEST_FOLDER, self.id())) as stor: mixin.storage = stor with mock.patch('requests.post') as post: post.return_value = None mixin._transmit_from_storage()
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_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_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_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_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)