From 6f3cb199d5f3f48f31a206cdadc1b91131568085 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Thu, 17 Jun 2021 19:46:07 +0200
Subject: [PATCH] erp5testnode: fix clean up of old instance root folder under
 Python 3

This fixes commit ea4cb55ba607745ba642c60e5e727b68b842f604.

Our 'rmtree' helper has never been supposed to be called on a
non-existing folder: the ENOENT handling is an unrelated
implementation detail for Python 2.

Also drop erp5.util's rmtree and use the one from slapos.core,
which is a [testnode] dependency.
---
 erp5/util/testnode/SlapOSControler.py |  9 +++---
 erp5/util/testnode/Updater.py         |  3 +-
 erp5/util/testnode/Utils.py           | 42 +--------------------------
 erp5/util/testnode/testnode.py        |  2 +-
 4 files changed, 8 insertions(+), 48 deletions(-)

diff --git a/erp5/util/testnode/SlapOSControler.py b/erp5/util/testnode/SlapOSControler.py
index 7a14a6d567..b888b69e16 100644
--- a/erp5/util/testnode/SlapOSControler.py
+++ b/erp5/util/testnode/SlapOSControler.py
@@ -31,11 +31,11 @@ import subprocess
 import time
 import xml_marshaller
 import argparse
+from six.moves import range
 from slapos import client
+from slapos.util import rmtree
 from . import logger
-from .Utils import createFolder, rmtree
-
-from six.moves import range
+from .Utils import createFolder
 
 MAX_PARTITIONS = 10
 MAX_SR_RETRIES = 3
@@ -278,7 +278,8 @@ class SlapOSControler(object):
       self._resetSoftware()
     else:
       createFolder(self.software_root)
-    rmtree(self.old_instance_root) # BBB
+    if os.path.exists(self.old_instance_root): # BBB
+      rmtree(self.old_instance_root)
     instance_root = self.instance_root
     # Delete any existing partition in order to not get its data (ex.
     # MySQL DB content) from previous runs. To support changes of partition
diff --git a/erp5/util/testnode/Updater.py b/erp5/util/testnode/Updater.py
index 05040da730..eaeddcb37e 100644
--- a/erp5/util/testnode/Updater.py
+++ b/erp5/util/testnode/Updater.py
@@ -29,8 +29,7 @@ import os
 import re
 from . import logger
 from .ProcessManager import SubprocessError
-from .Utils import rmtree
-from slapos.util import bytes2str, str2bytes
+from slapos.util import bytes2str, str2bytes, rmtree
 
 SVN_UP_REV = re.compile(r'^(?:At|Updated to) revision (\d+).$')
 SVN_CHANGED_REV = re.compile(r'^Last Changed Rev.*:\s*(\d+)', re.MULTILINE)
diff --git a/erp5/util/testnode/Utils.py b/erp5/util/testnode/Utils.py
index 87b2df8ae8..c109e47865 100644
--- a/erp5/util/testnode/Utils.py
+++ b/erp5/util/testnode/Utils.py
@@ -1,47 +1,7 @@
 import os
-import stat
-import shutil
-import errno
-
 import six
 from six.moves import map
-
-def rmtree(path):
-  """Delete a path recursively.
-
-  Like shutil.rmtree, but supporting the case that some files or folder
-  might have been marked read only.  """
-  def chmod_retry(func, failed_path, exc_info):
-    """Make sure the directories are executable and writable.
-    """
-    # Depending on the Python version, the following items differ.
-    if six.PY3:
-      expected_error_type = PermissionError
-      expected_func = os.lstat
-    else:
-      expected_error_type = OSError
-      expected_func = os.listdir
-
-    e = exc_info[1]
-    if isinstance(e, expected_error_type):
-      if e.errno == errno.ENOENT:
-        # because we are calling again rmtree on listdir errors, this path might
-        # have been already deleted by the recursive call to rmtree.
-        return
-      if e.errno == errno.EACCES:
-        if func is expected_func:
-          os.chmod(failed_path, 0o700)
-          # corner case to handle errors in listing directories.
-          # https://bugs.python.org/issue8523
-          return shutil.rmtree(failed_path, onerror=chmod_retry)
-        # If parent directory is not writable, we still cannot delete the file.
-        # But make sure not to change the parent of the folder we are deleting.
-        if failed_path != path:
-          os.chmod(os.path.dirname(failed_path), 0o700)
-          return func(failed_path)
-    raise
-
-  shutil.rmtree(path, onerror=chmod_retry)
+from slapos.util import rmtree
 
 def createFolder(folder, clean=False):
   if os.path.exists(folder):
diff --git a/erp5/util/testnode/testnode.py b/erp5/util/testnode/testnode.py
index d96be5cf2a..ae369a8f18 100644
--- a/erp5/util/testnode/testnode.py
+++ b/erp5/util/testnode/testnode.py
@@ -31,6 +31,7 @@ import logging
 from six.moves.urllib.parse import urljoin
 from contextlib import contextmanager
 from slapos.slap.slap import ConnectionError
+from slapos.util import rmtree
 from . import logger, log_formatter
 from .ProcessManager import SubprocessError, ProcessManager, CancellationError
 from subprocess import CalledProcessError
@@ -39,7 +40,6 @@ from .NodeTestSuite import NodeTestSuite, SlapOSInstance
 from .ScalabilityTestRunner import ScalabilityTestRunner
 from .UnitTestRunner import UnitTestRunner
 from .Utils import deunicodeData
-from .Utils import rmtree
 from .. import taskdistribution
 
 MAX_TEMP_TIME = 0.01 # time in days we should keep temp files
-- 
2.30.9