def test_transmission_206_500(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id())) test_envelope = Envelope(name="testEnvelope") envelopes_to_export = map( lambda x: x.to_dict(), tuple([Envelope(), Envelope(), test_envelope]), ) exporter.storage.put(envelopes_to_export) 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", }, ], }), ) exporter._transmit_from_storage() self.assertEqual(len(os.listdir(exporter.storage.path)), 1) self.assertEqual(exporter.storage.get().get()[0]["name"], "testEnvelope")
def test_transmission_nothing(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id()) ) with mock.patch("requests.post") as post: post.return_value = None exporter._transmit_from_storage()
def test_transmission_400(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id())) envelopes_to_export = map(lambda x: x.to_dict(), tuple([Envelope()])) exporter.storage.put(envelopes_to_export) with mock.patch("requests.post") as post: post.return_value = MockResponse(400, "{}") exporter._transmit_from_storage() self.assertEqual(len(os.listdir(exporter.storage.path)), 0)
def test_transmit_request_exception(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id())) envelopes_to_export = map(lambda x: x.to_dict(), tuple([Envelope()])) exporter.storage.put(envelopes_to_export) with mock.patch("requests.post", throw(Exception)): exporter._transmit_from_storage() self.assertIsNone(exporter.storage.get()) self.assertEqual(len(os.listdir(exporter.storage.path)), 1)
def test_transmission_lease_failure(self, requests_mock): requests_mock.return_value = MockResponse(200, "unknown") exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id())) envelopes_to_export = map(lambda x: x.to_dict(), tuple([Envelope()])) exporter.storage.put(envelopes_to_export) with mock.patch("azure_monitor.storage.LocalFileBlob.lease" ) as lease: # noqa: E501 lease.return_value = False exporter._transmit_from_storage() self.assertTrue(exporter.storage.get())
def test_constructor(self): """Test the constructor.""" base = BaseExporter( instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab", proxies={"https": "https://test-proxy.com"}, storage_maintenance_period=2, storage_max_size=3, storage_path=os.path.join(TEST_FOLDER, self.id()), storage_retention_period=4, timeout=5, ) self.assertIsInstance(base.options, ExporterOptions) self.assertEqual( base.options.instrumentation_key, "4321abcd-5678-4efa-8abc-1234567890ab", ) self.assertEqual( base.options.proxies, {"https": "https://test-proxy.com"}, ) self.assertEqual(base.options.storage_maintenance_period, 2) self.assertEqual(base.options.storage_max_size, 3) self.assertEqual(base.options.storage_retention_period, 4) self.assertEqual(base.options.timeout, 5) self.assertEqual(base.options.storage_path, os.path.join(TEST_FOLDER, self.id()))
def test_transmission_206_bogus(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id())) envelopes_to_export = map(lambda x: x.to_dict(), tuple([Envelope()])) exporter.storage.put(envelopes_to_export) with mock.patch("requests.post") as post: post.return_value = MockResponse( 206, json.dumps({ "itemsReceived": 5, "itemsAccepted": 3, "errors": [{ "foo": 0, "bar": 1 }], }), ) exporter._transmit_from_storage() self.assertIsNone(exporter.storage.get()) self.assertEqual(len(os.listdir(exporter.storage.path)), 0)
def test_constructor_wrong_options(self): """Test the constructor with wrong options.""" with self.assertRaises(TypeError): BaseExporter(something_else=6)
def setUpClass(cls): os.environ[ "APPINSIGHTS_INSTRUMENTATIONKEY"] = "1234abcd-5678-4efa-8abc-1234567890ab" cls._base = BaseExporter(storage_path=STORAGE_PATH)
def test_transmission_empty(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id())) status = exporter._transmit([]) self.assertEqual(status, ExportResult.SUCCESS)