IPv6 ND Router discovery data plane (VPP-1095)
[vpp.git] / test / hook.py
1 import signal
2 import os
3 import traceback
4 from log import RED, single_line_delim, double_line_delim
5 from debug import spawn_gdb
6
7
8 class Hook(object):
9     """
10     Generic hooks before/after API/CLI calls
11     """
12
13     def __init__(self, logger):
14         self.logger = logger
15
16     def before_api(self, api_name, api_args):
17         """
18         Function called before API call
19         Emit a debug message describing the API name and arguments
20
21         @param api_name: name of the API
22         @param api_args: tuple containing the API arguments
23         """
24         self.logger.debug("API: %s (%s)" %
25                           (api_name, api_args), extra={'color': RED})
26
27     def after_api(self, api_name, api_args):
28         """
29         Function called after API call
30
31         @param api_name: name of the API
32         @param api_args: tuple containing the API arguments
33         """
34         pass
35
36     def before_cli(self, cli):
37         """
38         Function called before CLI call
39         Emit a debug message describing the CLI
40
41         @param cli: CLI string
42         """
43         self.logger.debug("CLI: %s" % (cli), extra={'color': RED})
44
45     def after_cli(self, cli):
46         """
47         Function called after CLI call
48         """
49         pass
50
51
52 class VppDiedError(Exception):
53     pass
54
55
56 class PollHook(Hook):
57     """ Hook which checks if the vpp subprocess is alive """
58
59     def __init__(self, testcase):
60         self.testcase = testcase
61         self.logger = testcase.logger
62
63     def on_crash(self, core_path):
64         if self.testcase.debug_core:
65             # notify parent process that we're handling a core file
66             open('%s/_core_handled' % self.testcase.tempdir, 'a').close()
67             spawn_gdb(self.testcase.vpp_bin, core_path, self.logger)
68         else:
69             self.logger.critical("Core file present, debug with: gdb %s %s" %
70                                  (self.testcase.vpp_bin, core_path))
71
72     def poll_vpp(self):
73         """
74         Poll the vpp status and throw an exception if it's not running
75         :raises VppDiedError: exception if VPP is not running anymore
76         """
77         if self.testcase.vpp_dead:
78             # already dead, nothing to do
79             return
80
81         self.testcase.vpp.poll()
82         if self.testcase.vpp.returncode is not None:
83             signaldict = dict(
84                 (k, v) for v, k in reversed(sorted(signal.__dict__.items()))
85                 if v.startswith('SIG') and not v.startswith('SIG_'))
86
87             if self.testcase.vpp.returncode in signaldict:
88                 s = signaldict[abs(self.testcase.vpp.returncode)]
89             else:
90                 s = "unknown"
91             msg = "VPP subprocess died unexpectedly with returncode %d [%s]" %\
92                 (self.testcase.vpp.returncode, s)
93             self.logger.critical(msg)
94             core_path = self.testcase.tempdir + '/core'
95             if os.path.isfile(core_path):
96                 self.on_crash(core_path)
97             self.testcase.vpp_dead = True
98             raise VppDiedError(msg)
99
100     def before_api(self, api_name, api_args):
101         """
102         Check if VPP died before executing an API
103
104         :param api_name: name of the API
105         :param api_args: tuple containing the API arguments
106         :raises VppDiedError: exception if VPP is not running anymore
107
108         """
109         super(PollHook, self).before_api(api_name, api_args)
110         self.poll_vpp()
111
112     def before_cli(self, cli):
113         """
114         Check if VPP died before executing a CLI
115
116         :param cli: CLI string
117         :raises Exception: exception if VPP is not running anymore
118
119         """
120         super(PollHook, self).before_cli(cli)
121         self.poll_vpp()
122
123
124 class StepHook(PollHook):
125     """ Hook which requires user to press ENTER before doing any API/CLI """
126
127     def __init__(self, testcase):
128         self.skip_stack = None
129         self.skip_num = None
130         self.skip_count = 0
131         super(StepHook, self).__init__(testcase)
132
133     def skip(self):
134         if self.skip_stack is None:
135             return False
136         stack = traceback.extract_stack()
137         counter = 0
138         skip = True
139         for e in stack:
140             if counter > self.skip_num:
141                 break
142             if e[0] != self.skip_stack[counter][0]:
143                 skip = False
144             if e[1] != self.skip_stack[counter][1]:
145                 skip = False
146             counter += 1
147         if skip:
148             self.skip_count += 1
149             return True
150         else:
151             print("%d API/CLI calls skipped in specified stack "
152                   "frame" % self.skip_count)
153             self.skip_count = 0
154             self.skip_stack = None
155             self.skip_num = None
156             return False
157
158     def user_input(self):
159         print('number\tfunction\tfile\tcode')
160         counter = 0
161         stack = traceback.extract_stack()
162         for e in stack:
163             print('%02d.\t%s\t%s:%d\t[%s]' % (counter, e[2], e[0], e[1], e[3]))
164             counter += 1
165         print(single_line_delim)
166         print("You can enter a number of stack frame chosen from above")
167         print("Calls in/below that stack frame will be not be stepped anymore")
168         print(single_line_delim)
169         while True:
170             choice = raw_input("Enter your choice, if any, and press ENTER to "
171                                "continue running the testcase...")
172             if choice == "":
173                 choice = None
174             try:
175                 if choice is not None:
176                     num = int(choice)
177             except:
178                 print("Invalid input")
179                 continue
180             if choice is not None and (num < 0 or num >= len(stack)):
181                 print("Invalid choice")
182                 continue
183             break
184         if choice is not None:
185             self.skip_stack = stack
186             self.skip_num = num
187
188     def before_cli(self, cli):
189         """ Wait for ENTER before executing CLI """
190         if self.skip():
191             print("Skip pause before executing CLI: %s" % cli)
192         else:
193             print(double_line_delim)
194             print("Test paused before executing CLI: %s" % cli)
195             print(single_line_delim)
196             self.user_input()
197         super(StepHook, self).before_cli(cli)
198
199     def before_api(self, api_name, api_args):
200         """ Wait for ENTER before executing API """
201         if self.skip():
202             print("Skip pause before executing API: %s (%s)"
203                   % (api_name, api_args))
204         else:
205             print(double_line_delim)
206             print("Test paused before executing API: %s (%s)"
207                   % (api_name, api_args))
208             print(single_line_delim)
209             self.user_input()
210         super(StepHook, self).before_api(api_name, api_args)