|
15 | 15 | import os
|
16 | 16 | import pytest
|
17 | 17 | import yaml
|
| 18 | +import logging |
18 | 19 | from mock import Mock, MagicMock
|
19 | 20 |
|
20 |
| -from sagemaker.config.config import load_sagemaker_config |
| 21 | +from sagemaker.config.config import ( |
| 22 | + load_sagemaker_config, |
| 23 | + logger, |
| 24 | + _DEFAULT_ADMIN_CONFIG_FILE_PATH, |
| 25 | + _DEFAULT_USER_CONFIG_FILE_PATH, |
| 26 | +) |
21 | 27 | from jsonschema import exceptions
|
22 | 28 | from yaml.constructor import ConstructorError
|
23 | 29 |
|
@@ -236,3 +242,163 @@ def test_merge_of_s3_default_config_file_and_regular_config_file(
|
236 | 242 | s3_resource=s3_resource_mock,
|
237 | 243 | )
|
238 | 244 | del os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"]
|
| 245 | + |
| 246 | + |
| 247 | +def test_logging_when_overridden_admin_is_found_and_overridden_user_config_is_found( |
| 248 | + get_data_dir, caplog |
| 249 | +): |
| 250 | + # Should log info message stating defaults were fetched since both exist |
| 251 | + logger.propagate = True |
| 252 | + |
| 253 | + os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] = get_data_dir |
| 254 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = get_data_dir |
| 255 | + load_sagemaker_config() |
| 256 | + assert "Fetched defaults config from location: {}".format(get_data_dir) in caplog.text |
| 257 | + assert ( |
| 258 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 259 | + not in caplog.text |
| 260 | + ) |
| 261 | + assert ( |
| 262 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 263 | + not in caplog.text |
| 264 | + ) |
| 265 | + del os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] |
| 266 | + del os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] |
| 267 | + logger.propagate = False |
| 268 | + |
| 269 | + |
| 270 | +def test_logging_when_overridden_admin_is_found_and_default_user_config_not_found( |
| 271 | + get_data_dir, caplog |
| 272 | +): |
| 273 | + logger.propagate = True |
| 274 | + caplog.set_level(logging.DEBUG, logger=logger.name) |
| 275 | + os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] = get_data_dir |
| 276 | + load_sagemaker_config() |
| 277 | + assert "Fetched defaults config from location: {}".format(get_data_dir) in caplog.text |
| 278 | + assert ( |
| 279 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 280 | + in caplog.text |
| 281 | + ) |
| 282 | + assert ( |
| 283 | + "Unable to load the config file from the location: {}".format( |
| 284 | + _DEFAULT_USER_CONFIG_FILE_PATH |
| 285 | + ) |
| 286 | + in caplog.text |
| 287 | + ) |
| 288 | + del os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] |
| 289 | + logger.propagate = False |
| 290 | + |
| 291 | + |
| 292 | +def test_logging_when_default_admin_not_found_and_overriden_user_config_is_found( |
| 293 | + get_data_dir, caplog |
| 294 | +): |
| 295 | + logger.propagate = True |
| 296 | + caplog.set_level(logging.DEBUG, logger=logger.name) |
| 297 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = get_data_dir |
| 298 | + load_sagemaker_config() |
| 299 | + assert "Fetched defaults config from location: {}".format(get_data_dir) in caplog.text |
| 300 | + assert ( |
| 301 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 302 | + in caplog.text |
| 303 | + ) |
| 304 | + assert ( |
| 305 | + "Unable to load the config file from the location: {}".format( |
| 306 | + _DEFAULT_ADMIN_CONFIG_FILE_PATH |
| 307 | + ) |
| 308 | + in caplog.text |
| 309 | + ) |
| 310 | + del os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] |
| 311 | + logger.propagate = False |
| 312 | + |
| 313 | + |
| 314 | +def test_logging_when_default_admin_not_found_and_default_user_config_not_found(caplog): |
| 315 | + # Should log info message stating sdk defaults were not applied |
| 316 | + # for admin and user config since both are missing from default location |
| 317 | + logger.propagate = True |
| 318 | + caplog.set_level(logging.DEBUG, logger=logger.name) |
| 319 | + load_sagemaker_config() |
| 320 | + assert ( |
| 321 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 322 | + in caplog.text |
| 323 | + ) |
| 324 | + assert ( |
| 325 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 326 | + in caplog.text |
| 327 | + ) |
| 328 | + assert ( |
| 329 | + "Unable to load the config file from the location: {}".format( |
| 330 | + _DEFAULT_ADMIN_CONFIG_FILE_PATH |
| 331 | + ) |
| 332 | + in caplog.text |
| 333 | + ) |
| 334 | + assert ( |
| 335 | + "Unable to load the config file from the location: {}".format( |
| 336 | + _DEFAULT_USER_CONFIG_FILE_PATH |
| 337 | + ) |
| 338 | + in caplog.text |
| 339 | + ) |
| 340 | + logger.propagate = False |
| 341 | + |
| 342 | + |
| 343 | +def test_logging_when_default_admin_not_found_and_overriden_user_config_not_found( |
| 344 | + get_data_dir, caplog |
| 345 | +): |
| 346 | + # Should only log info message stating sdk defaults were not applied from default admin config. |
| 347 | + # Failing to load overriden user config should throw exception. |
| 348 | + logger.propagate = True |
| 349 | + fake_config_file_path = os.path.join(get_data_dir, "config-not-found.yaml") |
| 350 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = fake_config_file_path |
| 351 | + with pytest.raises(ValueError): |
| 352 | + load_sagemaker_config() |
| 353 | + assert ( |
| 354 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 355 | + in caplog.text |
| 356 | + ) |
| 357 | + assert ( |
| 358 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 359 | + not in caplog.text |
| 360 | + ) |
| 361 | + del os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] |
| 362 | + logger.propagate = False |
| 363 | + |
| 364 | + |
| 365 | +def test_logging_when_overriden_admin_not_found_and_overridden_user_config_not_found( |
| 366 | + get_data_dir, caplog |
| 367 | +): |
| 368 | + # Should not log any info messages since both config paths are overridden. |
| 369 | + # Should throw an exception on failure since both will fail to load. |
| 370 | + logger.propagate = True |
| 371 | + fake_config_file_path = os.path.join(get_data_dir, "config-not-found.yaml") |
| 372 | + os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] = fake_config_file_path |
| 373 | + os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] = fake_config_file_path |
| 374 | + with pytest.raises(ValueError): |
| 375 | + load_sagemaker_config() |
| 376 | + assert ( |
| 377 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 378 | + not in caplog.text |
| 379 | + ) |
| 380 | + assert ( |
| 381 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 382 | + not in caplog.text |
| 383 | + ) |
| 384 | + del os.environ["SAGEMAKER_USER_CONFIG_OVERRIDE"] |
| 385 | + del os.environ["SAGEMAKER_ADMIN_CONFIG_OVERRIDE"] |
| 386 | + logger.propagate = False |
| 387 | + |
| 388 | + |
| 389 | +def test_logging_with_additional_configs_and_none_are_found(caplog): |
| 390 | + # Should log info message stating sdk defaults were not applied |
| 391 | + # for admin and user config since both are missing from default location. |
| 392 | + # Should throw exception when config in additional_config_path is missing |
| 393 | + logger.propagate = True |
| 394 | + with pytest.raises(ValueError): |
| 395 | + load_sagemaker_config(additional_config_paths=["fake-path"]) |
| 396 | + assert ( |
| 397 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_ADMIN_CONFIG_FILE_PATH) |
| 398 | + in caplog.text |
| 399 | + ) |
| 400 | + assert ( |
| 401 | + "Not applying SDK defaults from location: {}".format(_DEFAULT_USER_CONFIG_FILE_PATH) |
| 402 | + in caplog.text |
| 403 | + ) |
| 404 | + logger.propagate = False |
0 commit comments