Skip to content

Commit

Permalink
Respect ENV variables when finding DBs etc for the test suite
Browse files Browse the repository at this point in the history
If they're not set we'll still fall back to localhost, but this makes it
possible to run the tests against a remote Postgres / Redis / whatever.
  • Loading branch information
matthewd committed Feb 5, 2019
1 parent 44232b4 commit 287920c
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 36 deletions.
4 changes: 3 additions & 1 deletion actioncable/Rakefile
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "base64"
require "rake/testtask"
require "pathname"
require "open3"
Expand All @@ -25,7 +26,8 @@ namespace :test do
end

task :integration do
system("yarn test") || raise("Failures")
system(Hash[*Base64.decode64(ENV.fetch("ENCODED", "")).split(/[ =]/)], "yarn", "test")
exit($?.exitstatus) unless $?.success?
end
end

Expand Down
8 changes: 6 additions & 2 deletions actioncable/test/subscription_adapter/redis_test.rb
Expand Up @@ -11,7 +11,11 @@ class RedisAdapterTest < ActionCable::TestCase
include ChannelPrefixTest

def cable_config
{ adapter: "redis", driver: "ruby" }
{ adapter: "redis", driver: "ruby" }.tap do |x|
if host = URI(ENV["REDIS_URL"] || "").hostname
x[:host] = host
end
end
end
end

Expand All @@ -25,7 +29,7 @@ class RedisAdapterTest::AlternateConfiguration < RedisAdapterTest
def cable_config
alt_cable_config = super.dup
alt_cable_config.delete(:url)
alt_cable_config.merge(host: "127.0.0.1", port: 6379, db: 12)
alt_cable_config.merge(host: URI(ENV["REDIS_URL"] || "").hostname || "127.0.0.1", port: 6379, db: 12)
end
end

Expand Down
9 changes: 6 additions & 3 deletions actionpack/test/dispatch/session/mem_cache_store_test.rb
Expand Up @@ -38,8 +38,9 @@ def call_reset_session

begin
require "dalli"
ss = Dalli::Client.new("localhost:11211").stats
raise Dalli::DalliError unless ss["localhost:11211"]
servers = ENV["MEMCACHE_SERVERS"] || "localhost:11211"
ss = Dalli::Client.new(servers).stats
raise Dalli::DalliError unless ss[servers]

def test_setting_and_getting_session_value
with_test_route_set do
Expand Down Expand Up @@ -195,7 +196,9 @@ def with_test_route_set
end

@app = self.class.build_app(set) do |middleware|
middleware.use ActionDispatch::Session::MemCacheStore, key: "_session_id", namespace: "mem_cache_store_test:#{SecureRandom.hex(10)}"
middleware.use ActionDispatch::Session::MemCacheStore,
key: "_session_id", namespace: "mem_cache_store_test:#{SecureRandom.hex(10)}",
memcache_server: ENV["MEMCACHE_SERVERS"] || "localhost:11211"
middleware.delete ActionDispatch::ShowExceptions
end

Expand Down
27 changes: 24 additions & 3 deletions actionview/Rakefile
Expand Up @@ -31,22 +31,43 @@ namespace :test do

desc "Run tests for rails-ujs"
task :ujs do
system("npm run lint")
exit $?.exitstatus unless $?.success?

begin
listen_host = "localhost"
listen_port = "4567"

