test__cloud_sdk.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright 2016 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import io
  15. import json
  16. import os
  17. import subprocess
  18. import sys
  19. import mock
  20. import pytest # type: ignore
  21. from google.auth import _cloud_sdk
  22. from google.auth import environment_vars
  23. from google.auth import exceptions
  24. import yatest.common as yc
  25. DATA_DIR = os.path.join(os.path.dirname(yc.source_path(__file__)), "data")
  26. AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json")
  27. with io.open(AUTHORIZED_USER_FILE, "rb") as fh:
  28. AUTHORIZED_USER_FILE_DATA = json.load(fh)
  29. SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
  30. with io.open(SERVICE_ACCOUNT_FILE, "rb") as fh:
  31. SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
  32. @pytest.mark.parametrize(
  33. "data, expected_project_id",
  34. [(b"example-project\n", "example-project"), (b"", None)],
  35. )
  36. def test_get_project_id(data, expected_project_id):
  37. check_output_patch = mock.patch(
  38. "subprocess.check_output", autospec=True, return_value=data
  39. )
  40. with check_output_patch as check_output:
  41. project_id = _cloud_sdk.get_project_id()
  42. assert project_id == expected_project_id
  43. assert check_output.called
  44. @mock.patch(
  45. "subprocess.check_output",
  46. autospec=True,
  47. side_effect=subprocess.CalledProcessError(-1, "testing"),
  48. )
  49. def test_get_project_id_call_error(check_output):
  50. project_id = _cloud_sdk.get_project_id()
  51. assert project_id is None
  52. assert check_output.called
  53. def _test__run_subprocess_ignore_stderr():
  54. command = [
  55. sys.executable,
  56. "-c",
  57. "from __future__ import print_function;"
  58. + "import sys;"
  59. + "print('error', file=sys.stderr);"
  60. + "print('output', file=sys.stdout)",
  61. ]
  62. # If we ignore stderr, then the output only has stdout
  63. output = _cloud_sdk._run_subprocess_ignore_stderr(command)
  64. assert output == b"output\n"
  65. # If we pipe stderr to stdout, then the output is mixed with stdout and stderr.
  66. output = subprocess.check_output(command, stderr=subprocess.STDOUT)
  67. assert output == b"output\nerror\n" or output == b"error\noutput\n"
  68. @mock.patch("os.name", new="nt")
  69. def test_get_project_id_windows():
  70. check_output_patch = mock.patch(
  71. "subprocess.check_output", autospec=True, return_value=b"example-project\n"
  72. )
  73. with check_output_patch as check_output:
  74. project_id = _cloud_sdk.get_project_id()
  75. assert project_id == "example-project"
  76. assert check_output.called
  77. # Make sure the executable is `gcloud.cmd`.
  78. args = check_output.call_args[0]
  79. command = args[0]
  80. executable = command[0]
  81. assert executable == "gcloud.cmd"
  82. @mock.patch("google.auth._cloud_sdk.get_config_path", autospec=True)
  83. def test_get_application_default_credentials_path(get_config_dir):
  84. config_path = "config_path"
  85. get_config_dir.return_value = config_path
  86. credentials_path = _cloud_sdk.get_application_default_credentials_path()
  87. assert credentials_path == os.path.join(
  88. config_path, _cloud_sdk._CREDENTIALS_FILENAME
  89. )
  90. def test_get_config_path_env_var(monkeypatch):
  91. config_path_sentinel = "config_path"
  92. monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
  93. config_path = _cloud_sdk.get_config_path()
  94. assert config_path == config_path_sentinel
  95. @mock.patch("os.path.expanduser")
  96. def test_get_config_path_unix(expanduser):
  97. expanduser.side_effect = lambda path: path
  98. config_path = _cloud_sdk.get_config_path()
  99. assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY)
  100. @mock.patch("os.name", new="nt")
  101. def test_get_config_path_windows(monkeypatch):
  102. appdata = "appdata"
  103. monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
  104. config_path = _cloud_sdk.get_config_path()
  105. assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY)
  106. @mock.patch("os.name", new="nt")
  107. def test_get_config_path_no_appdata(monkeypatch):
  108. monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
  109. monkeypatch.setenv("SystemDrive", "G:")
  110. config_path = _cloud_sdk.get_config_path()
  111. assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)
  112. @mock.patch("os.name", new="nt")
  113. @mock.patch("subprocess.check_output", autospec=True)
  114. def test_get_auth_access_token_windows(check_output):
  115. check_output.return_value = b"access_token\n"
  116. token = _cloud_sdk.get_auth_access_token()
  117. assert token == "access_token"
  118. check_output.assert_called_with(
  119. ("gcloud.cmd", "auth", "print-access-token"), stderr=subprocess.STDOUT
  120. )
  121. @mock.patch("subprocess.check_output", autospec=True)
  122. def test_get_auth_access_token_with_account(check_output):
  123. check_output.return_value = b"access_token\n"
  124. token = _cloud_sdk.get_auth_access_token(account="account")
  125. assert token == "access_token"
  126. check_output.assert_called_with(
  127. ("gcloud", "auth", "print-access-token", "--account=account"),
  128. stderr=subprocess.STDOUT,
  129. )
  130. @mock.patch("subprocess.check_output", autospec=True)
  131. def test_get_auth_access_token_with_exception(check_output):
  132. check_output.side_effect = OSError()
  133. with pytest.raises(exceptions.UserAccessTokenError):
  134. _cloud_sdk.get_auth_access_token(account="account")