forked from facebookarchive/ocpjbod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testjson.py
executable file
·110 lines (94 loc) · 2.99 KB
/
testjson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
## Copyright (c) 2016-present, Facebook, Inc.
## All rights reserved.
##
## This source code is licensed under the BSD-style license found in the
## LICENSE file in the root directory of this source tree. An additional grant
## of patent rights can be found in the PATENTS file in the same directory.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import commands
import json
import sys
# cmd, parameter, whether test non-JSON
all_tests = [
['list', '', True],
['list', '--detail', True],
['hdd', '--all', True],
['hdd', '%s', True],
['info', '%s', True],
['sensor', '%s', True],
['sensor', '%s --thresholds', False],
['led', '%s', True],
['fan', '%s', True],
['gpio', '%s', True],
['tag', '%s', True],
['event', '--log %s', False],
['event', '--status %s', True],
['phyerr', '%s', True],
['config', '%s', True],
['version', '', True],
]
def find_device(binary):
ret = []
fullcmd = '%s list 2> /dev/null' % (binary)
output = commands.getoutput(fullcmd)
for line in output.split('\n'):
device = line.split()[0]
if device.startswith('/dev/sg'):
ret.append(device)
print('Testing on %s\n' % str(ret))
return ret
def testJSONCommand(binary, cmd, parameters):
fullcmd = '%s %s --json %s 2>/dev/null' % (binary, cmd, parameters)
output = commands.getoutput(fullcmd)
try:
json_output = json.loads(output)
except:
print('Fail JSON: %s' % fullcmd)
print(output)
return False
print('Pass JSON: %s' % (fullcmd))
return True
def testNoneJSONCommand(binary, cmd, parameters):
fullcmd = '%s %s %s 2>/dev/null' % (binary, cmd, parameters)
output = commands.getoutput(fullcmd)
unexpected = ['{', '}', ',']
for c in unexpected:
try:
if c in output:
print('Fail None JSON: %s' % fullcmd)
return False
except:
print('Expection in testNoneJSONCommand')
print('Pass None JSON: %s' % (fullcmd))
return True
def usage():
print('Usage:')
print('\t%s <ocpjbod_bin>')
def main():
if len(sys.argv) != 2:
usage()
binary = sys.argv[1]
devices = find_device(binary)
if len(devices) == 0:
print('Cannot find a valid JBOD.')
return
total_test = 0
passed_test = 0
for device in devices:
for test in all_tests:
cmd = test[0]
parameters = test[1]
testNoneJSON = test[2]
if '%s' in parameters:
parameters = parameters % device
total_test += 1
if testJSONCommand(binary, cmd, parameters):
if not testNoneJSON or \
testNoneJSONCommand(binary, cmd, parameters):
passed_test += 1
print('\nPassed %.2f %% (%d of %d) tests.' % (
100 * passed_test / total_test, passed_test, total_test))
main()