refactor failure inject
[doctor.git] / tests / scenario / common.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import os
10 import re
11 import sys
12
13
14 def match_rep_in_file(regex, full_path):
15     if not os.path.isfile(full_path):
16         raise Exception('File(%s) does not exist' % full_path)
17
18     with open(full_path, 'r') as file:
19         for line in file:
20             result = re.search(regex, line)
21             if result:
22                 return result.group(0)
23
24     return None
25
26
27 def calculate_notification_time():
28     log_file = '{0}/{1}'.format(sys.path[0], 'doctor.log')
29
30     reg = '(?<=doctor monitor detected at )\d+.\d+'
31     detected = match_rep_in_file(reg, log_file)
32     if not detected:
33         raise Exception('Can not find detected time')
34
35     reg = '(?<=doctor consumer notified at )\d+.\d+'
36     notified = match_rep_in_file(reg, log_file)
37     if not notified:
38         raise Exception('Can not find notified time')
39
40     return float(notified) - float(detected)