Whamcloud - gitweb
LU-16267 lnet: fix missing error check in LUTF 51/48951/2
authorCyril Bordage <cbordage@whamcloud.com>
Tue, 25 Oct 2022 16:52:30 +0000 (18:52 +0200)
committerOleg Drokin <green@whamcloud.com>
Fri, 27 Jan 2023 00:33:50 +0000 (00:33 +0000)
In find_replace_file function, the file is opened with default
encoding option. If the file has a different encoding it will fail.
The solution is to use a try/except for UnicodeDecodeError and skip
bad encoded files.

Test-Parameters: @lnet
Signed-off-by: Cyril Bordage <cbordage@whamcloud.com>
Change-Id: I9115d39414d31b628d550e8289b3193d13787288
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/48951
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Amir Shehata <ashehata@whamcloud.com>
Reviewed-by: Chris Horn <chris.horn@hpe.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/tests/lutf/python/infra/lutf_file.py

index 5658f37..a7f9eec 100644 (file)
@@ -73,11 +73,14 @@ class LutfFile(BaseTest):
                # search string
                if not replace:
                        with open(fname) as old_file:
-                               for line in old_file:
-                                       if search in line:
-                                               if not found_line:
-                                                       found_line = line
-                                               count += 1
+                               try:
+                                       for line in old_file:
+                                               if search in line:
+                                                       if not found_line:
+                                                               found_line = line
+                                                       count += 1
+                               except UnicodeDecodeError:
+                                       pass
                        if getline:
                                return count, found_line
                        return count
@@ -85,20 +88,26 @@ class LutfFile(BaseTest):
                fh, abs_path = tempfile.mkstemp()
                with os.fdopen(fh,'w') as new_file:
                        with open(fname) as old_file:
-                               for line in old_file:
-                                       if search in line:
-                                               if not found_line:
-                                                       found_line = line
-                                               new_file.write(replace)
-                                               count += 1
-                                       else:
-                                               new_file.write(line)
-               #Copy the file permissions from the old file to the new file
-               shutil.copymode(fname, abs_path)
-               #Remove original file
-               os.remove(fname)
-               #Move new file
-               shutil.move(abs_path, fname)
+                               try:
+                                       for line in old_file:
+                                               if search in line:
+                                                       if not found_line:
+                                                               found_line = line
+                                                       new_file.write(replace)
+                                                       count += 1
+                                               else:
+                                                       new_file.write(line)
+                               except UnicodeDecodeError:
+                                       pass
+               if count:
+                       #Copy the file permissions from the old file to the new file
+                       shutil.copymode(fname, abs_path)
+                       #Remove original file
+                       os.remove(fname)
+                       #Move new file
+                       shutil.move(abs_path, fname)
+               else:
+                       os.remove(abs_path)
 
                if getline:
                        return count, found_line