12 class Fail(Exception):
13 def __init__(self
, test
, msg
):
17 return '\'%s\' - %s' % (self
.test
.path
, self
.msg
)
19 class Unsup(Exception):
20 def __init__(self
, test
):
23 return '\'%s\'' % self
.test
.path
55 'exclude_callchain_kernel',
56 'exclude_callchain_user',
68 log
.debug(" %s = %s" % (key
, val
))
71 def __init__(self
, name
, data
, base
):
72 log
.debug(" Event %s" % name
);
78 def compare_data(self
, a
, b
):
79 # Allow multiple values in assignment separated by '|'
85 if (a_item
== b_item
):
87 elif (a_item
== '*') or (b_item
== '*'):
92 def equal(self
, other
):
94 log
.debug(" [%s] %s %s" % (t
, self
[t
], other
[t
]));
95 if not self
.has_key(t
) or not other
.has_key(t
):
97 if not self
.compare_data(self
[t
], other
[t
]):
101 def diff(self
, other
):
102 for t
in Event
.terms
:
103 if not self
.has_key(t
) or not other
.has_key(t
):
105 if not self
.compare_data(self
[t
], other
[t
]):
106 log
.warning("expected %s=%s, got %s" % (t
, self
[t
], other
[t
]))
108 # Test file description needs to have following sections:
110 # - just single instance in file
111 # - needs to specify:
112 # 'command' - perf command name
113 # 'args' - special command arguments
114 # 'ret' - expected command return value (0 by default)
117 # - one or multiple instances in file
118 # - expected values assignments
120 def __init__(self
, path
, options
):
121 parser
= ConfigParser
.SafeConfigParser()
124 log
.warning("running '%s'" % path
)
127 self
.test_dir
= options
.test_dir
128 self
.perf
= options
.perf
129 self
.command
= parser
.get('config', 'command')
130 self
.args
= parser
.get('config', 'args')
133 self
.ret
= parser
.get('config', 'ret')
139 log
.debug(" loading expected events");
140 self
.load_events(path
, self
.expect
)
142 def is_event(self
, name
):
143 if name
.find("event") == -1:
148 def load_events(self
, path
, events
):
149 parser_event
= ConfigParser
.SafeConfigParser()
150 parser_event
.read(path
)
152 # The event record section header contains 'event' word,
153 # optionaly followed by ':' allowing to load 'parent
154 # event' first as a base
155 for section
in filter(self
.is_event
, parser_event
.sections()):
157 parser_items
= parser_event
.items(section
);
160 # Read parent event if there's any
162 base
= section
[section
.index(':') + 1:]
163 parser_base
= ConfigParser
.SafeConfigParser()
164 parser_base
.read(self
.test_dir
+ '/' + base
)
165 base_items
= parser_base
.items('event')
167 e
= Event(section
, parser_items
, base_items
)
170 def run_cmd(self
, tempdir
):
171 cmd
= "PERF_TEST_ATTR=%s %s %s -o %s/perf.data %s" % (tempdir
,
172 self
.perf
, self
.command
, tempdir
, self
.args
)
173 ret
= os
.WEXITSTATUS(os
.system(cmd
))
175 log
.info(" '%s' ret %d " % (cmd
, ret
))
177 if ret
!= int(self
.ret
):
180 def compare(self
, expect
, result
):
183 log
.debug(" compare");
185 # For each expected event find all matching
186 # events in result. Fail if there's not any.
187 for exp_name
, exp_event
in expect
.items():
189 log
.debug(" matching [%s]" % exp_name
)
190 for res_name
, res_event
in result
.items():
191 log
.debug(" to [%s]" % res_name
)
192 if (exp_event
.equal(res_event
)):
193 exp_list
.append(res_name
)
196 log
.debug(" ->FAIL");
198 log
.debug(" match: [%s] matches %s" % (exp_name
, str(exp_list
)))
200 # we did not any matching event - fail
202 exp_event
.diff(res_event
)
203 raise Fail(self
, 'match failure');
205 match
[exp_name
] = exp_list
207 # For each defined group in the expected events
208 # check we match the same group in the result.
209 for exp_name
, exp_event
in expect
.items():
210 group
= exp_event
.group
215 for res_name
in match
[exp_name
]:
216 res_group
= result
[res_name
].group
217 if res_group
not in match
[group
]:
218 raise Fail(self
, 'group failure')
220 log
.debug(" group: [%s] matches group leader %s" %
221 (exp_name
, str(match
[group
])))
223 log
.debug(" matched")
225 def resolve_groups(self
, events
):
226 for name
, event
in events
.items():
227 group_fd
= event
['group_fd'];
231 for iname
, ievent
in events
.items():
232 if (ievent
['fd'] == group_fd
):
234 log
.debug('[%s] has group leader [%s]' % (name
, iname
))
238 tempdir
= tempfile
.mkdtemp();
241 # run the test script
242 self
.run_cmd(tempdir
);
244 # load events expectation for the test
245 log
.debug(" loading result events");
246 for f
in glob
.glob(tempdir
+ '/event*'):
247 self
.load_events(f
, self
.result
);
249 # resolve group_fd to event names
250 self
.resolve_groups(self
.expect
);
251 self
.resolve_groups(self
.result
);
253 # do the expectation - results matching - both ways
254 self
.compare(self
.expect
, self
.result
)
255 self
.compare(self
.result
, self
.expect
)
259 shutil
.rmtree(tempdir
)
262 def run_tests(options
):
263 for f
in glob
.glob(options
.test_dir
+ '/' + options
.test
):
265 Test(f
, options
).run()
267 log
.warning("unsupp %s" % obj
.getMsg())
269 def setup_log(verbose
):
271 level
= logging
.CRITICAL
274 level
= logging
.WARNING
278 level
= logging
.DEBUG
280 log
= logging
.getLogger('test')
282 ch
= logging
.StreamHandler()
284 formatter
= logging
.Formatter('%(message)s')
285 ch
.setFormatter(formatter
)
288 USAGE
= '''%s [OPTIONS]
290 -p path # perf binary
291 -t test # single test
296 parser
= optparse
.OptionParser(usage
=USAGE
)
298 parser
.add_option("-t", "--test",
299 action
="store", type="string", dest
="test")
300 parser
.add_option("-d", "--test-dir",
301 action
="store", type="string", dest
="test_dir")
302 parser
.add_option("-p", "--perf",
303 action
="store", type="string", dest
="perf")
304 parser
.add_option("-v", "--verbose",
305 action
="count", dest
="verbose")
307 options
, args
= parser
.parse_args()
309 parser
.error('FAILED wrong arguments %s' % ' '.join(args
))
312 setup_log(options
.verbose
)
314 if not options
.test_dir
:
315 print 'FAILED no -d option specified'
319 options
.test
= 'test*'
325 print "FAILED %s" % obj
.getMsg();
330 if __name__
== '__main__':