def testMetricNotFoundFromSetLastTimestamp(self, engineMock, repoMock, ec2InstanceMetricGetterMock, _metricStreamerMock): """Test handling of ObjectNotFoundError when calling repository.setMetricLastTimestamp in _processAutostackMetricRequests. In this case, we expect _processAutostackMetricRequests to skip this collection and continue processing the next one(s) """ # Ignore attemting to look for MySQL transient errors. # We're not testing those here. repoMock.retryOnTransientErrors.side_effect = lambda f: f # Define metric to skip over errMetric = Mock(spec_set=self.MetricRowSpec) errMetric.name = "errMetric" errInstanceID = "i-00000" errRefID = 0 # index into requests sequence errRequest = AutostackMetricRequest( refID=errRefID, autostack=Mock(spec_set=self.AutostackRowSpec), metric=errMetric) errMetricRecord = MetricRecord(timestamp=datetime.datetime.utcnow(), value=2) errData = InstanceMetricData(instanceID=errInstanceID, records=[errMetricRecord]) errCollection = MetricCollection(refID=errRefID, slices=[errData], timeRange=self.timeRange, nextMetricTime=self.timeRange.end) # Define "ok" metric okMetric = Mock(spec_set=self.MetricRowSpec) okMetric.name = "okMetric" okInstanceID = "i-11111" okRefID = 1 # index into requests sequence okRequest = AutostackMetricRequest( refID=okRefID, autostack=Mock(spec_set=self.AutostackRowSpec), metric=okMetric) okDataValue = 111 okMetricRecord = MetricRecord(timestamp=datetime.datetime.utcnow(), value=okDataValue) okData = InstanceMetricData(instanceID=okInstanceID, records=[okMetricRecord]) okCollection = MetricCollection(refID=okRefID, slices=[okData], timeRange=self.timeRange, nextMetricTime=self.timeRange.end) # Make setMetricLastTimestamp error on first call (error metric) and pass # on second call (ok metric) repoMock.setMetricLastTimestamp.side_effect = ( app_exceptions.ObjectNotFoundError("Expected: things happen"), None) requests = [errRequest, okRequest] collections = [errCollection, okCollection] metricGetterInstanceMock = ec2InstanceMetricGetterMock.return_value metricGetterInstanceMock.collectMetricData.return_value = iter( collections) streamedData = [] _metricStreamerMock.return_value.streamMetricData.side_effect = ( lambda data, *args, **kwargs: streamedData.append(data)) aggSvc = aggregator_service.AggregatorService() with patch.object(aggregator_service, "getAggregationFn", autospec=True, return_value=None): aggSvc._processAutostackMetricRequests( engine=engineMock, requests=requests, modelSwapper=Mock(spec_set=ModelSwapperInterface)) self.assertEqual(len(streamedData), 1) self.assertEqual(len(streamedData[0]), 1) self.assertEqual(streamedData[0][0][1], okDataValue)