-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests_simulate
executable file
·139 lines (114 loc) · 3.16 KB
/
tests_simulate
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby
require 'stringio'
$LOAD_PATH.unshift('lib')
require 'einarc/baseraid'
require 'einarc/meta'
# Throw in fake EINARC_LIB, as we'll mock up RAID adapter CLI calls anyway
Einarc::EINARC_LIB = ''
include Einarc
# Load all available RAID adapters
MODULES.each_pair { |k, v|
require "einarc/#{k}"
v[:class] = Kernel.const_get(v[:classname])
}
class LSIMegaCli
def self.run(x)
$test_runner.simulate_cli(x)
end
def run(x)
$test_runner.simulate_cli(x)
end
end
class LSIMegaRc
def self.megarc(command)
out = $test_runner.simulate_cli(command)
out.slice!(0..10)
return out
end
def megarc(command)
out = $test_runner.simulate_cli(command)
out.slice!(0..9)
out.delete_if { |l| l =~ /^Finding Devices On / or l =~ /Scanning Ha / or l =~ /^\*\*\*\*\*\*/ or l.nil? }
return out
end
end
class AssertionError < Exception
end
class TestRunner
attr_reader :current_test
attr_reader :adapter
def initialize(test_dir)
@test_dir = test_dir
@count = {
:test_run => 0,
:test_fail => 0,
:test_err => 0,
:test_success => 0,
}
end
def run
Dir.new(@test_dir).each { |adapter_name|
next if adapter_name[0..0] == '.'
raise "Test for unsupported adapter encountered: #{adapter_name}" unless MODULES[adapter_name]
puts "Adapter #{adapter_name}"
@adapter_class = MODULES[adapter_name][:class]
@adapter = @adapter_class.new(0)
@adapter.outstream = StringIO.new
# Fake sysfs path
@adapter.sysfs = "#{@test_dir}/#{adapter_name}/sysfs"
begin
Dir.new("#{@test_dir}/#{adapter_name}").sort.each { |test_name|
next if test_name[0..0] == '.' or test_name == 'sysfs'
@cli_query = 0
@count[:test_run] += 1
@current_test = "#{@test_dir}/#{adapter_name}/#{test_name}"
run_test
@count[:test_success] += 1
print '.'
$stdout.flush
}
puts
rescue AssertionError => e
puts 'F'
$stderr.puts "Assertion failed for test \"#{@current_test}\", CLI query = #{@cli_query}"
$stderr.puts e
$stderr.puts e.backtrace
@count[:test_fail] += 1
rescue => e
puts 'E'
$stderr.puts "Test error for test \"#{@current_test}\", CLI query = #{@cli_query}"
$stderr.puts e
$stderr.puts e.backtrace
@count[:test_err] += 1
end
}
p @count
end
def run_test
cmd = File.open("#{current_test}/command.txt").read.strip
result = File.open("#{current_test}/result.txt").read
if cmd == 'query'
r = []
@adapter_class::query(r)
assert_equal(r, eval(result))
else
assert_equal(@adapter.handle_method(cmd.split(/\s+/)), eval(result))
end
end
def simulate_cli(cmd)
@cli_query += 1
cli_input = File.open("#{$test_runner.current_test}/cli_input_#{@cli_query}.txt").read.strip
$test_runner.assert_equal(cmd, cli_input)
cli_output = File.open("#{$test_runner.current_test}/cli_output_#{@cli_query}.txt").readlines.map { |x| x.strip }
return cli_output
end
def assert_equal(real, expected)
raise AssertionError.new("got #{real.inspect}, but #{expected.inspect} was expected") unless real === expected
end
def failed?
@count[:test_err] > 0 or @count[:test_fail] > 0
end
end
$test_runner = TestRunner.new('test')
$test_runner.run
exit $test_runner.failed? ? 1 : 0