Example #1
0
    def testReexecEmptyOutput(self):
        """Test calling an inside chroot method that produced no output."""
        self.PatchObject(self.router,
                         '_GetMethod',
                         return_value=self._mock_callable(expect_called=False))
        self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
        expected_output_msg = build_api_test_pb2.TestResultMessage()

        # Set the command side effect to write out our expected output to the
        # output file for the inside the chroot reexecution of the endpoint.
        # This lets us make sure the logic moving everything out works as intended.
        self.rc.SetDefaultCmdResult(side_effect=self._writeChrootCallOutput(
            content=expected_output_msg.SerializeToString(), mode='wb'))

        service = 'chromite.api.OutsideChrootApiService'
        method = 'OutsideServiceInsideMethod'
        service_method = '%s/%s' % (service, method)
        self.router.Route(service, method, self.api_config,
                          self.binary_input_handler,
                          [self.binary_output_handler],
                          self.binary_config_handler)

        self.assertCommandContains(['build_api', service_method],
                                   enter_chroot=True)

        output_msg = build_api_test_pb2.TestResultMessage()
        self.binary_output_handler.read_into(output_msg)
        self.assertEqual(expected_output_msg, output_msg)
    def testReexecNonemptyOutput(self):
        """Test calling an inside chroot method that produced output."""
        osutils.WriteFile(self.output_file, '')
        self.PatchObject(self.router,
                         '_GetMethod',
                         return_value=self._mock_callable(expect_called=False))
        self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
        self.rc.SetDefaultCmdResult(side_effect=self._writeChrootCallOutput(
            content='{"result": "foo"}'))

        service = 'chromite.api.OutsideChrootApiService'
        method = 'OutsideServiceInsideMethod'
        service_method = '%s/%s' % (service, method)
        self.router.Route(service, method, self.input_file, self.output_file,
                          self.api_config)

        self.assertCommandContains(['build_api', service_method],
                                   enter_chroot=True)

        # It should be writing the result out to our output file.
        expected = build_api_test_pb2.TestResultMessage()
        json_format.Parse('{"result": "foo"}', expected)
        result = build_api_test_pb2.TestResultMessage()
        json_format.Parse(osutils.ReadFile(self.output_file), result)
        self.assertEqual(expected, result)
Example #3
0
    def testMultipleOutputHandling(self):
        """Test multiple output handling."""
        expected_result = 'Success!'

        def impl(input_msg, output_msg, config):
            self.assertIsInstance(input_msg,
                                  build_api_test_pb2.TestRequestMessage)
            self.assertIsInstance(output_msg,
                                  build_api_test_pb2.TestResultMessage)
            self.assertIsInstance(config, api_config.ApiConfig)
            self.assertEqual(config, self.api_config)
            # Set the property on the output to test against.
            output_msg.result = expected_result

        self.PatchObject(self.router, '_GetMethod', return_value=impl)

        self.router.Route(
            'chromite.api.TestApiService', 'InputOutputMethod',
            self.api_config, self.binary_input_handler,
            [self.binary_output_handler, self.json_output_handler],
            self.binary_config_handler)

        # Make sure it did write out all the expected files.
        self.assertExists(self.binary_output_file)
        self.assertExists(self.json_output_file)

        # Parse the output files back into a message.
        binary_msg = build_api_test_pb2.TestResultMessage()
        json_msg = build_api_test_pb2.TestResultMessage()
        self.binary_output_handler.read_into(binary_msg)
        self.json_output_handler.read_into(json_msg)

        # Make sure the parsed messages have the expected content.
        self.assertEqual(binary_msg.result, expected_result)
        self.assertEqual(json_msg.result, expected_result)
Example #4
0
    def testReexecNonemptyOutput(self):
        """Test calling an inside chroot method that produced output."""
        self.PatchObject(self.router,
                         '_GetMethod',
                         return_value=self._mock_callable(expect_called=False))
        self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)

        # Patch the chroot tempdir method to return a tempdir with our subprocess
        # tempdir so the output file will be in the expected location.
        tempdir = osutils.TempDir()
        original = tempdir.tempdir
        tempdir.tempdir = self.subprocess_tempdir
        self.PatchObject(chroot_lib.Chroot, 'tempdir', return_value=tempdir)

        expected_output_msg = build_api_test_pb2.TestResultMessage()
        expected_output_msg.result = 'foo'

        # Set the command side effect to write out our expected output to the
        # output file for the inside the chroot reexecution of the endpoint.
        # This lets us make sure the logic moving everything out works as intended.
        self.rc.SetDefaultCmdResult(side_effect=self._writeChrootCallOutput(
            content=expected_output_msg.SerializeToString(), mode='wb'))

        service = 'chromite.api.OutsideChrootApiService'
        method = 'OutsideServiceInsideMethod'
        service_method = '%s/%s' % (service, method)
        self.router.Route(service, method, self.api_config,
                          self.binary_input_handler,
                          [self.binary_output_handler],
                          self.binary_config_handler)

        self.assertCommandContains(['build_api', service_method],
                                   enter_chroot=True)

        # It should be writing the result out to our output file.
        output_msg = build_api_test_pb2.TestResultMessage()
        self.binary_output_handler.read_into(output_msg)
        self.assertEqual(expected_output_msg, output_msg)

        tempdir.tempdir = original
        del tempdir
 def testDeserializeGauge(self):
     """Test deserialization of a gauge."""
     response = build_api_test_pb2.TestResultMessage()
     mock_events = [
         MetricEvent(1000, 'a.gauge', OP_GAUGE, arg=17),
     ]
     with mock.patch('chromite.api.metrics.metrics.read_metrics_events',
                     return_value=mock_events):
         metrics.deserialize_metrics_log(response.events)
         self.assertEqual(len(response.events), 1)
         self.assertEqual(response.events[0].name, 'a.gauge')
         self.assertEqual(response.events[0].timestamp_milliseconds, 1000)
         self.assertEqual(response.events[0].gauge, 17)
