Skip to content

Commit

Permalink
run_test.py: Implement --no-execute-watch needed for deployment
Browse files Browse the repository at this point in the history
Needed for: elastic/elasticsearch#30112 (comment)

> I already have a workaround in place for this which consists of automatically deploying as many watches as I need to send different mails. Those watches are derived from my watch definition. For this, I extended [run_test.py](https://github.com/elastic/examples/blob/master/Alerting/Sample%20Watches/run_test.py) to inject Python code after the watch definition is read. Not ideal, but it is maintainable.
  • Loading branch information
ypid-geberit committed Jun 25, 2020
1 parent b45ba77 commit fa043dd
Showing 1 changed file with 69 additions and 63 deletions.
132 changes: 69 additions & 63 deletions Alerting/Sample Watches/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def load_file(serialized_file):
parser.add_argument('--protocol', help='protocol')
parser.add_argument('--test_file', help='test file')
parser.add_argument('--keep-index', help='Keep the index where test documents have been loaded to after the test', action='store_true')
parser.add_argument(
'--no-execute-watch',
help='Do not force watch execution. This can be useful when you use this script to deploy the watch.',
action='store_false',
dest='execute_watch')

parser.set_defaults(host='localhost', port="9200", protocol="http", test_file='data.json', user='elastic', password='changeme')
args = parser.parse_args()
Expand Down Expand Up @@ -71,73 +76,74 @@ def load_file(serialized_file):
for script in test['scripts']:
es.put_script(id=script["name"], body=load_file(script['path']))


# Load Watch and Execute
watch = load_file(test['watch_file'])

watcher = XPackClient(es).watcher
watcher.put_watch(id=test["watch_name"], body=watch)

response = watcher.execute_watch(test["watch_name"])

# Cleanup after the test to not pollute the environment for other tests.
if not args.keep_index:
try:
es.indices.delete(test['index'])
except Exception as err:
print("Unable to delete current dataset")
pass

# Confirm Matches
match = test['match'] if 'match' in test else True
print("Expected: Watch Condition: %s" % match)
if 'condition' not in response['watch_record']['result']:
print("Condition not evaluated due to watch error: {}".format(
json.dumps(response['watch_record']['result'], indent=2)
))
print("TEST FAIL")
sys.exit(1)
met = response['watch_record']['result']['condition']['met']
print("Received: Watch Condition: %s" % met)
if match:
if met and response['watch_record']['result']['condition']['status'] == "success":
print("Expected: %s" % test['expected_response'])
if len(response['watch_record']['result']['actions']) == 0:
if response['watch_record']['result']['transform']['status'] == 'failure':
print("No actions where taken because transform failed: {}".format(
response['watch_record']['result']['transform']['reason']
if args.execute_watch:
response = watcher.execute_watch(test["watch_name"])

# Cleanup after the test to not pollute the environment for other tests.
if not args.keep_index:
try:
es.indices.delete(test['index'])
except Exception as err:
print("Unable to delete current dataset")
pass

# Confirm Matches
match = test['match'] if 'match' in test else True
print("Expected: Watch Condition: %s" % match)
if 'condition' not in response['watch_record']['result']:
print("Condition not evaluated due to watch error: {}".format(
json.dumps(response['watch_record']['result'], indent=2)
))
print("TEST FAIL")
sys.exit(1)
met = response['watch_record']['result']['condition']['met']
print("Received: Watch Condition: %s" % met)
if match:
if met and response['watch_record']['result']['condition']['status'] == "success":
print("Expected: %s" % test['expected_response'])
if len(response['watch_record']['result']['actions']) == 0:
if response['watch_record']['result']['transform']['status'] == 'failure':
print("No actions where taken because transform failed: {}".format(
response['watch_record']['result']['transform']['reason']
))
else:
print("No actions where taken: {}".format(
json.dumps(response['watch_record']['result'], indent=2)
))
print("TEST FAIL")
sys.exit(1)

logging_action = next((action for action in response['watch_record']['result']['actions'] if action["type"] == "logging"), None)
if logging_action is None:
print("No logging actions was taken. This test framework uses the logging action for comparison so you might need enable this action.")
print("TEST FAIL")
sys.exit(1)
if logging_action.get('transform', {}).get('status', 'success') != 'success':
print("Logging transform script failed: {}".format(
logging_action.get('transform', {}).get('reason', 'unknown'),
))
print("TEST FAIL")
sys.exit(1)
if 'logging' not in logging_action:
print("Logging action is not present: {}".format(logging_action))
print("TEST FAIL")
sys.exit(1)
logging = logging_action['logging']
if logging:
print("Received: %s" % logging['logged_text'])
if logging['logged_text'] == test['expected_response']:
print("TEST PASS")
sys.exit(0)
else:
print("No actions where taken: {}".format(
json.dumps(response['watch_record']['result'], indent=2)
))
print("TEST FAIL")
sys.exit(1)

logging_action = next((action for action in response['watch_record']['result']['actions'] if action["type"] == "logging"), None)
if logging_action is None:
print("No logging actions was taken. This test framework uses the logging action for comparison so you might need enable this action.")
print("TEST FAIL")
sys.exit(1)
if logging_action.get('transform', {}).get('status', 'success') != 'success':
print("Logging transform script failed: {}".format(
logging_action.get('transform', {}).get('reason', 'unknown'),
))
print("TEST FAIL")
sys.exit(1)
if 'logging' not in logging_action:
print("Logging action is not present: {}".format(logging_action))
print("TEST FAIL")
sys.exit(1)
logging = logging_action['logging']
if logging:
print("Received: %s" % logging['logged_text'])
if logging['logged_text'] == test['expected_response']:
print("TEST PASS")
sys.exit(0)
else:
print("Logging action required for testing")
print("TEST FAIL")
sys.exit(1)
else:
print("TEST %s" % ("PASS" if not response['watch_record']['result']['condition']['met'] else "FAIL"))
sys.exit(met)
print("Logging action required for testing")
print("TEST FAIL")
sys.exit(1)
else:
print("TEST %s" % ("PASS" if not response['watch_record']['result']['condition']['met'] else "FAIL"))
sys.exit(met)

0 comments on commit fa043dd

Please sign in to comment.