77import pytest
88import yaml
99from databricks .sdk .core import DatabricksError
10- from databricks .sdk .errors import OperationFailed
10+ from databricks .sdk .errors import NotFound , OperationFailed
1111from databricks .sdk .service import iam , jobs
1212from databricks .sdk .service .compute import (
1313 GlobalInitScriptDetails ,
@@ -41,8 +41,6 @@ def ws(mocker):
4141 ws .
current_user .
me = lambda :
iam .
User (
user_name = "[email protected] " ,
groups = [
iam .
ComplexValue (
display = "admins" )])
4242 ws .config .host = "https://foo"
4343 ws .config .is_aws = True
44- config_bytes = yaml .dump (WorkspaceConfig (inventory_database = "a" ).as_dict ()).encode ("utf8" )
45- ws .workspace .download = lambda _ : io .BytesIO (config_bytes )
4644 ws .workspace .get_status = lambda _ : ObjectInfo (object_id = 123 )
4745 ws .data_sources .list = lambda : [DataSource (id = "bcd" , warehouse_id = "abc" )]
4846 ws .warehouses .list = lambda ** _ : [EndpointInfo (id = "abc" , warehouse_type = EndpointInfoWarehouseType .PRO )]
@@ -55,6 +53,8 @@ def ws(mocker):
5553
5654
5755def test_replace_clusters_for_integration_tests (ws ):
56+ config_bytes = yaml .dump (WorkspaceConfig (inventory_database = "a" ).as_dict ()).encode ("utf8" )
57+ ws .workspace .download = lambda _ : io .BytesIO (config_bytes )
5858 return_value = WorkspaceInstaller .run_for_config (
5959 ws ,
6060 WorkspaceConfig (inventory_database = "a" ),
@@ -148,22 +148,127 @@ def not_found(_):
148148workspace_start_path: /
149149""" ,
150150 format = ImportFormat .AUTO ,
151+ overwrite = False ,
152+ )
153+
154+
155+ def test_migrate_from_v1 (ws , mocker ):
156+ mocker .patch ("builtins.input" , return_value = "yes" )
157+ mock_file = MagicMock ()
158+ mocker .patch ("builtins.open" , return_value = mock_file )
159+ mocker .patch ("base64.b64encode" )
160+
161+ ws .
current_user .
me = lambda :
iam .
User (
user_name = "[email protected] " ,
groups = [
iam .
ComplexValue (
display = "admins" )])
162+ ws .config .host = "https://foo"
163+ ws .config .is_aws = True
164+ config_bytes = b"""default_catalog: ucx_default
165+ groups:
166+ auto: true
167+ backup_group_prefix: db-temp-
168+ inventory_database: ucx
169+ log_level: INFO
170+ num_threads: 8
171+ version: 1
172+ warehouse_id: abc
173+ workspace_start_path: /
174+ """
175+ ws .workspace .download = lambda _ : io .BytesIO (config_bytes )
176+ ws .workspace .get_status = lambda _ : ObjectInfo (object_id = 123 )
177+ mocker .patch ("builtins.input" , return_value = "42" )
178+
179+ ws .warehouses .list = lambda ** _ : [
180+ EndpointInfo (id = "abc" , warehouse_type = EndpointInfoWarehouseType .PRO , state = State .RUNNING )
181+ ]
182+ ws .cluster_policies .list = lambda : []
183+
184+ install = WorkspaceInstaller (ws )
185+ install ._choice = lambda _1 , _2 : "None (abc, PRO, RUNNING)"
186+ install ._configure ()
187+
188+ ws .workspace .upload .assert_called_with (
189+ "/Users/[email protected] /.ucx/config.yml" ,
190+ b"""default_catalog: ucx_default
191+ inventory_database: ucx
192+ log_level: INFO
193+ num_threads: 8
194+ renamed_group_prefix: db-temp-
195+ version: 2
196+ warehouse_id: abc
197+ workspace_start_path: /
198+ """ ,
199+ format = ImportFormat .AUTO ,
200+ overwrite = True ,
201+ )
202+
203+
204+ def test_migrate_from_v1_selected_groups (ws , mocker ):
205+ mocker .patch ("builtins.input" , return_value = "yes" )
206+ mock_file = MagicMock ()
207+ mocker .patch ("builtins.open" , return_value = mock_file )
208+ mocker .patch ("base64.b64encode" )
209+
210+ ws .
current_user .
me = lambda :
iam .
User (
user_name = "[email protected] " ,
groups = [
iam .
ComplexValue (
display = "admins" )])
211+ ws .config .host = "https://foo"
212+ ws .config .is_aws = True
213+ config_bytes = b"""default_catalog: ucx_default
214+ groups:
215+ backup_group_prefix: 'backup_baguette_prefix'
216+ selected:
217+ - '42'
218+ - '100'
219+ inventory_database: '42'
220+ log_level: '42'
221+ num_threads: 42
222+ version: 1
223+ warehouse_id: abc
224+ workspace_start_path: /
225+ """
226+ ws .workspace .download = lambda _ : io .BytesIO (config_bytes )
227+ ws .workspace .get_status = lambda _ : ObjectInfo (object_id = 123 )
228+ mocker .patch ("builtins.input" , return_value = "42" )
229+
230+ ws .warehouses .list = lambda ** _ : [
231+ EndpointInfo (id = "abc" , warehouse_type = EndpointInfoWarehouseType .PRO , state = State .RUNNING )
232+ ]
233+ ws .cluster_policies .list = lambda : []
234+
235+ install = WorkspaceInstaller (ws )
236+ install ._choice = lambda _1 , _2 : "None (abc, PRO, RUNNING)"
237+ install ._configure ()
238+
239+ ws .workspace .upload .assert_called_with (
240+ "/Users/[email protected] /.ucx/config.yml" ,
241+ b"""default_catalog: ucx_default
242+ include_group_names:
243+ - '42'
244+ - '100'
245+ inventory_database: '42'
246+ log_level: '42'
247+ num_threads: 42
248+ renamed_group_prefix: backup_baguette_prefix
249+ version: 2
250+ warehouse_id: abc
251+ workspace_start_path: /
252+ """ ,
253+ format = ImportFormat .AUTO ,
254+ overwrite = True ,
151255 )
152256
153257
154258def test_save_config_with_error (ws , mocker ):
155259 def not_found (_ ):
156- raise DatabricksError ( error_code = "RAISED_FOR_TESTING " )
260+ raise NotFound ( message = "File not found " )
157261
158262 mocker .patch ("builtins.input" , return_value = "42" )
159263
160- ws .workspace .get_status = not_found
264+ ws .workspace .download = not_found
161265 ws .cluster_policies .list = lambda : []
162266
163267 install = WorkspaceInstaller (ws )
164- with pytest .raises (DatabricksError ) as e_info :
268+ with pytest .raises (NotFound ) as e_info :
165269 install ._configure ()
166- assert str (e_info .value .error_code ) == "RAISED_FOR_TESTING"
270+
271+ assert str (e_info .value .args [0 ]) == "File not found"
167272
168273
169274def test_save_config_auto_groups (ws , mocker ):
@@ -201,6 +306,7 @@ def mock_question(text: str, *, default: str | None = None) -> str:
201306workspace_start_path: /
202307""" ,
203308 format = ImportFormat .AUTO ,
309+ overwrite = False ,
204310 )
205311
206312
@@ -243,6 +349,7 @@ def mock_question(text: str, *, default: str | None = None) -> str:
243349workspace_start_path: /
244350""" ,
245351 format = ImportFormat .AUTO ,
352+ overwrite = False ,
246353 )
247354
248355
@@ -307,6 +414,7 @@ def mock_choice_from_dict(text: str, choices: dict[str, Any]) -> Any:
307414workspace_start_path: /
308415""" ,
309416 format = ImportFormat .AUTO ,
417+ overwrite = False ,
310418 )
311419
312420
@@ -330,7 +438,7 @@ def test_main_with_existing_conf_does_not_recreate_config(ws, mocker):
330438renamed_group_prefix: '42'
331439spark_conf:
332440 spark.databricks.hive.metastore.glueCatalog.enabled: 'true'
333- version: 1
441+ version: 2
334442warehouse_id: abc
335443workspace_start_path: /
336444"""
0 commit comments