def testProcessCollectedDataWithGetInstanceStatusError(self, engineMock,
                                                         *_mocks):
    # Test MetricCollector._processCollectedData with collection result
    # containing getInstanceStatus error
    metricID = 1

    mockMetric = _makeFreshMetricMockInstance(metricPollInterval=5,
                                              uid=metricID)

    dataCollectionResult = metric_collector._DataCollectionResult(
      metricID=metricID)
    dataCollectionResult.data = []
    dataCollectionResult.resourceStatus = BotoServerError(
      500, "Fake getInstanceStatus BotoServerError")

    metricsToUpdate = OrderedDict()
    metricsToUpdate[mockMetric.uid] = mockMetric

    collector = metric_collector.MetricCollector()
    numEmpty, numErrors = collector._processCollectedData(
      engineMock,
      metricsToUpdate=metricsToUpdate,
      collectResult=dataCollectionResult,
      modelSwapper=Mock())

    self.assertEqual(numEmpty, 1)
    self.assertEqual(numErrors, 1)
  def testProcessCollectedDataWithClearingOfCollectorError(self, engineMock,
                                                           repoMock, *_mocks):
    # Test MetricCollector._processCollectedData with clearing of collector
    # error state
    metricID = 1

    mockMetric = _makeFreshMetricMockInstance(metricPollInterval=5,
                                              uid=metricID)
    mockMetric.collector_error = dict(deadline=time.time(), message="Blah")

    dataCollectionResult = metric_collector._DataCollectionResult(
      metricID=metricID)
    dataCollectionResult.data = []

    metricsToUpdate = OrderedDict()
    metricsToUpdate[mockMetric.uid] = mockMetric

    collector = metric_collector.MetricCollector()
    numEmpty, numErrors = collector._processCollectedData(
      engineMock,
      metricsToUpdate=metricsToUpdate,
      collectResult=dataCollectionResult,
      modelSwapper=Mock())

    self.assertEqual(numEmpty, 1)
    self.assertEqual(numErrors, 0)
    repoMock.retryOnTransientErrors('repository.setMetricCollectorError')\
      .assert_called_once_with(
        engineMock.connect.return_value.__enter__.return_value,
        metricID,
        None)
  def testProcessCollectedDataWithEmptyNewData(self, engineMock, *_mocks):
    # Test MetricCollector._processCollectedData with collection result
    # containing empty data set
    metricID = 1
    mockMetric = _makeFreshMetricMockInstance(metricPollInterval=5,
                                              uid=metricID)

    dataCollectionResult = metric_collector._DataCollectionResult(
      metricID=metricID)
    dataCollectionResult.data = []

    metricsToUpdate = OrderedDict()
    metricsToUpdate[mockMetric.uid] = mockMetric

    collector = metric_collector.MetricCollector()
    numEmpty, numErrors = collector._processCollectedData(
      engineMock,
      metricsToUpdate=metricsToUpdate,
      collectResult=dataCollectionResult,
      modelSwapper=Mock())

    self.assertEqual(numEmpty, 1)
    self.assertEqual(numErrors, 0)