Introduce yaml_safe_load, a util wrapper to invoke
yaml.safe_load and correctly handle malformed YAML files.
This wrapper catches exceptions thrown (e.g., incorrect syntax)
while loading/parsing a yaml file. Currently, there's no error
reported back to the user on such failures, rather an ambiguous
python object string is reported and the user is unaware of the
cause of failure. All calls for reading yaml files being uploaded
by the user have been replaced with the wrapper for correct error
reporting.
Test:
1. PASS: Ensure that the following error message is reported when
a malformed file is passed to a command: "Error: Unable to load file contents. (problem on line: #)."
- Add an entry in the bootstrap yaml file without a space after the colon: "key:val"
- Run dcmanager subcloud add with the malformed bootstrap file
- Check the cmdline reported error
2. PASS: Repeat test #1 with common YAML issues, for example:
- Incorrect indentation
- Missing new line between entries
3. PASS: Repeat test #1 with an empty file. Ensure that the following error message is reported: "Error: Unable to load file contents. (empty file provided)."
Reviewed: https:/ /review. opendev. org/c/starlingx /distcloud/ +/899490 /opendev. org/starlingx/ distcloud/ commit/ a46055a2efd9a7a cb1e37368b55197 9ea3d650f1
Committed: https:/
Submitter: "Zuul (22348)"
Branch: master
commit a46055a2efd9a7a cb1e37368b55197 9ea3d650f1
Author: srana <email address hidden>
Date: Fri Oct 27 11:40:39 2023 -0400
Fix: Error Handling for YAML Load
Introduce yaml_safe_load, a util wrapper to invoke
yaml.safe_load and correctly handle malformed YAML files.
This wrapper catches exceptions thrown (e.g., incorrect syntax)
while loading/parsing a yaml file. Currently, there's no error
reported back to the user on such failures, rather an ambiguous
python object string is reported and the user is unaware of the
cause of failure. All calls for reading yaml files being uploaded
by the user have been replaced with the wrapper for correct error
reporting.
Test:
"Error: Unable to load file contents.
(problem on line: #)."
space after the colon:
" key:val"
malformed bootstrap file
following error message is reported:
"Error: Unable to load file contents.
(empty file provided)."
1. PASS: Ensure that the following error message is reported when
a malformed file is passed to a command:
- Add an entry in the bootstrap yaml file without a
- Run dcmanager subcloud add with the
- Check the cmdline reported error
2. PASS: Repeat test #1 with common YAML issues, for example:
- Incorrect indentation
- Missing new line between entries
3. PASS: Repeat test #1 with an empty file. Ensure that the
Closes-Bug: 2041679
Change-Id: I33210a45038d6d 4e98b69809ffa8d 2d431b43190
Signed-off-by: Salman Rana <email address hidden>