diff --git a/roles/nvidia-dgx-firmware/files/parse_manifest.py b/roles/nvidia-dgx-firmware/files/parse_manifest.py index 0c361cfc4..d0fb1506a 100644 --- a/roles/nvidia-dgx-firmware/files/parse_manifest.py +++ b/roles/nvidia-dgx-firmware/files/parse_manifest.py @@ -3,7 +3,7 @@ import json from collections import OrderedDict -from sys import argv +from sys import argv, exit def return_json(payload): @@ -173,6 +173,11 @@ def return_json(payload): manifest_json = json.loads(line) except ValueError: pass + + if not isinstance(manifest_json, dict): + print('No JSON could be loaded, is the container already running?') + exit(1) + try: if manifest_json['ErrorWritingVersioning']: print('No JSON could be loaded, is the container already running?') diff --git a/roles/nvidia-dgx-firmware/tests/test_parse_manifest.py b/roles/nvidia-dgx-firmware/tests/test_parse_manifest.py new file mode 100644 index 000000000..9135d0978 --- /dev/null +++ b/roles/nvidia-dgx-firmware/tests/test_parse_manifest.py @@ -0,0 +1,34 @@ +import subprocess +import sys +import tempfile +import unittest + +from pathlib import Path + + +SCRIPT = Path(__file__).resolve().parents[1] / 'files' / 'parse_manifest.py' + + +class ParseManifestTest(unittest.TestCase): + def test_parse_versioning_rejects_non_dict_json(self): + with tempfile.TemporaryDirectory() as temp_dir: + manifest = Path(temp_dir) / 'versioning.json' + manifest.write_text('[1,2,3]\n') + + result = subprocess.run( + [sys.executable, str(SCRIPT), 'parse_versioning', str(manifest)], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) + + self.assertEqual(result.returncode, 1) + self.assertEqual( + result.stdout, + 'No JSON could be loaded, is the container already running?\n', + ) + self.assertEqual(result.stderr, '') + + +if __name__ == '__main__': + unittest.main()