def validate(self): """Validate that script/playbook path is valid and exists.""" # schema validation for ansible_orchestrate schema schema_validator(schema_data=self.build_profile(self.action), schema_files=[self.__schema_file_path__], schema_ext_files=[self.__schema_ext_path__]) # verifying when script or playbook is present in the orchestrate task, the name key provides a path that exist if self.script: if os.path.exists(self.script.get('name').split(' ', 1)[0]): self.logger.debug('Found Action resource script %s' % self.script.get('name')) else: raise TefloOrchestratorError( 'Cannot find Action resource script %s' % self.script.get('name')) elif self.playbook: if os.path.exists(self.playbook.get('name').split(' ', 1)[0]): self.logger.debug('Found Action resource playbook %s' % self.playbook.get('name')) else: raise TefloOrchestratorError( 'Cannot find Action resource playbook %s' % self.playbook.get('name'))
def backwards_compat_check(self): """ This method does the check if name field is a script/playbook path or name of the orchestrator task by checking is '/' i spresent in the string. If it is a path then it checks if ansible_script field is a boolean and is True . If so a new dictionary is created with key=name and the value= script path. This is then assigned to ansible_script. If the ansible_script is not present then it is understood that the path belongs to a playbook. a new dictionary is created with key=name and the value= playbook path. This is then assigned to ansible_playbook. """ if os.sep in self.action_name: self.logger.warning( 'Using name field to provide ansible_script/ansible_playbook path' ) self.logger.debug( 'Joining current workspace %s to the ansible_script/playbook path %s' % (self.workspace, self.action_name)) new_item = {'name': os.path.join(self.workspace, self.action_name)} self.logger.debug( 'Converting ansible_script/playbook path to dictionary %s' % new_item) if isinstance(self.script, bool) and self.script: self.script = new_item elif not self.script: self.playbook = new_item else: raise TefloOrchestratorError( 'Error in defining the orchestrate name/ansible_script/ansible_playbook' ' fields')
def test_run_orchestrate_failure(mock_method, orchestrate_task): mock_method.return_value = 'Traceback' mock_orchestrator = mock.MagicMock(run=mock.MagicMock( side_effect=TefloOrchestratorError('e'))) orchestrate_task.orchestrator = mock_orchestrator with pytest.raises(TefloOrchestratorError): orchestrate_task.run()
def backwards_compat_check(self): """ This method is put in place to check if any older ways of assigning ansible_playbook/script names are being used, and if so then code exits""" if os.sep in self.action_name: raise TefloOrchestratorError('Using name field to provide ansible_script/ansible_playbook path is ' 'not supported. Please see examples at this link to understand how to' ' use the orchestrate task parameters ' ' https://teflo.readthedocs.io/en/latest/users/' 'definitions/orchestrate.html#examples ') if isinstance(self.script, bool): raise TefloOrchestratorError('ansible_script as boolean is not supported any more. ' 'Please see examples at this link to understand how to' ' use the orchestrate task parameters ' ' https://teflo.readthedocs.io/en/latest/users/' 'definitions/orchestrate.html#examples ')
def test_run_execute_failure(mock_method, validate_task): mock_method.return_value = 'Traceback' mock_resource = mock.MagicMock(validate=mock.MagicMock( side_effect=TefloOrchestratorError('e'))) validate_task.resource = mock_resource with pytest.raises(TefloOrchestratorError): validate_task.run()
def __playbook__(self): self.logger.info('Executing playbook:') results = self.ans_service.run_playbook(self.playbook) if results[0] != 0: raise TefloOrchestratorError('Playbook %s failed to run\nThe error is:\n%s' % (self.playbook['name'], results[1])) else: self.logger.info('Successfully completed playbook : %s' % self.playbook['name'])
def test_run_provision_failure(mock_method, provision_task): mock_method.return_value = 'Traceback' mock_provisioner = mock.MagicMock(spec=AssetProvisioner, create=mock.MagicMock(side_effect=TefloOrchestratorError('e'))) provision_task.provision = True provision_task.provisioner = mock_provisioner with pytest.raises(TefloOrchestratorError): provision_task.run()
def __shell__(self): self.logger.info('Executing shell command:') for shell in self.shell: result = self.ans_service.run_shell_playbook(shell) if result['rc'] != 0: raise TefloOrchestratorError('Command %s failed. Host=%s rc=%d Error: %s' % (shell['command'], result['host'], result['rc'], result['err'])) else: self.logger.info('Successfully completed command : %s' % shell['command'])
def __script__(self): self.logger.info('Executing script:') result = self.ans_service.run_script_playbook(self.script) if result['rc'] != 0: raise TefloOrchestratorError('Script %s failed. Host=%s rc=%d Error: %s' % (self.script['name'], result['host'], result['rc'], result['err'])) else: self.logger.info('Successfully completed script : %s' % self.script['name'])
def test_teflo_orchestrator_error(): with pytest.raises(TefloOrchestratorError): raise TefloOrchestratorError('error message')
def test_run_cleanup_failure(mock_method, cleanup_task): orchestrator = mock.MagicMock() orchestrator.run.side_effect = TefloOrchestratorError('e') mock_method.return_value = orchestrator cleanup_task.run()