Example #1
0
    def __init__(self, test_desc: 'test_descriptor.TestDescriptor',
                 execution_uid: Text,
                 test_options: 'test_descriptor.TestOptions'):
        """Initializer.

    Args:
      test_desc: openhtf.TestDescriptor instance describing the test to run,
        used to initialize some values here, but it is not modified.
      execution_uid: a unique uuid use to identify a test being run.
      test_options: test_options passed through from Test.
    """
        super(TestState, self).__init__()
        self._status = self.Status.WAITING_FOR_TEST_START  # type: TestState.Status

        self.test_record = test_record.TestRecord(
            dut_id=None,
            station_id=conf.station_id,
            code_info=test_desc.code_info,
            start_time_millis=0,
            # Copy metadata so we don't modify test_desc.
            metadata=copy.deepcopy(test_desc.metadata),
            diagnosers=test_options.diagnosers)
        logs.initialize_record_handler(execution_uid, self.test_record,
                                       self.notify_update)
        self.state_logger = logs.get_record_logger_for(execution_uid)
        self.plug_manager = plugs.PlugManager(test_desc.plug_types,
                                              self.state_logger)
        self.diagnoses_manager = diagnoses_lib.DiagnosesManager(
            self.state_logger.getChild('diagnoses'))
        self.running_phase_state = None  # type: Optional['PhaseState']
        self._running_test_api = None  # type: Optional['test_descriptor.TestApi']
        # TODO(arsharma): Change to Dict[Any, Any] when pytype handles it correctly.
        self.user_defined_state = {}  # type: Any
        self.execution_uid = execution_uid
        self.test_options = test_options
Example #2
0
  def test_attach_record_as_json(self):
    record = test_record.TestRecord('mock-dut-id', 'mock-station-id')
    mfg_event = mfg_event_pb2.MfgEvent()
    mfg_event_converter._attach_record_as_json(mfg_event, record)

    self.assertEqual(mfg_event.attachment[0].name, 'OpenHTF_record.json')
    self.assertTrue(mfg_event.attachment[0].value_binary)  # Assert truthy.
    self.assertEqual(mfg_event.attachment[0].type, test_runs_pb2.TEXT_UTF8)
Example #3
0
  def test_attach_config(self):
    record = test_record.TestRecord('mock-dut-id', 'mock-station-id',
                                    metadata={'config': {'key': 'value'}})
    mfg_event = mfg_event_pb2.MfgEvent()
    mfg_event_converter._attach_config(mfg_event, record)

    self.assertEqual(mfg_event.attachment[0].name, 'config')
    self.assertTrue(mfg_event.attachment[0].value_binary)  # Assert truthy.
    self.assertEqual(mfg_event.attachment[0].type, test_runs_pb2.TEXT_UTF8)
Example #4
0
  def test_mfg_event_from_test_record(self):
    """Test for the full conversion flow."""
    record = test_record.TestRecord(
        dut_id='dut_serial',
        start_time_millis=1,
        end_time_millis=1,
        station_id='localhost',
        outcome=test_record.Outcome.PASS,
    )
    record.outcome = test_record.Outcome.PASS
    record.metadata = {
        'assembly_events': [assembly_event_pb2.AssemblyEvent()] * 2,
        'config': {'mock-config-key': 'mock-config-value'},
        'operator_name': 'mock-operator-name',
    }
    record.phases = [
        test_record.PhaseRecord(
            name='phase-%d' % idx,
            descriptor_id=idx,
            codeinfo=test_record.CodeInfo.uncaptured(),
            result=None,
            attachments={},
            start_time_millis=1,
            end_time_millis=1
        )
        for idx in range(1, 5)
    ]
    for phase in record.phases:
      phase.measurements = {
          'meas-1': measurements.Measurement('meas-1'),
          'meas-2': measurements.Measurement('meas-2'),
          'meas-3': measurements.Measurement('meas-3').with_dimensions('V'),
      }
      phase.attachments = {
          'attach-1': test_record.Attachment(data='data-1', mimetype=''),
          'attach-2': test_record.Attachment(data='data-2', mimetype=''),
      }

    mfg_event = mfg_event_converter.mfg_event_from_test_record(record)

    self.assertEqual(mfg_event.dut_serial, record.dut_id)
    self.assertEqual(len(mfg_event.assembly_events), 2)
    self.assertEqual(len(mfg_event.measurement), 8)
    self.assertEqual(sorted(m.name for m in mfg_event.measurement),
                     ['meas-1_0', 'meas-1_1', 'meas-1_2', 'meas-1_3',
                      'meas-2_0', 'meas-2_1', 'meas-2_2', 'meas-2_3'])
    self.assertEqual(len(mfg_event.attachment), 15)
    self.assertEqual(sorted(str(m.name) for m in mfg_event.attachment),
                     ['OpenHTF_record.json', 'argv',
                      'attach-1_0', 'attach-1_1', 'attach-1_2', 'attach-1_3',
                      'attach-2_0', 'attach-2_1', 'attach-2_2', 'attach-2_3',
                      'config',
                      'multidim_meas-3_0', 'multidim_meas-3_1',
                      'multidim_meas-3_2', 'multidim_meas-3_3'])
Example #5
0
 def testGetTestOutcomeHeadline_TestColorized(self, outcome, headline):
     record = test_record.TestRecord(dut_id='TestDutId',
                                     station_id='test_station',
                                     outcome=test_record.Outcome[outcome])
     # TODO(b/70517332): Pytype currently doesn't properly support the functional
     # API of enums: https://github.com/google/pytype/issues/459. Remove
     # disabling pytype once fixed.
     self.assertEqual(
         text._GetTestOutcomeHeadline(record, colorize_text=True),
         f'{text._ColorFromTestOutcome[outcome].value}{headline}'  # pytype: disable=unsupported-operands
         f'{colorama.Style.RESET_ALL}')
