22145742 pkgrecv unit tests for --mog-file leave temporary files in tests directory s12b87
authorXiaobo Shen <xiaobo.shen@oracle.com>
Tue, 10 Nov 2015 13:20:34 +1100
changeset 3279 60851566e7ef
parent 3278 c7b566047d35
child 3280 99b5adc29aca
22145742 pkgrecv unit tests for --mog-file leave temporary files in tests directory
src/pull.py
src/tests/cli/t_pkgrecv.py
--- a/src/pull.py	Mon Nov 09 15:50:23 2015 -0800
+++ b/src/pull.py	Tue Nov 10 13:20:34 2015 +1100
@@ -732,7 +732,21 @@
                 new_lines.append(al)
 
         return (nfmri, new_lines)
-    
+
+def _rm_temp_raw_files(fmri, xport_cfg, ignore_errors=False):
+        # pkgdir is a directory with format: pkg_name/version.
+        # pkg_parentdir is the actual pkg_name directory.
+        pkgdir = xport_cfg.get_pkg_dir(fmri)
+        pkg_parentdir = os.path.dirname(pkgdir)
+        shutil.rmtree(pkgdir,
+            ignore_errors=ignore_errors)
+
+        # If the parent directory become empty,
+        # remove it as well.
+        if not os.listdir(pkg_parentdir):
+                shutil.rmtree(pkg_parentdir,
+                    ignore_errors=ignore_errors)
+
 def archive_pkgs(pargs, target, list_newest, all_versions, all_timestamps,
     keep_compresed, raw, recursive, dry_run, verbose, dest_xport_cfg, src_uri,
     dkey, dcert, mog_files):
@@ -806,26 +820,23 @@
 
                         nf = f
                         if do_mog:
-                                nf, line_buffer = __mog_helper(mog_files,
-                                    f, m.pathname)
                                 try:
+                                        nf, line_buffer = __mog_helper(mog_files,
+                                            f, m.pathname)
                                         # Create mogrified manifest.
-                                        # Remove the old manifest cache first.
-                                        oldpkgdir = xport_cfg.get_pkg_dir(f)
-                                        oldpkg_parentdir = os.path.dirname(
-                                            oldpkgdir)
-                                        shutil.rmtree(oldpkgdir)
-                                        # If the parent directory become empty,
-                                        # remove it as well.
-                                        if not os.listdir(oldpkg_parentdir):
-                                                shutil.rmtree(oldpkg_parentdir)
+                                        # Remove the old raw pkg data first.
+                                        _rm_temp_raw_files(f, xport_cfg)
                                         nm = pkg.manifest.FactoredManifest(nf,
                                             xport_cfg.get_pkg_dir(nf),
                                             contents="\n".join(
                                             line_buffer))
                                 except EnvironmentError as e:
+                                        _rm_temp_raw_files(nf, xport_cfg,
+                                            ignore_errors=True)
                                         raise apx._convert_error(e)
                                 except Exception as e:
+                                        _rm_temp_raw_files(nf, xport_cfg,
+                                            ignore_errors=True)
                                         abort(_("Creating mogrified "
                                             "manifest failed: {0}"
                                             ).format(str(e)))
@@ -1428,8 +1439,13 @@
 
                         nf = f
                         if do_mog:
-                                nf, line_buffer = __mog_helper(mog_files,
-                                    f, m.pathname)
+                                try:
+                                        nf, line_buffer = __mog_helper(mog_files,
+                                            f, m.pathname)
+                                except Exception as e:
+                                        _rm_temp_raw_files(f, xport_cfg,
+                                            ignore_errors=True)
+                                        abort(err=e)
 
                         # Figure out whether the package is already in
                         # the target repository or not.
@@ -1473,25 +1489,23 @@
                                 # mogrified manifest for future use.
                                 try:
                                         # Create mogrified manifest.
-                                        # Remove the old manifest cache first.
-                                        oldpkgdir = xport_cfg.get_pkg_dir(f)
-                                        oldpkg_parentdir = os.path.dirname(
-                                            oldpkgdir)
-                                        shutil.rmtree(oldpkgdir)
-                                        # If the parent directory become empty,
-                                        # remove it as well.
-                                        if not os.listdir(oldpkg_parentdir):
-                                                shutil.rmtree(oldpkg_parentdir)
+                                        # Remove the old raw pkg data first.
+                                        _rm_temp_raw_files(f, xport_cfg)
                                         nm = pkg.manifest.FactoredManifest(nf,
                                             xport_cfg.get_pkg_dir(nf),
                                             contents="\n".join(
                                             line_buffer))
                                 except EnvironmentError as e:
+                                        _rm_temp_raw_files(nf, xport_cfg,
+                                            ignore_errors=True)
                                         raise apx._convert_error(e)
                                 except Exception as e:
+                                        _rm_temp_raw_files(nf, xport_cfg,
+                                            ignore_errors=True)
                                         abort(_("Creating mogrified "
                                             "manifest failed: {0}"
                                             ).format(str(e)))
+
                         else:
                                 # Use the original manifest if no
                                 # mogrify is done.
@@ -1512,6 +1526,9 @@
                         get_bytes += getb
                         get_files += getf
 
+                        if dry_run:
+                                _rm_temp_raw_files(nf, xport_cfg,
+                                    ignore_errors=True)
                         tracker.manifest_fetch_progress(completion=True)
                 tracker.manifest_fetch_done()
                 # Next, retrieve and store the content for each package.
--- a/src/tests/cli/t_pkgrecv.py	Mon Nov 09 15:50:23 2015 -0800
+++ b/src/tests/cli/t_pkgrecv.py	Tue Nov 10 13:20:34 2015 +1100
@@ -1307,12 +1307,13 @@
                 republish behaviour."""
 
                 # Test with add an invalid action will fail.
+                rpth = tempfile.mkdtemp(dir=self.test_root)
                 self.pkgrecv(self.durl5, "--mog-file {0} -d {1} -v filetrans1"
                     .format(self.transforms["add_invalid_act"], self.durl6),
                     exit=1)
                 self.pkgrecv(self.durl5, "--raw --mog-file {0} -d {1} "
                     "-v filetrans1".format(self.transforms["add_invalid_act"],
-                    self.durl6), exit=1)
+                    rpth), exit=1)
 
                 # Test add a depend action without fmri attribute will fail.
                 self.pkgrecv(self.durl5, "--mog-file {0} -d {1} -v filetrans1"
@@ -1320,7 +1321,7 @@
                     exit=1)
                 self.pkgrecv(self.durl5, "--raw --mog-file {0} -d {1} "
                     "-v filetrans1".format(self.transforms["add_invalid_act2"],
-                    self.durl6), exit=1)
+                    rpth), exit=1)
 
                 # Test add a depend action with invalid fmri attribute will
                 # fail.
@@ -1329,7 +1330,7 @@
                     exit=1)
                 self.pkgrecv(self.durl5, "--raw --mog-file {0} -d {1} -v "
                     "filetrans1".format(self.transforms["add_invalid_act3"],
-                    self.durl6), exit=1)
+                    rpth), exit=1)
 
                 # Test with adding a non-existing file will fail.
                 self.pkgrecv(self.durl5, "--mog-file {0} -d {1} -v filetrans1"
@@ -1337,7 +1338,7 @@
                     exit=1)
                 self.pkgrecv(self.durl5, "--raw --mog-file {0} -d {1} -v "
                     "filetrans1".format(self.transforms["add_none_file"],
-                    self.durl6), exit=1)
+                    rpth), exit=1)
                 self.assert_("not allowed" in self.errout)
 
                 # Test with adding an additional existing file