]> git.cworth.org Git - apitrace-tests/blob - cli_driver.py
cli: Don't rely on image generation for diff-image tests.
[apitrace-tests] / cli_driver.py
1 #!/usr/bin/env python
2
3 # Copyright 2012 Intel Corporation
4 #
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation files
7 # (the "Software"), to deal in the Software without restriction,
8 # including without limitation the rights to use, copy, modify, merge,
9 # publish, distribute, sublicense, and/or sell copies of the Software,
10 # and to permit persons to whom the Software is furnished to do so,
11 # subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 '''Test driver for scripts in the cli directory.'''
26
27 import json, errno, shutil, subprocess
28
29 from base_driver import *
30
31 class CliDriver(Driver):
32
33     def do_apitrace(self, args):
34         cmd = [self.options.apitrace] + args.split()
35  
36         print " ".join(cmd)
37         proc = subprocess.Popen(cmd, stdout = subprocess.PIPE)
38         self.output = proc.communicate()[0]
39
40         proc.wait()
41
42         if (self.expect_failure):
43             if (proc.returncode == 0):
44                 fail("Command unexpectedly passed when expecting failure:\n    " + " ".join(cmd))
45         else:
46             if (proc.returncode != 0):
47                 fail("Command failed (returned non-zero):\n    " + " ".join(cmd))
48
49     def do_expect(self, args):
50         expected = json.loads(args)
51         if (self.output != expected):
52             fail("Unexpected output:\n    Expected: %s\n    Received: %s\n" % (expected, self.output))
53
54     def do_rm_and_mkdir(self, args):
55
56         args = args.split()
57
58         # Operate only on local directories
59         dir = './' + args[0]
60
61         # Failing to delete a directory that doesn't exist is no failure
62         def rmtree_onerror(function, path, excinfo):
63             if excinfo[0] == OSError and excinfo[1].errno != errno.ENOENT:
64                 raise
65
66         shutil.rmtree(dir, onerror = rmtree_onerror)
67         os.makedirs(dir)
68
69     def unknown_command(self, args):
70         fail('Broken test script: Unknown command: %s' % (args))
71
72     def run_script(self, cli_script):
73         "Execute the commands in the given cli script."
74
75         commands = {
76             'apitrace': self.do_apitrace,
77             'expect': self.do_expect,
78             'rm_and_mkdir': self.do_rm_and_mkdir
79         }
80
81         script = open(cli_script, 'rt')
82
83         while True:
84
85             self.expect_failure = False
86
87             line = script.readline()
88
89             # Exit loop on EOF
90             if (line == ''):
91                 break
92
93             line = line.rstrip()
94
95             if " " in line:
96                 (cmd, args) = line.split(None,1)
97             else:
98                 cmd = line
99                 args = ''
100
101             # Ignore blank lines and comments
102             if (len(cmd) == 0 or cmd == '\n' or cmd[0] == '#'):
103                 continue
104
105             if (cmd == 'EXPECT_FAILURE:'):
106                 self.expect_failure = True
107                 if " " in args:
108                     (cmd, args) = args.split(None, 1)
109                 else:
110                     cmd = args
111                     args = ''
112
113             commands.get(cmd, self.unknown_command)(args)
114
115     def run(self):
116         self.parseOptions()
117
118         self.run_script(self.args[0])
119
120         pass_()
121
122 if __name__ == '__main__':
123     CliDriver().run()