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_notifies_failed_job_with_correct_payload(self): fake_post = MagicMock() fake_upload = MagicMock() cloud_notifier = CloudNotifier(fake_post, fake_upload) def get_failed_job(): job_id = uuid.uuid4() resource_dir = Path(os.path.dirname(__file__)).joinpath( 'resources', 'logs') job_ = Job(Executable(output_path=resource_dir), job_number=0, job_uuid=job_id) job_.status = JobStatus.FAILED return job_ failed_job = get_failed_job() cloud_notifier.notify_job_end(failed_job) cloud_notification_status = cloud_notifier.get_last_notification_status( failed_job.id)["CloudNotifier"] assert cloud_notification_status.status == NotificationStatus.SUCCESS, "Notification status should be success" # Posted payload fake_post.assert_called_once() args, _ = fake_post.call_args assert len( args ) == 1, "Expected mock post to have been called with one argument" payload = args[0] assert payload is not None, "Expected posted payload to not be None" # Query assert "query" in payload, "Expected posted payload to have query field" query = payload["query"] assert query is not None # Variables assert "variables" in payload, "Expected posted payload to have variables field" variables = payload["variables"] assert variables is not None assert "in" in variables, "Expected variables to have 'in' field" inp = variables["in"] stderr = inp["stderr"] assert stderr is not None assert len(stderr) > 50 assert inp["job_id"] is not None
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'"