Repository: Azure/WALinuxAgent
Pull request #698 (2017-05-03 23:15:35)

Deprovision cloud-init data (#631)
Signed-off-by: Brendan Dixon <brendandixon@me.com>


Patch:
diff --git a/azurelinuxagent/pa/deprovision/default.py b/azurelinuxagent/pa/deprovision/default.py
index d2a00898..b5f5069f 100644
--- a/azurelinuxagent/pa/deprovision/default.py
+++ b/azurelinuxagent/pa/deprovision/default.py
@@ -99,6 +99,21 @@ class DeprovisionHandler(object):
         dirs_to_del = [conf.get_lib_dir()]
         actions.append(DeprovisionAction(fileutil.rm_dirs, dirs_to_del))
 
+    def cloud_init_directories(self):
+        return ["/var/lib/cloud"]
+
+    def cloud_init_files(self):
+        return ["/etc/sudoers.d/90-cloud-init-users"]
+
+    def del_cloud_init(self, warnings, actions):
+        dirs = [d for d in self.cloud_init_directories() if os.path.isdir(d)]
+        if len(dirs) > 0:
+            actions.append(DeprovisionAction(fileutil.rm_dirs, dirs))
+
+        files = [f for f in self.cloud_init_files() if os.path.isfile(f)]
+        if len(files) > 0:
+            actions.append(DeprovisionAction(fileutil.rm_files, files))
+
     def reset_hostname(self, warnings, actions):
         localhost = ["localhost.localdomain"]
         actions.append(DeprovisionAction(self.osutil.set_hostname, 
@@ -120,6 +135,7 @@ class DeprovisionHandler(object):
         if conf.get_delete_root_password():
             self.del_root_password(warnings, actions)
 
+        self.del_cloud_init(warnings, actions)
         self.del_lib_dir(warnings, actions)
         self.del_files(warnings, actions)
         self.del_resolv(warnings, actions)


Test patch:
diff --git a/tests/pa/test_deprovision.py b/tests/pa/test_deprovision.py
index be349153..c53fa4aa 100644
--- a/tests/pa/test_deprovision.py
+++ b/tests/pa/test_deprovision.py
@@ -15,11 +15,57 @@
 # Requires Python 2.4+ and Openssl 1.0+
 #
 
-from tests.tools import *
+import tempfile
+
+import azurelinuxagent.common.utils.fileutil as fileutil
+
 from azurelinuxagent.pa.deprovision import get_deprovision_handler
+from azurelinuxagent.pa.deprovision.default import DeprovisionHandler
+from tests.tools import *
 
 
 class TestDeprovision(AgentTestCase):
+    @patch("signal.signal")
+    @patch("azurelinuxagent.common.protocol.get_protocol_util")
+    @patch("azurelinuxagent.common.osutil.get_osutil")
+    @patch("azurelinuxagent.pa.deprovision.default.DeprovisionHandler.cloud_init_directories")
+    @patch("azurelinuxagent.pa.deprovision.default.DeprovisionHandler.cloud_init_files")
+    def test_del_cloud_init(self, mock_files, mock_dirs, mock_osutil, mock_util, mock_signal):
+        try:
+            with tempfile.NamedTemporaryFile() as f:
+                warnings = []
+                actions = []
+
+                dirs = [tempfile.mkdtemp()]
+                mock_dirs.return_value = dirs
+
+                files = [f.name]
+                mock_files.return_value = files
+
+                deprovision_handler = get_deprovision_handler("","","")
+                deprovision_handler.del_cloud_init(warnings, actions)
+
+                self.assertEqual(len(warnings), 0)
+                self.assertEqual(len(actions), 2)
+                for da in actions:
+                    if da.func == fileutil.rm_dirs:
+                        self.assertEqual(da.args, dirs)
+                    elif da.func == fileutil.rm_files:
+                        self.assertEqual(da.args, files)
+                    else:
+                        self.assertTrue(False)
+
+                try:
+                    for da in actions:
+                        da.invoke()
+                    self.assertEqual(len([d for d in dirs if os.path.isdir(d)]), 0)
+                    self.assertEqual(len([f for f in files if os.path.isfile(f)]), 0)
+                except Exception as e:
+                    self.assertTrue(False, "Exception {0}".format(e))
+        except OSError:
+            # Ignore the error caused by removing the file within the "with"
+            pass
+
     @distros("redhat")
     def test_deprovision(self,
                          distro_name,