runner_command = %w(ruby ../ci/qunit-selenium-runner.rb)
if ENV["SELENIUM_DRIVER_URL"]
require "socket"
runner_command += %W(http://#{Socket.gethostname}:#{listen_port}/ #{ENV["SELENIUM_DRIVER_URL"]})
listen_host = "0.0.0.0"
else
runner_command += %W(http://localhost:#{listen_port}/)
end

Dir.mkdir("log")
pid = spawn("bundle exec rackup test/ujs/config.ru -p 4567 -s puma > log/test.log 2>&1", pgroup: true)
pid = File.open("log/test.log", "w") do |f|
spawn(*%W(rackup test/ujs/config.ru -o #{listen_host} -p #{listen_port} -s puma), out: f, err: f, pgroup: true)
end

start_time = Time.now

loop do
break if system("lsof -i :4567", 1 => File::NULL)

if Time.now - start_time > 5
puts "Timed out after 5 seconds"
puts "Failed to start puma after 5 seconds"
puts
puts File.read("log/test.log")
exit 1
end

sleep 0.2
end

system("npm run lint && bundle exec ruby ../ci/qunit-selenium-runner.rb http://localhost:4567/")
system(*runner_command)
status = $?.exitstatus
ensure
Process.kill("KILL", -pid) if pid
Expand Down
1 change: 1 addition & 0 deletions actionview/test/ujs/server.rb
Expand Up @@ -23,6 +23,7 @@ class Server < Rails::Application
config.public_file_server.enabled = true
config.logger = Logger.new(STDOUT)
config.log_level = :error
config.hosts << proc { true }

config.content_security_policy do |policy|
policy.default_src :self, :https
Expand Down
1 change: 1 addition & 0 deletions activejob/test/support/integration/adapters/backburner.rb
Expand Up @@ -4,6 +4,7 @@ module BackburnerJobsManager
def setup
ActiveJob::Base.queue_adapter = :backburner
Backburner.configure do |config|
config.beanstalk_url = ENV["BEANSTALK_URL"] if ENV["BEANSTALK_URL"]
config.logger = Rails.logger
end
unless can_run?
Expand Down
2 changes: 1 addition & 1 deletion activejob/test/support/integration/adapters/resque.rb
Expand Up @@ -3,7 +3,7 @@
module ResqueJobsManager
def setup
ActiveJob::Base.queue_adapter = :resque
Resque.redis = Redis::Namespace.new "active_jobs_int_test", redis: Redis.new(url: "redis://127.0.0.1:6379/12", thread_safe: true)
Resque.redis = Redis::Namespace.new "active_jobs_int_test", redis: Redis.new(url: ENV["REDIS_URL"] || "redis://127.0.0.1:6379/12", thread_safe: true)
Resque.logger = Rails.logger
unless can_run?
puts "Cannot run integration tests for resque. To be able to run integration tests for resque you need to install and start redis.\n"
Expand Down
2 changes: 1 addition & 1 deletion activejob/test/support/integration/adapters/sneakers.rb
Expand Up @@ -7,7 +7,7 @@ module SneakersJobsManager
def setup
ActiveJob::Base.queue_adapter = :sneakers
Sneakers.configure heartbeat: 2,
amqp: "amqp://guest:guest@localhost:5672",
amqp: ENV["RABBITMQ_URL"] || "amqp://guest:guest@localhost:5672",
vhost: "/",
exchange: "active_jobs_sneakers_int_test",
exchange_type: :direct,
Expand Down
13 changes: 9 additions & 4 deletions activerecord/Rakefile
Expand Up @@ -89,18 +89,23 @@ end

namespace :db do
namespace :mysql do
connection_arguments = lambda do |connection_name|
config = ARTest.config["connections"]["mysql2"][connection_name]
["--user=#{config["username"]}", "--password=#{config["password"]}", ("--host=#{config["host"]}" if config["host"])].join(" ")
end

desc "Build the MySQL test databases"
task :build do
config = ARTest.config["connections"]["mysql2"]
%x( mysql --user=#{config["arunit"]["username"]} --password=#{config["arunit"]["password"]} -e "create DATABASE #{config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
%x( mysql --user=#{config["arunit2"]["username"]} --password=#{config["arunit2"]["password"]} -e "create DATABASE #{config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
%x( mysql #{connection_arguments["arunit"]} -e "create DATABASE #{config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
%x( mysql #{connection_arguments["arunit2"]} -e "create DATABASE #{config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
end

desc "Drop the MySQL test databases"
task :drop do
config = ARTest.config["connections"]["mysql2"]
%x( mysqladmin --user=#{config["arunit"]["username"]} --password=#{config["arunit"]["password"]} -f drop #{config["arunit"]["database"]} )
%x( mysqladmin --user=#{config["arunit2"]["username"]} --password=#{config["arunit2"]["password"]} -f drop #{config["arunit2"]["database"]} )
%x( mysqladmin #{connection_arguments["arunit"]} -f drop #{config["arunit"]["database"]} )
%x( mysqladmin #{connection_arguments["arunit2"]} -f drop #{config["arunit2"]["database"]} )
end

desc "Rebuild the MySQL test databases"
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/config.example.yml
Expand Up @@ -54,10 +54,16 @@ connections:
username: rails
encoding: utf8mb4
collation: utf8mb4_unicode_ci
<% if ENV['MYSQL_HOST'] %>
host: <%= ENV['MYSQL_HOST'] %>
<% end %>
arunit2:
username: rails
encoding: utf8mb4
collation: utf8mb4_general_ci
<% if ENV['MYSQL_HOST'] %>
host: <%= ENV['MYSQL_HOST'] %>
<% end %>

oracle:
arunit:
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/cache/stores/redis_cache_store_test.rb
Expand Up @@ -204,7 +204,7 @@ def emulating_latency
class RedisDistributedConnectionPoolBehaviourTest < ConnectionPoolBehaviourTest
private
def store_options
{ url: %w[ redis://localhost:6379/0 redis://localhost:6379/0 ] }
{ url: [ENV["REDIS_URL"] || "redis://localhost:6379/0"] * 2 }
end
end

Expand Down
18 changes: 12 additions & 6 deletions ci/qunit-selenium-runner.rb
@@ -1,14 +1,20 @@
# frozen_string_literal: true

require "qunit/selenium/test_runner"
require "chromedriver-helper"

driver_options = Selenium::WebDriver::Chrome::Options.new
driver_options.add_argument("--headless")
driver_options.add_argument("--disable-gpu")
driver_options.add_argument("--no-sandbox")
if ARGV[1]
driver = ::Selenium::WebDriver.for(:remote, url: ARGV[1], desired_capabilities: :chrome)
else
require "chromedriver-helper"

driver_options = Selenium::WebDriver::Chrome::Options.new
driver_options.add_argument("--headless")
driver_options.add_argument("--disable-gpu")
driver_options.add_argument("--no-sandbox")

driver = ::Selenium::WebDriver.for(:chrome, options: driver_options)
end

driver = ::Selenium::WebDriver.for(:chrome, options: driver_options)
result = QUnit::Selenium::TestRunner.new(driver).open(ARGV[0], timeout: 60)
driver.quit

Expand Down
11 changes: 11 additions & 0 deletions guides/Rakefile
Expand Up @@ -88,4 +88,15 @@ HELP
end
end

task :test do
templates = Dir.glob("bug_report_templates/*.rb")
counter = templates.count do |file|
puts "--- Running #{file}"
Bundler.clean_system(Gem.ruby, "-w", file) ||
puts("+++ 💥 FAILED (exit #{$?.exitstatus})")
end
puts "+++ #{counter} / #{templates.size} templates executed successfully"
exit 1 if counter < templates.size
end

task default: "guides:help"
21 changes: 17 additions & 4 deletions railties/Rakefile
Expand Up @@ -36,10 +36,20 @@ namespace :test do
dirs = (ENV["TEST_DIR"] || ENV["TEST_DIRS"] || "**").split(",")
test_options = ENV["TESTOPTS"].to_s.split(/[\s]+/)

test_files = dirs.map { |dir| "test/#{dir}/*_test.rb" }
Dir[*test_files].each do |file|
next true if file.start_with?("test/fixtures/")
test_patterns = dirs.map { |dir| "test/#{dir}/*_test.rb" }
test_files = Dir[*test_patterns].select do |file|
!file.start_with?("test/fixtures/")
end.sort

if ENV["BUILDKITE_PARALLEL_JOB_COUNT"]
n = ENV["BUILDKITE_PARALLEL_JOB"].to_i
m = ENV["BUILDKITE_PARALLEL_JOB_COUNT"].to_i

test_files = test_files.each_slice(m).map { |slice| slice[n] }.compact
end

test_files.each do |file|
puts "--- #{file}"
fake_command = Shellwords.join([
FileUtils::RUBY,
"-w",
Expand All @@ -59,18 +69,21 @@ namespace :test do

unless $?.success?
failing_files << file
puts "^^^ +++"
end
end

puts "--- All tests completed"
unless failing_files.empty?
puts "^^^ +++"
puts
puts "Failed in:"
failing_files.each do |file|
puts " #{file}"
end
puts

raise "Failure in isolated test runner"
exit 1
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -93,6 +93,8 @@ def with_database_existing

def with_bad_permissions
Dir.chdir(app_path) do
skip "Can't avoid permissions as root" if Process.uid.zero?

set_database_url
FileUtils.chmod("-w", "db")
yield
Expand Down
22 changes: 12 additions & 10 deletions railties/test/application/server_test.rb
Expand Up @@ -29,16 +29,18 @@ def teardown
primary, replica = PTY.open
pid = nil

begin
pid = Process.spawn("#{app_path}/bin/rails server -P tmp/dummy.pid", in: replica, out: replica, err: replica)
assert_output("Listening", primary)

rails("restart")

assert_output("Restarting", primary)
assert_output("Inherited", primary)
ensure
kill(pid) if pid
Bundler.with_original_env do
begin
pid = Process.spawn("bin/rails server -P tmp/dummy.pid", chdir: app_path, in: replica, out: replica, err: replica)
assert_output("Listening", primary)

rails("restart")

assert_output("Restarting", primary)
assert_output("Inherited", primary)
ensure
kill(pid) if pid
end
end
end

Expand Down
1 change: 1 addition & 0 deletions railties/test/application/url_generation_test.rb
Expand Up @@ -19,6 +19,7 @@ class MyApp < Rails::Application
config.session_store :cookie_store, key: "_myapp_session"
config.active_support.deprecation = :log
config.eager_load = false
config.hosts << proc { true }
end

Rails.application.initialize!
Expand Down

0 comments on commit 287920c

Please sign in to comment.