-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathjsc-stress-test-writer-ruby.rb
More file actions
498 lines (459 loc) · 17.9 KB
/
jsc-stress-test-writer-ruby.rb
File metadata and controls
498 lines (459 loc) · 17.9 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# Copyright (C) 2017 Sony Interactive Entertainment Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'open3'
$hasDiff = false
if ($hostOS == 'windows')
out, err, status = Open3.capture3("where", "/q", "diff")
$hasDiff = status.success?
else
out, err, status = Open3.capture3("which", "diff")
$hasDiff = status.success?
end
# Prefix each line of str with the name
def prefixString(str, name)
"#{str}.empty? ? \"\" : #{str}.gsub(/^/m, \"#{name}: \")"
end
def silentOutputHandler
Proc.new {
| name |
<<-END_SILENT_OUTPUT_HANDLER
out = out + err
err = nil
STDOUT.puts #{prefixString("out", name)} if (!out.empty?)
File.open("#{Shellwords.shellescape((Pathname("..") + (name + ".out")).to_s)}", "w") do |out_file|
out_file.puts out
end
END_SILENT_OUTPUT_HANDLER
}
end
# Output handler for tests that are expected to produce meaningful output.
def noisyOutputHandler
Proc.new {
| name |
<<-END_NOISY_OUTPUT_HANDLER
out = out + err
err = nil
File.open("#{Shellwords.shellescape((Pathname("..") + (name + ".out")).to_s)}", "w") do |out_file|
out_file.puts out
end
END_NOISY_OUTPUT_HANDLER
}
end
# Error handler for tests that fail exactly when they return non-zero exit status.
# This is useful when a test is expected to fail.
def simpleErrorHandler
Proc.new {
| outp, plan |
outp.puts "if !success(status)\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code \#{status.exitstatus}\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "else\n"
outp.puts " " + plan.successCommand
outp.puts "end\n"
}
end
# Error handler for tests that fail exactly when they return zero exit status.
def expectedFailErrorHandler
Proc.new {
| outp, plan |
outp.puts "if success(status)\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code 0\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "else\n"
outp.puts " " + plan.successCommand
outp.puts "end\n"
}
end
# Error handler for tests that fail exactly when they return non-zero exit status and produce
# lots of spew. This will echo that spew when the test fails.
def noisyErrorHandler
Proc.new {
| outp, plan |
outp.puts "if !success(status)\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code \#{status.exitstatus}\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "else\n"
outp.puts " " + plan.successCommand
outp.puts "end\n"
}
end
def fallbackDiff(expected, output)
<<-END_FALLBACK_DIFF
# Fallback diff for when diff(1) isn't available
diffs = []
line_number = 0
File.open("#{expected}") do | expected |
File.open("#{output}") do | actual |
loop do
l1 = expected.gets
l2 = actual.gets
if (l1 != l2)
diffs.push([line_number, l1, l2])
end
line_number = line_number + 1
break if (l1 == nil && l2 == nil)
end
end
end
isDifferent = !diffs.empty?
diffOut = diffs.map {
| diff |
"@@ -\#{diff[0]},1 +\#{diff[0]},1 @@\\n" +
(diff[1] ? "-\#{diff[1]}" : "") +
(diff[2] ? "+\#{diff[2]}" : "")
}.join("")
END_FALLBACK_DIFF
end
def runDiff(expected, output)
<<-END_RUN_DIFF
diffOut, diffStatus = Open3.capture2("diff",
"--strip-trailing-cr",
"-u",
"#{expected}",
"#{output}");
isDifferent = !diffStatus.success?
END_RUN_DIFF
end
# Get a difference between two files, using diff where available, falling back
# on a limited comparison when diff is not available
def getDiff(expected, output)
if $hasDiff
runDiff(expected, output)
else
fallbackDiff(expected,output)
end
end
# Error handler for tests that diff their output with some expectation.
def diffErrorHandler(expectedFilename)
Proc.new {
| outp, plan |
outputFilename = Shellwords.shellescape((Pathname("..") + (plan.name + ".out")).to_s)
outp.puts "if !success(status)\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code \#{status.exitstatus}\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "elsif File.exist?(\"../#{Shellwords.shellescape(expectedFilename)}\")\n"
outp.puts getDiff("../#{Shellwords.shellescape(expectedFilename)}", outputFilename)
outp.puts " if isDifferent\n"
outp.puts " print " + prefixString("\"DIFF FAILURE!\\n\"", plan.name) + "\n"
outp.puts " print " + prefixString("diffOut", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts " else"
outp.puts " " + plan.successCommand
outp.puts " end"
outp.puts "else\n"
outp.puts " print " + prefixString("\"NO EXPECTATION!\\n\"", plan.name) + "\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "end"
}
end
# Error handler for tests that report error by saying "failed!". This is used by Mozilla
# tests.
def mozillaErrorHandler
Proc.new {
| outp, plan |
outp.puts "if !success(status)\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code \#{status.exitstatus}\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "elsif /failed!/i =~ out\n"
outp.puts " print " + prefixString("\"Detected failures:\\n\"", plan.name) + "\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "else\n"
outp.puts " " + plan.successCommand
outp.puts "end\n"
}
end
# Error handler for tests that report error by saying "failed!", and are expected to
# fail. This is used by Mozilla tests.
def mozillaFailErrorHandler
Proc.new {
| outp, plan |
outp.puts "if !success(status)\n"
outp.puts " " + plan.successCommand
outp.puts "elsif /failed!/i =~ out\n"
outp.puts " " + plan.successCommand
outp.puts "else\n"
outp.puts " print " + prefixString("\"NOTICE: You made this test pass, but it was expected to fail\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "end\n"
}
end
# Error handler for tests that report error by saying "failed!", and are expected to have
# an exit code of 3.
def mozillaExit3ErrorHandler
Proc.new {
| outp, plan |
outp.puts "if success(status)\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " print " + prefixString("\"ERROR: Test expected to fail, but returned successfully\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "elsif status.exitstatus != 3"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code: \#{status.exitstatus}\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "elsif /failed!/i =~ out\n"
outp.puts " print " + prefixString("\"Detected failures:\\n\"", plan.name) + "\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "else\n"
outp.puts " " + plan.successCommand
outp.puts "end\n"
}
end
# Error handler for tests that report success by saying "Passed" or error by saying "FAILED".
# This is used by Chakra tests.
def chakraPassFailErrorHandler
Proc.new {
| outp, plan |
outp.puts "if !success(status)\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " print " + prefixString("\"ERROR: Unexpected exit code \#{status.exitstatus}\\n\"", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "elsif /FAILED/i =~ out\n"
outp.puts " print " + prefixString("\"Detected failures:\\n\"", plan.name) + "\n"
outp.puts " print " + prefixString("out", plan.name) + "\n"
outp.puts " " + plan.failCommand
outp.puts "else\n"
outp.puts " " + plan.successCommand
outp.puts "end\n"
}
end
class Plan < BasePlan
def shellCommand
script = "out = nil\n"
script += "err = nil\n"
script += "status = nil\n"
script += "Dir.chdir(\"../#{Shellwords.shellescape(@directory.to_s)}\") do\n"
script += " env = {}\n"
($envVars + additionalEnv).each {
|var|
(key, value) = var.split(/=/, 2)
script += " env[\"#{key}\"] = \"#{value}\"\n"
}
script += " out, err, status = Open3.capture3(env, \n"
script += @arguments.map { | argument | " \"#{argument}\""}.join(",\n")
script += " )\n"
script += "end\n"
return script
end
def reproScriptHelper
script = "def error_script_contents\n"
script += " <<-END_OF_SCRIPT\n"
script += " require 'open3'\n"
script += " def success(status)\n"
script += " status.success?\n"
script += " end\n"
script += " script_location = File.expand_path(File.dirname(__FILE__))\n"
script += " Dir.chdir(\"\\\#{script_location}"
Pathname.new(@name).dirname.each_filename {
| pathComponent |
script += "/.."
}
script += "/.runner\") do\n"
script += " ENV[\"DYLD_FRAMEWORK_PATH\"] = \"#{$testingFrameworkPath.dirname}\"\n"
script += " ENV[\"JSCTEST_timeout\"] = \"#{ENV['JSCTEST_timeout']}\"\n"
script += " ENV[\"JSCTEST_hardTimeout\"] = \"#{ENV['JSCTEST_hardTimeout']}\"\n"
script += " ENV[\"JSCTEST_memoryLimit\"] = \"#{ENV['JSCTEST_memoryLimit']}\"\n"
script += " #{shellCommand}"
script += " print out\n"
script += " if (!success(status))\n"
script += " exit(1)\n"
script += " end\n"
script += " end\n"
script += " END_OF_SCRIPT\n"
script += "end\n"
return script
end
def reproScriptCommand
<<-END_REPRO_SCRIPT_COMMAND
File.open("#{Shellwords.shellescape((Pathname.new("..") + @name).to_s)}", "w") do |scr|
scr.puts "\#{error_script_contents}"
end
END_REPRO_SCRIPT_COMMAND
end
def statusCommand(status_code)
# May be called in th rescue block, so status is not
# guaranteed to be set; if it isn't, set the exit code to
# something that's clearly invalid.
<<-END_STATUS_COMMAND
File.open("#{statusFile}", "a") { |f|
f.puts("#{@index} #{$runUniqueId} \#{status.nil? ? 999999999 : status.exitstatus} #{status_code}")
}
END_STATUS_COMMAND
end
def failCommand
<<-END_FAIL_COMMAND
print "FAIL: #{Shellwords.shellescape(@name)}\n"
#{statusCommand(STATUS_FILE_FAIL)}
#{reproScriptCommand}
END_FAIL_COMMAND
end
def successCommand
if $progressMeter or $verbosity >= 2
<<-END_VERBOSE_SUCCESS_COMMAND
print "PASS: #{Shellwords.shellescape(@name)}\n"
#{statusCommand(STATUS_FILE_PASS)}
END_VERBOSE_SUCCESS_COMMAND
else
"#{statusCommand(STATUS_FILE_PASS)}\n"
end
end
def statusFile
"#{STATUS_FILE}"
end
def writeRunScript(filename)
File.open(filename, "w") {
| outp |
outp.puts "print \"Running #{Shellwords.shellescape(@name)}\\n\""
outp.puts "#{reproScriptHelper}"
outp.puts "begin"
outp.puts "require 'open3'"
outp.puts "require 'fileutils'"
outp.puts "def success(status)"
outp.puts " status.success?"
outp.puts "end"
cmd = shellCommand
cmd += @outputHandler.call(@name)
if $verbosity >= 3
outp.puts "print \"#{Shellwords.shellescape(cmd)}\\n\""
end
outp.puts cmd
@errorHandler.call(outp, self)
outp.puts "rescue"
outp.puts " print \"FAIL: #{Shellwords.shellescape(@name)}\\n\""
outp.puts " #{statusCommand(STATUS_FILE_FAIL)}"
outp.puts "end"
}
end
end
class TestRunnerShell < TestRunner
def prepareRunner(runlist, serialPlans, exclusivePlans, completedPlans, remoteHosts)
File.open("#{@runnerDir + "runscript"}", "w") { |f|
runlist.each { |plan|
if completedPlans.include?(plan)
next
end
f.puts("ruby test_script_#{plan.index}")
}
}
`dos2unix #{@runnerDir + "runscript"}`
end
def command(remoteIndex=0)
"sh runscript"
end
end
class TestRunnerMake < TestRunner
def output_target(outp, plan, prereqs)
index = plan.index
target = "test_done_#{index}"
outp.puts "#{target}: #{prereqs.join(" ")}"
outp.puts "\truby test_script_#{index}"
target
end
def prepareRunnerForRemote(runlist, serialPlans, exclusivePlans, completedPlans, remoteIndex)
runPlans = []
serialRunPlans = []
exclusiveRunPlans = []
runlist.each {
| plan |
if completedPlans.include?(plan)
next
end
if @remoteHosts.nil? or plan.index % @remoteHosts.length == remoteIndex
if serialPlans.include?(plan)
serialRunPlans << plan
elsif exclusivePlans.include?(plan)
exclusiveRunPlans << plan
else
runPlans << plan
end
end
}
File.open(@runnerDir + "Makefile.#{remoteIndex}", "w") {
| outp |
all_deps = []
if serialRunPlans.empty?
all_deps << "parallel"
else
all_deps << "test_done_#{serialRunPlans[-1].index}"
end
if !exclusiveRunPlans.empty?
all_deps << "test_done_#{exclusiveRunPlans[-1].index}"
end
outp.puts("all: " + all_deps.join(" "))
if !serialRunPlans.empty?
prev_target = "parallel"
serialRunPlans.each {
| plan |
prev_target = output_target(outp, plan, [prev_target])
}
end
if !exclusiveRunPlans.empty?
prev_target = nil
exclusiveRunPlans.each {
| plan |
prev_target = output_target(outp, plan, prev_target ? [prev_target] : [])
}
end
parallelTargets = runPlans.collect {
| plan |
output_target(outp, plan, [])
}
outp.puts("parallel: " + parallelTargets.join(" "))
}
end
def prepareRunner(runlist, serialPlans, exclusivePlans, completedPlans, remoteHosts)
if remoteHosts.nil?
prepareRunnerForRemote(runlist, serialPlans, exclusivePlans, completedPlans, 0)
else
remoteHosts.each_index {
|remoteIndex|
prepareRunnerForRemote(runlist, serialPlans, exclusivePlans, completedPlans, remoteIndex)
}
end
end
def command(remoteIndex=0)
"make -j #{$numChildProcesses} -s -f Makefile.#{remoteIndex}"
end
end
class TestRunnerRuby < TestRunner
def prepareRunner(runlist, serialPlans, exclusivePlans, completedPlans, remoteHosts)
File.open(@runnerDir + "runscript", "w") {
| outp |
runlist.each {
| plan |
if completedPlans.include?(plan)
next
end
outp.puts "system \"ruby test_script_#{plan.index}\""
}
}
end
def command(remoteIndex=0)
"ruby runscript"
end
end