I have a full suite of cucumber/rails tests for my application. I've recently implemented a containerized architecture for testing using docker-compose to more accurately model the production environment and in an attempt to identify and solve for some front-end timezone bugs. This was working, but going full container is overkill for some tests so I created a separate docker environment by adding:
docker db configuration in config/database.yml
config/environments/docker.rb
in the expectation of being able to pass in RAILS_ENV=docker to hit the remote (dockerized) DB and then have it default to using the DB on localhost.
Unfortunately, when I attempt to run in the containerized configuration (i.e. RAILS_ENV=docker and no db installed on localhost), as soon I try to execute (in features/support/env.rb):
require 'cucumber/rails'
I'm met with:
** Invoke cucumber (first_time)
** Invoke cucumber:ok (first_time)
** Invoke test:prepare (first_time)
** Execute test:prepare
** Execute cucumber:ok
/home/colab/.asdf/installs/ruby/3.1.2/bin/ruby -S bundle exec cucumber --profile default features/verify_env.feature
beginning of execution
rails aborted!
ActiveRecord::DatabaseConnectionError: There is an issue connecting with your hostname: 127.0.0.1.
Please check your database configuration and ensure there is a valid connection to your database.
Caused by:
Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1:3306' (111)
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
Oh! and I am actually running it with the --trace option.
Does anyone know why this is happening or how I might specify an environment and make it stick?
I am deploying a Rails application using AWS OpsWorks. To precompile the assets, I use the following Chef recipe:
node[:deploy].each do |application, deploy|
deploy_to = node[:deploy][application][:deploy_to]
rails_env = node[:deploy][application][:rails_env]
directory "#{deploy_to}/shared/assets"
link "#{deploy_to}/current/public/assets" do
to "#{deploy_to}/shared/assets"
end
execute "rake assets:precompile" do
cwd "#{deploy_to}/current"
command "bundle exec rake assets:precompile"
environment "RAILS_ENV" => rails_env
end
end
It precompiles correctly, but in following deployments it goes through the whole precompile process again, even though no asset was modified and the assets folder is shared. I also tried a Chef hook, as suggested here, with the same result. How could you make it run only when needed?
You can add not_if or only_if clause to the statement:
Something like this:
execute "rake assets:precompile" do
cwd "#{deploy_to}/current"
command "bundle exec rake assets:precompile"
environment "RAILS_ENV" => rails_env
not_if { File.exists?("<path to expected precompiled asset>") }
end
If you want it to run every time there is a change in a certain directory, you can use chef notifications.
What's the difference between adding a RAILS_ENV before or after a rake task? Here are samples from my staging environments:
Adding RAILS_ENV after rake task.
This raised an error, and the reason for this is accepting development environment as by default and not taking devutility as the environment.
$bundle exec rake -T RAILS_ENV=devutility
$rake aborted!
$cannot load such file -- rack/bug
Adding RAILS_ENV before rake task
This works and lists all the rake task available.
$RAILS_ENV=devutility bundle exec rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake bourbon:install[sass_path] # Move files to the Rails assets directory
rake ci # Continuous Integration build (simplecov-rcov and deploy)
rake cucumber # Alias for cucumber:ok
rake cucumber:all # Run all features
rake cucumber:ok # Run features that should pass
rake cucumber:rerun # Record failing features and run only them if any exist
rake cucumber:wip # Run features that are being worked on
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:data:dump ....................
..............
RAILS_ENV is an environment variable that needs to be available before running your rake task.
When you do:
RAILS_ENV=devutility bundle exec rake -T
It has the same affect as:
export RAILS_ENV=devutility
bundle exec rake -T
RAILS_ENV is not an argument to rake as it may appear, it's part of the environment available to Ruby though it's ENV constant.
I want to start my workers when I start my development server to test the a new cron job I have in my resque scheduler, so I run this command when starting dev server-
QUEUE=* rake environment resque:work rails s
it has worked for me before and if I'm reading their documentation correctly should still work.
But I'm getting the following error after I interrupt it after it gets hung up-
^Crake aborted!
Don't know how to build task 'rails'
here is what I get after keyboard interrupt and running it with --trace
** Invoke environment (first_time)
** Execute environment
** Invoke resque:work (first_time)
** Invoke resque:preload (first_time)
** Invoke resque:setup (first_time)
** Invoke environment
** Execute resque:setup
** Execute resque:preload
** Invoke resque:setup
** Execute resque:work
^Crake aborted!
Don't know how to build task 'rails'
I don't understand why I would be getting the error and additionally why it previously loaded and worked but not anymore. What am I missing here?
Are you sure that you manage to run this command sometime back successfully
QUEUE=* rake environment resque:work rails s
because the way I know rails s is a list of rails command not a rake task
you can run consecutive rake separating spaces like
QUEUE=* rake environment rake1 rake2 rake3
but you cant run a rake and rails command they way you mention above
what I see from your trace above that rake(resque rake) is running now instead of passing a second rake you specified a rails command to start the server rake is considering that as rake task (which is not true)
I believe you are looking for this
QUEUE=* rake environment resque:work && rails s
But I dont believe that what you have mention would ever work please let me know if something conflict over here
Hope It make sense
This is better:
BACKGROUND=yes QUEUE=* PIDFILE=./tmp/resque.pid bundle exec rake environment resque:work
Then:
rails s
If you need stop resque worker:
kill -QUIT $(cat ./tmp/resque.pid)
I'm trying to test some environment-specific settings (middleware to be specific, but it doesn't seem to matter), but I'm having trouble getting the tests to run in the correct environment. The symptoms:
If I run ruby test/unit/my_test.rb, all is fine because the first thing it does is require test/test_helper.rb, which sets the environment to "test" and then loads the Rails environment.
If I run rake test, the first batch (functionals) run fine, but the second batch (units) fail. The failure is that ENV['RAILS_ENV] somehow gets unset between batches, then config/environment.rb sees that none is set and uses the default of "development". The environment is correct at the beginning of config/environment.rb and at the beginning of the configuration block in that file, but wrong by the end. I can tell by using a puts or by deleting config/development.rb causing it to not find the file.
If I run rake test:units, I get the same problem as the second batch of rake test (that is, all fail)
If I run rake test:functionals, I get the same as for rake test:units
If I run rake test RAILS_ENV=test or rake test:units RAILS_ENV=test or rake test:functionals RAILS_ENV=test, all is fine!
One thing I tried that doesn't work at all is the following:
# in Rakefile:
task :set_test_env { ENV['RAILS_ENV'] = 'test' }
Rake::Task[:test].prerequisites.unshift :set_test_env
# and similarly for other test tasks
I even tried creating a separate one of those :set_test_env tasks for each test task so that I was sure it would get called before each one instead of just once. Still no dice. It does get called, and the environment is correct at the beginning of config/environment.rb but something goes wrong inside.
I have no calls to set ENV['RAILS_ENV'] in my code anywhere.
To give you a full answer I would have to take a look at the code, but I'll try to give you some clues that might be helpful.
First of all rake test and all the other variants (test:units, test:functionals etc.) work as follows
Rake process is invoked and task test is executed in the current environment (which is development by default), that's why development.rb is always read.
The Rake task invokes the test loader in a separate child process (you can verify this with ps or pstree), this is where the test_helper.rb is sourced and environment is set to test.
When you run ruby test/unit/my_test.rb the first step is skipped, so it looks like the problem lies there. Maybe you do something in development.rb that has side effects for the subprocess?
I pretty much always want to force my tests to run themselves and their prerequisites in the "test" environment, especially when ENV['RAILS_ENV'] is set to any of the common defaults (to avoid catastrophic accidents), but I also want to be able to run tests on, say, an environment named "v_2_0_maint_test" or something like that by calling rake test:units RAILS_ENV=v_2_0_maint_test on the command line.
So I have a test_tasks.rake file that prepends a prerequisite onto each of the test tasks that I'm interested in. Since this prerequisite is prepended, any other prerequisites (e.g. db:test:prepare, db:fixtures:load) run in the same environment. This claims the virtue of affecting only the tests you want to affect, and their prerequisites.
namespace :test do |n|
[ n[:units], n[:functionals], n[:integration] ].each do |t|
t.prerequisites.unshift(:set_test_env_dammit)
end
task :set_test_env_dammit do |t|
if [ nil, "", "development", "staging", "production" ].index ENV['RAILS_ENV']
RAILS_ENV = "test"
end
end
end
At the top of the test_helper.rb file I have the code
ENV["RAILS_ENV"] = "test"
If you do not have that line then the system problem would run in the default environment (i.e. development).
Search through your entire project for RAILS_ENV and its variants. See if you set it somewhere in your application or in your tests.
Also, what platform are you running on? Can you run tests on another machine and see if the results are the same?
If this is a relatively recent development and you're using a RCS like Git or SVN, you should look through the recent commits, and if you're specifically using Git, you should look into git bisect. If you're not using an RCS, you should be.
If this is truly a fresh app problem, then it's probably a problem with your environment.
What plugins and gems do you have installed/configured? Can we see the backtrace?
I do not actually believe that you have an issue here at all. I believe that at some time you spotted that your rake task was actually hitting the development environment and you started to try and work out why that was the case. You then added a line within the development config file to raise an exception and this is why your rake tests are failing.
If you remove the line that is raising an exception then you may find that the tests all run succesfuly.
If you try the following you may find that it explains the issue.
Set the environment to development (Just for purposes of the trace)
export RAILS_ENV=development
Remove all lines that artificially
raise exceptions in your environment
files.
Add the following line to the end of
each file in config/environments
puts "**** In #{ENV['RAILS_ENV']} environment config ****"
Add the following line to the test/test_helper.rb just below the line that sets the environment to test.
puts "**** Loading test helper **** Environment = #{ENV['RAILS_ENV']}"
Run rake using the trace option to watch the tasks execute with a trace of the currently active environment.
rake -t test
Examine the output to determine where your environment is not set correctly.
Run the tests again but this time set the environment directly before running the tests using
export RAILS_ENV=test
If you really do have an issue then perhaps you should post up the output of the rake -t with the tracing code in.
This is the output of my tests in a brand new project with some minimal testing in place. You will notice that before the actual tests are run the environment is always "test"
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
**** In development environment config ****
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:units
**** Loading test helper **** Environment = test
**** In test environment config ****
Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
.
Finished in 0.071771 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
** Invoke test:functionals (first_time)
** Invoke db:test:prepare
** Execute test:functionals
**** Loading test helper **** Environment = test
**** In test environment config ****
Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
.....
Finished in 0.133776 seconds.
5 tests, 6 assertions, 0 failures, 0 errors
** Invoke test:integration (first_time)
** Invoke db:test:prepare
** Execute test:integration