-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_command_runner.rb
186 lines (155 loc) · 6.15 KB
/
test_command_runner.rb
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'test/unit'
require 'command_runner'
class TestCommandRunner < Test::Unit::TestCase
def test_shell_echo
result = CommandRunner.run('echo hello')
assert_equal "hello\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_shell_stderr_echo
# Verify that we merge stderr and stdout by default
result = CommandRunner.run('echo hello 1>&2')
assert_equal "hello\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_shell_stderr_null_echo
result = CommandRunner.run('echo stderr 1>&2 && echo stdout', options: {:err => "/dev/null"})
assert_equal "stdout\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_shell_echo_environment_variable
result = CommandRunner.run('echo hello $MESSAGE', environment: {'MESSAGE' => 'world'})
assert_equal "hello world\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_no_shell_echo
result = CommandRunner.run(['echo', 'hello'])
assert_equal "hello\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_utf8_shell_echo
result = CommandRunner.run(['echo', 'hellò'], encoding: :safe)
assert_equal "hellò\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_utf8_split_err
result = CommandRunner.run('echo ÒUT ; echo ÊRR 1>&2', split_stderr: true, encoding: :safe)
assert_equal "ÒUT\n", result[:out]
assert_equal "ÊRR\n", result[:err]
end
def test_shell_echo_sleep_timeout
result = CommandRunner.run('echo hello && sleep 5', timeout: 2)
assert_equal "hello\n", result[:out]
assert_equal 9, result[:status].termsig
end
def test_shell_echo_sleep_timeout_term
result = CommandRunner.run('echo hello && sleep 5', {:timeout => {2 => 'TERM'}})
assert_equal "hello\n", result[:out]
assert_equal Signal.list['TERM'], result[:status].termsig
end
def test_shell_timeout_before_echo
result = CommandRunner.run('sleep 5; echo hello', {:timeout => {2 => 'TERM'}})
assert_equal "", result[:out]
assert_equal Signal.list['TERM'], result[:status].termsig
end
def test_no_shell_timeout_procs_in_order
proc_callbacks = []
result = CommandRunner.run(['sleep', '10'], {:timeout => {
6 => Proc.new { proc_callbacks << 'proc6' },
1 => Proc.new { proc_callbacks << 'proc1' },
3 => Proc.new { proc_callbacks << 'proc3' },
5 => 'KILL',
2 => Proc.new { proc_callbacks << 'proc2' },
}})
assert_equal "", result[:out]
assert_equal 9, result[:status].termsig
assert_equal ['proc1', 'proc2', 'proc3'], proc_callbacks
end
def test_no_shell_raising_action
proc_callbacks = []
begin
CommandRunner.run("echo hello; sleep 10; echo world", {:timeout => {
1 => Proc.new { proc_callbacks << 'proc1' },
3 => Proc.new { proc_callbacks << 'proc3' },
2 => Proc.new { raise "KillMeNow" },
}})
fail "Previous command should raise"
rescue => e
# Expected
assert_equal "KillMeNow", e.message
end
end
def test_shell_no_stdout_preemption
result = CommandRunner.run('echo hello; sleep 2; echo world; sleep 2; echo OMG',
{:timeout => {
1 => Proc.new {|pid| },
3 => 'KILL'
}})
assert_equal "hello\nworld\n", result[:out]
assert_equal 9, result[:status].termsig
end
def test_shell_with_background_subshell
result = CommandRunner.run('echo hello && (sleep 10 && echo world) &',
{:timeout => 2})
assert_equal "hello\n", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_shell_stdout_to_null
result = CommandRunner.run('echo hello > /dev/null',
{:timeout => 2})
assert_equal "", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_shell_stdout_err_to_null
result = CommandRunner.run('echo hello > /dev/null 2>&1',
{:timeout => 2})
assert_equal "", result[:out]
assert_equal 0, result[:status].exitstatus
end
def test_multi_string_convenience
result = CommandRunner.run('ls', '-r', 'test')
assert result[:out].start_with?("test_command_runner"), "Unexpected output: #{result[:out]}"
assert_equal 0, result[:status].exitstatus
end
def test_debug_log
rd, wr = IO.pipe
result = CommandRunner.run('ls', 'test', debug_log: wr)
wr.close
assert result[:out].start_with?("test_command_runner"), "Unexpected output: #{result[:out]}"
assert_equal 0, result[:status].exitstatus
assert_equal "CommandRunnerNG spawn: args=[[\"ls\", \"test\"]], timeout=, encoding=, options: {:err=>[:child, :out]}, PID: #{result[:pid]}\nCommandRunnerNG exit: PID: #{result[:pid]}, code: 0\n", rd.read
ensure
rd.close
end
def test_split_err
result = CommandRunner.run('echo OUT ; echo ERR 1>&2', split_stderr: true)
assert_equal "OUT\n", result[:out]
assert_equal "ERR\n", result[:err]
end
# Test disabled as it requires thin.
# Most perculiar behaviour have been observed when backgrounding thin through a subshell.
# Note that correct usage would be to daemonize it with -d.
# The intermidiate shell dies immediately, but stdout is never closed. Presumably thin
# inherits it, and we never get an EOF in CommandRunnerNG.
# Requires two files 'config.ru' and 'app.rb' to run (as well as thin and sinatra gems):
#
# # config.ru
# require './app'
# run HelloWorldApp
#
# # app.rb
# require 'sinatra'
#
# class HelloWorldApp < Sinatra::Base
# get '/' do
# "Hello, world!"
# end
# end
#
#def test_thin_background
# result = CommandRunner.run('thin start &',
# timeout: 5)
#
# assert_equal 0, result[:status].exitstatus
#end
end