-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_install_requirements.py
More file actions
264 lines (218 loc) · 10.7 KB
/
Copy pathtest_install_requirements.py
File metadata and controls
264 lines (218 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import
import logging
import os
import subprocess
import sys
from unittest import mock
import pytest
from sagemaker.core.utils.install_requirements import (
CA_REPOSITORY_ARN_ENV,
CodeArtifactAuthMethod,
_parse_arn,
configure_pip,
install_requirements,
main,
)
_MODULE = "sagemaker.core.utils.install_requirements"
VALID_ARN = "arn:aws:codeartifact:us-west-2:123456789012:repository/my-domain/my-repo"
PARSED = ("us-west-2", "123456789012", "my-domain", "my-repo")
FAKE_TOKEN = "fake-auth-token"
FAKE_ENDPOINT = (
"https://my-domain-123456789012.d.codeartifact.us-west-2.amazonaws.com/pypi/my-repo/"
)
EXPECTED_INDEX = (
f"https://aws:{FAKE_TOKEN}"
"@my-domain-123456789012.d.codeartifact.us-west-2.amazonaws.com/pypi/my-repo/simple/"
)
EXPECTED_CLI_CMD = [
"aws", "codeartifact", "login", "--tool", "pip",
"--domain", "my-domain", "--domain-owner", "123456789012",
"--repository", "my-repo", "--region", "us-west-2",
] # fmt: skip
@pytest.fixture()
def ca_env():
"""Set CA_REPOSITORY_ARN in the environment for the duration of a test."""
with mock.patch.dict("os.environ", {CA_REPOSITORY_ARN_ENV: VALID_ARN}):
yield
@pytest.fixture()
def mock_boto3_ca():
"""Mock boto3 CodeArtifact client with valid responses."""
client = mock.MagicMock()
client.get_authorization_token.return_value = {"authorizationToken": FAKE_TOKEN}
client.get_repository_endpoint.return_value = {"repositoryEndpoint": FAKE_ENDPOINT}
with mock.patch("boto3.client", return_value=client) as factory:
yield factory, client
def _pip_cmd(*extra):
return [sys.executable, "-m", "pip", "install", "-r", "reqs.txt", *extra]
# ---------------------------------------------------------------------------
# _parse_arn
# ---------------------------------------------------------------------------
class TestParseArn:
def test_valid_arn(self):
assert _parse_arn(VALID_ARN) == PARSED
def test_invalid_arn(self):
with pytest.raises(ValueError, match="Invalid CA_REPOSITORY_ARN"):
_parse_arn("not-an-arn")
def test_arn_with_nested_repo(self):
region, account, domain, repo = _parse_arn(
"arn:aws:codeartifact:eu-west-1:111111111111:repository/dom/nested/repo"
)
assert (region, account, domain, repo) == (
"eu-west-1",
"111111111111",
"dom",
"nested/repo",
)
# ---------------------------------------------------------------------------
# configure_pip — AUTO (default)
# ---------------------------------------------------------------------------
class TestConfigurePipAuto:
def test_no_env_var_returns_none(self):
with mock.patch.dict("os.environ", {}, clear=True):
assert configure_pip() is None
def test_invalid_arn_raises(self, ca_env):
with mock.patch.dict("os.environ", {CA_REPOSITORY_ARN_ENV: "garbage"}):
with pytest.raises(ValueError, match="Invalid"):
configure_pip()
def test_boto3_success(self, ca_env, mock_boto3_ca):
factory, client = mock_boto3_ca
result = configure_pip()
factory.assert_called_once_with("codeartifact", region_name="us-west-2")
client.get_authorization_token.assert_called_once_with(
domain="my-domain", domainOwner="123456789012"
)
client.get_repository_endpoint.assert_called_once_with(
domain="my-domain", domainOwner="123456789012", repository="my-repo", format="pypi"
)
assert result == EXPECTED_INDEX
def test_falls_back_to_cli_when_no_boto3(self, ca_env):
with mock.patch(f"{_MODULE}._get_index_boto3", side_effect=ImportError):
with mock.patch("subprocess.check_call") as mock_call:
result = configure_pip()
mock_call.assert_called_once_with(EXPECTED_CLI_CMD)
assert result is None
def test_hard_fails_when_nothing_available(self, ca_env):
with mock.patch(f"{_MODULE}._get_index_boto3", side_effect=ImportError):
with mock.patch(f"{_MODULE}._login_awscli", side_effect=FileNotFoundError):
with pytest.raises(SystemExit):
configure_pip()
def test_boto3_api_error_propagates_not_fallback(self, ca_env):
"""boto3 available but API call fails → raise, don't fall back to CLI."""
with mock.patch(f"{_MODULE}._get_index_boto3", side_effect=Exception("AccessDenied")):
with mock.patch(f"{_MODULE}._login_awscli") as mock_cli:
with pytest.raises(Exception, match="AccessDenied"):
configure_pip()
mock_cli.assert_not_called()
# ---------------------------------------------------------------------------
# configure_pip — BOTO3 only
# ---------------------------------------------------------------------------
class TestConfigurePipBoto3Only:
def test_fails_when_unavailable(self, ca_env):
with mock.patch(f"{_MODULE}._get_index_boto3", side_effect=ImportError):
with pytest.raises(SystemExit):
configure_pip(auth_method=CodeArtifactAuthMethod.BOTO3)
def test_does_not_try_cli(self, ca_env, mock_boto3_ca):
with mock.patch(f"{_MODULE}._login_awscli") as mock_cli:
configure_pip(auth_method=CodeArtifactAuthMethod.BOTO3)
mock_cli.assert_not_called()
# ---------------------------------------------------------------------------
# configure_pip — AWS_CLI only
# ---------------------------------------------------------------------------
class TestConfigurePipCliOnly:
def test_succeeds(self, ca_env):
with mock.patch("subprocess.check_call") as mock_call:
result = configure_pip(auth_method=CodeArtifactAuthMethod.AWS_CLI)
assert result is None
mock_call.assert_called_once()
def test_fails_when_unavailable(self, ca_env):
with mock.patch(f"{_MODULE}._login_awscli", side_effect=FileNotFoundError):
with pytest.raises(SystemExit):
configure_pip(auth_method=CodeArtifactAuthMethod.AWS_CLI)
def test_does_not_try_boto3(self, ca_env):
with mock.patch(f"{_MODULE}._get_index_boto3") as mock_boto3:
with mock.patch("subprocess.check_call"):
configure_pip(auth_method=CodeArtifactAuthMethod.AWS_CLI)
mock_boto3.assert_not_called()
# ---------------------------------------------------------------------------
# install_requirements
# ---------------------------------------------------------------------------
class TestInstallRequirements:
def test_without_codeartifact(self):
with mock.patch.dict("os.environ", {}, clear=True):
with mock.patch("subprocess.check_call") as mock_call:
install_requirements("reqs.txt")
mock_call.assert_called_once_with(_pip_cmd())
def test_with_codeartifact_index(self):
captured = {}
def fake_check_call(cmd, env=None):
captured["cmd"] = cmd
captured["env"] = env
with open(env["PIP_CONFIG_FILE"], "r") as config:
captured["config"] = config.read()
with mock.patch(f"{_MODULE}.configure_pip", return_value=EXPECTED_INDEX):
with mock.patch("subprocess.check_call", side_effect=fake_check_call):
install_requirements("reqs.txt")
assert captured["cmd"] == _pip_cmd()
assert captured["env"]["PIP_CONFIG_FILE"]
assert EXPECTED_INDEX in captured["config"]
assert not os.path.exists(captured["env"]["PIP_CONFIG_FILE"])
def test_codeartifact_index_not_logged_or_passed_in_argv(self, caplog):
caplog.set_level(logging.INFO, logger=_MODULE)
with mock.patch(f"{_MODULE}.configure_pip", return_value=EXPECTED_INDEX):
with mock.patch("subprocess.check_call") as mock_call:
install_requirements("reqs.txt")
assert FAKE_TOKEN not in caplog.text
assert EXPECTED_INDEX not in caplog.text
argv = mock_call.call_args[0][0]
assert FAKE_TOKEN not in " ".join(argv)
assert EXPECTED_INDEX not in " ".join(argv)
def test_with_cli_fallback_no_index_flag(self):
with mock.patch(f"{_MODULE}.configure_pip", return_value=None):
with mock.patch("subprocess.check_call") as mock_call:
install_requirements("reqs.txt")
mock_call.assert_called_once_with(_pip_cmd())
def test_custom_python_executable(self):
with mock.patch.dict("os.environ", {}, clear=True):
with mock.patch("subprocess.check_call") as mock_call:
install_requirements("reqs.txt", python_executable="/usr/bin/python3")
mock_call.assert_called_once_with(
["/usr/bin/python3", "-m", "pip", "install", "-r", "reqs.txt"]
)
def test_pip_failure_propagates(self):
with mock.patch.dict("os.environ", {}, clear=True):
with mock.patch(
"subprocess.check_call", side_effect=subprocess.CalledProcessError(1, "pip")
):
with pytest.raises(subprocess.CalledProcessError):
install_requirements("reqs.txt")
def test_auth_method_passed_through(self):
with mock.patch(f"{_MODULE}.configure_pip", return_value=None) as mock_configure:
with mock.patch("subprocess.check_call"):
install_requirements("reqs.txt", auth_method=CodeArtifactAuthMethod.BOTO3)
mock_configure.assert_called_once_with(auth_method=CodeArtifactAuthMethod.BOTO3)
# ---------------------------------------------------------------------------
# main (CLI entry point)
# ---------------------------------------------------------------------------
class TestMain:
def test_default_requirements_file(self):
with mock.patch(f"{_MODULE}.install_requirements") as mock_install:
with mock.patch("sys.argv", ["install_requirements.py"]):
main()
mock_install.assert_called_once_with("requirements.txt")
def test_custom_requirements_file(self):
with mock.patch(f"{_MODULE}.install_requirements") as mock_install:
with mock.patch("sys.argv", ["install_requirements.py", "custom.txt"]):
main()
mock_install.assert_called_once_with("custom.txt")