Example #6
0
    def testReexecNoOutput(self):
        """Test calling an inside chroot method that produced no output."""
        self.PatchObject(self.router,
                         '_GetMethod',
                         return_value=self._mock_callable(expect_called=False))
        self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
        self.rc.SetDefaultCmdResult(returncode=1)

        service = 'chromite.api.OutsideChrootApiService'
        method = 'OutsideServiceInsideMethod'
        service_method = '%s/%s' % (service, method)
        self.router.Route(service, method, self.api_config,
                          self.binary_input_handler,
                          [self.binary_output_handler],
                          self.binary_config_handler)

        self.assertCommandContains(['build_api', service_method],
                                   enter_chroot=True)

        output_msg = build_api_test_pb2.TestResultMessage()
        empty_msg = build_api_test_pb2.TestResultMessage()
        self.binary_output_handler.read_into(output_msg)
        self.assertEqual(empty_msg, output_msg)
 def testDeserializeTimer(self):
   """Test timer math and deserialization into proto objects."""
   response = build_api_test_pb2.TestResultMessage()
   mock_events = [
       MetricEvent(600, 'a.b', OP_START_TIMER, arg='100'),
       MetricEvent(1000, 'a.b', OP_STOP_TIMER, arg='100'),
   ]
   with mock.patch('chromite.api.metrics.metrics.read_metrics_events',
                   return_value=mock_events):
     metrics.deserialize_metrics_log(response.events)
     self.assertEqual(len(response.events), 1)
     self.assertEqual(response.events[0].name, 'a.b')
     self.assertEqual(response.events[0].timestamp_milliseconds, 1000)
     self.assertEqual(response.events[0].duration_milliseconds, 1000-600)
    def testDeserializeNamedEvent(self):
        """Test deserialization of a named event.

    This test also includes a prefix to test for proper prepending.
    """
        response = build_api_test_pb2.TestResultMessage()
        mock_events = [
            MetricEvent(1000, 'a.named_event', OP_NAMED_EVENT, arg=None),
        ]
        with mock.patch('chromite.api.metrics.metrics.read_metrics_events',
                        return_value=mock_events):
            metrics.deserialize_metrics_log(response.events, prefix='prefix')
            self.assertEqual(len(response.events), 1)
            self.assertEqual(response.events[0].name, 'prefix.a.named_event')
            self.assertEqual(response.events[0].timestamp_milliseconds, 1000)
            self.assertFalse(response.events[0].duration_milliseconds)
Example #9
0
  def setUp(self):
    # Setup the directories.
    self.chroot_dir = os.path.join(self.tempdir, 'chroot')
    self.source_dir = '/source'
    self.chroot_source = os.path.join(self.chroot_dir,
                                      self.source_dir.lstrip(os.sep))
    self.source_dir2 = '/source2'
    self.chroot_source2 = os.path.join(self.chroot_dir,
                                       self.source_dir2.lstrip(os.sep))
    self.dest_dir = os.path.join(self.tempdir, 'destination')
    osutils.SafeMakedirs(self.chroot_source)
    osutils.SafeMakedirs(self.chroot_source2)
    osutils.SafeMakedirs(self.dest_dir)

    # Two files in the same directory inside the chroot.
    self.source_file1 = os.path.join(self.chroot_source, 'file1')
    self.source_file1_inside = os.path.join(self.source_dir, 'file1')
    self.file1_contents = 'file 1'
    osutils.WriteFile(self.source_file1, self.file1_contents)

    self.file2_contents = 'some data'
    self.source_file2 = os.path.join(self.chroot_source, 'file2')
    self.source_file2_inside = os.path.join(self.source_dir, 'file2')
    osutils.WriteFile(self.source_file2, self.file2_contents)

    # Third file in a different location.
    self.file3_contents = 'another file'
    self.source_file3 = os.path.join(self.chroot_source2, 'file3')
    self.source_file3_inside = os.path.join(self.source_dir2, 'file3')
    osutils.WriteFile(self.source_file3, self.file3_contents)

    self.request = build_api_test_pb2.TestRequestMessage()
    self.request.result_path.path.path = self.dest_dir
    self.request.result_path.path.location = common_pb2.Path.OUTSIDE
    self.response = build_api_test_pb2.TestResultMessage()
    self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
Example #10
0
 def setUp(self):
     self.request = build_api_test_pb2.TestRequestMessage()
     self.response = build_api_test_pb2.TestResultMessage()