test__cloud_sdk.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 mock
  19. import pytest
  20. from google.auth import _cloud_sdk
  21. from google.auth import environment_vars
  22. from google.auth import exceptions
  23. import yatest.common
  24. DATA_DIR = os.path.join(yatest.common.test_source_path(), "data")
  25. AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json")
  26. with io.open(AUTHORIZED_USER_FILE) as fh:
  27. AUTHORIZED_USER_FILE_DATA = json.load(fh)
  28. SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")
  29. with io.open(SERVICE_ACCOUNT_FILE) as fh:
  30. SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
  31. with io.open(os.path.join(DATA_DIR, "cloud_sdk_config.json"), "rb") as fh:
  32. CLOUD_SDK_CONFIG_FILE_DATA = fh.read()
  33. @pytest.mark.parametrize(
  34. "data, expected_project_id",
  35. [
  36. (CLOUD_SDK_CONFIG_FILE_DATA, "example-project"),
  37. (b"I am some bad json", None),
  38. (b"{}", None),
  39. ],
  40. )
  41. def test_get_project_id(data, expected_project_id):
  42. check_output_patch = mock.patch(
  43. "subprocess.check_output", autospec=True, return_value=data
  44. )
  45. with check_output_patch as check_output:
  46. project_id = _cloud_sdk.get_project_id()
  47. assert project_id == expected_project_id
  48. assert check_output.called
  49. @mock.patch(
  50. "subprocess.check_output",
  51. autospec=True,
  52. side_effect=subprocess.CalledProcessError(-1, None),
  53. )
  54. def test_get_project_id_call_error(check_output):
  55. project_id = _cloud_sdk.get_project_id()
  56. assert project_id is None
  57. assert check_output.called
  58. def test__run_subprocess_ignore_stderr():
  59. command = [
  60. "python",
  61. "-c",
  62. "from __future__ import print_function;"
  63. + "import sys;"
  64. + "print('error', file=sys.stderr);"
  65. + "print('output', file=sys.stdout)",
  66. ]
  67. # If we ignore stderr, then the output only has stdout
  68. output = _cloud_sdk._run_subprocess_ignore_stderr(command)
  69. assert output == b"output\n"
  70. # If we pipe stderr to stdout, then the output is mixed with stdout and stderr.
  71. output = subprocess.check_output(command, stderr=subprocess.STDOUT)
  72. assert output == b"output\nerror\n" or output == b"error\noutput\n"
  73. @mock.patch("os.name", new="nt")
  74. def test_get_project_id_windows():
  75. check_output_patch = mock.patch(
  76. "subprocess.check_output",
  77. autospec=True,
  78. return_value=CLOUD_SDK_CONFIG_FILE_DATA,
  79. )
  80. with check_output_patch as check_output:
  81. project_id = _cloud_sdk.get_project_id()
  82. assert project_id == "example-project"
  83. assert check_output.called
  84. # Make sure the executable is `gcloud.cmd`.
  85. args = check_output.call_args[0]
  86. command = args[0]
  87. executable = command[0]
  88. assert executable == "gcloud.cmd"
  89. @mock.patch("google.auth._cloud_sdk.get_config_path", autospec=True)
  90. def test_get_application_default_credentials_path(get_config_dir):
  91. config_path = "config_path"
  92. get_config_dir.return_value = config_path
  93. credentials_path = _cloud_sdk.get_application_default_credentials_path()
  94. assert credentials_path == os.path.join(
  95. config_path, _cloud_sdk._CREDENTIALS_FILENAME
  96. )
  97. def test_get_config_path_env_var(monkeypatch):
  98. config_path_sentinel = "config_path"
  99. monkeypatch.setenv(environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
  100. config_path = _cloud_sdk.get_config_path()
  101. assert config_path == config_path_sentinel
  102. @mock.patch("os.path.expanduser")
  103. def test_get_config_path_unix(expanduser):
  104. expanduser.side_effect = lambda path: path
  105. config_path = _cloud_sdk.get_config_path()
  106. assert os.path.split(config_path) == ("~/.config", _cloud_sdk._CONFIG_DIRECTORY)
  107. @mock.patch("os.name", new="nt")
  108. def test_get_config_path_windows(monkeypatch):
  109. appdata = "appdata"
  110. monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
  111. config_path = _cloud_sdk.get_config_path()
  112. assert os.path.split(config_path) == (appdata, _cloud_sdk._CONFIG_DIRECTORY)
  113. @mock.patch("os.name", new="nt")
  114. def test_get_config_path_no_appdata(monkeypatch):
  115. monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
  116. monkeypatch.setenv("SystemDrive", "G:")
  117. config_path = _cloud_sdk.get_config_path()
  118. assert os.path.split(config_path) == ("G:/\\", _cloud_sdk._CONFIG_DIRECTORY)
  119. @mock.patch("os.name", new="nt")
  120. @mock.patch("subprocess.check_output", autospec=True)
  121. def test_get_auth_access_token_windows(check_output):
  122. check_output.return_value = b"access_token\n"
  123. token = _cloud_sdk.get_auth_access_token()
  124. assert token == "access_token"
  125. check_output.assert_called_with(
  126. ("gcloud.cmd", "auth", "print-access-token"), stderr=subprocess.STDOUT
  127. )
  128. @mock.patch("subprocess.check_output", autospec=True)
  129. def test_get_auth_access_token_with_account(check_output):
  130. check_output.return_value = b"access_token\n"
  131. token = _cloud_sdk.get_auth_access_token(account="account")
  132. assert token == "access_token"
  133. check_output.assert_called_with(
  134. ("gcloud", "auth", "print-access-token", "--account=account"),
  135. stderr=subprocess.STDOUT,
  136. )
  137. @mock.patch("subprocess.check_output", autospec=True)
  138. def test_get_auth_access_token_with_exception(check_output):
  139. check_output.side_effect = OSError()
  140. with pytest.raises(exceptions.UserAccessTokenError):
  141. _cloud_sdk.get_auth_access_token(account="account")