def _CreateCommandAttempt(self, leased_tasks):
    for task in leased_tasks:
      attempt_id = str(uuid.uuid4())
      task.attempt_id = attempt_id

      plugin_data_ = api_messages.KeyValuePairMessagesToMap(task.plugin_data)
      attempt_key = ndb.Key(
          datastore_entities.Request, task.request_id,
          datastore_entities.Command, task.command_id,
          datastore_entities.CommandAttempt, attempt_id,
          namespace=common.NAMESPACE)
      attempt_entity = datastore_entities.CommandAttempt(
          key=attempt_key,
          attempt_id=attempt_id,
          state=common.CommandState.UNKNOWN,
          command_id=task.command_id,
          last_event_time=Now(),
          task_id=task.task_id,
          run_index=task.run_index,
          attempt_index=task.attempt_index,
          hostname=plugin_data_.get(_HOSTNAME_KEY),
          device_serial=task.device_serials[0],
          device_serials=task.device_serials,
          plugin_data=plugin_data_)
      command_manager.AddToSyncCommandAttemptQueue(attempt_entity)
      attempt_entity.put()
 def _CreateAttempt(self, attempt_id, task_id, state):
   # Helper to create an attempt
   command = command_manager.CreateCommands(
       request_id=self.request.key.id(),
       command_infos=[
           datastore_entities.CommandInfo(
               command_line='long command line',
               run_target='foo',
               run_count=1,
               shard_count=1,
               cluster='foobar')
       ],
       shard_indexes=list(range(1)),
       request_plugin_data={
           'ants_invocation_id': 'i123',
           'ants_work_unit_id': 'w123'
       })[0]
   _, request_id, _, command_id = command.key.flat()
   attempt_key = ndb.Key(
       datastore_entities.Request, request_id,
       datastore_entities.Command, command_id,
       datastore_entities.CommandAttempt, attempt_id,
       namespace=common.NAMESPACE)
   attempt = datastore_entities.CommandAttempt(
       key=attempt_key,
       attempt_id=attempt_id,
       state=state,
       command_id=command_id,
       task_id=task_id)
   attempt.put()
   return attempt
Beispiel #3
0
  def testGetInvocationProgress(self):
    mock_test_group_status = datastore_entities.TestGroupStatus(
        name='foo', total_test_count=100, completed_test_count=10,
        failed_test_count=1, is_complete=False, elapsed_time=1)
    attempt1 = datastore_entities.CommandAttempt(
        id='command_attempt1',
        parent=self.command1.key,
        invocation_status=datastore_entities.InvocationStatus(
            test_group_statuses=[mock_test_group_status]))
    attempt1.put()
    attempt2 = datastore_entities.CommandAttempt(
        id='command_attempt2',
        parent=self.command1.key,
        invocation_status=datastore_entities.InvocationStatus(
            test_group_statuses=[mock_test_group_status]))
    attempt2.put()

    req = {'request_id': 1}

    res = self.testapp.post_json(
        '/_ah/api/RequestApi.GetInvocationStatus', req)

    self.assertEqual('200 OK', res.status)
    obj = protojson.decode_message(
        api_messages.InvocationStatus, res.body)
    self.assertEqual(1, len(obj.test_group_statuses))
    self.assertEqual(
        mock_test_group_status.name, obj.test_group_statuses[0].name)
    self.assertEqual(
        mock_test_group_status.total_test_count,
        obj.test_group_statuses[0].total_test_count)
    self.assertEqual(
        mock_test_group_status.completed_test_count,
        obj.test_group_statuses[0].completed_test_count)
    self.assertEqual(
        mock_test_group_status.failed_test_count,
        obj.test_group_statuses[0].failed_test_count)
    self.assertEqual(
        mock_test_group_status.is_complete,
        obj.test_group_statuses[0].is_complete)
    self.assertEqual(
        mock_test_group_status.elapsed_time,
        obj.test_group_statuses[0].elapsed_time)
