|
19 | 19 | import pytest
|
20 | 20 | import six
|
21 | 21 | from botocore.exceptions import ClientError
|
22 |
| -from mock import Mock, patch, call |
| 22 | +from mock import MagicMock, Mock, patch, call, mock_open |
23 | 23 |
|
24 | 24 | import sagemaker
|
25 | 25 | from sagemaker import s3_input, Session, get_execution_role
|
|
36 | 36 | @pytest.fixture()
|
37 | 37 | def boto_session():
|
38 | 38 | boto_session = Mock(region_name=REGION)
|
| 39 | + |
| 40 | + mock_client = Mock() |
| 41 | + mock_client._client_config.user_agent = \ |
| 42 | + 'Boto3/1.9.69 Python/3.6.5 Linux/4.14.77-70.82.amzn1.x86_64 Botocore/1.12.69 Resource' |
| 43 | + |
| 44 | + boto_session.client.return_value = mock_client |
39 | 45 | return boto_session
|
40 | 46 |
|
41 | 47 |
|
@@ -139,6 +145,45 @@ def test_delete_endpoint(boto_session):
|
139 | 145 | boto_session.client().delete_endpoint.assert_called_with(EndpointName='my_endpoint')
|
140 | 146 |
|
141 | 147 |
|
| 148 | +def test_user_agent_injected(boto_session): |
| 149 | + assert 'AWS-SageMaker-Python-SDK' not in boto_session.client('sagemaker')._client_config.user_agent |
| 150 | + |
| 151 | + sess = Session(boto_session) |
| 152 | + |
| 153 | + assert 'AWS-SageMaker-Python-SDK' in sess.sagemaker_client._client_config.user_agent |
| 154 | + assert 'AWS-SageMaker-Python-SDK' in sess.sagemaker_runtime_client._client_config.user_agent |
| 155 | + assert 'AWS-SageMaker-Notebook-Instance' not in sess.sagemaker_client._client_config.user_agent |
| 156 | + assert 'AWS-SageMaker-Notebook-Instance' not in sess.sagemaker_runtime_client._client_config.user_agent |
| 157 | + |
| 158 | + |
| 159 | +def test_user_agent_injected_with_nbi(boto_session): |
| 160 | + assert 'AWS-SageMaker-Python-SDK' not in boto_session.client('sagemaker')._client_config.user_agent |
| 161 | + |
| 162 | + with patch('six.moves.builtins.open', mock_open(read_data='120.0-0')) as mo: |
| 163 | + sess = Session(boto_session) |
| 164 | + |
| 165 | + mo.assert_called_with('/etc/opt/ml/sagemaker-notebook-instance-version.txt') |
| 166 | + |
| 167 | + assert 'AWS-SageMaker-Python-SDK' in sess.sagemaker_client._client_config.user_agent |
| 168 | + assert 'AWS-SageMaker-Python-SDK' in sess.sagemaker_runtime_client._client_config.user_agent |
| 169 | + assert 'AWS-SageMaker-Notebook-Instance' in sess.sagemaker_client._client_config.user_agent |
| 170 | + assert 'AWS-SageMaker-Notebook-Instance' in sess.sagemaker_runtime_client._client_config.user_agent |
| 171 | + |
| 172 | + |
| 173 | +def test_user_agent_injected_with_nbi_ioerror(boto_session): |
| 174 | + assert 'AWS-SageMaker-Python-SDK' not in boto_session.client('sagemaker')._client_config.user_agent |
| 175 | + |
| 176 | + with patch('six.moves.builtins.open', MagicMock(side_effect=IOError('File not found'))) as mo: |
| 177 | + sess = Session(boto_session) |
| 178 | + |
| 179 | + mo.assert_called_with('/etc/opt/ml/sagemaker-notebook-instance-version.txt') |
| 180 | + |
| 181 | + assert 'AWS-SageMaker-Python-SDK' in sess.sagemaker_client._client_config.user_agent |
| 182 | + assert 'AWS-SageMaker-Python-SDK' in sess.sagemaker_runtime_client._client_config.user_agent |
| 183 | + assert 'AWS-SageMaker-Notebook-Instance' not in sess.sagemaker_client._client_config.user_agent |
| 184 | + assert 'AWS-SageMaker-Notebook-Instance' not in sess.sagemaker_runtime_client._client_config.user_agent |
| 185 | + |
| 186 | + |
142 | 187 | def test_s3_input_all_defaults():
|
143 | 188 | prefix = 'pre'
|
144 | 189 | actual = s3_input(s3_data=prefix)
|
|
0 commit comments