|
11 | 11 | import shutil |
12 | 12 | import sqlite3 |
13 | 13 |
|
14 | | -from drivers import MinioDriver, LocalDriver, GoogleDriveDriver |
| 14 | +from drivers import MinioDriver, LocalDriver, GoogleDriveDriver, AzureBlobDriver |
15 | 15 | from media_sync import ( |
16 | 16 | main, |
17 | 17 | config, |
|
33 | 33 | MINIO_SECRET_KEY, |
34 | 34 | GOOGLE_DRIVE_SERVICE_ACCOUNT_FILE, |
35 | 35 | GOOGLE_DRIVE_FOLDER, |
| 36 | + AZURE_STORAGE_ACCOUNT_NAME, |
| 37 | + AZURE_STORAGE_ACCOUNT_KEY, |
| 38 | + AZURE_STORAGE_CONTAINER, |
36 | 39 | cleanup, |
37 | 40 | prepare_mergin_project, |
38 | 41 | ) |
@@ -634,3 +637,111 @@ def test_google_drive_backend(mc): |
634 | 637 | # files in mergin project still exist (copy mode) |
635 | 638 | assert os.path.exists(os.path.join(work_project_dir, "img1.png")) |
636 | 639 | assert os.path.exists(os.path.join(work_project_dir, "images", "img2.jpg")) |
| 640 | + |
| 641 | + |
| 642 | +def test_azure_blob_backend(mc): |
| 643 | + """Test media sync connected to Azure Blob Storage backend (needs valid Azure credentials)""" |
| 644 | + project_name = "mediasync_test_azure" |
| 645 | + full_project_name = WORKSPACE + "/" + project_name |
| 646 | + work_project_dir = os.path.join(TMP_DIR, project_name + "_work") |
| 647 | + |
| 648 | + cleanup(mc, full_project_name, [work_project_dir]) |
| 649 | + prepare_mergin_project(mc, full_project_name) |
| 650 | + |
| 651 | + # invalid config - missing required fields |
| 652 | + config.update( |
| 653 | + { |
| 654 | + "MERGIN__USERNAME": API_USER, |
| 655 | + "MERGIN__PASSWORD": USER_PWD, |
| 656 | + "MERGIN__URL": SERVER_URL, |
| 657 | + "MERGIN__PROJECT_NAME": full_project_name, |
| 658 | + "PROJECT_WORKING_DIR": work_project_dir, |
| 659 | + "OPERATION_MODE": "copy", |
| 660 | + "REFERENCES": [ |
| 661 | + { |
| 662 | + "file": None, |
| 663 | + "table": None, |
| 664 | + "local_path_column": None, |
| 665 | + "driver_path_column": None, |
| 666 | + } |
| 667 | + ], |
| 668 | + "DRIVER": "azure", |
| 669 | + "AZURE_BLOB__ACCOUNT_NAME": AZURE_STORAGE_ACCOUNT_NAME, |
| 670 | + "AZURE_BLOB__ACCOUNT_KEY": "", |
| 671 | + "AZURE_BLOB__CONTAINER": AZURE_STORAGE_CONTAINER, |
| 672 | + } |
| 673 | + ) |
| 674 | + |
| 675 | + with pytest.raises(ConfigError): |
| 676 | + validate_config(config) |
| 677 | + |
| 678 | + # patch config to fit testing purposes |
| 679 | + config.update( |
| 680 | + { |
| 681 | + "MERGIN__USERNAME": API_USER, |
| 682 | + "MERGIN__PASSWORD": USER_PWD, |
| 683 | + "MERGIN__URL": SERVER_URL, |
| 684 | + "MERGIN__PROJECT_NAME": full_project_name, |
| 685 | + "PROJECT_WORKING_DIR": work_project_dir, |
| 686 | + "OPERATION_MODE": "copy", |
| 687 | + "REFERENCES": [ |
| 688 | + { |
| 689 | + "file": None, |
| 690 | + "table": None, |
| 691 | + "local_path_column": None, |
| 692 | + "driver_path_column": None, |
| 693 | + } |
| 694 | + ], |
| 695 | + "DRIVER": "azure", |
| 696 | + "AZURE_BLOB__ACCOUNT_NAME": AZURE_STORAGE_ACCOUNT_NAME, |
| 697 | + "AZURE_BLOB__ACCOUNT_KEY": AZURE_STORAGE_ACCOUNT_KEY, |
| 698 | + "AZURE_BLOB__CONTAINER": AZURE_STORAGE_CONTAINER, |
| 699 | + } |
| 700 | + ) |
| 701 | + |
| 702 | + main() |
| 703 | + |
| 704 | + # verify files were uploaded to Azure Blob Storage |
| 705 | + driver = AzureBlobDriver(config) |
| 706 | + blob_names = [b.name for b in driver.client.list_blobs()] |
| 707 | + assert "img1.png" in blob_names |
| 708 | + assert "images/img2.jpg" in blob_names |
| 709 | + |
| 710 | + # files in mergin project still exist (copy mode) |
| 711 | + assert os.path.exists(os.path.join(work_project_dir, "img1.png")) |
| 712 | + assert os.path.exists(os.path.join(work_project_dir, "images", "img2.jpg")) |
| 713 | + |
| 714 | + # test with blob_path_prefix |
| 715 | + cleanup(mc, full_project_name, [work_project_dir]) |
| 716 | + prepare_mergin_project(mc, full_project_name) |
| 717 | + |
| 718 | + config.update( |
| 719 | + { |
| 720 | + "MERGIN__USERNAME": API_USER, |
| 721 | + "MERGIN__PASSWORD": USER_PWD, |
| 722 | + "MERGIN__URL": SERVER_URL, |
| 723 | + "MERGIN__PROJECT_NAME": full_project_name, |
| 724 | + "PROJECT_WORKING_DIR": work_project_dir, |
| 725 | + "OPERATION_MODE": "copy", |
| 726 | + "REFERENCES": [ |
| 727 | + { |
| 728 | + "file": None, |
| 729 | + "table": None, |
| 730 | + "local_path_column": None, |
| 731 | + "driver_path_column": None, |
| 732 | + } |
| 733 | + ], |
| 734 | + "DRIVER": "azure", |
| 735 | + "AZURE_BLOB__ACCOUNT_NAME": AZURE_STORAGE_ACCOUNT_NAME, |
| 736 | + "AZURE_BLOB__ACCOUNT_KEY": AZURE_STORAGE_ACCOUNT_KEY, |
| 737 | + "AZURE_BLOB__CONTAINER": AZURE_STORAGE_CONTAINER, |
| 738 | + "AZURE_BLOB__BLOB_PATH_PREFIX": "subPath", |
| 739 | + } |
| 740 | + ) |
| 741 | + |
| 742 | + main() |
| 743 | + |
| 744 | + driver = AzureBlobDriver(config) |
| 745 | + blob_names = [b.name for b in driver.client.list_blobs()] |
| 746 | + assert "subPath/img1.png" in blob_names |
| 747 | + assert "subPath/images/img2.jpg" in blob_names |
0 commit comments