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>
```
This commit is contained in:
Alan Guo Xiang Tan
2023-09-07 13:46:23 +08:00
committed by GitHub
parent ad58b6d604
commit 9caba30d5c
3 changed files with 111 additions and 70 deletions

View File

@ -1,12 +1,44 @@
# frozen_string_literal: true
# This script is run in the discourse_test docker image
# Available environment variables:
# => NO_UPDATE disables updating the source code within the discourse_test docker image
# => COMMIT_HASH used by the discourse_test docker image to load a specific commit of discourse
# this can also be set to a branch, e.g. "origin/tests-passed"
# => RUN_SMOKE_TESTS executes the smoke tests instead of the regular tests from docker.rake
# See lib/tasks/docker.rake and lib/tasks/smoke_test.rake for more information
# 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}"
@ -19,20 +51,19 @@ def run_or_fail(command)
exit 1 unless $?.exitstatus == 0
end
unless ENV["NO_UPDATE"]
unless no_checkout
run_or_fail("git reset --hard")
run_or_fail("git fetch")
checkout = ENV["COMMIT_HASH"] || "origin/tests-passed"
run_or_fail("LEFTHOOK=0 git checkout #{checkout}")
run_or_fail("LEFTHOOK=0 git checkout #{checkout_ref || "origin/tests-passed"}")
run_or_fail("bundle")
end
log("Running tests")
if ENV["RUN_SMOKE_TESTS"]
run_or_fail("bundle exec rake smoke:test")
else
run_or_fail("bundle exec rake docker:test")
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