Example #6
0
 def __init__(self):
     self.mock_logger = mock.create_autospec(logging.Logger)
     self.mock_phase_state = mock.create_autospec(test_state.PhaseState,
                                                  logger=self.mock_logger)
     self.mock_test_state = mock.create_autospec(
         test_state.TestState,
         test_record=test_record.TestRecord('DUT', 'STATION'),
         user_defined_state={})
     super(FakeTestApi,
           self).__init__(measurements={},
                          running_phase_state=self.mock_phase_state,
                          running_test_state=self.mock_test_state)
Example #7
0
  def __init__(self, test_desc, execution_uid):
    super(TestState, self).__init__()
    self._status = self.Status.WAITING_FOR_TEST_START

    self.test_record = test_record.TestRecord(
        dut_id=None, station_id=conf.station_id, code_info=test_desc.code_info,
        # Copy metadata so we don't modify test_desc.
        metadata=copy.deepcopy(test_desc.metadata))
    self.logger = logs.initialize_record_logger(
        execution_uid, self.test_record, self.notify_update)
    self.plug_manager = plugs.PlugManager(test_desc.plug_types, self.logger)
    self.running_phase_state = None
    self.user_defined_state = {}
    self.execution_uid = execution_uid
Example #8
0
    def __init__(self, test_desc, execution_uid, test_options):
        super(TestState, self).__init__()
        self._status = self.Status.WAITING_FOR_TEST_START

        self.test_record = test_record.TestRecord(
            dut_id=None,
            station_id=conf.station_id,
            code_info=test_desc.code_info,
            start_time_millis=0,
            # Copy metadata so we don't modify test_desc.
            metadata=copy.deepcopy(test_desc.metadata),
            diagnosers=test_options.diagnosers)
        logs.initialize_record_handler(execution_uid, self.test_record,
                                       self.notify_update)
        self.state_logger = logs.get_record_logger_for(execution_uid)
        self.plug_manager = plugs.PlugManager(test_desc.plug_types,
                                              self.state_logger)
        self.diagnoses_manager = diagnoses_lib.DiagnosesManager(
            self.state_logger.getChild('diagnoses'))
        self.running_phase_state = None
        self._running_test_api = None
        self.user_defined_state = {}
        self.execution_uid = execution_uid
        self.test_options = test_options
Example #9
0
  def test_populate_basic_data(self):
    outcome_details = test_record.OutcomeDetails(
        code='mock-code',
        description='mock-description',
    )
    phase = test_record.PhaseRecord(
        name='mock-phase-name',
        descriptor_id=1,
        codeinfo=self.create_codeinfo(),
        start_time_millis=200,
        end_time_millis=400,
    )
    log_record = test_logs.LogRecord(
        level=logging.INFO,
        logger_name='mock-logger-name',
        source='mock-source',
        lineno=123,
        timestamp_millis=300,
        message='mock-message',
    )
    record = test_record.TestRecord(
        dut_id='mock-dut-id',
        station_id='mock-station-id',
        start_time_millis=100,
        end_time_millis=500,
        outcome=test_record.Outcome.PASS,
        outcome_details=[outcome_details],
        metadata={
            'test_name': 'mock-test-name',
            'operator_name': 'mock-operator-name',
            'test_version': 1.0,
            'test_description': 'mock-test-description',
        },
        phases=[phase],
        log_records=[log_record],
    )

    mfg_event = mfg_event_pb2.MfgEvent()
    mfg_event_converter._populate_basic_data(mfg_event, record)

    self.assertEqual(mfg_event.dut_serial, 'mock-dut-id')
    self.assertEqual(mfg_event.start_time_ms, 100)
    self.assertEqual(mfg_event.end_time_ms, 500)
    self.assertEqual(mfg_event.tester_name, 'mock-station-id')
    self.assertEqual(mfg_event.test_name, 'mock-test-name')
    self.assertEqual(mfg_event.test_version, '1.0')
    self.assertEqual(mfg_event.test_description, 'mock-test-description')
    self.assertEqual(mfg_event.test_status, test_runs_pb2.PASS)

    # Phases.
    self.assertEqual(mfg_event.phases[0].name, 'mock-phase-name')
    self.assertEqual(mfg_event.phases[0].description, 'mock-sourcecode')
    self.assertEqual(mfg_event.phases[0].timing.start_time_millis, 200)
    self.assertEqual(mfg_event.phases[0].timing.end_time_millis, 400)

    # Failure codes.
    self.assertEqual(mfg_event.failure_codes[0].code, 'mock-code')
    self.assertEqual(mfg_event.failure_codes[0].details, 'mock-description')

    # Test logs.
    self.assertEqual(mfg_event.test_logs[0].timestamp_millis, 300)
    self.assertEqual(mfg_event.test_logs[0].log_message, 'mock-message')
    self.assertEqual(mfg_event.test_logs[0].logger_name, 'mock-logger-name')
    self.assertEqual(mfg_event.test_logs[0].levelno, logging.INFO)
    self.assertEqual(mfg_event.test_logs[0].level,
                     test_runs_pb2.TestRunLogMessage.INFO)
    self.assertEqual(mfg_event.test_logs[0].log_source, 'mock-source')
    self.assertEqual(mfg_event.test_logs[0].lineno, 123)
Example #10
0
 def testGetTestOutcomeHeadline_TestNotColorized(self, outcome, headline):
     record = test_record.TestRecord(dut_id='TestDutId',
                                     station_id='test_station',
                                     outcome=test_record.Outcome[outcome])
     self.assertEqual(text._GetTestOutcomeHeadline(record), headline)