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
]))
109 # Test file description needs to have following sections:
111 # - just single instance in file
112 # - needs to specify:
113 # 'command' - perf command name
114 # 'args' - special command arguments
115 # 'ret' - expected command return value (0 by default)
118 # - one or multiple instances in file
119 # - expected values assignments
121 def __init__(self
, path
, options
):
122 parser
= ConfigParser
.SafeConfigParser()
125 log
.warning("running '%s'" % path
)
128 self
.test_dir
= options
.test_dir
129 self
.perf
= options
.perf
130 self
.command
= parser
.get('config', 'command')
131 self
.args
= parser
.get('config', 'args')
134 self
.ret
= parser
.get('config', 'ret')
140 log
.debug(" loading expected events");
141 self
.load_events(path
, self
.expect
)
143 def is_event(self
, name
):
144 if name
.find("event") == -1:
149 def load_events(self
, path
, events
):
150 parser_event
= ConfigParser
.SafeConfigParser()
151 parser_event
.read(path
)
153 # The event record section header contains 'event' word,
154 # optionaly followed by ':' allowing to load 'parent
155 # event' first as a base
156 for section
in filter(self
.is_event
, parser_event
.sections()):
158 parser_items
= parser_event
.items(section
);
161 # Read parent event if there's any
163 base
= section
[section
.index(':') + 1:]
164 parser_base
= ConfigParser
.SafeConfigParser()
165 parser_base
.read(self
.test_dir
+ '/' + base
)
166 base_items
= parser_base
.items('event')
168 e
= Event(section
, parser_items
, base_items
)
171 def run_cmd(self
, tempdir
):
172 cmd
= "PERF_TEST_ATTR=%s %s %s -o %s/perf.data %s" % (tempdir
,
173 self
.perf
, self
.command
, tempdir
, self
.args
)
174 ret
= os
.WEXITSTATUS(os
.system(cmd
))
176 log
.info(" '%s' ret %d " % (cmd
, ret
))
178 if ret
!= int(self
.ret
):
181 def compare(self
, expect
, result
):
184 log
.debug(" compare");
186 # For each expected event find all matching
187 # events in result. Fail if there's not any.
188 for exp_name
, exp_event
in expect
.items():
190 log
.debug(" matching [%s]" % exp_name
)
191 for res_name
, res_event
in result
.items():
192 log
.debug(" to [%s]" % res_name
)
193 if (exp_event
.equal(res_event
)):
194 exp_list
.append(res_name
)
197 log
.debug(" ->FAIL");
199 log
.debug(" match: [%s] matches %s" % (exp_name
, str(exp_list
)))
201 # we did not any matching event - fail
203 exp_event
.diff(res_event
)
204 raise Fail(self
, 'match failure');
206 match
[exp_name
] = exp_list
208 # For each defined group in the expected events
209 # check we match the same group in the result.
210 for exp_name
, exp_event
in expect
.items():
211 group
= exp_event
.group
216 for res_name
in match
[exp_name
]:
217 res_group
= result
[res_name
].group
218 if res_group
not in match
[group
]:
219 raise Fail(self
, 'group failure')
221 log
.debug(" group: [%s] matches group leader %s" %
222 (exp_name
, str(match
[group
])))
224 log
.debug(" matched")
226 def resolve_groups(self
, events
):
227 for name
, event
in events
.items():
228 group_fd
= event
['group_fd'];
232 for iname
, ievent
in events
.items():
233 if (ievent
['fd'] == group_fd
):
235 log
.debug('[%s] has group leader [%s]' % (name
, iname
))
239 tempdir
= tempfile
.mkdtemp();
242 # run the test script
243 self
.run_cmd(tempdir
);
245 # load events expectation for the test
246 log
.debug(" loading result events");
247 for f
in glob
.glob(tempdir
+ '/event*'):
248 self
.load_events(f
, self
.result
);
250 # resolve group_fd to event names
251 self
.resolve_groups(self
.expect
);
252 self
.resolve_groups(self
.result
);
254 # do the expectation - results matching - both ways
255 self
.compare(self
.expect
, self
.result
)
256 self
.compare(self
.result
, self
.expect
)
260 shutil
.rmtree(tempdir
)
263 def run_tests(options
):
264 for f
in glob
.glob(options
.test_dir
+ '/' + options
.test
):
266 Test(f
, options
).run()
268 log
.warning("unsupp %s" % obj
.getMsg())
270 def setup_log(verbose
):
272 level
= logging
.CRITICAL
275 level
= logging
.WARNING
279 level
= logging
.DEBUG
281 log
= logging
.getLogger('test')
283 ch
= logging
.StreamHandler()
285 formatter
= logging
.Formatter('%(message)s')
286 ch
.setFormatter(formatter
)
289 USAGE
= '''%s [OPTIONS]
291 -p path # perf binary
292 -t test # single test
297 parser
= optparse
.OptionParser(usage
=USAGE
)
299 parser
.add_option("-t", "--test",
300 action
="store", type="string", dest
="test")
301 parser
.add_option("-d", "--test-dir",
302 action
="store", type="string", dest
="test_dir")
303 parser
.add_option("-p", "--perf",
304 action
="store", type="string", dest
="perf")
305 parser
.add_option("-v", "--verbose",
306 action
="count", dest
="verbose")
308 options
, args
= parser
.parse_args()
310 parser
.error('FAILED wrong arguments %s' % ' '.join(args
))
313 setup_log(options
.verbose
)
315 if not options
.test_dir
:
316 print 'FAILED no -d option specified'
320 options
.test
= 'test*'
326 print "FAILED %s" % obj
.getMsg();
331 if __name__
== '__main__':