From 823708ea031290c864e3aef67f60f6eb495f281d Mon Sep 17 00:00:00 2001
From: Ryan Harper <ryan.harper@canonical.com>
Date: Fri, 11 Oct 2019 16:52:13 +0000
Subject: cloud_test/lxd: Retry container delete a few times

LXD integration tests fail sometimes due to failure to delete the
container, usually related to ZFS backend.  This is a transient
issue unrelated to the test itself.  Teach LXD platform to retry
this a few times before returning an error.
---
 tests/cloud_tests/platforms/lxd/instance.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

(limited to 'tests/cloud_tests')

diff --git a/tests/cloud_tests/platforms/lxd/instance.py b/tests/cloud_tests/platforms/lxd/instance.py
index 83c97ab4..2b804a62 100644
--- a/tests/cloud_tests/platforms/lxd/instance.py
+++ b/tests/cloud_tests/platforms/lxd/instance.py
@@ -4,6 +4,7 @@
 
 import os
 import shutil
+import time
 from tempfile import mkdtemp
 
 from cloudinit.util import load_yaml, subp, ProcessExecutionError, which
@@ -224,7 +225,18 @@ class LXDInstance(Instance):
         LOG.debug("%s: deleting container.", self)
         self.unfreeze()
         self.shutdown()
-        self.pylxd_container.delete(wait=True)
+        retries = [1] * 5
+        for attempt, wait in enumerate(retries):
+            try:
+                self.pylxd_container.delete(wait=True)
+                break
+            except Exception:
+                if attempt + 1 >= len(retries):
+                    raise
+                LOG.debug('Failed to delete container %s (%s/%s) retrying...',
+                          self, attempt + 1, len(retries))
+                time.sleep(wait)
+
         self._pylxd_container = None
 
         if self.platform.container_exists(self.name):
-- 
cgit v1.2.3