This commit is contained in:
Deklan Dieterly 2014-06-11 10:11:27 -06:00
commit bbdd674111
6 changed files with 59 additions and 33 deletions

View File

@ -53,6 +53,7 @@ def main():
# Add Alarm # Add Alarm
alarm_id = cli_wrapper.create_alarm(alarm_name, expression, alarm_id = cli_wrapper.create_alarm(alarm_name, expression,
description=description) description=description)
print('Created Alarm with id %s' % alarm_id)
# Ensure it is created in the right state # Ensure it is created in the right state
initial_state = 'UNDETERMINED' initial_state = 'UNDETERMINED'
@ -82,11 +83,13 @@ def main():
states.append('ALARM') states.append('ALARM')
# Modify Alarm by adding new expression that will cause it to go OK # Modify Alarm by adding new expression that will cause it to go OK
print('Modify Alarm expression so it will go to OK')
new_metric_name = 'other_metric' new_metric_name = 'other_metric'
new_dimension = 'dim=42' new_dimension = 'dim=42'
new_expression = '%s and max(%s{%s}) > 100' % (expression, new_expression = '%s and max(%s{%s}) > 100' % (expression,
new_metric_name, new_metric_name,
new_dimension) new_dimension)
alarm_json = cli_wrapper.patch_alarm(alarm_id, '--expression', alarm_json = cli_wrapper.patch_alarm(alarm_id, '--expression',
new_expression) new_expression)
if alarm_json['expression'] != new_expression: if alarm_json['expression'] != new_expression:
@ -96,6 +99,7 @@ def main():
# Output metrics that will cause it to go OK # Output metrics that will cause it to go OK
# Wait for it to change to OK # Wait for it to change to OK
if not output_metrics(alarm_id, 'OK', [[metric_name, base_dimension], if not output_metrics(alarm_id, 'OK', [[metric_name, base_dimension],
[new_metric_name, new_dimension]]): [new_metric_name, new_dimension]]):
return 1 return 1
@ -103,10 +107,27 @@ def main():
states.append('OK') states.append('OK')
# Modify Alarm by deleting expression that will cause Alarm to go ALARM # Modify Alarm by deleting expression that will cause Alarm to go ALARM
print('Delete Alarm sub expression so it will go to ALARM')
cli_wrapper.patch_alarm(alarm_id, '--expression', expression) cli_wrapper.patch_alarm(alarm_id, '--expression', expression)
# Output metrics that will cause it to go ALARM # Output metrics that will cause it to go ALARM
# Wait for it to change to ALARM # Wait for it to change to ALARM
print('Output extra dimensions to make sure match occurs')
extra_dimension = base_dimension + ',Extra=More'
if not output_metrics(alarm_id, 'ALARM',
[[metric_name, extra_dimension]]):
return 1
states.append('ALARM')
# Modify Alarm by setting alarm state to OK
print('Set Alarm to OK, wait for transition back to ALARM')
cli_wrapper.change_alarm_state(alarm_id, 'OK')
states.append('OK')
# Output metrics that will cause it to go back to ALARM
# Wait for it to change to ALARM
if not output_metrics(alarm_id, 'ALARM', if not output_metrics(alarm_id, 'ALARM',
[[metric_name, base_dimension], [[metric_name, base_dimension],
[new_metric_name, new_dimension]]): [new_metric_name, new_dimension]]):
@ -116,6 +137,7 @@ def main():
# Query History # Query History
# Delete ALARM # Delete ALARM
print('Delete alarm')
cli_wrapper.run_mon_cli(['alarm-delete', alarm_id], useJson=False) cli_wrapper.run_mon_cli(['alarm-delete', alarm_id], useJson=False)
# Ensure it can't be queried # Ensure it can't be queried

View File

@ -3,23 +3,6 @@ from __future__ import print_function
""" """
Utility methods for notifications Utility methods for notifications
""" """
import sys
import json
import subprocess
def find_notifications(alarm_id, user):
args = ['sudo', 'cat', '/var/mail/' + user]
result = []
try:
stdout = subprocess.check_output(args)
except subprocess.CalledProcessError as e:
print(e, file=sys.stderr)
sys.exit(1)
for line in stdout.splitlines():
if alarm_id in line:
result.append(json.loads(line)['state'])
return result
def create(mon_client, name, email): def create(mon_client, name, email):

View File

@ -21,7 +21,7 @@ def cycle_states(mon_client, alarm_id, states):
def check_notification(alarm_id, user, expected_state, existing): def check_notification(alarm_id, user, expected_state, existing):
for i in range(0, 20): for i in range(0, 20):
notifications = notification.find_notifications(alarm_id, user) notifications = utils.find_notifications(alarm_id, user)
if len(notifications) > existing: if len(notifications) > existing:
break break
time.sleep(1) time.sleep(1)

