def test_get_handler(self): osutil.get_osutil() protocol.get_protocol_util() dhcp.get_dhcp_handler() provision.get_provision_handler() deprovision.get_deprovision_handler() daemon.get_daemon_handler() resourcedisk.get_resourcedisk_handler() scvmm.get_scvmm_handler() monitor.get_monitor_handler() update.get_update_handler() exthandlers.get_exthandlers_handler()
def is_provisioned(self): ''' A VM is considered provisionend *anytime* the provisioning sentinel file exists and not provisioned *anytime* the file is absent. If the VM was provisioned using an agent that did not record the VM unique identifier, the provisioning file will be re-written to include the identifier. A warning is logged *if* the VM unique identifier has changed since VM was provisioned. ''' if not os.path.isfile(self.provisioned_file_path()): return False s = fileutil.read_file(self.provisioned_file_path()).strip() if s != self.osutil.get_instance_id(): if len(s) > 0: logger.warn("VM is provisioned, " "but the VM unique identifier has changed -- " "clearing cached state") from azurelinuxagent.pa.deprovision \ import get_deprovision_handler deprovision_handler = get_deprovision_handler() deprovision_handler.run_changed_unique_id() self.write_provisioned() return True
def check_provisioned_file(self): """ If the VM was provisioned using an agent that did not record the VM unique identifier, the provisioning file will be re-written to include the identifier. A warning is logged *if* the VM unique identifier has changed since VM was provisioned. Returns False if the VM has not been provisioned. """ if not ProvisionHandler.is_provisioned(): return False s = fileutil.read_file(ProvisionHandler.provisioned_file_path()).strip() # pylint: disable=C0103 if not self.osutil.is_current_instance_id(s): if len(s) > 0: # pylint: disable=len-as-condition logger.warn("VM is provisioned, " "but the VM unique identifier has changed -- " "clearing cached state") from azurelinuxagent.pa.deprovision \ import get_deprovision_handler deprovision_handler = get_deprovision_handler() deprovision_handler.run_changed_unique_id() self.write_provisioned() self.report_ready() return True
def test_del_lib_dir_files(self, distro_name, distro_version, distro_full_name, mock_conf): files = [ 'HostingEnvironmentConfig.xml', 'Incarnation', 'Protocol', 'SharedConfig.xml', 'WireServerEndpoint', 'Extensions.1.xml', 'ExtensionsConfig.1.xml', 'GoalState.1.xml', 'Extensions.2.xml', 'ExtensionsConfig.2.xml', 'GoalState.2.xml' ] tmp = tempfile.mkdtemp() mock_conf.return_value = tmp for f in files: fileutil.write_file(os.path.join(tmp, f), "Value") deprovision_handler = get_deprovision_handler(distro_name, distro_version, distro_full_name) warnings = [] actions = [] deprovision_handler.del_lib_dir_files(warnings, actions) self.assertTrue(len(warnings) == 0) self.assertTrue(len(actions) == 1) self.assertEqual(fileutil.rm_files, actions[0].func) self.assertTrue(len(actions[0].args) > 0) for f in actions[0].args: self.assertTrue(os.path.basename(f) in files)
def is_provisioned(self): ''' A VM is considered provisionend *anytime* the provisioning sentinel file exists and not provisioned *anytime* the file is absent. If the VM was provisioned using an agent that did not record the VM unique identifier, the provisioning file will be re-written to include the identifier. A warning is logged *if* the VM unique identifier has changed since VM was provisioned. ''' if not os.path.isfile(self.provisioned_file_path()): return False s = fileutil.read_file(self.provisioned_file_path()).strip() if not self.osutil.is_current_instance_id(s): if len(s) > 0: logger.warn("VM is provisioned, " "but the VM unique identifier has changed -- " "clearing cached state") from azurelinuxagent.pa.deprovision \ import get_deprovision_handler deprovision_handler = get_deprovision_handler() deprovision_handler.run_changed_unique_id() self.write_provisioned() self.report_ready() return True
def deprovision(self, force=False, deluser=False): """ Run deprovision command """ from azurelinuxagent.pa.deprovision import get_deprovision_handler deprovision_handler = get_deprovision_handler() deprovision_handler.run(force=force, deluser=deluser)
def test_del_cloud_init_without_once(self, mock_files, mock_dirs): deprovision_handler = get_deprovision_handler("","","") deprovision_handler.del_cloud_init([], [], include_once=False) mock_dirs.assert_called_with(include_once=False) mock_files.assert_called_with(include_once=False)
def test_del_cloud_init_without_once(self, mock_files, mock_dirs): deprovision_handler = get_deprovision_handler("", "", "") deprovision_handler.del_cloud_init([], [], include_once=False, deluser=False) mock_dirs.assert_called_with(include_once=False) mock_files.assert_called_with(include_once=False, deluser=False)
def test_deprovision(self, distro_name, distro_version, distro_full_name): deprovision_handler = get_deprovision_handler(distro_name, distro_version, distro_full_name) warnings, actions = deprovision_handler.setup(deluser=False) assert any("/etc/resolv.conf" in w for w in warnings)
def test_del_lib_dir_files(self, distro_name, distro_version, distro_full_name, mock_conf): dirs = [ 'WALinuxAgent-2.2.26/config', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/config', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/status' ] files = [ 'HostingEnvironmentConfig.xml', 'Incarnation', 'Protocol', 'SharedConfig.xml', 'WireServerEndpoint', 'Extensions.1.xml', 'ExtensionsConfig.1.xml', 'GoalState.1.xml', 'Extensions.2.xml', 'ExtensionsConfig.2.xml', 'GoalState.2.xml', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/config/42.settings', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/config/HandlerStatus', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/config/HandlerState', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/status/12.notstatus', 'Microsoft.Azure.Extensions.CustomScript-2.0.6/mrseq', 'WALinuxAgent-2.2.26/config/0.settings' ] tmp = tempfile.mkdtemp() mock_conf.return_value = tmp for d in dirs: fileutil.mkdir(os.path.join(tmp, d)) for f in files: fileutil.write_file(os.path.join(tmp, f), "Value") deprovision_handler = get_deprovision_handler(distro_name, distro_version, distro_full_name) warnings = [] actions = [] deprovision_handler.del_lib_dir_files(warnings, actions) deprovision_handler.del_ext_handler_files(warnings, actions) self.assertTrue(len(warnings) == 0) self.assertTrue(len(actions) == 2) self.assertEqual(fileutil.rm_files, actions[0].func) self.assertEqual(fileutil.rm_files, actions[1].func) self.assertEqual(11, len(actions[0].args)) self.assertEqual(3, len(actions[1].args)) for f in actions[0].args: self.assertTrue(os.path.basename(f) in files) for f in actions[1].args: self.assertTrue(f[len(tmp)+1:] in files)
def test_deprovision_ubuntu(self, distro_name, distro_version, distro_full_name): deprovision_handler = get_deprovision_handler(distro_name, distro_version, distro_full_name) with patch("os.path.realpath", return_value="/run/resolvconf/resolv.conf"): warnings, actions = deprovision_handler.setup(deluser=False) assert any("/etc/resolvconf/resolv.conf.d/tail" in w for w in warnings)
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) mock_dirs.assert_called_with(include_once=True) mock_files.assert_called_with(include_once=True) 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
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, deluser=True) mock_dirs.assert_called_with(include_once=True) mock_files.assert_called_with(include_once=True, deluser=True) 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