From 63b4a741c9de9c2ce1b56bebef8c94ac4baba9c2 Mon Sep 17 00:00:00 2001 From: junfeng-li Date: Mon, 5 Feb 2024 16:41:14 +0000 Subject: [PATCH] Improve software upload-dir error handling This commit is to enhance the 'software upload-dir' to handle when the given parameter is invalid, and to improve the error messages to indicate the error that occurred. [sysadmin@controller-0 ~(keystone_admin)]$ software upload-dir abc Invalid release directory: abc No files to upload [sysadmin@controller-0 ~(keystone_admin)]$ Test Plan: PASS: run 'software upload-dir' with invalid directory PASS: run 'software upload-dir' with valid directory Task: 49527 Story: 2010676 Change-Id: I73a82eac713ec90e3df1ea70b033c3ddf1368910 --- .../software_client/software_client.py | 17 +++++++++++++---- software/software/software_controller.py | 2 +- .../software/tests/test_software_controller.py | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/software-client/software_client/software_client.py b/software-client/software_client/software_client.py index 948a8438..c22e8209 100644 --- a/software-client/software_client/software_client.py +++ b/software-client/software_client/software_client.py @@ -868,16 +868,25 @@ def release_upload_dir_req(args): # Find all files that need to be uploaded in given directories for release_dir in release_dirs: - raw_files = [f for f in os.listdir(release_dir) - if os.path.isfile(os.path.join(release_dir, f))] + if os.path.isdir(release_dir): + raw_files = [f for f in os.listdir(release_dir) + if os.path.isfile(os.path.join(release_dir, f))] - # Get absolute path of files - raw_files = [os.path.abspath(os.path.join(release_dir, f)) for f in raw_files] + # Get absolute path of files + raw_files = [os.path.abspath(os.path.join(release_dir, f)) for f in raw_files] + else: + print("Skipping invalid directory: %s" % release_dir, file=sys.stderr) + + if len(raw_files) == 0: + print("No file to upload") + return 0 for software_file in sorted(set(raw_files)): _, ext = os.path.splitext(software_file) if ext in constants.SUPPORTED_UPLOAD_FILE_EXT: to_upload_files[software_file] = (software_file, open(software_file, 'rb')) + else: + print("Skipping unsupported file: %s" % software_file, file=sys.stderr) encoder = MultipartEncoder(fields=to_upload_files) url = "http://%s/v1/software/upload" % api_addr diff --git a/software/software/software_controller.py b/software/software/software_controller.py index f8720425..c78b2244 100644 --- a/software/software/software_controller.py +++ b/software/software/software_controller.py @@ -1001,7 +1001,7 @@ class PatchController(PatchService): upgrade_files[constants.SIG_EXTENSION]): raise ReleaseValidationFailure("Invalid signature file") - msg = ("iso and signature files uploaded completed\n" + msg = ("iso and signature files upload completed\n" "Importing iso is in progress\n") LOG.info(msg) local_info += msg diff --git a/software/software/tests/test_software_controller.py b/software/software/tests/test_software_controller.py index 03b5e4e0..5c6356df 100644 --- a/software/software/tests/test_software_controller.py +++ b/software/software/tests/test_software_controller.py @@ -74,7 +74,7 @@ class TestSoftwareController(unittest.TestCase): # Verify that the expected messages were returned self.assertEqual( info, - 'iso and signature files uploaded completed\nImporting iso is in progress\nLoad import successful') + 'iso and signature files upload completed\nImporting iso is in progress\nLoad import successful') self.assertEqual(warning, '') self.assertEqual(error, '') self.assertEqual(