def _test_get_cb_init_files(self, mock_get_dir, mock_encode_file, output_directory="fake_output_directory"): CONFIG.argus.output_directory = output_directory fake_location = "fake_logs" cb_fake_files = [] expected_logging = [ "Obtaining Cloudbase-Init files from %s" % fake_location ] if not output_directory: expected_logging.append("The output directory wasn't given, " "the files will not be grabbed.") else: self._recipe._backend.instance_server.return_value = { 'id': "fake_id" } mock_get_dir.return_value = "fake_dir" cb_fake_files = [ "cloudbase-init.log", "cloudbase-init-unattend.log" ] with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.get_cb_init_files(fake_location, cb_fake_files) self.assertEqual(snatcher.output, expected_logging) if output_directory: self._recipe._backend.instance_server.assert_called_once_with() mock_get_dir.assert_called_once_with(self._recipe._execute) self.assertEqual(len(cb_fake_files), (self._recipe._backend.remote_client.manager. copy_file.call_count)) self.assertEqual(mock_encode_file.call_count, len(cb_fake_files))
def _test_mkfile(self, mock_new_item, mock_is_dir, mock_is_file, is_file=False, is_dir=False, is_file_exc=None, is_dir_exc=None, new_item_exc=None, run_command_exc=None): self._client.run_command_with_retry = mock.Mock() mock_is_file.return_value = is_file mock_is_dir.return_value = is_dir if is_file and not run_command_exc: log = ("File '{}' already exists. LastWriteTime and" " LastAccessTime will be updated.".format(test_utils.PATH)) with test_utils.LogSnatcher('argus.action_manager.windows.Windows' 'ActionManager.mkfile') as snatcher: self.assertIsNone(self._action_manager.mkfile(test_utils.PATH)) self.assertEqual(snatcher.output, [log]) self._client.run_command_with_retry.assert_called_once_with( "echo $null >> '{}'".format(test_utils.PATH), command_type=util.POWERSHELL) return if is_file_exc: mock_is_file.side_effect = is_file_exc with self.assertRaises(is_file_exc): self._action_manager.mkfile(test_utils.PATH) return if is_file and run_command_exc: self._client.run_command_with_retry.side_effect = run_command_exc with self.assertRaises(run_command_exc): self._action_manager.mkfile(test_utils.PATH) return if not is_file and is_dir: with self.assertRaises(exceptions.ArgusCLIError): self._action_manager.mkfile(test_utils.PATH) return if not is_file and is_dir_exc: mock_is_dir.side_effect = is_dir_exc with self.assertRaises(is_dir_exc): self._action_manager.mkfile(test_utils.PATH) return if not is_file and not is_dir and new_item_exc: mock_new_item.side_effect = new_item_exc with self.assertRaises(new_item_exc): self._action_manager.mkfile(test_utils.PATH) return self._action_manager.mkfile(test_utils.PATH) mock_new_item.assert_called_once_with(test_utils.PATH, self._action_manager._FILE)
def test_sysprep(self): expected_logging = ["Running sysprep..."] with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.sysprep() self.assertEqual(expected_logging, snatcher.output) (self._recipe._backend.remote_client.manager.sysprep. assert_called_once_with())
def _test_replace_install(self, mock_execute, mock_get_cbinit_dir, link="fake link"): CONFIG.argus.patch_install = link expected_logging = [] if link: expected_logging = [ "Replacing Cloudbase-Init's files with %s" % CONFIG.argus.patch_install, "Download and extract installation bundle.", "Replace old files with the new ones." ] with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.replace_install() self.assertEqual(expected_logging, snatcher.output) if link: execute_count = 2 if link.startswith("\\\\"): execute_count = 3 else: location = r'C:\install.zip' (self._recipe._backend.remote_client.manager.download. assert_called_once_with(uri=link, location=location)) self.assertEqual(mock_execute.call_count, execute_count) mock_get_cbinit_dir.assert_called_once_with(mock_execute) resource_location = "windows/updateCbinit.ps1" (self._recipe._backend.remote_client.manager. execute_powershell_resource_script.assert_called_once_with( resource_location=resource_location))
def _test_set_mtu(self, mock_parse_netsh, exception=False): times = 5 cmd = 'netsh interface ipv4 show subinterfaces level=verbose' mock_subinterface = mock.Mock() mock_subinterface.name = "fake name" mock_parse_netsh.return_value = [mock_subinterface] * times expected_logging = ["Setting the MTU for %r" % mock_subinterface.name ] * times side_effect_list = [None] * times if exception: side_effect_list[2] = exceptions.ArgusTimeoutError side_effect_list[4] = exceptions.ArgusTimeoutError expected_logging.insert( 3, 'Setting MTU failed with ' 'ArgusTimeoutError().') expected_logging.insert( 6, 'Setting MTU failed with ' 'ArgusTimeoutError().') (self._recipe._backend.remote_client.run_command_with_retry.side_effect ) = side_effect_list with test_utils.LogSnatcher('argus.recipes.cloud.' 'windows') as snatcher: self._recipe.set_mtu() self.assertEqual(snatcher.output, expected_logging) (self._recipe._backend.remote_client.run_command_verbose. assert_called_once_with(cmd, command_type=util.CMD)) self.assertEqual( self._recipe._backend.remote_client.run_command_with_retry. call_count, times)
def _test_replace_code(self, mock_get_python_dir, mock_execute, mock_join, git_command=True, exception=False): CONFIG.argus.git_command = git_command expected_logging = [] if git_command: expected_logging = [ "Replacing Cloudbase-Init's code with %s" % CONFIG.argus.git_command, "Getting Cloudbase-Init location...", "Recursively removing Cloudbase-Init..." ] python_dir = mock_get_python_dir.return_value mock_join.return_value = "fake join" if exception: (self._recipe._backend.remote_client.manager.git_clone. return_value) = None else: expected_logging.append("Applying cli patch...") expected_logging.append("Replacing code...") if exception: with self.assertRaises(exceptions.ArgusError) as ex: with test_utils.LogSnatcher('argus.recipes.cloud.' 'windows') as snatcher: self._recipe.replace_code() else: with test_utils.LogSnatcher('argus.recipes.cloud.' 'windows') as snatcher: self._recipe.replace_code() self.assertEqual(expected_logging, snatcher.output) if git_command: mock_get_python_dir.assert_called_once_with(mock_execute) (self._recipe._backend.remote_client.manager.git_clone. assert_called_once_with(repo_url=windows._CBINIT_REPO, location=windows._CBINIT_TARGET_LOCATION)) if exception: self.assertEqual('Code repository could not ' 'be cloned.', ex.exception.message) mock_join.assert_called_once_with(python_dir, "Lib", "site-packages", "cloudbaseinit") self.assertEqual(mock_execute.call_count, 1) else: self.assertEqual(mock_execute.call_count, 5) self.assertEqual(mock_join.call_count, 2)
def test_wait_for_boot_completion(self): expected_logging = ["Waiting for first boot completion..."] with test_utils.LogSnatcher('argus.recipes.cloud.' 'windows') as snatcher: self._recipe.wait_for_boot_completion() (self._recipe._backend.remote_client.manager.wait_boot_completion. assert_called_once_with()) self.assertEqual(expected_logging, snatcher.output)
def test_specific_prepare(self): with test_utils.LogSnatcher('argus.action_manager.windows' '.WindowsActionManager' '.specific_prepare') as snatcher: self.assertIsNone(self._action_manager.specific_prepare()) self.assertEqual(snatcher.output, [ "Prepare something specific" " for OS Type {}".format(self._os_type) ])
def test_pre_sysprep(self, mock_pre_sysprep): expected_logging = ["Doing last step before sysprepping."] resource_location = "windows/test_exe.exe" location = r"C:\Scripts\test_exe.exe" with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.pre_sysprep() self.assertEqual(expected_logging, snatcher.output) self._recipe._backend.remote_client.manager.download_resource( resource_location=resource_location, location=location) mock_pre_sysprep.assert_called_once_with()
def test_prepare_config(self): with test_utils.LogSnatcher('argus.action_manager.windows' '.WindowsActionManager' '.prepare_config') as snatcher: self.assertIsNone( self._action_manager.prepare_config(mock.Mock(), mock.Mock())) self.assertEqual( snatcher.output, ["Config Cloudbase-Init" " for {}".format(self._os_type)])
def _test_prepare(self, mock_execution_prologue, mock_wait_finalization): expected_logging = [ "Preparing already sysprepped instance...", "Finished preparing instance." ] with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.prepare() self.assertEqual(expected_logging, snatcher.output) mock_execution_prologue.assert_called_once_with() mock_wait_finalization.assert_called_once_with()
def test_prepare_cbinit_config(self, mock_prepare_cbinit_config): service_type = mock.sentinel expected_logging = ["Injecting netbios option in conf file."] self._recipe._cbinit_conf = mock.Mock() with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.prepare_cbinit_config(service_type) self.assertEqual(expected_logging, snatcher.output) mock_prepare_cbinit_config.assert_called_once_with(service_type) self._recipe._cbinit_conf.set_conf_value.assert_called_once_with( name='netbios_host_name_compatibility', value='False')
def test_pre_sysprep(self, mock_pre_sysprep): expected_logging = ["Downloading reboot-required local script."] self._recipe._cbinit_conf = mock.Mock() with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.pre_sysprep() self.assertEqual(expected_logging, snatcher.output) mock_pre_sysprep.assert_called_once_with() resource_location = "windows/reboot.cmd" (self._recipe._backend.remote_client.manager.download_resource. assert_called_once_with(resource_location=resource_location, location=r'C:\Scripts\reboot.cmd'))
def test_prepare_cbinit_config(self, mock_prepare_cbinit_config): expected_logging = ["Inject guest IP for mocked service access."] service_type = mock.sentinel self._recipe._cbinit_conf = mock.Mock() with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.prepare_cbinit_config(service_type) self.assertEqual(expected_logging, snatcher.output) mock_prepare_cbinit_config.assert_called_once_with(service_type) self._recipe._cbinit_conf.set_conf_value.assert_called_once_with( name=self._recipe.config_entry, value=self._recipe.metadata_address, section=self._recipe.config_group)
def test_execution_prologue(self): expected_logging = [ "Retrieve common module for proper script execution." ] with test_utils.LogSnatcher('argus.recipes.cloud.' 'windows') as snatcher: self._recipe.execution_prologue() (self._recipe._backend.remote_client.manager.specific_prepare. assert_called_once_with()) resource_location = "windows/common.psm1" (self._recipe._backend.remote_client.manager.download_resource. assert_called_once_with(resource_location=resource_location, location=r'C:\common.psm1')) self.assertEqual(expected_logging, snatcher.output)
def test_wait_cbinit_finalization(self): paths = [r"C:\cloudbaseinit_unattended", r"C:\cloudbaseinit_normal"] expected_logging = [ "Check the heartbeat patch ...", "Wait for the Cloudbase-Init service to stop ..." ] with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.wait_cbinit_finalization() self.assertEqual(expected_logging, snatcher.output) (self._recipe._backend.remote_client.manager.check_cbinit_service. assert_called_once_with(searched_paths=paths)) (self._recipe._backend.remote_client.manager.wait_cbinit_service. assert_called_once_with())
def test_pre_sysprep(self, mock_pre_sysprep): CONFIG.cloudbaseinit.created_user = "******" expected_logging = [ "Creating the user %s..." % CONFIG.cloudbaseinit.created_user ] resource_location = "windows/create_user.ps1" params = r" -user {}".format(CONFIG.cloudbaseinit.created_user) with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.pre_sysprep() self.assertEqual(expected_logging, snatcher.output) (self._recipe._backend.remote_client.manager. execute_powershell_resource_script.assert_called_once_with( resource_location=resource_location, parameters=params)) mock_pre_sysprep.assert_called_once_with()
def test_wait_cbinit_finalization(self, mock_get_cbinit_dir): expected_logging = [ "Check the heartbeat patch ...", "Wait for the Cloudbase-Init service to stop ..." ] self._recipe._execute = mock.sentinel mock_get_cbinit_dir.return_value = "fake path" paths = [ ntpath.join(mock_get_cbinit_dir.return_value, "log", name) for name in ["cloudbase-init-unattend.log", "cloudbase-init.log"] ] with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe.wait_cbinit_finalization() self.assertEqual(expected_logging, snatcher.output) mock_get_cbinit_dir.assert_called_once_with(self._recipe._execute) (self._recipe._backend.remote_client.manager.check_cbinit_service. assert_called_once_with(searched_paths=paths)) (self._recipe._backend.remote_client.manager.wait_cbinit_service. assert_called_once_with())
def test_instance_setup_create_server(self): expected_logging = ["Creating server..."] self._base_tempest_backend._configure_networking = mock.Mock() self._base_tempest_backend._manager.create_keypair = mock.Mock() self._base_tempest_backend._create_server = mock.Mock( return_value="fake server") self._base_tempest_backend._assign_floating_ip = mock.Mock() self._base_tempest_backend._create_security_groups = mock.Mock() self._base_tempest_backend._availability_zone = mock.Mock() self._base_tempest_backend.__get_id_tenant_network = mock.Mock() with test_utils.LogSnatcher('argus.backends.base') as snatcher: self._base_tempest_backend.setup_instance() self.assertEqual(expected_logging, snatcher.output) self._base_tempest_backend._configure_networking.assert_called_once() self._base_tempest_backend._manager.create_keypair.assert_called_once() self._base_tempest_backend._create_server.assert_called_once() self._base_tempest_backend._assign_floating_ip.assert_called_once() self._base_tempest_backend._create_security_groups.assert_called_once()
def _test_install_cbinit(self, mock_get_cbinit_dir, mock_install_log, exception=False): expected_logging = [ "Cloudbase-Init is already installed, skipping installation." ] self._recipe._execute = mock.sentinel if exception: expected_logging = [] mock_get_cbinit_dir.side_effect = exceptions.ArgusError with test_utils.LogSnatcher('argus.recipes.cloud.' 'windows') as snatcher: self._recipe.install_cbinit() mock_get_cbinit_dir.assert_called_once_with(self._recipe._execute) self.assertEqual(expected_logging, snatcher.output) if exception: (self._recipe._backend.remote_client.manager.install_cbinit. assert_called_once_with()) mock_install_log.assert_called_once_with()
def _test_sysprep(self, mock_download_resource, mock_wait_boot_completion, download_exc=None, run_exc=None, wait_exc=None): cmd = r"C:\{}".format( test_utils.SYSPREP_RESOURCE_LOCATION.split('/')[-1]) self._client.run_remote_cmd = mock.Mock() if download_exc: mock_download_resource.side_effect = download_exc with self.assertRaises(download_exc): self._action_manager.sysprep() self._client.run_remote_cmd.assert_not_called() return if wait_exc: mock_wait_boot_completion.side_effect = wait_exc with self.assertRaises(wait_exc): self._action_manager.sysprep() return if run_exc: self._client.run_remote_cmd.side_effect = run_exc with test_utils.LogSnatcher('argus.action_manager.windows.Windows' 'ActionManager.sysprep') as snatcher: self.assertIsNone(self._action_manager.sysprep()) self.assertEqual(snatcher.output[-2:], [ 'Currently rebooting...', 'Wait for the machine to finish rebooting ...' ]) else: self.assertIsNone(self._action_manager.sysprep()) mock_download_resource.assert_called_once_with( test_utils.SYSPREP_RESOURCE_LOCATION, cmd) mock_wait_boot_completion.assert_called_once_with()
def _test_grab_cbinit_installation_log(self, mock_join, mock_transfer_file, mock_get_extra_item, output_directory=True): expected_logging = ["Obtaining the installation logs."] CONFIG.argus.output_directory = output_directory if output_directory is False: expected_logging.append("The output directory wasn't given, " "the log will not be grabbed.") else: installation_log = r"C:\installation.log" self._recipe._backend.instance_server.return_value = { 'id': mock.sentinel } mock_get_extra_item.return_value = "fake-scenario-log" renamed_log = (r"C:\{0}-installation-{1}.log".format( mock_get_extra_item.return_value, self._recipe._backend.instance_server.return_value['id'])) zip_source = r"C:\installation.zip" log_template = "installation-{}.zip".format( self._recipe._backend.instance_server.return_value['id']) mock_join.return_value = mock.sentinel with test_utils.LogSnatcher('argus.recipes.cloud.windows') as snatcher: self._recipe._grab_cbinit_installation_log() self.assertEqual(expected_logging, snatcher.output) if output_directory: (self._recipe._backend.remote_client.manager.copy_file. assert_called_once_with(installation_log, renamed_log)) (self._recipe._backend.remote_client.manager.archive_file( file_path=renamed_log, destination_path=zip_source)) mock_join.assert_called_once_with(CONFIG.argus.output_directory, log_template) mock_transfer_file.assert_called_once_with(zip_source, mock_join.return_value, archive=True)
def _test_cleanup(self, mock_waiters, security_groups_rules=None, security_group=None, server=None, floating_ip=None, keypair=None): expected_logging = ["Cleaning up..."] if security_groups_rules is not None: (self._base_tempest_backend._security_groups_rules ) = security_groups_rules (self._base_tempest_backend._manager.security_group_rules_client. delete_security_group_rule) = mock.Mock() if security_group is not None: (self._base_tempest_backend._manager.servers_client. remove_security_group) = mock.Mock() self._base_tempest_backend.internal_instance_id = mock.Mock( return_value="fake id") self._base_tempest_backend._security_group = security_group if server is not None: mock_servers_client = mock.Mock() mock_servers_client.delete_server = mock.Mock() (self._base_tempest_backend._manager.servers_client ) = mock_servers_client self._base_tempest_backend.internal_instance_id = mock.Mock( return_value="fake id") self._base_tempest_backend._server = server if floating_ip is not None: (self._base_tempest_backend._manager.floating_ips_client. delete_floating_ip) = mock.Mock() self._base_tempest_backend._floating_ip = floating_ip if keypair is not None: self._base_tempest_backend._keypair = keypair self._base_tempest_backend._manager.cleanup_credentials = mock.Mock() with test_utils.LogSnatcher('argus.backends.tempest.' 'tempest_backend') as snatcher: self._base_tempest_backend.cleanup() if security_groups_rules is not None: (self.assertEqual( self._base_tempest_backend._manager.security_group_rules_client .delete_security_group_rule.call_count, len(security_groups_rules))) if security_group is not None: (self._base_tempest_backend._manager.servers_client. remove_security_group.assert_called_once_with( server_id="fake id", name=security_group['name'])) (self._base_tempest_backend.internal_instance_id. assert_called_once()) if server is not None: (self._base_tempest_backend._manager.servers_client.delete_server. assert_called_once_with("fake id")) (mock_waiters.assert_called_once_with( self._base_tempest_backend._manager.servers_client, "fake id")) self.assertEqual( self._base_tempest_backend.internal_instance_id.call_count, 2) if floating_ip is not None: (self._base_tempest_backend._manager.floating_ips_client. delete_floating_ip.assert_called_once_with(floating_ip['id'])) if keypair is not None: self._base_tempest_backend._keypair.destroy.assert_called_once() (self._base_tempest_backend._manager.cleanup_credentials. assert_called_once()) self.assertEqual(expected_logging, snatcher.output)