Skip to content

Instantly share code, notes, and snippets.

@mycargus
Created May 10, 2018 23:17
Show Gist options
  • Save mycargus/e2a8176faa29e115b1d1e06f8cdf62b1 to your computer and use it in GitHub Desktop.
Save mycargus/e2a8176faa29e115b1d1e06f8cdf62b1 to your computer and use it in GitHub Desktop.
repeat spec(s) until failure
require_relative 'task_helpers'
desc 'Repeat e2e spec(s) until failure (default :max_number_of_repetitions = 100)'
task :repeat, [:max_number_of_repetitions] do |_t, args|
maximum_repetitions = args[:max_number_of_repetitions] ? args[:max_number_of_repetitions].to_i : 100
test_run = 'docker-compose run --rm testrunner bundle exec rspec spec'
Console.log_info "\n Executing #{maximum_repetitions} times (or until failure):"
Console.log_info "\n\t '#{test_run}'"
TaskHelpers.repeat_until_failure(test_run, maximum_repetitions)
end
require 'rake'
require_relative 'debug'
require_relative 'console'
require_relative 'ruby_extensions'
module TaskHelpers
extend Rake::DSL
class << self
def check_ruby_version
# Given this project is dockerized, its Ruby version is specified by the
# base docker image in the testrunner container's Dockerfile. Because of
# this, it doesn't make sense to also define the Ruby version in this
# project's Gemfile, which is why we're using this method to enforce the
# Ruby version.
#
# Furthermore, although native non-docker use of this project isn't the
# best practice given we've dockerized everything, we want to provide a
# familiar "byebug" debugging workflow for those who may prefer it.
project_ruby = `docker-compose run --rm --no-deps testrunner ruby -v \
| awk '{print $2}' | cut -d p -f1 | tr -d '\n' | tr -d '\r'`
local_ruby = `ruby -v | awk '{print $2}' | cut -d p -f1 | tr -d '\n' | tr -d '\r'`
return if project_ruby.eql?(local_ruby)
Console.log_error "Error! This project uses Ruby v#{project_ruby}. You're using v#{local_ruby}."
abort
end
def check_bundler_version
project_bundler = `docker-compose run --rm --no-deps testrunner bundler -v \
| awk '{print $3}' | tr -d '\n' | tr -d '\r'`
local_bundler = `bundler -v | awk '{print $3}' | tr -d '\n' | tr -d '\r'`
return if project_bundler.eql?(local_bundler)
message = "Error! This project uses bundler v#{project_bundler}. You're using v#{local_bundler}."
Console.log_error message
abort
end
def container_ip_address_web
ip = `docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
$(docker-compose ps -q web | tr -d '\n' | tr -d '\r') | tr -d '\n' | tr -d '\r'`
"#{ip}:8443"
end
def container_ip_address_api
ip = `docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
$(docker-compose ps -q api | tr -d '\n' | tr -d '\r') | tr -d '\n' | tr -d '\r'`
"#{ip}:8443"
end
def print_summary(number_of_tests_passed, number_of_tests_failed)
summary = "\n Test Run Results: \
\n\t Tests Passed: #{number_of_tests_passed} \
\n\t Tests Failed: #{number_of_tests_failed}"
Console.log_tip summary
end
def execute_test_run(script)
if script.start_with? 'docker-compose'
execute_docker_compose_test_run script
else
execute_native_test_run script
end
end
def repeat_until_failure(script, max_repetitions)
@pass = 0
@fail = 0
max_repetitions.times do |count|
Console.log_tip "\n\n Starting #{count + 1}/#{max_repetitions} \n"
sh %( #{script} ) do |status, _output|
if status
@pass += 1
else
@fail += 1
copy_spec_results
sh %( bin/results )
print_summary(@pass, @fail)
exit 1
end
end
end
print_summary(@pass, @fail)
end
def copy_example_file(example_file)
file = example_file.chomp '.example'
FileUtils.cp file, "#{file}.orig" if File.exist? file
FileUtils.cp example_file, file
end
private
def execute_docker_compose_test_run(script)
sh %( #{script} ) do |status, _output|
if status
Console.log_success "\n Awesome! ツ \n"
else
copy_spec_results
Console.log_tip "\n Run 'bin/results' to view screenshots and HTML. \n"
end
end
end
def execute_native_test_run(script)
sh %( #{script} ) do |status, _output|
Console.log_success "\n Awesome! ツ \n" if status
end
end
def copy_spec_results
sh %( set -e; source src/common.sh && copy_spec_results )
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment