From 2398f28295f030f789aaa34c1aabafae034c2166 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Mon, 8 Jul 2024 15:51:56 +0200 Subject: [PATCH 1/5] update --- tests/test_core.py | 56 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index eb2f6d954..3b7ba5893 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -23,6 +23,7 @@ databricks_cli) from databricks.sdk.environments import (ENVIRONMENTS, AzureEnvironment, Cloud, DatabricksEnvironment) +from databricks.sdk.oauth import Token from databricks.sdk.service.catalog import PermissionsChange from databricks.sdk.service.iam import AccessControlRequest from databricks.sdk.version import __version__ @@ -67,9 +68,14 @@ def test_databricks_cli_token_parse_expiry(date_string, expected): def write_small_dummy_executable(path: pathlib.Path): - cli = path.joinpath('databricks') - cli.write_text('#!/bin/sh\necho "hello world"\n') - cli.chmod(0o755) + if platform.system() == "Windows": + cli = path.joinpath('databricks.exe') + cli.touch() + cli.write_text('@echo off\necho "hello world"\n') + else: + cli = path.joinpath('databricks') + cli.write_text('#!/bin/sh\necho "hello world"\n') + cli.chmod(0o755) assert cli.stat().st_size < 1024 return cli @@ -101,11 +107,28 @@ def test_streaming_response_read_closes(config): def write_large_dummy_executable(path: pathlib.Path): - cli = path.joinpath('databricks') + if platform.system() == "Windows": + cli = path.joinpath('databricks.ps1') + random_string = ''.join(random.choice(string.ascii_letters) for i in range(1024 * 1024)) + cli.write_text("""#!C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe - # Generate a long random string to inflate the file size. - random_string = ''.join(random.choice(string.ascii_letters) for i in range(1024 * 1024)) - cli.write_text("""#!/bin/sh +$tokenInfo = @" +{ +"access_token": "token", +"token_type": "Bearer", +"expiry": "2023-05-22T00:00:00.000000+00:00" +} +"@ + +Write-Output $tokenInfo +exit 0""" + random_string) + cli.chmod(0o755) + else: + cli = path.joinpath('databricks') + + # Generate a long random string to inflate the file size. + random_string = ''.join(random.choice(string.ascii_letters) for i in range(1024 * 1024)) + cli.write_text("""#!/bin/sh cat <= (1024 * 1024) return cli @@ -175,10 +198,21 @@ def test_databricks_cli_credential_provider_installed_legacy(config, monkeypatch assert databricks_cli(config) == None -def test_databricks_cli_credential_provider_installed_new(config, monkeypatch, tmp_path): - write_large_dummy_executable(tmp_path) - monkeypatch.setenv('PATH', str(os.pathsep).join([tmp_path.as_posix(), os.environ['PATH']])) +def test_databricks_cli_credential_provider_installed_new(config, monkeypatch, tmp_path, mocker): + get_mock = mocker.patch('databricks.sdk.credentials_provider.CliTokenSource.refresh', + return_value=Token(access_token='token', + token_type='Bearer', + expiry=datetime(2023, 5, 22, 0, 0, 0))) + path = str(os.pathsep).join([tmp_path.as_posix(), os.environ['PATH']]) + if platform.system() == 'Windows': + path = pathlib.PureWindowsPath(path) + monkeypatch.setenv('COMSPEC', 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe') + else: + path = pathlib.PurePosixPath(path) + path = path.__str__() + monkeypatch.setenv('PATH', path) assert databricks_cli(config) is not None + assert get_mock.call_count == 1 def test_extra_and_upstream_user_agent(monkeypatch): From 2cdf612f1e38d99ea12ea11db3c853ff13b9a4e3 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Mon, 8 Jul 2024 15:55:52 +0200 Subject: [PATCH 2/5] update --- tests/test_core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index 3b7ba5893..74f5a9063 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -203,6 +203,7 @@ def test_databricks_cli_credential_provider_installed_new(config, monkeypatch, t return_value=Token(access_token='token', token_type='Bearer', expiry=datetime(2023, 5, 22, 0, 0, 0))) + write_large_dummy_executable(tmp_path) path = str(os.pathsep).join([tmp_path.as_posix(), os.environ['PATH']]) if platform.system() == 'Windows': path = pathlib.PureWindowsPath(path) @@ -211,6 +212,7 @@ def test_databricks_cli_credential_provider_installed_new(config, monkeypatch, t path = pathlib.PurePosixPath(path) path = path.__str__() monkeypatch.setenv('PATH', path) + assert databricks_cli(config) is not None assert get_mock.call_count == 1 From 88417a5c19574e591600963430fba01b91a49b04 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Mon, 8 Jul 2024 16:04:15 +0200 Subject: [PATCH 3/5] update --- tests/test_core.py | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 74f5a9063..1a6a765d6 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -107,28 +107,11 @@ def test_streaming_response_read_closes(config): def write_large_dummy_executable(path: pathlib.Path): - if platform.system() == "Windows": - cli = path.joinpath('databricks.ps1') - random_string = ''.join(random.choice(string.ascii_letters) for i in range(1024 * 1024)) - cli.write_text("""#!C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe - -$tokenInfo = @" -{ -"access_token": "token", -"token_type": "Bearer", -"expiry": "2023-05-22T00:00:00.000000+00:00" -} -"@ - -Write-Output $tokenInfo -exit 0""" + random_string) - cli.chmod(0o755) - else: - cli = path.joinpath('databricks') + cli = path.joinpath('databricks') - # Generate a long random string to inflate the file size. - random_string = ''.join(random.choice(string.ascii_letters) for i in range(1024 * 1024)) - cli.write_text("""#!/bin/sh + # Generate a long random string to inflate the file size. + random_string = ''.join(random.choice(string.ascii_letters) for i in range(1024 * 1024)) + cli.write_text("""#!/bin/sh cat <= (1024 * 1024) return cli From 7abc83a38451d77f7ce149794fd5970b24cccda9 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Mon, 8 Jul 2024 16:29:28 +0200 Subject: [PATCH 4/5] update --- tests/test_core.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 1a6a765d6..e5a5466cf 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -139,9 +139,15 @@ def test_databricks_cli_token_source_installed_legacy_with_symlink(config, monke dir1.mkdir() dir2.mkdir() - (dir1 / "databricks").symlink_to(write_small_dummy_executable(dir2)) + if platform.system() == 'Windows': + (dir1 / "databricks.exe").symlink_to(write_small_dummy_executable(dir2)) + path = pathlib.PureWindowsPath(dir1) + else: + (dir1 / "databricks").symlink_to(write_small_dummy_executable(dir2)) + path = pathlib.PurePosixPath(dir1) + path = path.__str__() + monkeypatch.setenv('PATH', path) - monkeypatch.setenv('PATH', dir1.as_posix()) with pytest.raises(FileNotFoundError, match="version <0.100.0 detected"): DatabricksCliTokenSource(config) From 7841b482301f2e813f99061b7c6ea8edc1a59ebb Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Mon, 8 Jul 2024 17:07:55 +0200 Subject: [PATCH 5/5] update --- tests/test_core.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index e5a5466cf..057147159 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -141,11 +141,11 @@ def test_databricks_cli_token_source_installed_legacy_with_symlink(config, monke if platform.system() == 'Windows': (dir1 / "databricks.exe").symlink_to(write_small_dummy_executable(dir2)) - path = pathlib.PureWindowsPath(dir1) else: (dir1 / "databricks").symlink_to(write_small_dummy_executable(dir2)) - path = pathlib.PurePosixPath(dir1) - path = path.__str__() + + path = pathlib.Path(dir1) + path = str(path) monkeypatch.setenv('PATH', path) with pytest.raises(FileNotFoundError, match="version <0.100.0 detected"): @@ -194,12 +194,8 @@ def test_databricks_cli_credential_provider_installed_new(config, monkeypatch, t expiry=datetime(2023, 5, 22, 0, 0, 0))) write_large_dummy_executable(tmp_path) path = str(os.pathsep).join([tmp_path.as_posix(), os.environ['PATH']]) - if platform.system() == 'Windows': - path = pathlib.PureWindowsPath(path) - monkeypatch.setenv('COMSPEC', 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe') - else: - path = pathlib.PurePosixPath(path) - path = path.__str__() + path = pathlib.Path(path) + path = str(path) monkeypatch.setenv('PATH', path) assert databricks_cli(config) is not None