View File

@ -42,7 +42,7 @@ def main():
initial_state = alarm.get_state(mon_client, alarm_id) initial_state = alarm.get_state(mon_client, alarm_id)
state = initial_state state = initial_state
existing_notifications = notification.find_notifications(alarm_id, user) existing_notifications = utils.find_notifications(alarm_id, user)
notifications_sent = num_cycles * 2 notifications_sent = num_cycles * 2
for _ in range(0, notifications_sent): for _ in range(0, notifications_sent):
if state == 'OK': if state == 'OK':
@ -56,7 +56,7 @@ def main():
((time.time() - start_time), num_cycles * 2)) ((time.time() - start_time), num_cycles * 2))
for i in range(0, 30): for i in range(0, 30):
notifications = notification.find_notifications(alarm_id, user) notifications = utils.find_notifications(alarm_id, user)
notifications_found = len(notifications) - len(existing_notifications) notifications_found = len(notifications) - len(existing_notifications)
if notifications_found >= notifications_sent: if notifications_found >= notifications_sent:
break break

View File

@ -20,7 +20,6 @@ import subprocess
import time import time
import cli_wrapper import cli_wrapper
import utils import utils
from notification import find_notifications
# export OS_AUTH_TOKEN=82510970543135 # export OS_AUTH_TOKEN=82510970543135
# export OS_NO_CLIENT_AUTH=1 # export OS_NO_CLIENT_AUTH=1
@ -63,7 +62,7 @@ def check_notifications(alarm_id, state_changes):
' skipping Notifications test', ' skipping Notifications test',
file=sys.stderr) file=sys.stderr)
return True return True
notifications = find_notifications(alarm_id, "root") notifications = utils.find_notifications(alarm_id, "root")
if len(notifications) != len(state_changes): if len(notifications) != len(state_changes):
print('Expected %d notifications but only found %d' % print('Expected %d notifications but only found %d' %
(len(state_changes), len(notifications)), file=sys.stderr) (len(state_changes), len(notifications)), file=sys.stderr)

View File

@ -2,6 +2,8 @@ from __future__ import print_function
import sys import sys
import time import time
import os import os
import json
import subprocess
import cli_wrapper import cli_wrapper
from monclient import client from monclient import client
@ -21,31 +23,37 @@ def check_alarm_history(alarm_id, states):
time.sleep(4) time.sleep(4)
result = True result = True
if not check_expected(transitions, len(result_json), if transitions != len(result_json):
'number of history entries'): print('Wrong number of history entries, expected %d but was %d' %
(transitions, len(result_json)), file=sys.stderr)
return False return False
result_json.sort(key=lambda x: x['timestamp']) # Alarm history is reverse sorted by date
index = transitions - 1
for i in range(0, transitions): for i in range(0, transitions):
old_state = states[i] old_state = states[i]
new_state = states[i+1] new_state = states[i+1]
alarm_json = result_json[i] alarm_json = result_json[index]
if not check_expected(old_state, alarm_json['old_state'], 'old_state'): if not check_expected(old_state, alarm_json['old_state'], 'old_state',
i):
result = False result = False
if not check_expected(new_state, alarm_json['new_state'], 'new_state'): if not check_expected(new_state, alarm_json['new_state'], 'new_state',
i):
result = False result = False
if not check_expected(alarm_id, alarm_json['alarm_id'], 'alarm_id'): if not check_expected(alarm_id, alarm_json['alarm_id'], 'alarm_id',
i):
result = False result = False
index = index - 1
if result: if result:
print('Alarm History is OK') print('Alarm History is OK')
return result return result
def check_expected(expected, actual, what): def check_expected(expected, actual, what, index):
if (expected == actual): if (expected == actual):
return True return True
print("Incorrect value for alarm history %s expected '%s' but was '%s'" % print('Wrong %s for alarm history expected %s but was %s transition %d' %
(what, str(expected), str(actual)), file=sys.stderr) (what, expected, actual, index+1), file=sys.stderr)
return False return False
@ -90,3 +98,17 @@ def ensure_has_notification_engine():
file=sys.stderr) file=sys.stderr)
return False return False
return True return True
def find_notifications(alarm_id, user):
args = ['sudo', 'cat', '/var/mail/' + user]
result = []
try:
stdout = subprocess.check_output(args)
except subprocess.CalledProcessError as e:
print(e, file=sys.stderr)
sys.exit(1)
for line in stdout.splitlines():
if alarm_id in line:
result.append(json.loads(line)['state'])
return result