Repository navigation
Expand file tree
/
Copy pathsmoke.rb
More file actions
421 lines (375 loc) · 15.5 KB
/
Copy pathsmoke.rb
File metadata and controls
421 lines (375 loc) · 15.5 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
# rbs_inline: enabled
require "bundler"
require "digest"
require "fileutils"
require "json"
require "rbconfig"
require "rubygems/package"
require "timeout"
require "tmpdir"
class QuickstartSmoke
class Failure < StandardError; end
HoldProcess = Data.define(:buyer, :pid, :input, :output)
REPOSITORY_ROOT = File.expand_path("../..", __dir__)
QUICKSTART_ROOT = __dir__
ACTOR_PATH = File.join(QUICKSTART_ROOT, "app/actors/ticket_sale.rb")
RECIPE_PATH = File.join(QUICKSTART_ROOT, "README.md")
INITIALIZER = "config/initializers/solid_objects.rb"
DEMO_GRANTS = {
"configuration.authorize_message = ->(**) { false }" => "configuration.authorize_message = ->(**) { true }",
"configuration.authorize_query = ->(**) { false }" => "configuration.authorize_query = ->(**) { true }"
}.freeze
DENIED_POLICIES = %w[authorize_destroy authorize_subscription authorize_administration authorize_transmission].freeze
CONCURRENT_BUYERS = 8
RESTART_DEADLINE_SECONDS = 8
COMMAND_TIMEOUT_SECONDS = 600
RECOVERY_TIMEOUT_SECONDS = 60
HOLD_PROGRAM = <<~RUBY
$stdout.sync = true
puts "ready"
$stdin.gets
puts JSON.generate(TicketSale.ref(ARGV.fetch(0)).hold(buyer: ARGV.fetch(1)))
RUBY
SHIFTED_CLOCK_HOLD_PROGRAM = <<~RUBY
require "active_support/testing/time_helpers"
include ActiveSupport::Testing::TimeHelpers
travel_to(Time.current - 10.minutes + Integer(ARGV.fetch(2)).seconds) do
puts JSON.generate(TicketSale.ref(ARGV.fetch(0)).hold(buyer: ARGV.fetch(1)))
end
RUBY
STATE_PROGRAM = <<~RUBY
puts JSON.generate(TicketSale.ref(ARGV.fetch(0)).snapshot.to_h)
RUBY
RECOVERY_PROGRAM = <<~RUBY
sale = TicketSale.ref(ARGV.fetch(0))
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + Integer(ARGV.fetch(1))
state = sale.snapshot.to_h
until state.fetch("holds").empty? || Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
sleep 0.25
state = sale.snapshot.to_h
end
puts JSON.generate(state)
RUBY
RESOLUTION_PROGRAM = <<~RUBY
specification = Gem.loaded_specs.fetch("solid_objects")
puts JSON.generate(
version: SolidObjects::VERSION,
rails: Rails.version,
json: JSON::VERSION,
full_gem_path: specification.full_gem_path,
cache_file: specification.cache_file,
loaded_feature: $LOADED_FEATURES.find { |feature| feature.end_with?("/solid_objects/version.rb") }
)
RUBY
# @rbs @root: String
# @rbs @log_directory: String
# @rbs @application_path: String
# @rbs @bundle_path: String
# @rbs @child_pids: Array[Integer]
# @rbs @started_at: Float
# @rbs () -> void
def initialize
@root = Dir.mktmpdir("solid_objects_quickstart_")
@log_directory = File.join(@root, "logs")
@application_path = File.join(@root, "ticket_demo")
@bundle_path = File.join(@root, "bundle")
@child_pids = []
@started_at = monotonic_now
end
# @rbs () -> void
def call
actor_source = verify_recipe
artifact = build_gem
generate_application
install_bundle(artifact)
resolution = verify_resolution(artifact)
install_solid_objects(actor_source)
concurrency = prove_concurrent_holds
restart = prove_restart_recovery
puts JSON.pretty_generate(
artifact: { version: artifact.fetch(:version), sha256: artifact.fetch(:sha256), resolved_from: resolution.fetch("full_gem_path") },
application: { rails: resolution.fetch("rails"), json: resolution.fetch("json") },
concurrency:,
restart:,
seconds: (monotonic_now - @started_at).round(1)
)
ensure
stop_children
FileUtils.remove_entry(@root) if File.exist?(@root)
end
private
# @rbs () -> String
def verify_recipe
prove("examples/quickstart/README.md exists") { File.file?(RECIPE_PATH) }
prove("examples/quickstart/app/actors/ticket_sale.rb exists") { File.file?(ACTOR_PATH) }
actor_source = File.read(ACTOR_PATH).delete_prefix("# rbs_inline: enabled\n\n")
recipe = File.read(RECIPE_PATH)
prove("the recipe shows the exact actor that the check runs") { recipe.include?(actor_source) }
DEMO_GRANTS.each_value do |grant|
prove("the recipe shows the demo grant #{grant}") { recipe.include?(grant) }
end
published_samples.each do |path, sample|
prove("#{path} shows the same TicketSale actor") { sample == actor_source.strip }
end
actor_source
end
# @rbs () -> Array[[String, String]]
def published_samples
paths = Dir[File.join(REPOSITORY_ROOT, "{README.md,docs/**/*.md,examples/**/*.md}")]
paths.flat_map do |path|
relative_path = path.delete_prefix("#{REPOSITORY_ROOT}/")
File.read(path).scan(/```ruby\n(class TicketSale < SolidObjects::Actor\n.*?\nend)\n```/m).map do |(sample)|
[ relative_path, sample ]
end
end
end
# @rbs () -> Hash[Symbol, String]
def build_gem
artifact_directory = File.join(@root, "artifact")
FileUtils.mkdir_p(artifact_directory)
version = File.read(File.join(REPOSITORY_ROOT, "lib/solid_objects/version.rb"))[/VERSION = "([^"]+)"/, 1] ||
raise(Failure, "lib/solid_objects/version.rb declares no version")
path = File.join(artifact_directory, "solid_objects-#{version}.gem")
run_in_repository("gem-build", RbConfig.ruby, gem_executable, "build", "solid_objects.gemspec", "--output", path)
prove("the built gem declares version #{version}") { Gem::Package.new(path).spec.version.to_s == version }
{ path:, version:, sha256: Digest::SHA256.file(path).hexdigest }
end
# @rbs () -> void
def generate_application
run_in_repository(
"rails-new",
RbConfig.ruby, Gem.bin_path("railties", "rails"), "new", @application_path,
"--minimal", "--skip-bundle", "--skip-git", "--skip-test", "--skip-system-test", "--quiet"
)
end
# @rbs (Hash[Symbol, String]) -> void
def install_bundle(artifact)
gemfile = File.join(@application_path, "Gemfile")
run_in_application("bundle-install", RbConfig.ruby, bundle_executable, "install")
cache_directory = File.join(@application_path, "vendor/cache")
FileUtils.mkdir_p(cache_directory)
FileUtils.cp(artifact.fetch(:path), cache_directory)
File.write(gemfile, "#{File.read(gemfile)}\ngem \"solid_objects\", \"= #{artifact.fetch(:version)}\"\n")
run_in_application("bundle-install-local", RbConfig.ruby, bundle_executable, "install", "--local")
end
# @rbs (Hash[Symbol, String]) -> Hash[String, String]
def verify_resolution(artifact)
gemfile = File.read(File.join(@application_path, "Gemfile"))
prove("the Gemfile names no path dependency") { !gemfile.match?(/^\s*gem\s+"solid_objects".*path:/) }
lockfile = File.read(File.join(@application_path, "Gemfile.lock"))
prove("Gemfile.lock records the checksum of the built gem") do
lockfile.include?("solid_objects (#{artifact.fetch(:version)}) sha256=#{artifact.fetch(:sha256)}")
end
resolution = JSON.parse(rails_runner("resolution", RESOLUTION_PROGRAM).lines.last)
prove("the application loads the built version") { resolution.fetch("version") == artifact.fetch(:version) }
prove("the gem resolves inside the isolated bundle") { resolution.fetch("full_gem_path").start_with?(@bundle_path) }
loaded_feature = resolution.fetch("loaded_feature").to_s
prove("the gem loads its files from the isolated bundle") { loaded_feature.start_with?(@bundle_path) }
prove("the gem does not resolve to the repository") { !loaded_feature.start_with?(REPOSITORY_ROOT) }
prove("the installed gem is the built artifact") do
Digest::SHA256.file(resolution.fetch("cache_file")).hexdigest == artifact.fetch(:sha256)
end
resolution
end
# @rbs (String) -> void
def install_solid_objects(actor_source)
run_in_application("generate", RbConfig.ruby, "bin/rails", "generate", "solid_objects:install")
run_in_application("migrate", RbConfig.ruby, "bin/rails", "db:migrate")
doctor = run_in_application("doctor", RbConfig.ruby, "bin/rails", "solid_objects:doctor")
prove("the doctor warns that every policy denies by default") { doctor.include?("all five policies denied") }
actor_path = File.join(@application_path, "app/actors/ticket_sale.rb")
FileUtils.mkdir_p(File.dirname(actor_path))
File.write(actor_path, actor_source)
grant_demo_policies
end
# @rbs () -> void
def grant_demo_policies
path = File.join(@application_path, INITIALIZER)
initializer = File.read(path)
DEMO_GRANTS.each do |denial, grant|
prove("the generated initializer denies with #{denial}") { initializer.include?(denial) }
initializer = initializer.sub(denial, grant)
end
DENIED_POLICIES.each do |policy|
prove("#{policy} stays denied") { initializer.include?("configuration.#{policy} = ->(**) { false }") }
end
File.write(path, initializer)
end
# @rbs () -> Hash[Symbol, Integer]
def prove_concurrent_holds
runtime = start_runtime("runtime-concurrency")
buyers = (1..CONCURRENT_BUYERS).map { |number| "buyer-#{number}" }
holders = buyers.map { |buyer| spawn_hold(event: "concert", buyer:) }
holders.each { |holder| await_ready(holder) }
holders.each do |holder|
holder.input.puts("go")
holder.input.close
end
results = holders.to_h { |holder| [ holder.buyer, read_hold(holder) ] }
prove("the runtime kept running during the concurrent holds") { alive?(runtime) }
stop_runtime(runtime)
winners = results.select { |_, result| result.fetch("held") }.keys
state = read_state("concert")
prove("exactly one concurrent buyer held the only ticket") { winners.length == 1 }
prove("no concurrent update was lost") do
state.fetch("available").zero? && state.fetch("holds").keys == winners
end
{ calls: buyers.length, held: winners.length, available: state.fetch("available") }
end
# @rbs () -> Hash[Symbol, Integer]
def prove_restart_recovery
hold = JSON.parse(
rails_runner("shifted-hold", SHIFTED_CLOCK_HOLD_PROGRAM, "matinee", "ada", RESTART_DEADLINE_SECONDS.to_s).lines.last
)
due_at = Time.now + RESTART_DEADLINE_SECONDS
prove("the restart hold succeeded while the runtime was stopped") { hold.fetch("held") }
sleep [ due_at + 2 - Time.now, 0 ].max
stopped = read_state("matinee")
prove("the due reminder waited while no runtime ran") do
stopped.fetch("available").zero? && stopped.fetch("holds").key?("ada")
end
runtime = start_runtime("runtime-recovery")
recovered = JSON.parse(
rails_runner("recovery", RECOVERY_PROGRAM, "matinee", RECOVERY_TIMEOUT_SECONDS.to_s).lines.last
)
stop_runtime(runtime)
prove("the reminder ran after the restart and released the hold once") do
recovered.fetch("available") == 1 && recovered.fetch("holds").empty?
end
{ available_while_stopped: stopped.fetch("available"), available_after_restart: recovered.fetch("available") }
end
# @rbs (event: String, buyer: String) -> HoldProcess
def spawn_hold(event:, buyer:)
input_reader, input_writer = IO.pipe
output_reader, output_writer = IO.pipe
pid = spawn_in_application(
"hold-#{buyer}", RbConfig.ruby, "bin/rails", "runner", HOLD_PROGRAM, event, buyer,
in: input_reader, out: output_writer
)
input_reader.close
output_writer.close
HoldProcess.new(buyer:, pid:, input: input_writer, output: output_reader)
end
# @rbs (HoldProcess) -> void
def await_ready(holder)
Timeout.timeout(COMMAND_TIMEOUT_SECONDS) do
line = holder.output.gets
raise Failure, "#{holder.buyer} exited before it was ready\n#{log_tail("hold-#{holder.buyer}")}" if line.nil?
raise Failure, "#{holder.buyer} printed #{line.inspect} before it was ready" unless line == "ready\n"
end
end
# @rbs (HoldProcess) -> Hash[String, untyped]
def read_hold(holder)
output = Timeout.timeout(COMMAND_TIMEOUT_SECONDS) { holder.output.read }
holder.output.close
status = wait(holder.pid)
raise Failure, "#{holder.buyer} failed\n#{log_tail("hold-#{holder.buyer}")}" unless status.success?
JSON.parse(output.lines.last)
end
# @rbs (String) -> Hash[String, untyped]
def read_state(event)
JSON.parse(rails_runner("state-#{event}", STATE_PROGRAM, event).lines.last)
end
# @rbs (String) -> Integer
def start_runtime(name)
spawn_in_application(name, RbConfig.ruby, bundle_executable, "exec", "solid_objects", "start")
end
# @rbs (Integer) -> void
def stop_runtime(pid)
::Process.kill("TERM", pid)
status = Timeout.timeout(COMMAND_TIMEOUT_SECONDS) { wait(pid) }
raise Failure, "the runtime did not stop cleanly: #{status.inspect}" unless status.success?
end
# @rbs (String, String, *String) -> String
def rails_runner(name, program, *arguments)
run_in_application(name, RbConfig.ruby, "bin/rails", "runner", program, *arguments)
end
# @rbs (String, *String) -> String
def run_in_repository(name, *command)
pid = spawn_logged(name, command, environment: {}, chdir: REPOSITORY_ROOT)
finish(name, pid)
end
# @rbs (String, *String) -> String
def run_in_application(name, *command)
pid = Bundler.with_unbundled_env do
spawn_logged(name, command, environment: application_environment, chdir: @application_path)
end
finish(name, pid)
end
# @rbs (String, *untyped, **untyped) -> Integer
def spawn_in_application(name, *command, **options)
Bundler.with_unbundled_env do
spawn_logged(name, command, environment: application_environment, chdir: @application_path, **options)
end
end
# @rbs (String, Array[String], environment: Hash[String, String], chdir: String, **untyped) -> Integer
def spawn_logged(name, command, environment:, chdir:, **options)
FileUtils.mkdir_p(@log_directory)
log_path = File.join(@log_directory, "#{name}.log")
pid = ::Process.spawn(environment, *command, { chdir:, out: log_path, err: log_path }.merge(options))
@child_pids << pid
pid
end
# @rbs (String, Integer) -> String
def finish(name, pid)
status = Timeout.timeout(COMMAND_TIMEOUT_SECONDS) { wait(pid) }
raise Failure, "#{name} failed with #{status.inspect}\n#{log_tail(name)}" unless status.success?
File.read(File.join(@log_directory, "#{name}.log"))
end
# @rbs (Integer) -> Process::Status
def wait(pid)
_pid, status = ::Process.wait2(pid)
@child_pids.delete(pid)
status
end
# @rbs (Integer) -> bool
def alive?(pid)
::Process.waitpid(pid, ::Process::WNOHANG).nil?
end
# @rbs () -> void
def stop_children
@child_pids.dup.each do |pid|
::Process.kill("TERM", pid)
Timeout.timeout(20) { wait(pid) }
rescue Errno::ESRCH, Errno::ECHILD
@child_pids.delete(pid)
rescue Timeout::Error
::Process.kill("KILL", pid)
wait(pid)
end
end
# @rbs () -> Hash[String, String]
def application_environment
{
"BUNDLE_GEMFILE" => File.join(@application_path, "Gemfile"),
"BUNDLE_PATH" => @bundle_path,
"BUNDLE_DEPLOYMENT" => "false",
"BUNDLE_FROZEN" => "false",
"BUNDLE_JOBS" => "4",
"RAILS_ENV" => "development"
}
end
# @rbs (String) -> String
def log_tail(name)
path = File.join(@log_directory, "#{name}.log")
return "(no log)" unless File.file?(path)
File.readlines(path).last(40).join
end
# @rbs () -> String
def gem_executable
File.join(RbConfig::CONFIG.fetch("bindir"), "gem")
end
# @rbs () -> String
def bundle_executable
Gem.bin_path("bundler", "bundle")
end
# @rbs () -> Float
def monotonic_now
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end
# @rbs (String) { () -> boolish } -> void
def prove(claim)
raise Failure, "quickstart proof failed: #{claim}" unless yield
end
end
QuickstartSmoke.new.call