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
def main():
    if not utils.ensure_has_notification_engine():
        return 1

    # Delete notification for OK.Cycle OK, ALARM, UNDETERMINED
    # Ensure proper notifications got written for ALARM, UNDETERMINED

    states = ['OK', 'ALARM', 'UNDETERMINED']
    mon_client = utils.create_mon_client()

    try:
        # Create 3 notifications with different emails, root, kafka,
        # and monasca-agent
        email1 = "root"
        email2 = "kafka"
        email3 = "monasca-agent"
        notification_id_1 = find_or_create_notification(mon_client, email1,
                                                        email1 + "@localhost")
        notification_id_2 = find_or_create_notification(mon_client, email2,
                                                        email2 + "@localhost")
        notification_id_3 = find_or_create_notification(mon_client, email3,
                                                        email3 + "@localhost")

        # Create an alarm. Cycle OK, ALARM, UNDETERMINED,
        alarm_name = "Test Notifications-" + str(os.getpid())
        expr = 'max(not_real_metric{}) > 10'
        alarm_id = alarm.create(mon_client, alarm_name, None, expr,
                                notification_id_1, notification_id_2,
                                notification_id_3)
        print('Created Alarm %s' % alarm_id)
        print_notification_setup(mon_client, alarm_id)
        print('Test initial cycle of Alarms')
        cycle_states(mon_client, alarm_id, states)

        # Ensure proper notifications got written to each
        if not check_notifications(alarm_id, email1, email2, email3,
                                   states[0], states[1], states[2], 0):
            return 1

        # Disable alarm. Cycle OK, ALARM, UNDETERMINED,
        print('Disable Alarm')
        alarm.disable(mon_client, alarm_id)
        cycle_states(mon_client, alarm_id, states)

        # Ensure no new notifications
        if not check_notifications(alarm_id, email1, email2, email3,
                                   states[0], states[1], states[2], 0):
            return 1

        # Enable alarm. Cycle OK, ALARM, UNDETERMINED
        print('Enable Alarm')
        alarm.enable(mon_client, alarm_id)
        cycle_states(mon_client, alarm_id, states)

        # Ensure proper notifications got written to each
        if not check_notifications(alarm_id, email1, email2, email3,
                                   states[0], states[1], states[2], 1):
            return 1

        # Switch Alarm notifications around. Cycle OK, ALARM, UNDETERMINED,
        print("Switch around Alarm notifications")
        alarm.patch(mon_client, alarm_id,
                    {'ok_actions': [notification_id_2],
                     'alarm_actions': [notification_id_3],
                     'undetermined_actions': [notification_id_1]})
        print_notification_setup(mon_client, alarm_id)
        cycle_states(mon_client, alarm_id, states)

        # Ensure proper notifications got written to each
        if not check_notifications(alarm_id, email2, email3, email1,
                                   states[0], states[1], states[2], 2):
            return 1

        # Switch the email addresses around. Cycle OK, ALARM, UNDETERMINED,
        # Ensure proper notifications got written to each
        return 0
    except exc.HTTPException as he:
        print(he.code)
        print(he.message)
        return 1