t/readonly: replace shell script with python script
[fio.git] / t / readonly.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-only
3 #
4 # Copyright (c) 2019 Western Digital Corporation or its affiliates.
5 #
6 #
7 # readonly.py
8 #
9 # Do some basic tests of the --readonly paramter
10 #
11 # USAGE
12 # python readonly.py [-f fio-executable]
13 #
14 # EXAMPLES
15 # python t/readonly.py
16 # python t/readonly.py -f ./fio
17 #
18 # REQUIREMENTS
19 # Python 3.5+
20 #
21 #
22
23 import sys
24 import argparse
25 import subprocess
26
27
28 def parse_args():
29     parser = argparse.ArgumentParser()
30     parser.add_argument('-f', '--fio',
31                         help='path to fio executable (e.g., ./fio)')
32     args = parser.parse_args()
33
34     return args
35
36
37 def run_fio(fio, test, index):
38     fio_args = [
39                 "--name=readonly",
40                 "--ioengine=null",
41                 "--time_based",
42                 "--runtime=1s",
43                 "--size=1M",
44                 "--rw={rw}".format(**test),
45                ]
46     if 'readonly-pre' in test:
47         fio_args.insert(0, "--readonly")
48     if 'readonly-post' in test:
49         fio_args.append("--readonly")
50
51     output = subprocess.run([fio] + fio_args, stdout=subprocess.PIPE,
52                             stderr=subprocess.PIPE)
53
54     return output
55
56
57 def check_output(output, test):
58     expect_error = False
59     if 'readonly-pre' in test or 'readonly-post' in test:
60         if 'write' in test['rw'] or 'trim' in test['rw']:
61             expect_error = True
62
63 #    print(output.stdout)
64 #    print(output.stderr)
65
66     if output.returncode == 0:
67         if expect_error:
68             return False
69         else:
70             return True
71     else:
72         if expect_error:
73             return True
74         else:
75             return False
76
77
78 if __name__ == '__main__':
79     args = parse_args()
80
81     tests = [
82                 {
83                     "rw": "randread",
84                     "readonly-pre": 1,
85                 },
86                 {
87                     "rw": "randwrite",
88                     "readonly-pre": 1,
89                 },
90                 {
91                     "rw": "randtrim",
92                     "readonly-pre": 1,
93                 },
94                 {
95                     "rw": "randread",
96                     "readonly-post": 1,
97                 },
98                 {
99                     "rw": "randwrite",
100                     "readonly-post": 1,
101                 },
102                 {
103                     "rw": "randtrim",
104                     "readonly-post": 1,
105                 },
106                 {
107                     "rw": "randread",
108                 },
109                 {
110                     "rw": "randwrite",
111                 },
112                 {
113                     "rw": "randtrim",
114                 },
115             ]
116
117     index = 1
118     passed = 0
119     failed = 0
120
121     if args.fio:
122         fio_path = args.fio
123     else:
124         fio_path = 'fio'
125
126     for test in tests:
127         output = run_fio(fio_path, test, index)
128         status = check_output(output, test)
129         print("Test {0} {1}".format(index, ("PASSED" if status else "FAILED")))
130         if status:
131             passed = passed + 1
132         else:
133             failed = failed + 1
134         index = index + 1
135
136     print("{0} tests passed, {1} failed".format(passed, failed))
137
138     sys.exit(failed)