diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..23c7c19 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,70 @@ +name: CI +on: + push: + pull_request: + +jobs: + test: + runs-on: ubuntu-20.04 + + strategy: + matrix: + include: + - { ruby: '2.2', gemfile: './gemfiles/rails_5.0.gemfile', middleware: 'true' } + - { ruby: '2.2', gemfile: './gemfiles/rails_5.0.gemfile', middleware: 'false' } + - { ruby: '2.3', gemfile: './gemfiles/rails_5.0.gemfile', middleware: 'true' } + - { ruby: '2.3', gemfile: './gemfiles/rails_5.0.gemfile', middleware: 'false' } + - { ruby: '2.2', gemfile: './gemfiles/rails_5.1.gemfile', middleware: 'true' } + - { ruby: '2.2', gemfile: './gemfiles/rails_5.1.gemfile', middleware: 'false' } + - { ruby: '2.3', gemfile: './gemfiles/rails_5.1.gemfile', middleware: 'true' } + - { ruby: '2.3', gemfile: './gemfiles/rails_5.1.gemfile', middleware: 'false' } + - { ruby: '2.4', gemfile: './gemfiles/rails_5.1.gemfile', middleware: 'true' } + - { ruby: '2.4', gemfile: './gemfiles/rails_5.1.gemfile', middleware: 'false' } + - { ruby: '2.5', gemfile: './gemfiles/rails_6.0.gemfile', middleware: 'true' } + - { ruby: '2.5', gemfile: './gemfiles/rails_6.0.gemfile', middleware: 'false' } + - { ruby: '2.6', gemfile: './gemfiles/rails_6.0.gemfile', middleware: 'true' } + - { ruby: '2.6', gemfile: './gemfiles/rails_6.0.gemfile', middleware: 'false' } + - { ruby: '2.5', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'true' } + - { ruby: '2.5', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'false' } + - { ruby: '2.6', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'true' } + - { ruby: '2.6', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'false' } + - { ruby: '2.7', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'true' } + - { ruby: '2.7', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'false' } + - { ruby: '3.0', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'true' } + - { ruby: '3.0', gemfile: './gemfiles/rails_6.1.gemfile', middleware: 'false' } + - { ruby: '2.7', gemfile: './gemfiles/rails_7.0.gemfile', middleware: 'true' } + - { ruby: '2.7', gemfile: './gemfiles/rails_7.0.gemfile', middleware: 'false' } + - { ruby: '3.0', gemfile: './gemfiles/rails_7.0.gemfile', middleware: 'true' } + - { ruby: '3.0', gemfile: './gemfiles/rails_7.0.gemfile', middleware: 'false' } + - { ruby: '3.1', gemfile: './gemfiles/rails_7.0.gemfile', middleware: 'true' } + - { ruby: '3.1', gemfile: './gemfiles/rails_7.0.gemfile', middleware: 'false' } + - { ruby: '2.7', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'true' } + - { ruby: '2.7', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'false' } + - { ruby: '3.0', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'true' } + - { ruby: '3.0', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'false' } + - { ruby: '3.1', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'true' } + - { ruby: '3.1', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'false' } + - { ruby: '3.2', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'true' } + - { ruby: '3.2', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'false' } + - { ruby: '3.3', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'true' } + - { ruby: '3.3', gemfile: './gemfiles/rails_7.1.gemfile', middleware: 'false' } + - { ruby: '3.1', gemfile: './gemfiles/rails_7.2.gemfile', middleware: 'true' } + - { ruby: '3.1', gemfile: './gemfiles/rails_7.2.gemfile', middleware: 'false' } + - { ruby: '3.2', gemfile: './gemfiles/rails_7.2.gemfile', middleware: 'true' } + - { ruby: '3.2', gemfile: './gemfiles/rails_7.2.gemfile', middleware: 'false' } + - { ruby: '3.3', gemfile: './gemfiles/rails_7.2.gemfile', middleware: 'true' } + - { ruby: '3.3', gemfile: './gemfiles/rails_7.2.gemfile', middleware: 'false' } + env: + BUNDLE_GEMFILE: ${{ matrix.gemfile }} + MIDDLEWARE: ${{ matrix.middleware }} + steps: + - uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + + - name: Run tests + run: bundle exec rake diff --git a/.gitignore b/.gitignore index e2b4423..c250371 100644 --- a/.gitignore +++ b/.gitignore @@ -13,19 +13,21 @@ lib/bundler/man pkg rdoc spec/reports -test/tmp -test/version_tmp -tmp +spec/dummy/db/*.sqlite3* +spec/dummy/log/* .ruby-version ## PROJECT::SPECIFIC bin/ -test/bin/ railsapps/ -test/*.gemfile.lock +gemfiles/*.gemfile.lock ,* .vagrant ubuntu*console.log - +tmp # See: https://gist.github.com/ianheggie/9327010 # for Global git ignore for OS/IDE/temp/backup files + +spec/dummy/tmp/* +!spec/dummy/tmp/.keep +spec/dummy/db/*.sqlite3 diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7c02aef..0000000 --- a/.travis.yml +++ /dev/null @@ -1,218 +0,0 @@ -dest: xenial -os: linux -# Released April 2018, Support ended: April 2023 -# ruby 2.4.9, 2.5.3, 2.5.7, 2.6.5 (default) and 2.7.0 pre-installed on travis -addons: - apt: - packages: - - net-tools - -cache: bundler -language: ruby -notifications: - email: - on_success: change - on_failure: always - -before_install: - - gem update --system $RUBYGEMS_VERSION - - gem --version - - gem install bundler ${BUNDLER_VERSION:+-v} ${BUNDLER_VERSION} - - gem install smarter_bundler - - bundle --version - - mkdir -p tmp/bundle - -bundler_args: "--binstubs" - -script: ./test/test_with_railsapp - -jobs: - fast_finish: true - - allow_failures: - - - rvm: 3.0.1 - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - - rvm: 3.0.1 - gemfile: test/rails_edge.gemfile - env: - - RAILS_VERSION=edge - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - - - rvm: ruby-head - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - - - rvm: ruby-head - gemfile: test/rails_edge.gemfile - env: - - RAILS_VERSION=edge - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - - - include: - # ------------------------------------- - # Standard - - # ruby 5.0 Jun. 2016 - - - rvm: 2.2.2 - gemfile: test/rails_5.0.gemfile - env: - - RAILS_VERSION=5.0 - - MIDDLEWARE=no - - RUBYGEMS_VERSION=2.7.11 - - BUNDLER_VERSION=1.17.3 - - - rvm: 2.3.6 - gemfile: test/rails_5.0.gemfile - env: - - RAILS_VERSION=5.0 - - MIDDLEWARE=no - - # rails 5.1 sometime before May 2017 - - - rvm: 2.2.2 - gemfile: test/rails_5.1.gemfile - env: - - RAILS_VERSION=5.1 - - MIDDLEWARE=no - - RUBYGEMS_VERSION=2.7.11 - - BUNDLER_VERSION=1.17.3 - - - rvm: 2.3.6 - gemfile: test/rails_5.1.gemfile - env: - - RAILS_VERSION=5.1 - - MIDDLEWARE=no - - BUNDLER_VERSION=1.17.3 - - - rvm: 2.4.3 - gemfile: test/rails_5.1.gemfile - env: - - RAILS_VERSION=5.1 - - MIDDLEWARE=no - - # ------------------------------------- - # Middleware - - # ruby 5.0 Jun. 2016 - - - rvm: 2.2.2 - gemfile: test/rails_5.0.gemfile - env: - - RAILS_VERSION=5.0 - - MIDDLEWARE=yes - - RUBYGEMS_VERSION=2.7.11 - - BUNDLER_VERSION=1.17.3 - - - rvm: 2.3.6 - gemfile: test/rails_5.0.gemfile - env: - - RAILS_VERSION=5.0 - - MIDDLEWARE=yes - - # rails 5.1 sometime before May 2017 - - - rvm: 2.2.2 - gemfile: test/rails_5.1.gemfile - env: - - RAILS_VERSION=5.1 - - MIDDLEWARE=yes - - RUBYGEMS_VERSION=2.7.11 - - BUNDLER_VERSION=1.17.3 - - - rvm: 2.4.3 - gemfile: test/rails_5.1.gemfile - env: - - RAILS_VERSION=5.1 - - MIDDLEWARE=yes - - - # rails 5.2 April 2018 - # ------------------------------------- - # INSTALLED AS MIDDLEWARE - - - # rails 6.0 - - - rvm: 2.5.0 - gemfile: test/rails_6.0.gemfile - env: - - RAILS_VERSION=6.0 - - MIDDLEWARE=YES - - - rvm: 2.6.7 - gemfile: test/rails_6.0.gemfile - env: - - RAILS_VERSION=6.0 - - MIDDLEWARE=YES - - # Not testing past recommended ruby version as listed on https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html - - # -------------------------------------- - - rvm: 2.5.0 - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - - rvm: 2.6.6 - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - - rvm: 2.7.3 - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - - rvm: 3.0.1 - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - - # ------------------ - # FIXME - # ------------------ - - - rvm: 3.0.1 - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - - rvm: 3.0.1 - gemfile: test/rails_edge.gemfile - env: - - RAILS_VERSION=edge - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - - - rvm: ruby-head - gemfile: test/rails_6.1.gemfile - env: - - RAILS_VERSION=6.1 - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - - - rvm: ruby-head - gemfile: test/rails_edge.gemfile - env: - - RAILS_VERSION=edge - - MIDDLEWARE=YES - - SMTP_STARTTLS=DISABLED - diff --git a/Gemfile b/Gemfile index ecc8bb8..7ddb29c 100644 --- a/Gemfile +++ b/Gemfile @@ -3,18 +3,3 @@ source 'https://rubygems.org' # Specify your gem's dependencies in health_check.gemspec gemspec - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', '~> 1.3.7' - end - # run travis-lint to check .travis.yml - gem 'travis-lint' - # mime-types 2.0 requires Ruby version >= 1.9.2 - # mime-types 3.0 requires Ruby version >= 2.0 - gem 'mime-types', defined?(JRUBY_VERSION) || RUBY_VERSION < '2.0' ? '< 3' : '>= 3.0' - -end diff --git a/README.rdoc b/README.rdoc index 1d82787..a15642b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -175,11 +175,11 @@ To change the configuration of health_check, create a file `config/initializers/ config.on_failure do |checks, msg| # log msg somewhere end - + config.on_success do |checks| # flag that everything is well end - + end @@ -292,7 +292,6 @@ Last-modified is set to the current time (rounded down to a multiple of max_age === Automated testing and other checks * {Gem Version}[http://badge.fury.io/rb/health_check] - Latest Gem -* {}[https://travis-ci.org/ianheggie/health_check] - Travis CI * {}[https://codeclimate.com/github/ianheggie/health_check] - Code quality * {}[https://gemnasium.com/ianheggie/health_check] - Gem dependencies diff --git a/Rakefile b/Rakefile index ee0f3c1..50ca0d1 100644 --- a/Rakefile +++ b/Rakefile @@ -3,12 +3,11 @@ require "bundler/gem_tasks" #require 'rubygems' require 'rake' -#tests as gem -task :test do - exec '/bin/bash', './test/test_with_railsapp' -end +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) -task default: :test +task :default => :spec begin gem 'rdoc' diff --git a/Vagrantfile b/Vagrantfile deleted file mode 100644 index 85cd839..0000000 --- a/Vagrantfile +++ /dev/null @@ -1,32 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -Vagrant.configure("2") do |config| - # For a complete reference, please see the online documentation at - # https://docs.vagrantup.com. - - config.vm.box = "ubuntu/focal64" - - # set auto_update to false, if you do NOT want to check the correct - # additions version when booting this machine - config.vbguest.auto_update = false - - # do NOT download the iso file from a webserver - config.vbguest.no_remote = true - - # provision with a shell script. - config.vm.provision "shell", path: "./test/provision_vagrant" - - config.vm.provider "virtualbox" do |v| - # travis allocates 7.5 GB, but this is sufficient - v.memory = 2048 - v.cpus = 2 - end - -# if File.file?('.git') && IO.read('.git') =~ %r{\Agitdir: (.+)/.git/worktrees.*} -# # Handle git worktrees ... -# path = $1 -# config.vm.synced_folder path, path -# end - -end diff --git a/gemfiles/rails_5.0.gemfile b/gemfiles/rails_5.0.gemfile new file mode 100644 index 0000000..9cf70e1 --- /dev/null +++ b/gemfiles/rails_5.0.gemfile @@ -0,0 +1,10 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 5.0.0' +gem 'rake', '>= 0.8.7' +gem 'sqlite3', "~> 1.3.7" +gem 'loofah', '<= 2.20.0' + +gemspec(path: '../') diff --git a/gemfiles/rails_5.1.gemfile b/gemfiles/rails_5.1.gemfile new file mode 100644 index 0000000..b5f7dfd --- /dev/null +++ b/gemfiles/rails_5.1.gemfile @@ -0,0 +1,10 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 5.1.0' +gem 'rake', '>= 0.8.7' +gem 'sqlite3', "~> 1.3.7" +gem 'loofah', '<= 2.20.0' + +gemspec(path: '../') diff --git a/gemfiles/rails_5.2.gemfile b/gemfiles/rails_5.2.gemfile new file mode 100644 index 0000000..b62f89d --- /dev/null +++ b/gemfiles/rails_5.2.gemfile @@ -0,0 +1,9 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 5.2.0' +gem 'rake', '>= 0.8.7' +gem 'sqlite3', "~> 1.3.7" + +gemspec(path: '../') diff --git a/gemfiles/rails_6.0.gemfile b/gemfiles/rails_6.0.gemfile new file mode 100644 index 0000000..6627b14 --- /dev/null +++ b/gemfiles/rails_6.0.gemfile @@ -0,0 +1,9 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 6.0.0' +gem 'rake', '>= 0.8.7' +gem 'sqlite3', "~> 1.4" + +gemspec(path: '../') diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile new file mode 100644 index 0000000..d457000 --- /dev/null +++ b/gemfiles/rails_6.1.gemfile @@ -0,0 +1,9 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 6.1.0' +gem 'rake', '>= 0.8.7' +gem 'sqlite3', "~> 1.4" + +gemspec(path: '../') diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile new file mode 100644 index 0000000..41229ac --- /dev/null +++ b/gemfiles/rails_7.0.gemfile @@ -0,0 +1,8 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 7.0.0' +gem 'sqlite3', "~> 1.4" + +gemspec(path: '../') diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile new file mode 100644 index 0000000..b3a88cb --- /dev/null +++ b/gemfiles/rails_7.1.gemfile @@ -0,0 +1,8 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 7.1.0' +gem 'sqlite3', "~> 1.4" + +gemspec(path: '../') diff --git a/gemfiles/rails_7.2.gemfile b/gemfiles/rails_7.2.gemfile new file mode 100644 index 0000000..ae477a7 --- /dev/null +++ b/gemfiles/rails_7.2.gemfile @@ -0,0 +1,8 @@ +# Gemfile for health_test testing + +source 'https://rubygems.org' + +gem 'rails', '~> 7.2.0' +gem 'sqlite3', "~> 1.4" + +gemspec(path: '../') diff --git a/health_check.gemspec b/health_check.gemspec index b2d9766..ab6db2d 100644 --- a/health_check.gemspec +++ b/health_check.gemspec @@ -22,8 +22,6 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.required_ruby_version = '>= 2.2.2' gem.add_dependency(%q, [">= 5.0"]) - gem.add_development_dependency(%q, [">= 0.1.0"]) - gem.add_development_dependency(%q, [">= 0.8.3"]) - gem.add_development_dependency(%q, ["~> 2.11.0"]) - gem.add_development_dependency(%q, [">= 1.2"]) + gem.add_development_dependency(%q) + gem.add_development_dependency(%q) end diff --git a/lib/health_check/utils.rb b/lib/health_check/utils.rb index 64a8d89..0077e4b 100644 --- a/lib/health_check/utils.rb +++ b/lib/health_check/utils.rb @@ -145,7 +145,15 @@ def self.check_smtp(settings, timeout) status = '250' else smtp = Net::SMTP.new(settings[:address], settings[:port]) - smtp.enable_starttls if settings[:enable_starttls_auto] + openssl_verify_mode = settings[:openssl_verify_mode] + + if openssl_verify_mode.kind_of?(String) + openssl_verify_mode = OpenSSL::SSL.const_get("VERIFY_#{openssl_verify_mode.upcase}") + end + + ssl_context = Net::SMTP.default_ssl_context + ssl_context.verify_mode = openssl_verify_mode if openssl_verify_mode + smtp.enable_starttls(ssl_context) if settings[:enable_starttls_auto] smtp.open_timeout = timeout smtp.read_timeout = timeout smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do @@ -162,7 +170,7 @@ def self.check_cache t = Time.now.to_i value = "ok #{t}" ret = ::Rails.cache.read('__health_check_cache_test__') - if ret.to_s =~ /^ok (\d+)$/ + if ret.to_s =~ /^ok (\d+)$/ diff = ($1.to_i - t).abs return('Cache expiry is broken. ') if diff > 30 elsif ret @@ -170,7 +178,7 @@ def self.check_cache end if ::Rails.cache.write('__health_check_cache_test__', value, expires_in: 2.seconds) ret = ::Rails.cache.read('__health_check_cache_test__') - if ret =~ /^ok (\d+)$/ + if ret =~ /^ok (\d+)$/ diff = ($1.to_i - t).abs (diff < 2 ? '' : 'Out of date cache or time is skewed. ') else diff --git a/spec/dummy/Rakefile b/spec/dummy/Rakefile new file mode 100644 index 0000000..d9ba6ca --- /dev/null +++ b/spec/dummy/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require File.expand_path('./fake_app', __dir__) + +Rails.application.load_tasks diff --git a/spec/dummy/app/assets/config/manifest.js b/spec/dummy/app/assets/config/manifest.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spec/dummy/app/assets/config/manifest.js @@ -0,0 +1 @@ + diff --git a/spec/dummy/app/controllers/example_controller.rb b/spec/dummy/app/controllers/example_controller.rb new file mode 100644 index 0000000..c05e0b7 --- /dev/null +++ b/spec/dummy/app/controllers/example_controller.rb @@ -0,0 +1,5 @@ +class ExampleController < ActionController::Base + def index + render plain: 'OK' + end +end diff --git a/spec/dummy/config/database.yml b/spec/dummy/config/database.yml new file mode 100644 index 0000000..4b507df --- /dev/null +++ b/spec/dummy/config/database.yml @@ -0,0 +1,10 @@ +default: &default + adapter: sqlite3 + +development: + <<: *default + database: db/development.sqlite3 + +test: + <<: *default + database: db/test.sqlite3 diff --git a/spec/dummy/config/initializers/health_check.rb b/spec/dummy/config/initializers/health_check.rb new file mode 100644 index 0000000..74fe79b --- /dev/null +++ b/spec/dummy/config/initializers/health_check.rb @@ -0,0 +1,16 @@ +CUSTOM_CHECK_FILE_PATH = 'spec/dummy/tmp/custom_file' + +HealthCheck.setup do |config| + config.success = "custom_success_message" + config.http_status_for_error_text = 550 + config.http_status_for_error_object = 555 + config.uri = 'custom_route_prefix' + + config.add_custom_check do + File.exist?(CUSTOM_CHECK_FILE_PATH) ? '' : 'custom_file is missing!' + end + + config.add_custom_check('pass') do + '' + end +end diff --git a/spec/dummy/config/initializers/middleware.rb b/spec/dummy/config/initializers/middleware.rb new file mode 100644 index 0000000..9b5d51f --- /dev/null +++ b/spec/dummy/config/initializers/middleware.rb @@ -0,0 +1 @@ +FakeApp.config.middleware.insert_after Rails::Rack::Logger, HealthCheck::MiddlewareHealthcheck if ENV['MIDDLEWARE'] == 'true' diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb new file mode 100644 index 0000000..35e7528 --- /dev/null +++ b/spec/dummy/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + get 'example' => 'example#index' +end diff --git a/spec/dummy/db/migrate/.keep b/spec/dummy/db/migrate/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/dummy/fake_app.rb b/spec/dummy/fake_app.rb new file mode 100644 index 0000000..ee11dc8 --- /dev/null +++ b/spec/dummy/fake_app.rb @@ -0,0 +1,16 @@ +Bundler.setup +require 'rails' +require 'rails/all' +require 'health_check' +Bundler.require + +FakeApp = Class.new(Rails::Application) +ENV['RAILS_ENV'] ||= 'test' +FakeApp.config.session_store :cookie_store, key: '_myapp_session' +FakeApp.config.root = File.dirname(__FILE__) +FakeApp.config.action_mailer.delivery_method = :smtp +FakeApp.config.action_mailer.smtp_settings = { address: "localhost", port: 3555, openssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, enable_starttls_auto: true } +FakeApp.config.secret_key_base = SecureRandom.hex(64) +FakeApp.initialize! + + diff --git a/spec/dummy/tmp/.keep b/spec/dummy/tmp/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/fake_smtp_server b/spec/fake_smtp_server.rb similarity index 83% rename from test/fake_smtp_server rename to spec/fake_smtp_server.rb index 0eb9014..73e951d 100755 --- a/test/fake_smtp_server +++ b/spec/fake_smtp_server.rb @@ -59,6 +59,11 @@ def start close end + def finish + sleep 1 + @socket.close + end + private def close @@ -66,6 +71,8 @@ def close @orig_client.close unless @orig_client.nil? end + + def send(line) @client.puts line puts "-> #{line}" @@ -89,7 +96,6 @@ def ssl_context context.key = key context.cert = cert context.verify_mode = OpenSSL::SSL::VERIFY_NONE - context.min_version = nil context end end @@ -130,28 +136,3 @@ def generate_certificate end end -FakeSmtpServer.new(3555).start - -puts 'fake_smtp_server: Exiting now the conversation has finished.' -exit 0 - -# Tested with irb script: -# require 'net/smtp' -# -# status = '' -# begin -# if @skip_external_checks -# status = '250' -# else -# smtp = Net::SMTP.new('localhost', 3555) -# smtp.enable_starttls -# smtp.open_timeout = 10 -# smtp.read_timeout = 10 -# smtp.start('domain', 'user_name', 'password', :plain) do -# status = smtp.helo('domain').status -# end -# end -# rescue Exception => ex -# status = ex.to_s -# end -# (status =~ /^250/) ? 'PASS' : "FAILED SMTP: #{status || 'unexpected error'}. " diff --git a/spec/fixtures/migrate/9_create_countries.rb b/spec/fixtures/migrate/9_create_countries.rb new file mode 100644 index 0000000..ecf8c63 --- /dev/null +++ b/spec/fixtures/migrate/9_create_countries.rb @@ -0,0 +1,7 @@ +class CreateCountries < ActiveRecord::Migration::Current + def change + create_table :countries do |t| + t.column :name, :string + end + end +end diff --git a/spec/health_check_spec.rb b/spec/health_check_spec.rb new file mode 100644 index 0000000..0e360d6 --- /dev/null +++ b/spec/health_check_spec.rb @@ -0,0 +1,294 @@ +require 'spec_helper' + +RSpec.describe HealthCheck, type: :request do + context '/custom_route_prefix' do + it 'works with smtp server and valid custom_check' do + enable_custom_check do + start_smtp_server do + get '/custom_route_prefix' + expect(response).to be_ok + end + end + end + + it 'fails with no smtp server and valid custom_check' do + enable_custom_check do + get '/custom_route_prefix' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + + it 'fails with smtp server and invalid custom_check' do + start_smtp_server do + get '/custom_route_prefix' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + end + + context '/custom_route_prefix/all' do + it 'works with smtp server and valid custom_check' do + enable_custom_check do + start_smtp_server do + get '/custom_route_prefix/all' + expect(response).to be_ok + end + end + end + + it 'fails with no smtp server and valid custom_check' do + enable_custom_check do + get '/custom_route_prefix/all' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + + it 'fails with smtp server and invalid custom_check' do + start_smtp_server do + get '/custom_route_prefix/all' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + end + + context '/custom_route_prefix/migration' do + before { reconnect_database } + after do + Dir.glob('spec/dummy/db/migrate/*').each do |f| + FileUtils.rm(f) + end + FileUtils.rm('spec/dummy/db/schema.rb') if File.exist?('spec/dummy/db/schema.rb') + FileUtils.cd(FakeApp.config.root) do + ActiveRecord::Tasks::DatabaseTasks.drop_current + end + end + + it 'works with no migration files' do + get '/custom_route_prefix/migration' + expect(response).to be_ok + end + + it 'fails with pending migration files' do + FileUtils.cp('spec/fixtures/migrate/9_create_countries.rb', 'spec/dummy/db/migrate/9_create_countries.rb') + FileUtils.cd(FakeApp.config.root) do + get '/custom_route_prefix/migration' + end + expect(response.status).to eq(550) + end + + it 'works with applied migration files' do + FileUtils.cp('spec/fixtures/migrate/9_create_countries.rb', 'spec/dummy/db/migrate/9_create_countries.rb') + FileUtils.cd(FakeApp.config.root) do + db_migrate + get '/custom_route_prefix/migration' + end + expect(response).to be_ok + end + end + + describe '/custom_route_prefix/database' do + after do + Dir.glob('spec/dummy/db/migrate/*').each do |f| + FileUtils.rm(f) + end + FileUtils.rm('spec/dummy/db/schema.rb') if File.exist?('spec/dummy/db/schema.rb') + FileUtils.cd(FakeApp.config.root) do + ActiveRecord::Tasks::DatabaseTasks.drop_current + end + end + + it 'works with no database' do + get '/custom_route_prefix/database' + expect(response).to be_ok + end + + it 'works with valid database' do + FileUtils.cp('spec/fixtures/migrate/9_create_countries.rb', 'spec/dummy/db/migrate/9_create_countries.rb') + FileUtils.cd(FakeApp.config.root) do + db_migrate + get '/custom_route_prefix/database' + end + expect(response).to be_ok + end + + it 'fails with invalid database' do + disconnect_database + Rails.root.join('db/test.sqlite3').write('invalid') + get '/custom_route_prefix/database' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + + describe '/custom_route_prefix/email' do + it 'works with smtp server' do + start_smtp_server do + get '/custom_route_prefix/email' + expect(response).to be_ok + end + end + + it 'fails with no smtp server' do + get '/custom_route_prefix/email' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + + describe '/custom_route_prefix/pass (a custom check does nothing)' do + it 'works if another custom check is invalid' do + get '/custom_route_prefix/pass' + expect(response).to be_ok + end + + it 'works if another custom check is valid' do + enable_custom_check do + get '/custom_route_prefix/pass' + expect(response).to be_ok + end + end + end + + describe '/heath_check/custom' do + it 'works with valid custom check' do + enable_custom_check do + get '/custom_route_prefix/custom' + end + expect(response).to be_ok + end + + it 'fails with invalid custom check' do + get '/custom_route_prefix/custom' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + + context 'specified format' do + it 'returns plain text if client requests html format' do + enable_custom_check do + get '/custom_route_prefix/custom.html' + end + expect(response).to be_ok + expect(response.content_type).to include('text/plain') + end + + it 'returns json if client requests json format' do + enable_custom_check do + get '/custom_route_prefix/custom.json' + end + expect(response).to be_ok + expect(response.content_type).to include('application/json') + expect(response.parsed_body).to include('healthy' => true, 'message' => 'custom_success_message') + end + + it 'returns json if client requests json format and custom check is invalid' do + get '/custom_route_prefix/custom.json' + expect(response.status).to eq(555) # config.http_status_for_error_object = 555 + expect(response.content_type).to include('application/json') + expect(response.parsed_body).to include('healthy' => false) + end + + it 'returns xml if client requests xml format' do + enable_custom_check do + get '/custom_route_prefix/custom.xml' + end + expect(response).to be_ok + expect(response.content_type).to include('application/xml') + expect(response.body).to include('true') + expect(response.body).to include('custom_success_message') + end + + it 'returns xml if client requests xml format and custom check is invalid' do + get '/custom_route_prefix/custom.xml' + expect(response.status).to eq(555) # config.http_status_for_error_object = 555 + expect(response.content_type).to include('application/xml') + expect(response.body).to include('false') + end + end + end + + describe '/custom_route_prefix/middleware' do + if ENV['MIDDLEWARE'] == 'true' + context 'when using middleware' do + it 'works with valid custom check' do + enable_custom_check do + get '/custom_route_prefix/middleware' + end + expect(response).to be_ok + end + end + end + if ENV['MIDDLEWARE'] != 'true' + context 'when not using middleware' do + it 'fails with invalid custom check' do + get '/custom_route_prefix/middleware' + expect(response.status).to eq(550) + expect(response.body).to include 'health_check failed' + end + end + end + end + + context 'whitelisted ip' do + after { HealthCheck.origin_ip_whitelist.clear } + + it 'works with access from valid ip address' do + HealthCheck.origin_ip_whitelist << '127.0.0.1' + get '/custom_route_prefix/site' + expect(response).to be_ok + end + + it 'fails with access from invalid ip address' do + HealthCheck.origin_ip_whitelist << '123.123.123.123' + get '/custom_route_prefix/site' + expect(response.status).to eq(403) + end + + it 'does not affect paths other than health_check' do + HealthCheck.origin_ip_whitelist << '123.123.123.123' + get '/example' + expect(response).to be_ok + end + end + + context 'basic auth' do + before do + HealthCheck.basic_auth_username = 'username' + HealthCheck.basic_auth_password = 'password' + end + + after do + HealthCheck.basic_auth_username = nil + HealthCheck.basic_auth_password = nil + end + + it 'works with valid credentials' do + get '/custom_route_prefix/site', headers: { 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials('username', 'password') } + expect(response).to be_ok + end + + it 'fails with wrong password' do + get '/custom_route_prefix/site', headers: { 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials('username', 'wrong_password') } + expect(response.status).to eq(401) + end + + it 'fails with wrong username' do + get '/custom_route_prefix/site', headers: { 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials('wrong_username', 'password') } + expect(response.status).to eq(401) + end + + it 'fails with no credentials' do + get '/custom_route_prefix/site' + expect(response.status).to eq(401) + end + + it 'does not affect paths other than health_check' do + get '/example' + expect(response).to be_ok + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a8bf327 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,44 @@ +require_relative './dummy/fake_app' +require 'rspec/rails' +require 'fake_smtp_server' + +def start_smtp_server(&block) + th = Thread.start do + server = FakeSmtpServer.new(3555) + server.start + server.finish + end + sleep 1 + block.call + socket = TCPSocket.open('localhost', 3555) + socket.write('QUIT') + socket.close + th.join +end + +def enable_custom_check(&block) + File.write(CUSTOM_CHECK_FILE_PATH, 'hello') + block.call +ensure + FileUtils.rm(CUSTOM_CHECK_FILE_PATH) if File.exist?(CUSTOM_CHECK_FILE_PATH) +end + +def disconnect_database + if Gem::Version.new(Rails.version) >= Gem::Version.new('7.1.0') + ActiveRecord::Tasks::DatabaseTasks.migration_connection.disconnect! + else + ActiveRecord::Base.connection.disconnect! + end +end + +def reconnect_database + if Gem::Version.new(Rails.version) >= Gem::Version.new('7.1.0') + ActiveRecord::Tasks::DatabaseTasks.migration_connection.reconnect! + else + ActiveRecord::Base.establish_connection + end +end + +def db_migrate + system 'RAILS_ENV=test bundle exec rake db:migrate' +end diff --git a/test/init_variables b/test/init_variables deleted file mode 100644 index 514f865..0000000 --- a/test/init_variables +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/bash - -# Any failure causes exit -set -eE -o functrace - -report_failure() { - local lineno=$2 - local fn=$3 - local exitstatus=$4 - local msg=$5 - local lineno_fns=${1% 0} - if [[ $lineno_fns != "0" ]] ; then - lineno="${lineno} ${lineno_fns}" - fi - if [[ $exitstatus == 0 ]] ; then - echo "${BASH_SOURCE[1]}: Finished!" - else - echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" - fi -} - -trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR - -echo Setting RAILS_ENV=test RACK_ENV=test -export RAILS_ENV=test RACK_ENV=test - -base_dir=$PWD -tmp_dir=$base_dir/tmp -railsapp=$tmp_dir/railsapp -custom_file="$railsapp/tmp/custom_check.ok" -catchall_file="$railsapp/tmp/catchall_route.enabled" -success=successful - -rehash='' -rbenv_which='which' - -if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then - echo "Detected user installed rvm" -elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then - echo "Detected root installed rvm" -elif [[ -d "$HOME/.rbenv" ]] ; then - echo "Detected rbenv: `rbenv version`" - rehash='rbenv rehash' - rbenv_which='rbenv which' -else - printf "Note: Neither rvm nor rbenv was not found.\n" -fi - -echo "Checking required commands exist:" -for cmd in bash gem egrep ls tail kill find cpio -do - echo -n " " - which $cmd || ( echo "Aborting setup_railsapp: Missing $cmd command!" && exit 2 ) -done -for cmd in ruby gem -do - echo -n " " - $rbenv_which $cmd || ( echo "Aborting setup_railsapp: Missing $cmd command!" && exit 2 ) -done - -rails="rails" -rake="rake" - -if [ -x $base_dir/test/bin/rails ] -then - rails="$base_dir/test/bin/rails" - rake="$base_dir/test/bin/rake" - export PATH="$base_dir/test/bin:$PATH" -fi - -if [ -x $railsapp/bin/rails ] -then - rails="$railsapp/bin/rails" - rake="$railsapp/bin/rake" - export PATH="$railsapp/bin:$PATH" -fi - -echo "Using rails=$rails, rake=$rake" - diff --git a/test/migrate/empty/do_not_remove.txt b/test/migrate/empty/do_not_remove.txt deleted file mode 100644 index bc71b2c..0000000 --- a/test/migrate/empty/do_not_remove.txt +++ /dev/null @@ -1 +0,0 @@ -Don't remove \ No newline at end of file diff --git a/test/migrate/nine/9_create_countries.rb b/test/migrate/nine/9_create_countries.rb deleted file mode 100644 index d9e7b9f..0000000 --- a/test/migrate/nine/9_create_countries.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateCountries < ActiveRecord::Migration[5.0] - def self.up - create_table :countries do |t| - t.column :name, :string - end - end - - def self.down - drop_table :countries - end -end diff --git a/test/migrate/twelve/011_create_roles.roles.rb b/test/migrate/twelve/011_create_roles.roles.rb deleted file mode 100644 index fe9e723..0000000 --- a/test/migrate/twelve/011_create_roles.roles.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateRoles < ActiveRecord::Migration[5.0] - def self.up - create_table :roles do |t| - t.column :name, :string - end - end - - def self.down - drop_table :roles - end -end diff --git a/test/migrate/twelve/012_create_users.rb b/test/migrate/twelve/012_create_users.rb deleted file mode 100644 index c97e0d8..0000000 --- a/test/migrate/twelve/012_create_users.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateUsers < ActiveRecord::Migration[5.0] - def self.up - create_table "users", force: true do |t| - t.column :name, :string - end - end - - def self.down - drop_table :users - end -end diff --git a/test/migrate/twelve/9_create_countries.rb b/test/migrate/twelve/9_create_countries.rb deleted file mode 100644 index d9e7b9f..0000000 --- a/test/migrate/twelve/9_create_countries.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateCountries < ActiveRecord::Migration[5.0] - def self.up - create_table :countries do |t| - t.column :name, :string - end - end - - def self.down - drop_table :countries - end -end diff --git a/test/provision_vagrant b/test/provision_vagrant deleted file mode 100644 index 5c9b484..0000000 --- a/test/provision_vagrant +++ /dev/null @@ -1,103 +0,0 @@ -#!/bin/bash - -case `id` in -*root*) - ;; -*) - exec echo Must be run as root - ;; -esac - -chruby_version=0.3.9 -chruby=chruby-${chruby_version} -set -x -set -eE -o functrace - -report_failure() { - local lineno=$2 - local fn=$3 - local exitstatus=$4 - local msg=$5 - local lineno_fns=${1% 0} - if [[ $lineno_fns != "0" ]] ; then - lineno="${lineno} ${lineno_fns}" - fi - if [[ $exitstatus == 0 ]] ; then - echo "${BASH_SOURCE[1]}: Finished!" - else - echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" - fi -} - -trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR - - -id -pwd -export DEBIAN_FRONTEND=noninteractive -find /tmp/got-apt-update -mtime -1 || ( apt-get update && touch /tmp/got-apt-update ) -apt install --yes -q build-essential ruby ruby-dev sqlite3 libsqlite3-dev nodejs git git-core -apt install --yes -q gcc autoconf bison libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev -apt install --yes -q libgdbm3 -# useful when debugging -apt install --yes -q silversearcher-ag vim exuberant-ctags -apt install --yes -q unattended-upgrades -unattended-upgrade -# The following is not required for testing, install if you are doing manual tests with extra gems -# apt install --yes -q mysql-client mysql-server libmysqlclient-dev -# apt install --yes -q libcurl4-openssl-dev libncurses5-dev libxml2-dev libxslt1-dev -( - echo Install chruby - [ -s ${chruby}.tar.gz ] || wget -q -O ${chruby}.tar.gz https://github.com/postmodern/chruby/archive/v${chruby_version}.tar.gz - [ -d ${chruby} ] || tar -xzf ${chruby}.tar.gz - cd ${chruby}/ - ./scripts/setup.sh - cat > /etc/profile.d/chruby.sh <<'EOF' -if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then - source /usr/local/share/chruby/chruby.sh - #source /usr/local/share/chruby/auto -fi -EOF - chmod a+r /etc/profile.d/chruby.sh -) - -( - [ -d ruby-build ] || git clone https://github.com/rbenv/ruby-build.git - which ruby-build || PREFIX=/usr/local ./ruby-build/install.sh - - mkdir -p /opt/rubies - for v in 2.2.2 - do - [ -x /opt/rubies/$v/bin/ruby ] || ( ruby-build $v /opt/rubies/$v ) - [ -x /opt/rubies/${v}/bin/bundle ] || ( /opt/rubies/${v}/bin/gem install bundler -v '<2.0' ) - done - - for v in 2.3.8 2.4.10 2.5.9 2.5.0 2.6.6 2.6.7 2.7.1 2.7.3 3.0.1 - do - [ -x /opt/rubies/$v/bin/ruby ] || ( ruby-build $v /opt/rubies/$v ) - [ -x /opt/rubies/$v/bin/bundle ] || ( /opt/rubies/$v/bin/gem install bundler ) - done -) - -echo Setup system ruby -which bundle || gem install bundler || gem install bundler -v '<2.0' -which bundle || gem install bundler -v '< 2.0' -bundle --version -set +x -cat < 5.0.0' -gem 'rake', '>= 0.8.7' - -gem 'listen', '<3.1.2' # REQUIRED - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', "~> 1.3.7" - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED -gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED -# s3 check -gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED - diff --git a/test/rails_5.1.gemfile b/test/rails_5.1.gemfile deleted file mode 100644 index 8409b3c..0000000 --- a/test/rails_5.1.gemfile +++ /dev/null @@ -1,34 +0,0 @@ -# Gemfile for health_test testing - -source 'https://rubygems.org' - -ruby RUBY_VERSION < '2.2.2' ? '2.2.2' : RUBY_VERSION - -gem 'rails', '~> 5.1.0' -gem 'rake', '>= 0.8.7' - -# spring-watcher-listen was resolved to 2.0.1, which depends on -# listen was resolved to 3.1.5, which depends on -# ruby_dep -# and ruby_dep 1.5 requires 2.2.3 or later -gem 'ruby_dep', '~> 1.3.0' # REQUIRED - -gem 'listen', '<3.1.2' # REQUIRED - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', "~> 1.3.7" - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED -gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED -# s3 check -gem 'aws-sdk', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED diff --git a/test/rails_5.2.gemfile b/test/rails_5.2.gemfile deleted file mode 100644 index 4c0b1e1..0000000 --- a/test/rails_5.2.gemfile +++ /dev/null @@ -1,34 +0,0 @@ -# Gemfile for health_test testing - -source 'https://rubygems.org' - -ruby RUBY_VERSION < '2.2.2' ? '2.2.2' : RUBY_VERSION - -gem 'rails', '~> 5.2.0' -gem 'rake', '>= 0.8.7' - -# spring-watcher-listen was resolved to 2.0.1, which depends on -# listen was resolved to 3.1.5, which depends on -# ruby_dep -# and ruby_dep 1.5 requires 2.2.3 or later -gem 'ruby_dep', '~> 1.3.0' # REQUIRED - -gem 'listen', '<3.1.2' # REQUIRED - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', "~> 1.3.7" - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED -gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED -# s3 check -gem 'aws-sdk', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED diff --git a/test/rails_6.0.gemfile b/test/rails_6.0.gemfile deleted file mode 100644 index a1fdaba..0000000 --- a/test/rails_6.0.gemfile +++ /dev/null @@ -1,30 +0,0 @@ -# Gemfile for health_test testing - -source 'https://rubygems.org' - -ruby RUBY_VERSION < '2.5' ? '2.5.0' : RUBY_VERSION - -gem 'rails', '~> 6.0.0' -gem 'rake', '>= 0.8.7' - -gem 'listen', '<3.1.2' # REQUIRED - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', "~> 1.3.7" - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED -gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED -# s3 check -gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED - -gem 'webpacker', '~> 4.0.7' # REQUIRED diff --git a/test/rails_6.1.gemfile b/test/rails_6.1.gemfile deleted file mode 100644 index 2cfe991..0000000 --- a/test/rails_6.1.gemfile +++ /dev/null @@ -1,29 +0,0 @@ -# Gemfile for health_test testing - -source 'https://rubygems.org' - -ruby RUBY_VERSION < '2.5' ? '2.5.0' : RUBY_VERSION - -gem 'rails', '~> 6.1.0' -gem 'rake', '>= 0.8.7' - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', "~> 1.3.7" - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', require: !ENV['RESQUE'].nil? # REQUIRED -# s3 check -gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED - -gem 'webpacker', '~> 4.0.7' # REQUIRED -gem 'rexml', '~> 3.2.4' # REQUIRED for ruby 3.0 -gem 'webrick' # REQUIRED for ruby 3.0 diff --git a/test/rails_6.2.gemfile b/test/rails_6.2.gemfile deleted file mode 100644 index b402a53..0000000 --- a/test/rails_6.2.gemfile +++ /dev/null @@ -1,30 +0,0 @@ -# Gemfile for health_test testing - -source 'https://rubygems.org' - -ruby RUBY_VERSION < '2.5' ? '2.5.0' : RUBY_VERSION - -gem 'rails', '~> 6.2.0' -gem 'rake', '>= 0.8.7' - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3', "~> 1.3.7" - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED -gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED -# s3 check -gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED - -gem 'webpacker', '~> 4.0.7' # REQUIRED -gem 'rexml', '~> 3.2.4' # REQUIRED for ruby 3.0 -gem 'webrick' # REQUIRED for ruby 3.0 diff --git a/test/rails_edge.gemfile b/test/rails_edge.gemfile deleted file mode 100644 index 3377171..0000000 --- a/test/rails_edge.gemfile +++ /dev/null @@ -1,37 +0,0 @@ -# Gemfile for health_test testing - -source 'https://rubygems.org' - -# Bundle edge Rails instead: - -ruby RUBY_VERSION < '2.2.2' ? '2.2.2' : RUBY_VERSION - -gem 'rails' -gem 'rake' -gem 'rack' - -group :development, :test do - if defined?(JRUBY_VERSION) - gem 'jruby-openssl' - gem 'activerecord-jdbcsqlite3-adapter' - else - gem 'sqlite3' - end - gem 'shoulda' -end - -# redis based checks -gem 'sidekiq', '~> 5.2.9', require: !ENV['SIDEKIQ'].nil? # REQUIRED -gem 'redis', '~> 4.0.3', require: !ENV['REDIS_URL'].nil? # REQUIRED -gem 'resque', '~> 1.27.4', require: !ENV['RESQUE'].nil? # REQUIRED -gem 'elasticsearch', '~> 6.3.1', require: !ENV['ELASTICSEARCH_URL'].nil? # REQUIRED -# s3 check -gem 'aws-sdk-s3', require: !ENV['AWS_ACCESS_KEY_ID'].nil? # REQUIRED - -# Initial Gemfile has therubyracer commented out -gem 'therubyrhino', platform: :jruby # REQUIRED -gem 'therubyracer', platform: :ruby # REQUIRED - -gem 'webpacker', '~> 4.0.7' # REQUIRED -gem 'rexml', '~> 3.2.4' # REQUIRED for ruby 3.0 -gem 'webrick' # REQUIRED for ruby 3.0 diff --git a/test/setup_railsapp b/test/setup_railsapp deleted file mode 100755 index 63bbb1c..0000000 --- a/test/setup_railsapp +++ /dev/null @@ -1,527 +0,0 @@ -#!/bin/bash - -route_prefix=medical_check - -# Any failure causes exit -set -eE -o functrace - -report_failure() { - local lineno=$2 - local fn=$3 - local exitstatus=$4 - local msg=$5 - local lineno_fns=${1% 0} - if [[ $lineno_fns != "0" ]] ; then - lineno="${lineno} ${lineno_fns}" - fi - if [[ $exitstatus == 0 ]] ; then - echo "${BASH_SOURCE[1]}: Finished!" - else - echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" - fi -} - -trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR - - -case "$1" in -[0-9]*) - export BUNDLE_GEMFILE=$PWD/test/rails_$1.gemfile - ;; -esac -while : -do - case "$BUNDLE_GEMFILE" in - */test/rails_[Qq].gemfile) - echo "Aborting..." - exit 2 - ;; - */test/rails_edge.gemfile|*/test/rails_[0-9].[0-9]*.gemfile) - if [ -f "$BUNDLE_GEMFILE" ]; then - break - fi - ;; - esac - echo "== SELECT GEMFILE ==" - echo - echo "Please select the gemfile for the required rails series:" - (cd test ; ls rails*gemfile | ruby -p -e '$_.sub!(/rails_(.*).gemfile/, " \\1")' ) - echo - echo -n "Enter choice (or q to quit): " - read x - export BUNDLE_GEMFILE=$PWD/test/rails_$x.gemfile -done - -if [ -z "$MIDDLEWARE" ]; then - echo -n "Add as middleware [N/y] : " - read MIDDLEWARE - export MIDDLEWARE -fi -rm -rf tmp/Gemfile* tmp/railsapp tmp/bin tmp/gems test/bin test/rails*.gemfile.lock - -mkdir -p tmp/gems - -. test/init_variables - -if $rbenv_which bundle ; then - echo Bundler is installed -else - gem install bundler ${BUNDLER_VERSION:+-v ${BUNDLER_VERSION}} - $rehash -fi - -echo "Running bundle with BUNDLE_GEMFILE=$BUNDLE_GEMFILE ..." -if ! smarter_bundle ; then - echo "Test aborted (missing required gems)" - exit 2 -elif [ ! -s $BUNDLE_GEMFILE.lock ] ; then - echo "Error: smarter_bundler return OK status BUT lock file ($BUNDLE_GEMFILE.lock) is missing!" - exit 3 -else - echo bundle passed - lock file contains: - cat $BUNDLE_GEMFILE.lock - echo -fi -$rehash - -rails="$base_dir/test/bin/rails" -rake="$base_dir/test/bin/rake" - -echo Checking $rails is present ... -[ -f $rails -a -f $rake ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} exec rake rails:update:bin || echo '(ignored rake rails:update:bin exit status)' -[ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs railties || echo '(ignored bundle exit status)' -[ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rails || echo '(ignored bundle exit status)' -if [ ! -f $rails ]; then - echo "Test aborted (unable to create $rails)" - exit 2 -fi - -if [ ! -f $rake ]; then - echo "Running bundle binstubs rake ..." - if ! bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rake || [ ! -f $rake ]; then - echo "Test aborted (unable to create $rake)" - exit 2 - fi -fi - -actual_rails_version=`$rails -v` - -[ -d lib/health_check ] || exec echo setup_railsapp MUST be executed in the base of the health_check gem/clone of git repository - -export GEM_PATH="$tmp_dir/gems:`gem environment gempath`" -echo Set GEM_PATH=$GEM_PATH - -echo Installing health_check as a gem into $tmp_dir/gems -rm -f pkg/health_check-*.gem -if env GEM_HOME=$tmp_dir/gems $rake install -then - echo rake install passed -else - echo rake install failed! running gem install pkg/health_check-*.gem manually to see error message: - env GEM_HOME=$tmp_dir/gems gem install pkg/health_check-*.gem - echo gem install worked, but flagging it as a FAIL anyway since rake install failed! - exit 2 -fi - -echo Gems in tmp/gems: -ls tmp/gems - -echo Environment: -env | egrep 'TRAVIS|RAILS|RUBY|_ENV|GEM|BUNDLE' || echo "No relevant variables set" - -cd $tmp_dir - -case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in - [0-9]*) - db=jdbcsqlite3 - # Appears to need a bit extra time - ;; - *) - db=sqlite3 - ;; -esac - -echo "Creating $actual_rails_version app in $tmp_dir/railsapp using adapter $db" -case "$actual_rails_version" in - *' '[345].*) - args="--skip-bundle -d $db" - case "$BUNDLE_GEMFILE" in - *rails_edge.gemfile) - $rails new railsapp $args --edge - ;; - *) - $rails new railsapp $args - ;; - esac - ;; - *' '[6].*) - args="--skip-bundle -d $db --skip-git --skip-bootsnap" - case "$BUNDLE_GEMFILE" in - *rails_edge.gemfile) - $rails new railsapp $args --edge - ;; - *) - $rails new railsapp $args - ;; - esac - ;; - *) - echo "Unknown rails version" - ;; -esac - -cd $railsapp - -[ -z "$rehash" ] || rbenv local `rbenv version-name` - -echo "Changed current directory to railsapp root: $railsapp" - -echo "Fixing rdoc require in Rakefile if needed" -ruby -p -i.bak -e '$_.gsub!(/rake.rdoctask/, "rdoc/task")' Rakefile - -echo "Configuring mailer to point to fake_smtp_server port 3555" -cat >> config/environment.rb <<'!EOF!' - -ActionMailer::Base.delivery_method = :smtp -ActionMailer::Base.smtp_settings = { address: "localhost", port: 3555 } - -!EOF! - -echo Adding an initializer for health_check gem ... -mkdir -p config/initializers -tee config/initializers/health_check.rb <> Gemfile - -case "$RAILS_SERVER" in -webrick|'') - echo "Using default webrick server" - ;; -*) - echo "Adding $RAILS_SERVER gem to Gemfile (for use as server)" - echo "gem '$RAILS_SERVER'" >> Gemfile - ;; -esac -TAB=$'\t' -QUOTES='"'"'" -case "$actual_rails_version" in -*' '5.0*) - if egrep -i 'gem.*sqlite3' Gemfile ; then - # Can't do this as a require as we may go back to testing JRuby - echo Force sqlite to 1.3.13+ version for Rails 5.0 ... - gem=sqlite3 - sed -i.bak -e "s/^\([ ${TAB}]*gem[ ${TAB}]*[${QUOTES}]${gem}[${QUOTES}]\)\(.*\)$/\1, '~> 1.3.13' # overriden: \2/" Gemfile - fi - ;; -esac -if egrep -q REQUIRED ${INITIAL_BUNDLE_GEMFILE} ; then - sed -n "s/^[ ${TAB}]*gem[ ${TAB}]*[${QUOTES}]\([^${QUOTES}]*\)[${QUOTES}].*REQUIRED.*/\1/p" ${INITIAL_BUNDLE_GEMFILE} | while read gem - do - echo "Commenting out gem '$gem' line in Gemfile" - sed -i.bak -e "s/^\([ ${TAB}]*gem[ ${TAB}]*[${QUOTES}]${gem}[${QUOTES}].*\)$/# overriden by REQUIRED below: \1/" Gemfile - done - echo Adding Required gems - egrep REQUIRED ${INITIAL_BUNDLE_GEMFILE} | tee -a Gemfile -else - echo No required gems to be added to Gemfile -fi - -echo -echo ================= $PWD/Gemfile =================== -cat Gemfile -echo -echo ================================================== -echo running smarter_bundle install -smarter_bundle install -$rehash - -if egrep webpacker Gemfile && [ ! -f config/webpacker.yml ] ; then - echo completing setup by running rails webpacker:install ... - $rails webpacker:install -fi - -echo "Using binstubs in $railsapp/bin for rails and rake commands" -rails="$railsapp/bin/rails" -rake="$railsapp/bin/rake" - -echo Checking $rails is present ... -[ -f $rails -a -f $rake ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} exec rake rails:update:bin || echo '(ignored rake rails:update:bin exit status)' -[ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs railties || echo '(ignored bundle exit status)' -[ -f $rails ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rails || echo '(ignored bundle exit status)' -if [ ! -f $rails ]; then - echo "Test aborted (unable to create $rails)" - exit 2 -fi - -echo Checking $rake is present ... -[ -f $rake ] || bundle ${BUNDLER_VERSION:+_${BUNDLER_VERSION}_} binstubs rake || echo '(ignored bundle exit status)' -if [ ! -f $rake ]; then - echo "Test aborted (unable to create $rake)" - exit 2 -fi - -$rehash -# Fix for rvm, otherwise bundle run from rails create fails -export PATH="`pwd`/bin:$PATH" -echo ================= $PWD/Gemfile.lock =================== -cat Gemfile.lock -echo ================================================== -echo - -for e in test ${RAILS_ENV2:-production} -do - if [ -f config/environments/$e.rb ]; then - echo ======== config/environments/$e.rb ================ - sed -i.bak -e 's/config.serve_static_assets = false/config.serve_static_assets = true # NOTE: health_check test: changed to true/' \ - -e 's/config.active_record.migration_error = :page_load/# & # NOTE: health_check test: commented out/' \ - config/environments/$e.rb - cat config/environments/$e.rb || true - echo - fi -done - -if egrep -q 'bootsnap.setup' config/boot.rb ; then - echo Commenting out bootsnap from config/boot.rb - sed -i.bak -e 's/^\(.*bootsnap.setup\)/# \1/' config/boot.rb - echo "============== $PWD/config/boot.rb =============" - cat config/boot.rb - echo ================================================== -fi -rm -rf tmp/cache/bootsnap* -echo - -echo "============== $PWD/config/environment.rb =============" -cat config/environment.rb -echo - -echo ========================================================= -case $db in - jdbcsqlite3) - for e in test ${RAILS_ENV2:-production} - do - echo - export RAILS_ENV=$e RACK_ENV=$e - echo "Jruby requires the database to be created before the server is started: running RAILS_ENV=$e rake db:migrate" - $rake db:migrate - echo - done - ;; -esac - -echo STATIC-FILE > public/static.txt - -cat > public/ajax_example.html <<'EOF' - - - Example static and dynamic calls to health_check - - - - -

Static calls

- -

Dynamic calls

- - - -EOF - -cat > app/controllers/example_controller.rb <<'EOF' -class ExampleController < ApplicationController - - def index - render plain: 'example page' - end - - def catchall - render plain: 'catch all route' - end - -end - -EOF - -echo ======================================= - -case "$MIDDLEWARE" in -[Yy]*) - if [ -s config/application.rb ]; then - mv -f config/application.rb config/application.rb-old - ( - ruby -n -e 'print if not /^ *end/..9999' config/application.rb-old | tee /tmp/t$$ - if [ ! -s /tmp/t$$ ]; then - echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 - sed -e '/^ *end/,$d' config/application.rb-old - fi - - echo " # -----------------------------------------" - echo " # START OF SECTION FOR TESTING HEALTH_CHECK" - echo " config.middleware.insert_after Rails::Rack::Logger, HealthCheck::MiddlewareHealthcheck" - echo " # END OF SECTION FOR TESTING HEALTH_CHECK" - echo " # ---------------------------------------" - ruby -n -e 'print if /^ *end/..9999' config/application.rb-old | tee /tmp/t$$ - if [ ! -s /tmp/t$$ ]; then - echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 - sed -n -e '/^ *end/,$p' config/application.rb-old - fi - ) 3>&1 > config/application.rb - - #echo =============== config/application.rb-old ======================== - #cat config/application.rb-old - echo =============== $PWD/config/application.rb ======================== - cat config/application.rb - else - echo FAILED: NO config/application.rb file!! - exit 2 - fi - - echo ======================================= - - ;; -esac - -if [ -s config/routes.rb ]; then - mv -f config/routes.rb config/routes.rb-old - ( - ruby -n -e 'print if not /^end/..9999' config/routes.rb-old | tee /tmp/t$$ - if [ ! -s /tmp/t$$ ]; then - echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 - sed -e '/^end/,$d' config/routes.rb-old - fi - - # rails 3.0+ - echo " # -----------------------------------------" - echo " # START OF SECTION FOR TESTING HEALTH_CHECK" - echo " get 'example/catchall(.:format)', controller: :example, action: :catchall" - echo " get 'example(.:format)', controller: :example, action: :index" - echo " if File.exists?('$catchall_file')" - echo " health_check_routes" - echo " # CATCH ALL ROUTE" - echo " get '*path', controller: :example, action: :catchall" - echo " end" - echo " # END OF SECTION FOR TESTING HEALTH_CHECK" - echo " # ---------------------------------------" - ruby -n -e 'print if /^end/..9999' config/routes.rb-old | tee /tmp/t$$ - if [ ! -s /tmp/t$$ ]; then - echo "WARNING: ruby -n -e failed silently - using sed instead!! (rbx-19mode has that problem)" >&3 - sed -n -e '/^end/,$p' config/routes.rb-old - fi - ) 3>&1 > config/routes.rb - - #echo =============== config/routes.rb-old ======================== - #cat config/routes.rb-old - echo =============== config/routes.rb ======================== - cat config/routes.rb -else - echo FAILED: NO config/routes.rb file!! - exit 2 -fi -echo ======================================= - -echo -echo "Created $actual_rails_version app in $railsapp using adapter $db" -echo -n "Using " -ruby --version diff --git a/test/test_with_railsapp b/test/test_with_railsapp deleted file mode 100755 index c514df4..0000000 --- a/test/test_with_railsapp +++ /dev/null @@ -1,757 +0,0 @@ -#!/bin/bash - -route_prefix=medical_check - -# Any failure causes exit -set -eE -o functrace - -report_failure() { - local lineno=$2 - local fn=$3 - local exitstatus=$4 - local msg=$5 - local lineno_fns=${1% 0} - if [[ $lineno_fns != "0" ]] ; then - lineno="${lineno} ${lineno_fns}" - fi - if [[ $exitstatus == 0 ]] ; then - echo "${BASH_SOURCE[1]}: Finished!" - else - echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" - fi -} - -trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR - - -# Any failure causes exit -set -eE -o functrace - -report_failure() { - local lineno=$2 - local fn=$3 - local exitstatus=$4 - local msg=$5 - local lineno_fns=${1% 0} - if [[ $lineno_fns != "0" ]] ; then - lineno="${lineno} ${lineno_fns}" - fi - if [[ $exitstatus == 0 ]] ; then - echo "${BASH_SOURCE[1]}: Finished!" - else - echo "${BASH_SOURCE[1]}:${fn}[${lineno}] Failed with status ${exitstatus}: $msg" - fi -} - -trap 'report_failure "${BASH_LINENO[*]}" "$LINENO" "${FUNCNAME[*]:-script}" "$?" "$BASH_COMMAND"' ERR - -export DISABLE_SPRING=1 - -cleanup_db() -{ - echo Dropping database ... - $rake db:drop - echo Removing migrations ... - rm -f db/migrate/* db/schema.rb - case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in - [0-9]*) - echo 'Jruby requires the database to be created before the server is started: running rake db:migrate' - $rake db:migrate - ;; - esac -} - -case "$1" in -redo) - . test/init_variables - cd $railsapp - cleanup_db - actual_rails_version=`$rails -v` - ;; -*) - . test/setup_railsapp $1 - ;; -esac - -run_test=$2 - -cd $railsapp -date > $custom_file -rm -f $catchall_file - -case `egrep '^[^#]*MiddlewareHealthcheck' config/application.rb` in -'') - export has_middleware=false - ;; -?*) - export has_middleware=true - ;; -esac - - -testurl="$base_dir/test/testurl" -fake_smtp_server="$base_dir/test/fake_smtp_server" - -server_pid='' -fake_smtp_pid='' - -pick_a_port() -{ - while : - do - port=`expr 10000 + $RANDOM` - # Check Tcp ports in Listen mode with No address resolution - if (netstat -tln | egrep ":${port} .*:"); then - echo "(Skipping used port)" - else - break - fi - done -} - - -start_server() -{ - # restart migration list - rm -rf db/migrate db/schema.rb - mkdir -p db/migrate - - # Increment port each time to make sure we have not trouble with address/port already allocated - pick_a_port - host=http://127.0.0.1:${port} - bundle_prefix='' - if [ -f Gemfile ] - then - bundle_prefix='bundle exec' - fi - server_arg=${RAILS_SERVER:-webrick} - case "$actual_rails_version" in - *' '[12345].*) - ;; - *) - server_arg="-u $server_arg" - ;; - esac - echo "start_server called using: `env | egrep '^RAILS|^RACK|^PATH='` $bundle_prefix $server_arg" - case "$server_arg" in - *puma) - $bundle_prefix puma -b tcp://127.0.0.1:$port & - ;; - *passenger) - $bundle_prefix passenger start -p $port & - ;; - *thin) - $bundle_prefix thin start -p $port & - ;; - *unicorn) - $bundle_prefix unicorn_rails -l 127.0.0.1:$port & - ;; - *) - if [ -x script/server ] - then - echo Starting server on port $port using $bundle_prefix ./script/server ... - $bundle_prefix ./script/server $server_arg -p $port & - else - echo Starting server on port $port using $rails s ... - $bundle_prefix $rails server $server_arg -p $port & - fi - ;; - esac - server_pid=$! - echo Server pid: $server_pid - sleep 3 - echo - echo 'Checking server is up ...' - for i in 1 2 3 4 5 6 - do - if $testurl ${host}/static.txt ; then - break - fi - if kill -0 $server_pid ; then - echo "waiting ${i} ..." - else - echo "ERROR: Server has died!!" - exit 3 - fi - done -} - -stop_server() -{ - case "$server_pid" in - [0-9]*) - echo ======================================================== - echo "Killing rails server [pid: $server_pid]" - kill -QUIT $server_pid || echo server has already exited .. - if [ -x bin/spring ] ; then - echo Stopping spring ... - bin/spring stop || echo spring had already exited .. - fi - sleep 1 - kill -9 $server_pid || echo server had already exited ... - sleep 1 - # needed for unicorn - it doesn't die when it is supposed to - killall "$server_arg" || echo server and child processes had already stopped ... - ;; - esac - case "$fake_smtp_pid" in - [0-9]*) - echo ======================================================== - echo "Killing fake smtp server [pid: $fake_smtp_pid]" - kill -QUIT $fake_smtp_pid || echo fake_smtp had already exited .. - sleep 2 - kill -9 $fake_smtp_pid || echo fake_smtp had already exited .. - ;; - esac - server_pid='' - fake_smtp_pid='' - ps -f - echo Waiting for sub processes to complete ... - wait - echo Finished waiting for sub processes, sleeping 2 seconds ... - sleep 2 -} - -finish() -{ - set +e - echo ======================================================== - echo TEST ${1:-FAILED} - echo ======================================================== - echo Result of: ls -lR $railsapp/log $railsapp/db - ls -lR $railsapp/log $railsapp/db - - if [ -s $railsapp/log/test.log ] - then - echo ======================================================== - echo Last 50 lines of test log - tail -50 $railsapp/log/test.log - fi - - if [ -s $railsapp/log/production.log ] - then - echo ======================================================== - echo Last 50 lines of production log - tail -50 $railsapp/log/production.log - fi - - stop_server - trap "" 0 - echo ======================================================== - ps uxf || echo ps failed - echo ======================================================== - echo TEST ${1:-FAILED}, exiting with status ${2:-2} - echo ======================================================== - exit ${2:-2} -} - -trap "finish FAILED 1" 0 - -common_tests() -{ - - test_no=$1 - - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: CHECKING routes exist..." - $rake routes | tee /tmp/t$$ - echo - case `egrep ${route_prefix} /tmp/t$$ || true` in - '') - echo WARNING - routes for ${route_prefix} not listed! - ;; - esac - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING can get a static file ..." - case "$RAILS_ENV=`egrep '^\s*config.serve_static_[asetfil]* *= *false' config/environments/${RAILS_ENV}.rb`" in - production*static*false*) - echo " SKIPPED (disabled in production)" - ;; - *) - grep serve_static_files config/environments/${RAILS_ENV}.rb config/[a-z]*.rb || echo no serve_static_files entry - $testurl ${host}/static.txt 200 text/plain STATIC-FILE - ;; - esac - echo - fi - - rm -f tmp/health_check_success.txt tmp/health_check_failure.txt - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING can get an example controller ..." - $testurl ${host}/example 200 text/plain 'example page' - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING direct call to catchall method on example controller ..." - $testurl ${host}/example/catchall 200 text/plain 'catch all route' - echo - fi - - if [ -f tmp/health_check_success.txt ] ; then - echo "FAIL tmp/health_check_success.txt exists on line $LINENO" - else - echo "PASS tmp/health_check_success.txt is missing as expected on line $LINENO" - fi - if [ -f tmp/health_check_failure.txt ] ; then - echo "FAIL tmp/health_check_failure.txt exists on line $LINENO" - else - echo "PASS tmp/health_check_failure.txt is missing as expected on line $LINENO" - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass with no database migrations ..." - ls db/migrate - rm -f tmp/health_check_success.txt tmp/health_check_failure.txt - $testurl ${host}/${route_prefix}/migration 200 text/plain $success - if [ -f tmp/health_check_success.txt ] ; then - echo "PASS tmp/health_check_success.txt exists as expected on line $LINENO" - cat tmp/health_check_success.txt - else - echo "FAIL tmp/health_check_success.txt is missing on line $LINENO" - fi - if [ -f tmp/health_check_failure.txt ] ; then - echo "FAIL tmp/health_check_failure.txt exists on line $LINENO" - else - echo "PASS tmp/health_check_failure.txt is missing as expected on line $LINENO" - fi - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should fail without initial database migration ..." - cp $base_dir/test/migrate/nine/* db/migrate - ls db/migrate - rm -f tmp/health_check_success.txt tmp/health_check_failure.txt - $testurl ${host}/${route_prefix}/migration 550 text/plain failed - if [ -f tmp/health_check_success.txt ] ; then - echo "FAIL tmp/health_check_success.txt exists on line $LINENO" - else - echo "PASS tmp/health_check_success.txt is missing as expected on line $LINENO" - fi - if [ -f tmp/health_check_failure.txt ] ; then - echo "PASS tmp/health_check_failure.txt exists as expected on line $LINENO" - cat tmp/health_check_failure.txt - else - echo "FAIL tmp/health_check_failure.txt is missing on line $LINENO" - fi - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/database should pass without initial database migration (since it ignores the difference) ..." - $testurl ${host}/${route_prefix}/database 200 text/plain $success - echo - fi - - - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/site should pass ..." - $testurl ${host}/${route_prefix}/site 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass after initial database migration ..." - $rake db:migrate - $testurl ${host}/${route_prefix}/migration 200 text/plain $success - echo - fi - - #test with coruppted DB - rm -rf db.bak - cp -R db db.bak - for f in db/*.sqlite3 - do - echo CORRUPTED > $f - done - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/database should fail if the database has been corrupted ..." - - $testurl ${host}/${route_prefix}/database 550 text/plain failed: - echo - fi - - export HIDE_ERROR_RESPONSE=true - stop_server - start_server - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/database should have response body 'health_check failed' but no error details if include_error_in_response_body is false" - $testurl ${host}/${route_prefix}/database 550 text/plain 'health_check failed' failed: - echo - fi - - unset HIDE_ERROR_RESPONSE - stop_server - start_server - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/site should pass ..." - $testurl ${host}/${route_prefix}/site 200 text/plain $success - if $has_middleware; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/middleware_site should pass ..." - $testurl ${host}/${route_prefix}/middleware_site 200 text/plain $success - fi - echo - fi - - # Restore database - cp -f db.bak/*.sqlite3 db/ - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should fail without all migrations ..." - cp $base_dir/test/migrate/twelve/* db/migrate - - ls db/migrate - $testurl ${host}/${route_prefix}/migration 550 text/plain failed - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass after both database migrations ..." - $rake db:migrate - $testurl ${host}/${route_prefix}/migration 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/migration should pass after both database migrations ..." - $rake db:migrate - $testurl ${host}/${route_prefix}/migration 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/email should fail without smtp available ..." - $testurl ${host}/${route_prefix}/email 550 text/plain failed - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/email should pass with smtp available ..." - $fake_smtp_server & - fake_smtp_pid=$! - sleep 5 - $testurl ${host}/${route_prefix}/email 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix} (all) should fail without smtp available ..." - $testurl ${host}/${route_prefix} 550 text/plain failed - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/all should fail without smtp available ..." - $testurl ${host}/${route_prefix} 550 text/plain failed - echo - fi - - kill -9 $fake_smtp_pid || echo fake_smtp_server had finished as expected - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix} (all) should pass with smtp available ..." - $fake_smtp_server & - fake_smtp_pid=$! - sleep 5 - $testurl ${host}/${route_prefix} 200 text/plain $success - echo - fi - - kill -9 $fake_smtp_pid || echo fake_smtp_server had finished as expected - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/all should pass with smtp available ..." - $fake_smtp_server & - fake_smtp_pid=$! - sleep 5 - $testurl ${host}/${route_prefix}/all 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/pass should pass ..." - $testurl ${host}/${route_prefix}/pass 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom should pass ..." - $testurl ${host}/${route_prefix}/custom 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom.html should pass (returning plain text) ..." - $testurl ${host}/${route_prefix}/custom.html 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom.json should pass ..." - $testurl ${host}/${route_prefix}/custom.json 200 application/json '"healthy":true' - $testurl ${host}/${route_prefix}/custom.json 200 application/json "\"message\":\"$success\"" - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom.xml should pass ..." - $testurl ${host}/${route_prefix}/custom.xml 200 application/xml 'true' - $testurl ${host}/${route_prefix}/custom.xml 200 application/xml "$success" - echo - fi - - test_no=`expr 1 + $test_no` - rm -f $custom_file - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/custom should fail when custom returns string ..." - $testurl ${host}/${route_prefix}/custom 550 text/plain failed - echo - fi - - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/pass should pass even if other custom test returns string ..." - $testurl ${host}/${route_prefix}/pass 200 text/plain $success - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix} (all) should fail when custom check fails ..." - $testurl ${host}/${route_prefix} 550 text/plain "$custom_file is missing!" - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}.json (all) should fail when custom check fails ..." - $testurl ${host}/${route_prefix}.json 555 application/json '"healthy":false' - $testurl ${host}/${route_prefix}.json 555 application/json "$custom_file is missing!" - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}.xml (all) should fail when custom check fails ..." - $testurl ${host}/${route_prefix}.xml 555 application/xml 'false' - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - if $has_middleware; then - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/middleware_site should pass ..." - $testurl ${host}/${route_prefix}/middleware_site 200 text/plain $success - else - echo "${test_no}[line $LINENO]: TESTING ${route_prefix}/middleware_site should fail ..." - $testurl ${host}/${route_prefix}/middleware_site 550 text/plain failed - fi - echo - fi - - test_no=`expr 1 + $test_no` - if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then - echo "${test_no}[line $LINENO]: TESTING log files to check for deprecation warnings ..." - if egrep ' is deprecated|DEPRECATION WARNING' $railsapp/log/[tp][er][so][td]*.log - then - echo Found deprecation warnings - failed test - exit 99 - fi - fi - - date > $custom_file -} - -# required for rails 4.1+ in production mode -export SECRET_KEY_BASE=cf2f49c38a3fe67416ddf680f4f3187c0fce7dd1b9b117b34d195df75b274e08a04877e23803b2fdf1aa9a655269d94bc4888aa325cf7e721cc47368cfe56a80 - -# required for rails 5 to server static files -export RAILS_SERVE_STATIC_FILES=on - -export IP_WHITELIST='123.123.123.123' -unset AUTH_USER -unset AUTH_PASSWORD - -case "$run_test" in -''|[12]) - echo ======================================================== - echo TESTING whitelist ban WITHOUT CATCHALL in test env - echo ======================================================== - export RAILS_ENV=test RACK_ENV=test - - start_server - - echo '1: TESTING controller prohibited by ip...' - expected_status=403 - $testurl ${host}/${route_prefix}/site $expected_status - - if $has_middleware; then - echo - echo '2: TESTING middleware prohibited by ip...' - expected_status=403 - $testurl ${host}/${route_prefix}/middleware $expected_status - fi - ;; -esac - -export IP_WHITELIST='' -export AUTH_USER='someone' -export AUTH_PASSWORD='secret' - -case "$run_test" in -''|[3456789]|10) - echo ======================================================== - echo TESTING basic auth, no whitelist WITHOUT CATCHALL in test env - echo ======================================================== - export RAILS_ENV=test RACK_ENV=test - - case "$run_trest" in - '') - stop_server - cleanup_db - ;; - esac - - start_server - - expected_status=401 - echo '3: TESTING controller without authentication insists on authentication ...' - AUTH_USER= $testurl ${host}/${route_prefix}/site $expected_status - - echo '4: TESTING controller with wrong password insists on authentication ...' - AUTH_PASSWORD=wrong $testurl ${host}/${route_prefix}/site $expected_status - - echo '5: TESTING controller with wrong user insists on authentication ...' - AUTH_USER=wrong $testurl ${host}/${route_prefix}/site $expected_status - - echo '6: TESTING controller with authentication works ...' - expected_status=200 - $testurl ${host}/${route_prefix}/site $expected_status - - if $has_middleware; then - echo - echo '7: TESTING middleware without authentication insists on authentication ...' - expected_status=401 - AUTH_USER= $testurl ${host}/${route_prefix}/middleware $expected_status - - echo - echo '8: TESTING middleware with wrong password insists on authentication ...' - AUTH_PASSWORD=wrong $testurl ${host}/${route_prefix}/middleware $expected_status - - echo - echo '9: TESTING middleware with wrong user insists on authentication ...' - AUTH_USER=wrong $testurl ${host}/${route_prefix}/middleware $expected_status - - echo - echo '10: TESTING middleware with authentication works ...' - expected_status=200 - $testurl ${host}/${route_prefix}/middleware $expected_status - else - echo - echo "Skipped middleware tests as it is not configured..." - fi - ;; -esac - -unset AUTH_USER -unset AUTH_PASSWORD - -case "$run_test" in -''|1??) - echo ======================================================== - echo TESTING WITHOUT CATCHALL, no whitelist or user in test env - echo ======================================================== - export RAILS_ENV=test RACK_ENV=test - - case "$run_trest" in - '') - stop_server - cleanup_db - ;; - esac - - start_server - - # get a static file - - echo - echo 'TESTING no catchall route active ...' - expected_status=404,500,502 - $testurl ${host}/another/url $expected_status - - echo 'TESTING default route has been overriden ...' - expected_status=404,500,502 - $testurl ${host}/health_check/site $expected_status - - common_tests 100 - ;; -esac - -export IP_WHITELIST='127.0.0.1' -export AUTH_USER='someone' -export AUTH_PASSWORD='secret' - -case "$run_test" in -''|2??) - echo ======================================================== - echo TESTING WITH CATCHALL with whitelist and user in ${RAILS_ENV2:-production} env - echo ======================================================== - export RAILS_ENV=${RAILS_ENV2:-production} RACK_ENV=${RAILS_ENV2:-production} - - case "$run_trest" in - '') - stop_server - cleanup_db - ;; - esac - - date > $catchall_file - - start_server - - echo - echo 'TESTING catchall route active ...' - $testurl ${host}/another/url 200 text/plain 'catch all route' - echo - - common_tests 200 - ;; -esac - -rm -f $catchall_file - -finish PASSED 0 -exit 0 - -# vi: sw=4 ai sm: diff --git a/test/testurl b/test/testurl deleted file mode 100755 index df22086..0000000 --- a/test/testurl +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env ruby -require 'net/http' -require 'uri' - -def open(url) - parsed_uri = URI.parse(url) - http = Net::HTTP.new(parsed_uri.host, parsed_uri.port) - req = Net::HTTP::Get.new(parsed_uri.request_uri) - req.basic_auth(ENV['AUTH_USER'], ENV['AUTH_PASSWORD'].to_s) if ENV['AUTH_USER'] - http.request(req) -end - -response = open(ARGV[0]) rescue nil -unless response - i=0 - print "waiting.." - while i < 120 and not response - #puts 'RESPONSE:', response.inspect - print "." - STDOUT.flush - i += 1 - sleep(1) - response = open(ARGV[0]) rescue nil - end - unless response - puts "\nFAIL: timed out after waiting #{i} seconds" - exit 9 - end - puts "\n got url content after waiting #{i} seconds" -end - -page_content = response.body - -puts " response code: #{response.code} #{response.message}" -response.header.each_header {|key,value| puts " #{key}: #{value}" } -puts " body: #{page_content}" - -if ARGV[1] and ARGV[1] != '' - if ARGV[1].split(',').include?(response.code) - puts "PASS (response code was #{response.code} which matched #{ARGV[1]})" - else - puts "FAIL (response code was #{response.code}, expected #{ARGV[1]})" - exit 1 - end -end - -if ARGV[2] and ARGV[2] != '' - if response.content_type == ARGV[2] - puts "PASS (content type was #{response.content_type})" - else - puts "FAIL (content type was #{response.content_type}, expected #{ARGV[2]})" - exit 2 - end -end - -if ARGV[3] and ARGV[3] != '' - if page_content.to_s.include? ARGV[3] - puts "PASS (found #{ARGV[3]})" - else - puts "FAIL (expected to find #{ARGV[3]}) - page contents:" , page_content.to_s, 'END-OF-CONTENTS' - exit 3 - end -end - -if ARGV[4] and ARGV[4] != '' - if !page_content.to_s.include? ARGV[4] - puts "PASS (did not find #{ARGV[4]})" - else - puts "FAIL (found #{ARGV[4]}) - page contents:" , page_content.to_s, 'END-OF-CONTENTS' - exit 3 - end -end - -exit 0