def test_notifier_history(self): # pylint:disable=unused-argument,redefined-outer-name """Tests the Notifier's built-in history management via CloudNotifier""" should_raise = False def _fake_post(payload): nonlocal should_raise if should_raise: raise RuntimeError cloud_notifier = CloudNotifier(_fake_post, _empty_upload) job = _get_job() cloud_notifier.notify_job_start(job) # Validate history for notify_job_start notifications = cloud_notifier.get_notification_history( job.id)[cloud_notifier.name] assert len( notifications ) == 1, "Expecting a single notification at this point (JobStart)" assert notifications[ 0].type == NotificationType.JOB_START, "Notification type should be Job Start" assert notifications[0].status == NotificationStatus.SUCCESS, "Notification is expected to succeed " \ "with Mock `post`" last_notification = cloud_notifier.get_last_notification_status( job.id)[cloud_notifier.name] assert last_notification is not None, "There was a single notification, we expect a concrete value" assert last_notification.type == NotificationType.JOB_START, "Notification type should be Job Start" assert last_notification.status == NotificationStatus.SUCCESS, "Notification was expected to succeed" should_raise = True cloud_notifier.notify_job_end(job) # Validate history for notify_job_end notifications = cloud_notifier.get_notification_history( job.id)[cloud_notifier.name] assert len( notifications ) == 2, "Expecting two notification at this point (JobStart, JobEnd)" assert notifications[ 1].type == NotificationType.JOB_END, "Second notification type should be Job End" assert notifications[ 1].status == NotificationStatus.FAILED, "Notification has failed here due to set flag" last_notification = cloud_notifier.get_last_notification_status( job.id)[cloud_notifier.name] assert last_notification is not None, "We expect a concrete value after any event has happened" assert last_notification.type == NotificationType.JOB_END, "Last notification type was Job End" assert last_notification.status == NotificationStatus.FAILED, "Last notification has failed to due to set flag"
def test_cloud_notifier_job_start_end_queries(self, job): # pylint:disable=redefined-outer-name """Tests the job start/end for CloudNotifier""" posted_payload = {} def fake_post(payload): nonlocal posted_payload posted_payload = payload # Initializations (sanity checks) assert posted_payload == {}, "Sanity test - `posted_payload` dictionary should be empty at this point" cloud_notifier = CloudNotifier(fake_post, _empty_upload) expected_payload_start = { "query": "mutation NotifyJobStart($in: JobStartInput!) { notifyJobStart(input: $in) }" } expected_payload_end = { "query": "mutation NotifyJobEnd($in: JobDoneInput!) { notifyJobDone(input: $in) }" } notifications = cloud_notifier.get_notification_history( job.id)[cloud_notifier.name] assert len( notifications ) == 0, "Expecting no notification at this point as no events have occurred" last_notification = cloud_notifier.get_last_notification_status( job.id)[cloud_notifier.name] assert last_notification is None, "There is no last notification either, expected `None` value" cloud_notifier.notify_job_start(job) # Validate query for notify_job_start assert "query" in posted_payload, "Payload is expected to contain the key 'query'" assert posted_payload["query"] == expected_payload_start[ "query"], "GraphQL queries should be identical" cloud_notifier.notify_job_end(job) # Validate query for notify_job_end assert "query" in posted_payload, "Payload is expected to contain the key 'query'" assert posted_payload["query"] == expected_payload_end[ "query"], "GraphQL queries should be identical" assert "variables" in posted_payload, "Posted payload is expected to contain a 'variables' key" variables = posted_payload["variables"] assert "in" in variables, "'variables' dictionary is expected to contain a key called 'in'"