discourse/script/docker_test.rb
Alan Guo Xiang Tan 9caba30d5c
DEV: Add docker:test:setup Rake task (#23430)
## What is the context here?

The `docker.rake` Rakefile contains Rake tasks that are meant to be run
in the `discourse/discourse_test:release` Docker image. For example, we
have the `docker:test` Rake task that makes it easier to run the test
suite for a particular Discourse commit.

Why are we introducing a `docker:test:setup` Rake task?

While we have the `docker:test` Rake task, it is very limited in the
test commands that can be executed. It is very useful for automated
testing but not very useful for running tests in the development
environment. Therefore, we are introducing a `docker:test:setup` rake
task that can be used to set up the test environment for running tests.

The envisioned example usage is something like this:

```
docker run -d --name=discourse_test --entrypoint=/sbin/boot discourse/discourse_test:release
docker exec -u discourse:discourse discourse_test ruby script/docker_test.rb --no-tests
docker exec -u discourse:discourse discourse_test bundle exec rake docker:test:setup
docker exec -u discourse:discourse discourse_test bundle exec rspec <path to file>
```
2023-09-07 13:46:23 +08:00

70 lines
2.1 KiB
Ruby

# frozen_string_literal: true
# This script is to be run in the `discourse/discourse_test:release` docker image.
require "optparse"
options = {}
OptionParser
.new do |opts|
opts.banner = "Usage: ruby script/docker_test.rb [options]"
opts.on(
"--checkout-ref CHECKOUT_REF",
"Checks out the working tree to a specified commit hash or branch. If not specified, defaults to 'origin/tests-passed'.",
) { |v| options[:checkout_ref] = v }
opts.on(
"--run-smoke-tests",
"Executes the smoke tests instead of the regular tests from docker.rake. See lib/tasks/smoke_test.rake for more information.",
) { options[:run_smoke_tests] = true }
opts.on(
"--no-checkout",
"Does not check out the working tree when this option is passed. By default, the working tree is checked out to the latest commit on the 'origin/tests-passed' branch.",
) { options[:no_checkout] = true }
opts.on("--no-tests", "Does not execute any tests") { options[:no_tests] = true }
opts.on_tail("-h", "--help", "Displays usage information") do
puts opts
exit
end
end
.parse!
no_checkout = options.has_key?(:no_checkout) ? options[:no_checkout] : ENV["NO_UPDATE"]
checkout_ref = options.has_key?(:checkout_ref) ? options[:checkout_ref] : ENV["COMMIT_HASH"]
run_smoke_test =
options.has_key?(:run_smoke_test) ? options[:run_smoke_test] : ENV["RUN_SMOKE_TESTS"]
no_tests = options.has_key?(:no_tests) ? options[:no_tests] : false
def log(message)
puts "[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] #{message}"
end
def run_or_fail(command)
log(command)
pid = Process.spawn(command)
Process.wait(pid)
exit 1 unless $?.exitstatus == 0
end
unless no_checkout
run_or_fail("git reset --hard")
run_or_fail("git fetch")
run_or_fail("LEFTHOOK=0 git checkout #{checkout_ref || "origin/tests-passed"}")
run_or_fail("bundle")
end
unless no_tests
if run_smoke_tests
log("Running smoke tests")
run_or_fail("bundle exec rake smoke:test")
else
log("Running tests")
run_or_fail("bundle exec rake docker:test")
end
end