Skip to content

Commit 82b582a

Browse files
authored
fix: log message when sdk defaults not applied (aws#4104)
* fix: log message when sdk defaults not applied * change: add debug level message and additional unit test for sdk default * chore: black-format * chore: pylint * fix: typos
1 parent ffb0a40 commit 82b582a

File tree

2 files changed

+172
-3
lines changed

2 files changed

+172
-3
lines changed

src/sagemaker/config/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def load_sagemaker_config(additional_config_paths: List[str] = None, s3_resource
110110
else:
111111
try:
112112
config_from_file = _load_config_from_file(file_path)
113-
except ValueError:
113+
except ValueError as error:
114114
if file_path not in (
115115
_DEFAULT_ADMIN_CONFIG_FILE_PATH,
116116
_DEFAULT_USER_CONFIG_FILE_PATH,
@@ -119,12 +119,15 @@ def load_sagemaker_config(additional_config_paths: List[str] = None, s3_resource
119119
# If there are no files in the Default config file locations, don't throw
120120
# Exceptions.
121121
raise
122+
123+
logger.debug(error)
122124
if config_from_file:
123125
validate_sagemaker_config(config_from_file)
124126
merge_dicts(merged_config, config_from_file)
125127
logger.info("Fetched defaults config from location: %s", file_path)
126128
else:
127-
logger.debug("Fetched defaults config from location: %s, but it was empty", file_path)
129+
logger.info("Not applying SDK defaults from location: %s", file_path)
130+
128131
return merged_config
129132

130133

tests/unit/sagemaker/config/test_config.py

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
import os
1616
import pytest
1717
import yaml
18+
import logging
1819
from mock import Mock, MagicMock
1920

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+
)
2127
from jsonschema import exceptions
2228
from yaml.constructor import ConstructorError
2329

@@ -236,3 +242,163 @@ def test_merge_of_s3_default_config_file_and_regular_config_file(
236242
s3_resource=s3_resource_mock,
237243
)
238244
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

Comments
 (0)