Beispiel #4
0
  def testGetRequest(self):
    attempt = datastore_entities.CommandAttempt(
        parent=self.command1.key,
        id='attempt_id',
        task_id='task_id',
        attempt_id='attempt_id',
        state=common.CommandState.RUNNING,
        hostname='hostname',
        device_serial='device_serial',
        start_time=START_TIME,
        end_time=END_TIME,
        status='status',
        error='error',
        summary='summary',
        total_test_count=1000,
        failed_test_count=100,
        failed_test_run_count=10)
    attempt.put()

    api_request = {
        'request_id': 1,
    }
    api_response = self.testapp.post_json('/_ah/api/RequestApi.GetRequest',
                                          api_request)
    request = protojson.decode_message(api_messages.RequestMessage,
                                       api_response.body)

    self.assertEqual(request.id, '1')
    self.assertEqual(request.user, 'user1')
    self.assertEqual(request.command_infos[0].command_line, 'command_line1')
    self.assertEqual(1, len(request.command_attempts))
    command_attempt = request.command_attempts[0]
    self.assertEqual('task_id', command_attempt.task_id)
    self.assertEqual('attempt_id', command_attempt.attempt_id)
    self.assertEqual(common.CommandState.RUNNING, command_attempt.state)
    self.assertEqual('hostname', command_attempt.hostname)
    self.assertEqual('device_serial', command_attempt.device_serial)
    self.assertEqual(START_TIME, command_attempt.start_time)
    self.assertEqual(END_TIME, command_attempt.end_time)
    self.assertEqual('status', command_attempt.status)
    self.assertEqual('summary', command_attempt.summary)
    self.assertEqual(1000, command_attempt.total_test_count)
    self.assertEqual(100, command_attempt.failed_test_count)
    self.assertEqual(10, command_attempt.failed_test_run_count)

    self.assertEqual(2, len(request.commands))
    for command, command_msg in zip(self.commands, request.commands):
      self.AssertEqualCommand(command, command_msg)
 def _CreateTestCommandAttempt(self, command, state, total_test_count=1,
                               failed_test_count=1, failed_test_run_count=1,
                               start_time=None, end_time=None):
   """Creates a CommandAttempt associated with a Command."""
   command_attempt = datastore_entities.CommandAttempt(
       parent=command.key,
       id=str(self._attempt_id),
       command_id=command.key.id(),
       task_id='task_id',
       attempt_id='attempt_id',
       state=state,
       summary='summary: %s\n' % self.result_link,
       error='error' if state == common.CommandState.ERROR else None,
       total_test_count=total_test_count,
       failed_test_count=failed_test_count,
       failed_test_run_count=failed_test_run_count,
       start_time=start_time,
       end_time=end_time)
   command_attempt.put()
   self._attempt_id += 1
   return command_attempt
