forked from elastic/connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_azure_blob_storage.py
687 lines (586 loc) · 23.5 KB
/
test_azure_blob_storage.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License 2.0;
# you may not use this file except in compliance with the Elastic License 2.0.
#
"""Tests the Azure Blob Storage source class methods"""
import asyncio
from contextlib import asynccontextmanager
from datetime import datetime
from unittest.mock import Mock, patch
import pytest
from azure.storage.blob.aio import BlobClient, BlobServiceClient, ContainerClient
from connectors.source import ConfigurableFieldValueError
from connectors.sources.azure_blob_storage import AzureBlobStorageDataSource
from tests.commons import AsyncIterator
from tests.sources.support import create_source
@asynccontextmanager
async def create_abs_source(
use_text_extraction_service=False,
):
async with create_source(
AzureBlobStorageDataSource,
account_name="foo",
account_key="bar",
blob_endpoint="https://foo.endpoint.com",
use_text_extraction_service=use_text_extraction_service,
) as source:
yield source
@pytest.mark.asyncio
async def test_ping_for_successful_connection():
"""Test ping method of AzureBlobStorageDataSource class"""
# Setup
mock_response = asyncio.Future()
mock_response.set_result(
{
"client_request_id": "dummy",
"request_id": "dummy",
"version": "v1",
"date": "dummy",
"sku_name": "dummy",
"account_kind": "StorageV2",
"is_hns_enabled": False,
}
)
with patch.object(
BlobServiceClient, "get_account_information", return_value=mock_response
):
async with create_abs_source() as source:
# Execute
await source.ping()
@pytest.mark.asyncio
async def test_ping_for_failed_connection():
"""Test ping method of AzureBlobStorageDataSource class with negative case"""
# Setup
async with create_abs_source() as source:
with patch.object(
BlobServiceClient,
"get_account_information",
side_effect=Exception("Something went wrong"),
):
# Execute
with pytest.raises(Exception):
await source.ping()
@pytest.mark.asyncio
async def test_prepare_blob_doc():
"""Test prepare_blob_doc method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
document = {
"container": "container1",
"name": "blob1",
"content_settings": {"content_type": "plain/text"},
"last_modified": datetime(2022, 4, 21, 12, 12, 30),
"creation_time": datetime(2022, 4, 21, 12, 12, 30),
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"lease": {"status": "Locked", "state": "Leased", "duration": "Infinite"},
"blob_tier": "private",
"size": 1000,
}
expected_output = {
"_id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1', 'key2': 'value2'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1",
"tier": "private",
"size": 1000,
"container": "container1",
}
# Execute
actual_output = source.prepare_blob_doc(
document, {"key1": "value1", "key2": "value2"}
)
# Assert
assert actual_output == expected_output
@pytest.mark.asyncio
async def test_get_container():
"""Test get_container method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
source.connection_string = source._configure_connection_string()
mock_repsonse = AsyncIterator(
[
{
"name": "container1",
"last_modified": datetime(2022, 4, 21, 12, 12, 30),
"metadata": {"key1": "value1"},
"lease": {
"status": "Locked",
"state": "Leased",
"duration": "Infinite",
},
"public_access": "private",
}
]
)
with patch.object(
BlobServiceClient, "list_containers", return_value=mock_repsonse
):
expected_output = [
{"name": "container1", "metadata": {"key1": "value1"}},
None,
]
# Execute
async for actual_document in source.get_container(
container_list=["container1"]
):
# Assert
assert actual_document in expected_output
@pytest.mark.asyncio
async def test_get_blob():
"""Test get_blob method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
source.connection_string = source._configure_connection_string()
mock_response = AsyncIterator(
[
{
"container": "container1",
"name": "blob1",
"content_settings": {"content_type": "plain/text"},
"last_modified": datetime(2022, 4, 21, 12, 12, 30),
"creation_time": datetime(2022, 4, 21, 12, 12, 30),
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"lease": {
"status": "Locked",
"state": "Leased",
"duration": "Infinite",
},
"blob_tier": "private",
"size": 1000,
}
]
)
with patch.object(ContainerClient, "list_blobs", return_value=mock_response):
expected_output = {
"_id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1",
"tier": "private",
"size": 1000,
"container": "container1",
}
# Execute
async for actual_document in source.get_blob(
{"name": "container1", "metadata": {"key1": "value1"}}
):
# Assert
assert actual_document == expected_output
@pytest.mark.asyncio
async def test_get_blob_negative():
"""Test get_blob negative method of AzureBlobStorageDataSource Class"""
async with create_abs_source() as source:
source.connection_string = source._configure_connection_string()
async for actual_document in source.get_blob(
{"name": "container1", "metadata": {"key1": "value1"}}
):
assert actual_document is None
@pytest.mark.asyncio
async def test_get_containr_negative():
"""Test get_container negative method of AzureBlobStorageDataSource Class"""
async with create_abs_source() as source:
source.connection_string = source._configure_connection_string()
async for actual_document in source.get_container(
container_list=["container1"]
):
assert actual_document is None
@pytest.mark.asyncio
async def test_get_doc():
"""Test get_doc method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
source.containers = ["*"]
source.get_container = Mock(
return_value=AsyncIterator(
[
{
"type": "container",
"_id": "container1",
"timestamp": "2022-04-21T12:12:30",
"metadata": "key1=value1",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "container1",
"access": "private",
}
]
)
)
source.get_blob = Mock(
return_value=AsyncIterator(
[
{
"type": "blob",
"_id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1",
"tier": "private",
"size": 1000,
"container": "container1",
}
]
)
)
expected_response = [
{
"type": "blob",
"_id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1",
"tier": "private",
"size": 1000,
"container": "container1",
}
]
actual_response = []
# Execute
async for document, _ in source.get_docs():
actual_response.append(document)
# Assert
assert actual_response == expected_response
async def create_fake_coroutine(item):
"""create a method for returning fake coroutine value for
Args:
item: Value for converting into coroutine
"""
return item
@pytest.mark.asyncio
async def test_get_doc_for_specific_container():
"""Test get_doc for specific container method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
source.containers = ["container1"]
source.get_blob = Mock(
return_value=AsyncIterator(
[
{
"type": "blob",
"_id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1",
"tier": "private",
"size": 1000,
"container": "container1",
}
]
)
)
source.get_container = Mock(
return_value=AsyncIterator(
[
{
"type": "container",
"_id": "container1",
"timestamp": "2022-04-21T12:12:30",
"metadata": "key1=value1",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "container1",
"access": "private",
}
]
)
)
expected_response = [
{
"type": "blob",
"_id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1",
"tier": "private",
"size": 1000,
"container": "container1",
}
]
actual_response = []
# Execute
async for document, _ in source.get_docs():
actual_response.append(document)
# Assert
assert actual_response == expected_response
@pytest.mark.asyncio
async def test_get_content():
"""Test get_content method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
source.connection_string = source._configure_connection_string()
class DownloadBlobMock:
"""This class is used Mock object of download_blob"""
async def chunks(self):
"""This Method is used to read content"""
yield b"Mock...."
mock_response = {
"type": "blob",
"id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.txt",
"tier": "private",
"size": 1000,
"container": "container1",
}
with patch.object(BlobClient, "download_blob", return_value=DownloadBlobMock()):
expected_output = {
"_id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"_attachment": "TW9jay4uLi4=",
}
actual_response = await source.get_content(mock_response, doit=True)
assert actual_response == expected_output
assert "body" not in actual_response
@pytest.mark.asyncio
async def test_get_content_with_upper_extension():
"""Test get_content method of AzureBlobStorageDataSource Class"""
# Setup
async with create_abs_source() as source:
source.connection_string = source._configure_connection_string()
class DownloadBlobMock:
"""This class is used Mock object of download_blob"""
async def chunks(self):
"""This Method is used to read content"""
yield b"Mock...."
mock_response = {
"type": "blob",
"id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.TXT",
"tier": "private",
"size": 1000,
"container": "container1",
}
with patch.object(BlobClient, "download_blob", return_value=DownloadBlobMock()):
expected_output = {
"_id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"_attachment": "TW9jay4uLi4=",
}
# Execute
actual_response = await source.get_content(mock_response, doit=True)
# Assert
assert actual_response == expected_output
@pytest.mark.asyncio
async def test_get_content_when_doit_false():
"""Test get_content method when doit is false."""
# Setup
async with create_abs_source() as source:
mock_response = {
"type": "blob",
"id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.txt",
"tier": "private",
"size": 1000,
"container": "container1",
}
# Execute
actual_response = await source.get_content(mock_response)
# Assert
assert actual_response is None
@pytest.mark.asyncio
async def test_get_content_when_file_size_0b():
"""Test get_content method when the file size is 0b"""
# Setup
async with create_abs_source() as source:
mock_response = {
"type": "blob",
"id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.pdf",
"tier": "private",
"size": 0,
"container": "container1",
}
# Execute
actual_response = await source.get_content(mock_response, doit=True)
# Assert
assert actual_response is None
@pytest.mark.asyncio
async def test_get_content_when_size_limit_exceeded():
"""Test get_content method when the file size is 10MB"""
# Setup
async with create_abs_source() as source:
mock_response = {
"type": "blob",
"id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.txt",
"tier": "private",
"size": 10000000000000,
"container": "container1",
}
# Execute
actual_response = await source.get_content(mock_response, doit=True)
# Assert
assert actual_response is None
@pytest.mark.asyncio
async def test_get_content_when_type_not_supported():
"""Test get_content method when the file type is not supported"""
# Setup
async with create_abs_source() as source:
mock_response = {
"type": "blob",
"id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.xyz",
"tier": "private",
"size": 10,
"container": "container1",
}
# Execute
actual_response = await source.get_content(mock_response, doit=True)
# Assert
assert actual_response is None
@pytest.mark.asyncio
async def test_validate_config_no_account_name():
"""Test configure connection string method of AzureBlobStorageDataSource class"""
# Setup
async with create_abs_source() as source:
source.configuration.get_field("account_name").value = ""
with pytest.raises(ConfigurableFieldValueError):
# Execute
await source.validate_config()
@pytest.mark.asyncio
async def test_tweak_bulk_options():
"""Test tweak_bulk_options method of BaseDataSource class"""
# Setup
async with create_abs_source() as source:
options = {}
options["concurrent_downloads"] = 10
# Execute
source.tweak_bulk_options(options)
@pytest.mark.asyncio
async def test_validate_config_invalid_concurrent_downloads():
"""Test tweak_bulk_options method of BaseDataSource class with invalid concurrent downloads"""
# Setup
async with create_source(
AzureBlobStorageDataSource, concurrent_downloads=1000
) as source:
with pytest.raises(ConfigurableFieldValueError):
# Execute
await source.validate_config()
@pytest.mark.asyncio
async def test_get_content_when_blob_tier_archive():
"""Test get_content method when the blob tier is archive"""
# Setup
async with create_abs_source() as source:
mock_response = {
"type": "blob",
"id": "container1/blob1",
"timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.pdf",
"tier": "Archive",
"size": 10,
"container": "container1",
}
# Execute
actual_response = await source.get_content(mock_response, doit=True)
# Assert
assert actual_response is None
@pytest.mark.asyncio
@patch(
"connectors.content_extraction.ContentExtraction._check_configured",
lambda *_: True,
)
async def test_get_content_with_text_extraction_enabled_adds_body():
mock_response = {
"type": "blob",
"id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"created at": "2022-04-21T12:12:30",
"content type": "plain/text",
"container metadata": "{'key1': 'value1'}",
"metadata": "{'key1': 'value1', 'key2': 'value2'}",
"leasedata": "{'status': 'Locked', 'state': 'Leased', 'duration': 'Infinite'}",
"title": "blob1.txt",
"tier": "private",
"size": 1000,
"container": "container1",
}
mock_download = b"Mock...."
with patch(
"connectors.content_extraction.ContentExtraction.extract_text",
return_value=str(mock_download),
) as extraction_service_mock, patch(
"connectors.content_extraction.ContentExtraction.get_extraction_config",
return_value={"host": "http://localhost:8090"},
):
async with create_abs_source(use_text_extraction_service=True) as source:
source.connection_string = source._configure_connection_string()
class DownloadBlobMock:
"""This class is used Mock object of download_blob"""
async def chunks(self):
"""This Method is used to read content"""
yield mock_download
with patch.object(
BlobClient, "download_blob", return_value=DownloadBlobMock()
):
expected_output = {
"_id": "container1/blob1",
"_timestamp": "2022-04-21T12:12:30",
"body": str(mock_download),
}
actual_response = await source.get_content(mock_response, doit=True)
extraction_service_mock.assert_called_once()
assert actual_response == expected_output
assert "_attachment" not in actual_response