Unable to run Cucumber/Rails against DB other than 'test' - ruby-on-rails

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?

Related

Why does Resque show "0 out of 0 Workers Working" while a worker is running? No jobs getting picked up

I'm trying to test out my Ruby on Rails app. I have redis running, I have a rails server setup and I started a Resque worker in the terminal via the command rake resque:work QUEUE=*, which responds correctly with INFO 2020-01-08 12:10:00 -0500: Starting worker main. When I check the Resque UI in my browser it shows 0 out of 0 workers working. I don't get any error messages in either the terminal or the browser. No jobs are getting picked up, and when I check Resque.workers in the rails console it returns an empty array even while the worker is running in an adjacent tab.
Another test I tried is using --trace as so:
bundle exec rake resque:work --trace
** Invoke resque:work (first_time)
** Invoke resque:preload (first_time)
** Invoke resque:setup (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute resque:setup
** Execute resque:preload
** Invoke resque:setup
** Execute resque:work
set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work
Which, as shown above, doesn't return anything of notice (at least to me). Anyone know why my workers aren't being acknowledged?
So I ended up fixing this by changing the redis db to use 0 instead of 1
'redis://localhost:6379/0'
Any other number results in the workers not being picked up or acknowledged although I can't say why. If anyone knows I'm still interested in hearing what's going on here even though it's solved.

rails and rake commands freeze/hang

I've recently updated an old application to from 3.2.13 to 4.2.10. I've got the application running in development mode but any rake or rails commands I try to run just hang/freeze. The terminal window will hang for a second and then nothing, I'm unable to even stop the process with ctrl-c. I've read a lot about running 'spring stop' to fix this but I don't have spring installed on this app.
Running the commands with --trace aren't particularly helpful.
rails generate model Foo --trace gives me nothing and rake db:migrate --trace gives me:
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
before freezing. Any pointers in the right direction would be appreciated.

Starting resque workers while started development server

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)

Running two thinking sphinx

Ok so i have two rails application on my server and both apps use thinking sphinx and all seems good with one app. when i do
rake ts:start RAILS_ENV=production
Started successfully (pid 9943).
but when i do it on the other app i get
rake ts:start RAILS_ENV=production --trace
** Invoke ts:start (first_time)
** Invoke thinking_sphinx:start (first_time)
** Invoke thinking_sphinx:app_env (first_time)
** Execute thinking_sphinx:app_env
** Invoke environment (first_time)
** Execute environment
Jammit Warning: Asset compression disabled -- Java unavailable.
Jammit Warning: No assets match 'public/stylesheets/public.css'
** Execute thinking_sphinx:start
Failed to start searchd daemon. Check /var/www/projects/log/searchd.log.
Failed to start searchd daemon. Check /var/www/projects/log/searchd.log
** Execute ts:start
they are both using different ports....any idea...also i checked the log file here /var/www/projects/log/searchd.log and its blank ...any idea what to do...i have tried almost everything
rake ts:start starts your sphinx daemon. If you want your apps to share the same search engine, you should only do ts:start once. and both apps should work.

Why do test:units and test:functionals insist on running in development environment?

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

Resources