def check_notifications(alarm_id, state_changes): print("Checking Notification Engine") if not os.path.isfile('/etc/monasca/notification.yaml'): print('Notification Engine not installed on this VM,' + ' skipping Notifications test', file=sys.stderr) return False notifications = utils.find_notifications(alarm_id, "root") if len(notifications) != len(state_changes): print('Expected {} notifications but only found {}'.format( len(state_changes), len(notifications)), file=sys.stderr) return False index = 0 for expected in state_changes: actual = notifications[index] if actual != expected: print('Expected {} but found {} for state change {}'.format( expected, actual, index+1), file=sys.stderr) return False index = index + 1 print('Received email notifications as expected') return True
def check_notification(alarm_id, user, expected_state, existing): for i in range(0, 20): notifications = utils.find_notifications(alarm_id, user) if len(notifications) > existing: break time.sleep(1) if len(notifications) <= existing: print('Did not receive new notification in %d seconds for user %s' % (i+1, user), file=sys.stderr) return False if (len(notifications) - existing) > 1: print('Received %d new notifications instead of 1 for user %s' % (len(notifications) - existing, user), file=sys.stderr) return False new_state = notifications[existing] if new_state != expected_state: print('Expected state %s for user %s but found state %s' % (expected_state, user, new_state), file=sys.stderr) return False print('Found notification for state %s for user %s in %d seconds' % (expected_state, user, i), file=sys.stderr) return True
def check_notifications(alarm_id, state_changes): print("Checking Notification Engine") if not os.path.isfile('/etc/monasca/notification.yaml'): print('Notification Engine not installed on this VM,' + ' skipping Notifications test', file=sys.stderr) return False notifications = utils.find_notifications(alarm_id, "root") if len(notifications) != len(state_changes): print('Expected {} notifications but only found {}'.format( len(state_changes), len(notifications)), file=sys.stderr) return False index = 0 for expected in state_changes: actual = notifications[index] if actual != expected: print('Expected {} but found {} for state change {}'.format( expected, actual, index + 1), file=sys.stderr) return False index = index + 1 print('Received email notifications as expected') return True
def main(): if len(sys.argv) == 1: print('usage: %s count [alarm-id]' % sys.argv[0], file=sys.stderr) return 1 if not utils.ensure_has_notification_engine(): return 1 mon_client = utils.create_mon_client() num_cycles = int(sys.argv[1]) alarm_name = 'notification_cycleTest' alarm_json = alarm.find_alarm_byname(mon_client, alarm_name) if alarm_json is not None: alarm_id = alarm_json['id'] else: existing = notification.find_by_name(mon_client, alarm_name) if existing is not None: notification_id = existing['id'] else: notification_id = notification.create(mon_client, alarm_name, "root@localhost") alarm_id = alarm.create(mon_client, alarm_name, None, 'max(cc) > 100', notification_id, notification_id, notification_id) user = '******' start_time = time.time() initial_state = alarm.get_state(mon_client, alarm_id) state = initial_state existing_notifications = utils.find_notifications(alarm_id, user) notifications_sent = num_cycles * 2 for _ in range(0, notifications_sent): if state == 'OK': state = 'ALARM' else: state = 'OK' if not alarm.set_state(mon_client, alarm_id, state): return 1 print("Took %d seconds to send %d alarm state changes" % ((time.time() - start_time), num_cycles * 2)) for i in range(0, 30): notifications = utils.find_notifications(alarm_id, user) notifications_found = len(notifications) - len(existing_notifications) if notifications_found >= notifications_sent: break print('Found %d of %d expected notifications so far' % (notifications_found, notifications_sent)) time.sleep(1) if notifications_found < notifications_sent: print('Expected %d notifications but found %d' % (notifications_sent, notifications_found), file=sys.stderr) return 1 print('Took %d seconds for notifications to fully arrive' % i) result = 0 return result