|
17 | 17 | import yaml
|
18 | 18 | from mock import Mock, MagicMock
|
19 | 19 |
|
20 |
| -from sagemaker.config.config import load_sagemaker_config |
| 20 | +from sagemaker.config.config import ( |
| 21 | + load_sagemaker_config, |
| 22 | + logger, |
| 23 | + _DEFAULT_ADMIN_CONFIG_FILE_PATH, |
| 24 | + _DEFAULT_USER_CONFIG_FILE_PATH, |
| 25 | +) |
21 | 26 | from jsonschema import exceptions
|
22 | 27 | from yaml.constructor import ConstructorError
|
23 | 28 |
|
@@ -236,3 +241,92 @@ def test_merge_of_s3_default_config_file_and_regular_config_file(
|
236 | 241 | s3_resource=s3_resource_mock,
|
237 | 242 | )
|
238 | 243 | del os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"]
|
| 244 | + |
| 245 | + |
| 246 | +def test_logging_when_overriden_admin_and_user_configs_are_found(get_data_dir, caplog): |
| 247 | + # Should log info message stating defaults were fetched since both exist |
| 248 | + logger.propagate = True |
| 249 | + os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] = get_data_dir |
| 250 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = get_data_dir |
| 251 | + load_sagemaker_config() |
| 252 | + assert "Fetched defaults config from location: {}".format(get_data_dir) in caplog.text |
| 253 | + assert ( |
| 254 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 255 | + not in caplog.text |
| 256 | + ) |
| 257 | + assert ( |
| 258 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 259 | + not in caplog.text |
| 260 | + ) |
| 261 | + |
| 262 | + |
| 263 | +def test_logging_when_default_admin_and_user_config_not_found(caplog): |
| 264 | + # Should log info message stating sdk defaults were not applied |
| 265 | + # for admin and user config since both are missing from default location |
| 266 | + logger.propagate = True |
| 267 | + load_sagemaker_config() |
| 268 | + assert ( |
| 269 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 270 | + in caplog.text |
| 271 | + ) |
| 272 | + assert ( |
| 273 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 274 | + in caplog.text |
| 275 | + ) |
| 276 | + |
| 277 | + |
| 278 | +def test_logging_when_default_admin_and_overriden_user_config_not_found(get_data_dir, caplog): |
| 279 | + # Should only log info message stating sdk defaults were not applied from default admin config. |
| 280 | + # Failing to load overriden user config should throw exception. |
| 281 | + logger.propagate = True |
| 282 | + fake_config_file_path = os.path.join(get_data_dir, "config-not-found.yaml") |
| 283 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = fake_config_file_path |
| 284 | + with pytest.raises(ValueError): |
| 285 | + load_sagemaker_config() |
| 286 | + assert ( |
| 287 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 288 | + in caplog.text |
| 289 | + ) |
| 290 | + assert ( |
| 291 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 292 | + not in caplog.text |
| 293 | + ) |
| 294 | + del os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] |
| 295 | + |
| 296 | + |
| 297 | +def test_logging_when_overriden_admin_and_overridden_user_config_not_found(get_data_dir, caplog): |
| 298 | + # Should not log any info messages since both config paths are overridden. |
| 299 | + # Should throw an exception on failure since both will fail to load. |
| 300 | + logger.propagate = True |
| 301 | + fake_config_file_path = os.path.join(get_data_dir, "config-not-found.yaml") |
| 302 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = fake_config_file_path |
| 303 | + os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] = fake_config_file_path |
| 304 | + with pytest.raises(ValueError): |
| 305 | + load_sagemaker_config() |
| 306 | + assert ( |
| 307 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 308 | + not in caplog.text |
| 309 | + ) |
| 310 | + assert ( |
| 311 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 312 | + not in caplog.text |
| 313 | + ) |
| 314 | + del os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] |
| 315 | + del os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] |
| 316 | + |
| 317 | + |
| 318 | +def test_logging_with_additional_configs_and_none_are_found(caplog): |
| 319 | + # Should log info message stating sdk defaults were not applied |
| 320 | + # for admin and user config since both are missing from default location. |
| 321 | + # Should throw exception when config in additional_config_path is missing |
| 322 | + logger.propagate = True |
| 323 | + with pytest.raises(ValueError): |
| 324 | + load_sagemaker_config(additional_config_paths=["fake-path"]) |
| 325 | + assert ( |
| 326 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 327 | + in caplog.text |
| 328 | + ) |
| 329 | + assert ( |
| 330 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 331 | + in caplog.text |
| 332 | + ) |
0 commit comments