def testComposeModelCommandResultForDeleteFail(self, repoMock, *_args): """ Make sure we can compose a model command result message for publishing a failed "deleteModel" on the AMQP exchange """ repoMock.getMetric.side_effect = Exception( "getMetric should not have been called here") service = anomaly_service.AnomalyService() modelID = "123456abcdef" result = anomaly_service.ModelCommandResult( commandID="123", method="deleteModel", status=1, errorMessage="bad, bad, bad") msg = service._composeModelCommandResultMessage(modelID=modelID, cmdResult=result) # Validate the message against its JSON schema schemaStream = pkg_resources.resource_stream( "htmengine.runtime.json_schema", "model_command_result_amqp_message.json") jsonSchema = json.load(schemaStream) validictory.validate(msg, jsonSchema) self.assertEqual(msg.pop("method"), result.method) self.assertEqual(msg.pop("modelId"), modelID) self.assertEqual(msg.pop("commandId"), result.commandID) self.assertEqual(msg.pop("status"), result.status) self.assertEqual(msg.pop("errorMessage"), result.errorMessage) self.assertFalse(msg)
def testProcessFailedDefineModelCommandResultWhileInCreatePendingState( self, repoMock, *_args): """Test the scenario where a failed "defineModel" result is delivered while the Metric is in CREATE_PENDING state """ class MetricRowSpec(object): status = None metricRowMock = Mock(spec_set=MetricRowSpec, status=MetricStatus.CREATE_PENDING) repoMock.getMetric.return_value = metricRowMock runner = anomaly_service.AnomalyService() metricID = "abc" result = anomaly_service.ModelCommandResult( commandID="123", method="defineModel", status=htmengineerrno.ERR_INVALID_ARG, errorMessage="invalid arg") runner._processModelCommandResult(metricID=metricID, result=result) repoMock.setMetricStatus.assert_called_with( (repoMock.engineFactory.return_value.connect.return_value. __enter__.return_value), metricID, MetricStatus.ERROR, result.errorMessage)
def testComposeModelCommandResultObjNotFound(self, repoMock, *_args): """ Make sure ObjectNotFoundError is raised when composing a model command result message for publishing "defineModel" and the metric is not found """ repoMock.getMetric.side_effect = app_exceptions.ObjectNotFoundError( "getMetric should not have been called here") service = anomaly_service.AnomalyService() modelID = "123456abcdef" result = anomaly_service.ModelCommandResult(commandID="123", method="defineModel", status=0) with self.assertRaises(app_exceptions.ObjectNotFoundError): service._composeModelCommandResultMessage(modelID=modelID, cmdResult=result)
def testComposeModelCommandResultForDefine(self, repoMock, *_args): """ Make sure we can compose a model command result message for publishing "defineModel" on the AMQP exchange """ class MetricRowSpec(object): status = MetricStatus.CREATE_PENDING name = "metric.name" server = "metric.server" parameters = json.dumps(dict(value1="one", value2=2)) repoMock.getMetric.return_value = MetricRowSpec service = anomaly_service.AnomalyService() modelID = "123456abcdef" result = anomaly_service.ModelCommandResult(commandID="123", method="defineModel", status=0) msg = service._composeModelCommandResultMessage(modelID=modelID, cmdResult=result) # Validate the message against its JSON schema schemaStream = pkg_resources.resource_stream( "htmengine.runtime.json_schema", "model_command_result_amqp_message.json") jsonSchema = json.load(schemaStream) validictory.validate(msg, jsonSchema) self.assertEqual(msg.pop("method"), result.method) self.assertEqual(msg.pop("modelId"), modelID) self.assertEqual(msg.pop("commandId"), result.commandID) self.assertEqual(msg.pop("status"), result.status) self.assertEqual(msg.pop("errorMessage"), result.errorMessage) modelInfo = msg.pop("modelInfo") self.assertEqual(modelInfo.pop("metricName"), MetricRowSpec.name) self.assertEqual(modelInfo.pop("resource"), MetricRowSpec.server) self.assertEqual(modelInfo.pop("modelSpec"), json.loads(MetricRowSpec.parameters)) self.assertFalse(modelInfo) self.assertFalse(msg)
def testProcessSuccessfulDefineModelCommandResultWhileInErrorState( self, repoMock, *_args): """Test the scenario where "defineModel" result is delivered after the Metric has already been placed in error state """ class MetricRowSpec(object): status = None metricRowMock = Mock(spec_set=MetricRowSpec, status=MetricStatus.ERROR) repoMock.getMetric.return_value = metricRowMock runner = anomaly_service.AnomalyService() metricID = "abc" result = anomaly_service.ModelCommandResult(commandID="123", method="defineModel", status=0) runner._processModelCommandResult(metricID=metricID, result=result) self.assertFalse(repoMock.setMetricStatus.called)
def testProcessSuccessfulDefineModelCommandResultWhileInActiveState( self, repoMock, *_args): """This is the other normal processing path where "defineModel" result is re-delivered as the side-effect of at-least-once delivery guarantee """ class MetricRowSpec(object): status = None metricRowMock = Mock(spec_set=MetricRowSpec, status=MetricStatus.ACTIVE) repoMock.getMetric.return_value = metricRowMock runner = anomaly_service.AnomalyService() metricID = "abc" result = anomaly_service.ModelCommandResult(commandID="123", method="defineModel", status=0) runner._processModelCommandResult(metricID=metricID, result=result) self.assertFalse(repoMock.setMetricStatus.called)
def testProcessSuccessfulDefineModelCommandResultWhileInCreatePendingState( self, repoMock, *_args): """This is the normal processing path for "defineModel" result""" class MetricRowSpec(object): status = None metricRowMock = Mock(spec_set=MetricRowSpec, status=MetricStatus.CREATE_PENDING) repoMock.getMetricWithSharedLock.return_value = metricRowMock runner = anomaly_service.AnomalyService() metricID = "abc" result = anomaly_service.ModelCommandResult(commandID="123", method="defineModel", status=0) runner._processModelCommandResult(metricID=metricID, result=result) repoMock.setMetricStatus.assert_called_with( (repoMock.engineFactory.return_value.connect.return_value. __enter__.return_value), metricID, MetricStatus.ACTIVE)
def testProcessFailedDefineModelCommandResultWhileInErrorState( self, repoMock, *_args): """Test the scenario where a failed "defineModel" result is delivered after the Metric has already been placed in ERROR state """ class MetricRowSpec(object): status = None metricRowMock = Mock(spec_set=MetricRowSpec, status=MetricStatus.ERROR) repoMock.getMetricWithSharedLock.return_value = metricRowMock runner = anomaly_service.AnomalyService() metricID = "abc" result = anomaly_service.ModelCommandResult( commandID="123", method="defineModel", status=htmengineerrno.ERR_INVALID_ARG, errorMessage="invalid arg") runner._processModelCommandResult(metricID=metricID, result=result) self.assertFalse(repoMock.setMetricStatus.called)
def testComposeModelCommandResultNotMonitored(self, repoMock, *_args): """ Make sure MetricNotMonitoredError is raised when composing a model command result message for publishing "defineModel" and metric properties are not set """ class MetricRowSpec(object): status = MetricStatus.UNMONITORED name = "metric.name" server = "metric.server" parameters = None repoMock.getMetric.return_value = MetricRowSpec service = anomaly_service.AnomalyService() modelID = "123456abcdef" result = anomaly_service.ModelCommandResult(commandID="123", method="defineModel", status=0) with self.assertRaises(app_exceptions.MetricNotMonitoredError): msg = service._composeModelCommandResultMessage(modelID=modelID, cmdResult=result)