def test_execute_input_data(self): """Tests calling CreateJobs.execute() with input data""" manifest = { 'seedVersion': '1.0.0', 'job': { 'name': 'name', 'jobVersion': '1.0.0', 'packageVersion': '1.0.0', 'title': 'Title', 'description': 'This is a description', 'maintainer': { 'name': 'John Doe', 'email': '*****@*****.**' }, 'timeout': 10, 'interface': { 'command': 'the command' } } } job_type = job_test_utils.create_seed_job_type(manifest=manifest) event = trigger_test_utils.create_trigger_event() data = Data() # Create and execute message message = create_jobs_message(job_type.name, job_type.version, job_type.revision_num, event.id, input_data=data) result = message.execute() self.assertTrue(result) # Check for job creation self.assertEqual( Job.objects.filter(job_type_id=job_type.id, event_id=event.id).count(), 1) # Check for process_job_input message self.assertEqual(len(message.new_messages), 1) msg = message.new_messages[0] self.assertEqual(msg.type, 'process_job_input') # Test executing message again message_json_dict = message.to_json() message = CreateJobs.from_json(message_json_dict) result = message.execute() self.assertTrue(result) # Check that a second job is not created self.assertEqual( Job.objects.filter(job_type_id=job_type.id, event_id=event.id).count(), 1) # Check for process_job_input message self.assertEqual(len(message.new_messages), 1) msg = message.new_messages[0] self.assertEqual(msg.type, 'process_job_input')
def test_execute_invalid_data(self): """Tests calling CreateJobs.execute() when the input data is invalid""" manifest = { 'seedVersion': '1.0.0', 'job': { 'name': 'name', 'jobVersion': '1.0.0', 'packageVersion': '1.0.0', 'title': 'Title', 'description': 'This is a description', 'maintainer': { 'name': 'John Doe', 'email': '*****@*****.**' }, 'timeout': 10, 'interface': { 'command': 'the command', 'inputs': { 'files': [{ 'name': 'input_a' }] } } } } job_type = job_test_utils.create_seed_job_type(manifest=manifest) event = trigger_test_utils.create_trigger_event() # Data does not provide required input_a so it is invalid data_dict = convert_data_to_v6_json(Data()).get_dict() # Create and execute message message = create_jobs_message(job_type.name, job_type.version, job_type.revision_num, event.id, count=10, input_data_dict=data_dict) result = message.execute() self.assertTrue(result) self.assertEqual( Job.objects.filter(job_type_id=job_type.id, event_id=event.id).count(), 0) # Should be no new messages self.assertEqual(len(message.new_messages), 0)
def execute(self): """See :meth:`messaging.messages.message.CommandMessage.execute` """ # Check to see if a force stop was placed on this purge process results = PurgeResults.objects.get(trigger_event=self.trigger_id) if results.force_stop_purge: return True files_to_delete = ScaleFile.objects.filter_files(job_ids=[self.job_id]) if files_to_delete: # Construct input data files = [] workspaces = [] for f in files_to_delete: files.append({ 'id': f.id, 'file_path': f.file_path, 'workspace': f.workspace.name }) if f.workspace.name not in [ k for wrkspc in workspaces for k in wrkspc.keys() ]: workspaces.append( {f.workspace.name: f.workspace.json_config}) inputs = Data() inputs.add_value(JsonValue('job_id', str(self.job_id))) inputs.add_value(JsonValue('trigger_id', str(self.trigger_id))) inputs.add_value( JsonValue('source_file_id', str(self.source_file_id))) inputs.add_value(JsonValue('purge', str(self.purge))) inputs.add_value(JsonValue('files', json.dumps(files))) inputs.add_value(JsonValue('workspaces', json.dumps(workspaces))) # Send message to create system job to delete files msg = create_jobs_message(job_type_name="scale-delete-files", job_type_version="1.0.0", event_id=self.trigger_id, job_type_rev_num=1, input_data=inputs) self.new_messages.append(msg) return True
def test_json_no_recipe(self): """Tests converting a CreateJobs message (without a recipe) to and from JSON""" manifest = { 'seedVersion': '1.0.0', 'job': { 'name': 'name', 'jobVersion': '1.0.0', 'packageVersion': '1.0.0', 'title': 'Title', 'description': 'This is a description', 'maintainer': { 'name': 'John Doe', 'email': '*****@*****.**' }, 'timeout': 10 } } job_type = job_test_utils.create_seed_job_type(manifest=manifest) event = trigger_test_utils.create_trigger_event() data_dict = convert_data_to_v6_json(Data()).get_dict() count = 10 # Create message message = create_jobs_message(job_type.name, job_type.version, job_type.revision_num, event.id, count=count, input_data_dict=data_dict) # Convert message to JSON and back, and then execute message_json_dict = message.to_json() new_message = CreateJobs.from_json(message_json_dict) result = new_message.execute() self.assertTrue(result) self.assertEqual( Job.objects.filter(job_type_id=job_type.id, event_id=event.id).count(), count) # Check for process_job_input messages self.assertEqual(len(new_message.new_messages), count) for msg in new_message.new_messages: self.assertEqual(msg.type, 'process_job_input')