11import logging
22import pytest
33
4- from unittest .mock import MagicMock , create_autospec
4+ from unittest .mock import MagicMock , create_autospec , Mock
55
66from databricks .sdk import WorkspaceClient
77from databricks .sdk .errors import (
1616)
1717from databricks .sdk .service .workspace import GetSecretResponse
1818
19- from databricks .labs .ucx .assessment .azure import StoragePermissionMapping
19+ from databricks .labs .ucx .assessment .azure import StoragePermissionMapping , \
20+ AzureServicePrincipalCrawler , AzureServicePrincipalInfo
2021from databricks .labs .ucx .migration .azure_credentials import (
21- AzureServicePrincipalMigration ,
22+ AzureServicePrincipalMigration , ServicePrincipalMigrationInfo ,
2223)
24+ from tests .unit .framework .mocks import MockBackend
2325
2426
2527def test_list_storage_credentials ():
@@ -74,4 +76,46 @@ def test_read_secret_read_exception(caplog, exception, expected_log, expected_re
7476 secret_value = sp_migration ._read_databricks_secret ("test_scope" ,"test_key" , "000" )
7577
7678 assert expected_log in caplog .text
77- assert secret_value == expected_return
79+ assert secret_value == expected_return
80+
81+
82+ def test_fetch_client_secret ():
83+ w = create_autospec (WorkspaceClient )
84+ w .secrets .get_secret .return_value = GetSecretResponse (value = "aGVsbG8gd29ybGQ=" )
85+
86+ crawled_sp = [AzureServicePrincipalInfo ("app_secret1" , "test_scope" , "test_key" , "tenant_id_1" , "storage1" ),
87+ AzureServicePrincipalInfo ("app_secret2" , "test_scope" , "test_key" , "tenant_id_1" , "storage1" ),
88+ AzureServicePrincipalInfo ("app_no_secret1" , "" , "" , "tenant_id_1" , "storage1" ),
89+ AzureServicePrincipalInfo ("app_no_secret2" , "test_scope" , "" , "tenant_id_1" , "storage1" ),]
90+ sp_crawler = AzureServicePrincipalCrawler (w , MockBackend (), "ucx" )
91+ sp_crawler ._try_fetch = Mock (return_value = crawled_sp )
92+ sp_crawler ._crawl = Mock (return_value = crawled_sp )
93+
94+ sp_to_be_checked = [StoragePermissionMapping (prefix = "prefix1" ,client_id = "app_secret1" ,principal = "principal_1" ,privilege = "WRITE_FILES" ,directory_id = "directory_id_1" ),
95+ StoragePermissionMapping (prefix = "prefix2" ,client_id = "app_secret2" ,principal = "principal_2" ,privilege = "READ_FILES" ,directory_id = "directory_id_1" ),
96+ StoragePermissionMapping (prefix = "prefix3" ,client_id = "app_no_secret1" ,principal = "principal_3" ,privilege = "WRITE_FILES" ,directory_id = "directory_id_2" ),
97+ StoragePermissionMapping (prefix = "prefix4" ,client_id = "app_no_secret2" ,principal = "principal_4" ,privilege = "READ_FILES" ,directory_id = "directory_id_2" )]
98+
99+ expected_sp_list = [ServicePrincipalMigrationInfo (StoragePermissionMapping (prefix = "prefix1" ,client_id = "app_secret1" ,principal = "principal_1" ,privilege = "WRITE_FILES" ,directory_id = "directory_id_1" ), "hello world" ),
100+ ServicePrincipalMigrationInfo (StoragePermissionMapping (prefix = "prefix2" ,client_id = "app_secret2" ,principal = "principal_2" ,privilege = "READ_FILES" ,directory_id = "directory_id_1" ), "hello world" )]
101+
102+ sp_migration = AzureServicePrincipalMigration (MagicMock (), w , MagicMock (), sp_crawler )
103+ filtered_sp_list = sp_migration ._fetch_client_secret (sp_to_be_checked )
104+
105+ assert filtered_sp_list == expected_sp_list
106+
107+
108+ def test_print_action_plan (capsys ):
109+ sp_list_with_secret = [ServicePrincipalMigrationInfo (StoragePermissionMapping (prefix = "prefix1" ,client_id = "app_secret1" ,principal = "principal_1" ,privilege = "WRITE_FILES" ,directory_id = "directory_id_1" ), "hello world" )]
110+ sp_migration = AzureServicePrincipalMigration (MagicMock (), MagicMock (), MagicMock (), MagicMock ())
111+ sp_migration ._print_action_plan (sp_list_with_secret )
112+
113+ expected_print = (f"Service Principal name: principal_1, "
114+ f"application_id: app_secret1, "
115+ f"privilege WRITE_FILES "
116+ f"on location prefix1\n " )
117+ assert expected_print == capsys .readouterr ().out
118+
119+
120+
121+
0 commit comments