Beispiel #6
0
def CreateCommandAttempt(command,
                         attempt_id,
                         state,
                         start_time=None,
                         end_time=None,
                         update_time=None,
                         task=None):
    """Helper to create a command attempt."""
    command_attempt = datastore_entities.CommandAttempt(
        key=ndb.Key(datastore_entities.CommandAttempt,
                    attempt_id,
                    parent=command.key,
                    namespace=common.NAMESPACE),
        attempt_id=attempt_id,
        command_id=command.key.id(),
        task_id=task.task_id if task else TASK_ID,
        state=state,
        start_time=start_time,
        end_time=end_time,
        update_time=update_time,
        attempt_index=task.attempt_index if task else None,
        run_index=task.run_index if task else None)
    command_attempt.put()
    return command_attempt
 def testCommandAttemptFromEntity(self):
     """Tests coverting a CommandAttempt entity to a message."""
     entity = datastore_entities.CommandAttempt(
         key=ndb.Key(datastore_entities.Request, '1',
                     datastore_entities.Command, '2',
                     datastore_entities.CommandAttempt, '3'),
         command_id='2',
         attempt_id='3',
         task_id='4',
         state=common.CommandState.UNKNOWN,
         hostname='hostname',
         device_serial='device_serial',
         start_time=TIMESTAMP,
         end_time=TIMESTAMP,
         status='status',
         error='error',
         summary='summary',
         total_test_count=1000,
         failed_test_count=100,
         passed_test_count=900,
         create_time=TIMESTAMP,
         update_time=TIMESTAMP)
     message = datastore_entities.ToMessage(entity)
     self.AssertEqualCommandAttempt(entity, message)
    def setUp(self):
        api_test.ApiTest.setUp(self)
        request_key = ndb.Key(datastore_entities.Request,
                              '1001',
                              namespace=common.NAMESPACE)
        request = datastore_test_util.CreateRequest(
            request_id=request_key.id(),
            user='******',
            command_infos=[
                datastore_entities.CommandInfo(command_line='command_line',
                                               cluster='cluster',
                                               run_target='run_target')
            ])
        request.put()

        command = datastore_entities.Command(parent=request_key,
                                             id='1',
                                             command_line='command_line',
                                             cluster='cluster',
                                             run_target='run_target',
                                             state=common.CommandState.RUNNING)
        command.put()

        command_attempt_0 = datastore_entities.CommandAttempt(
            parent=command.key,
            id='attempt_id-0',
            task_id='task-id-0',
            attempt_id='attempt_id-0',
            hostname='hostname_0',
            device_serial='device_serial_0',
            create_time=TIME,
            state=common.CommandState.COMPLETED)
        command_attempt_0.put()

        command_attempt_1 = datastore_entities.CommandAttempt(
            parent=command.key,
            id='attempt_id-1',
            task_id='task-id-1',
            attempt_id='attempt_id-1',
            hostname='hostname_0',
            device_serial='device_serial_1',
            create_time=TIME + TIME_DELTA,
            state=common.CommandState.RUNNING)
        command_attempt_1.put()

        command_attempt_2 = datastore_entities.CommandAttempt(
            parent=command.key,
            id='attempt_id-2',
            task_id='task-id-2',
            attempt_id='attempt_id-2',
            hostname='hostname_1',
            device_serial='device_serial_2',
            create_time=TIME + TIME_DELTA * 2,
            state=common.CommandState.RUNNING)
        command_attempt_2.put()

        command_attempt_3 = datastore_entities.CommandAttempt(
            parent=command.key,
            id='attempt_id-3',
            task_id='task-id-3',
            attempt_id='attempt_id-3',
            hostname='hostname_1',
            device_serial='device_serial_2',
            create_time=TIME + TIME_DELTA * 3,
            device_serials=['d2', 'd3'],
            state=common.CommandState.RUNNING)
        command_attempt_3.put()
    def testRequestFromEntity_multipleCommandsAndAttempts(self):
        """Tests converting a Request object with multiple Commands."""
        request_key = ndb.Key(datastore_entities.Request, '123')
        request = datastore_entities.Request(
            key=request_key,
            user='******',
            command_infos=[
                datastore_entities.CommandInfo(command_line='command_line',
                                               shard_count=2,
                                               run_count=3,
                                               cluster='cluster',
                                               run_target='run_target')
            ],
            state=common.RequestState.UNKNOWN,
            cancel_reason=common.CancelReason.QUEUE_TIMEOUT)
        command1_key = ndb.Key(datastore_entities.Command,
                               '456',
                               parent=request_key)
        cmd1 = datastore_entities.Command(key=command1_key,
                                          command_line='command_line1',
                                          cluster='cluster',
                                          run_target='run_target',
                                          run_count=10,
                                          state=common.CommandState.QUEUED,
                                          start_time=TIMESTAMP,
                                          end_time=None,
                                          create_time=TIMESTAMP,
                                          update_time=TIMESTAMP,
                                          shard_count=2,
                                          shard_index=0)
        command2_key = ndb.Key(datastore_entities.Command,
                               '457',
                               parent=request_key)
        cmd2 = datastore_entities.Command(key=command2_key,
                                          command_line='command_line2',
                                          cluster='cluster',
                                          run_target='run_target',
                                          run_count=10,
                                          state=common.CommandState.QUEUED,
                                          start_time=TIMESTAMP,
                                          end_time=None,
                                          create_time=TIMESTAMP,
                                          update_time=TIMESTAMP,
                                          shard_count=2,
                                          shard_index=1)
        command_attempt1_key = ndb.Key(datastore_entities.CommandAttempt,
                                       '890',
                                       parent=command1_key)
        cmd_attempt1 = datastore_entities.CommandAttempt(
            key=command_attempt1_key,
            task_id='task_id',
            attempt_id='attempt_id',
            state=common.CommandState.UNKNOWN,
            hostname='hostname',
            device_serial='device_serial',
            start_time=TIMESTAMP,
            end_time=TIMESTAMP,
            status='status',
            error='error',
            summary='summary',
            total_test_count=1000,
            failed_test_count=100,
            passed_test_count=900,
            create_time=TIMESTAMP,
            update_time=TIMESTAMP)
        command_attempt2_key = ndb.Key(datastore_entities.CommandAttempt,
                                       '891',
                                       parent=command1_key)
        cmd_attempt2 = datastore_entities.CommandAttempt(
            key=command_attempt2_key,
            task_id='task_id',
            attempt_id='attempt_id',
            state=common.CommandState.UNKNOWN,
            hostname='hostname',
            device_serial='device_serial',
            start_time=TIMESTAMP,
            end_time=TIMESTAMP,
            status='status',
            error='error',
            summary='summary',
            total_test_count=1000,
            failed_test_count=100,
            passed_test_count=900,
            create_time=TIMESTAMP,
            update_time=TIMESTAMP)
        commands = [cmd1, cmd2]
        command_attempts = [cmd_attempt1, cmd_attempt2]

        message = datastore_entities.ToMessage(
            request, command_attempts=command_attempts, commands=commands)
        self.AssertEqualRequest(request, message)
        self.assertEqual(len(message.commands), len(commands))
        for command, command_msg in zip(commands, message.commands):
            self.AssertEqualCommand(command, command_msg)
        self.assertEqual(len(message.command_attempts), len(command_attempts))
        for attempt, msg in zip(command_attempts, message.command_attempts):
            self.AssertEqualCommandAttempt(attempt, msg)