Search code examples
ruby-on-railsrubyelasticsearchgithub-actionssearchkick

Tests fail only on github actions with rails, elasticsearch and searchkick


Ruby 3.0.3 Rails 7.0.0.alpha2 elasticsearch 7.17.1 searchkick 5.0.3

My tests are all passing on local but not on GitHub action and I don't know why... Do you know how to show the details of the error 500? This is a Capybara test

This is the config that I added

The results

Run echo $ELASTIC_SEARCH_URL
http://localhost:49154
health
green
create index
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    99  100    66  100    33    985    492 --:--:-- --:--:-- --:--:--  1477
{"acknowledged":true,"shards_acknowledged":true,"index":"iot_log"}read_only_allow_delete
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    67  100    21  100    46   3000   6571 --:--:-- --:--:-- --:--:--  9571
{"acknowledged":true}watermarks
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   654  100   418  100   236  12294   6941 --:--:-- --:--:-- --:--:-- 19818
{
  "acknowledged" : true,
  "persistent" : { },
  "transient" : {
    "cluster" : {
      "routing" : {
        "allocation" : {
          "disk" : {
            "watermark" : {
              "low" : "50gb",
              "flood_stage" : "10gb",
              "high" : "20gb"
            }
          }
        }
      },
      "info" : {
        "update" : {
          "interval" : "1m"
        }
      }
    }
  }
}
Run bundle exec rails test
  bundle exec rails test
  bundle exec rails test:controllers
  # bundle exec rails test test/controllers/companies_controller_test.rb:105
  bundle exec rails test:system
  shell: /usr/bin/bash -e {0}
  env:
    RAILS_ENV: test
    NODE_ENV: test
    ES_HOME: /home/runner/elasticsearch/7.17.1
    DB_PASSWORD: postgres
    DB_PORT: 5432
    REDIS_PORT: 49153
    ELASTIC_SEARCH_URL: http://localhost:49154
Running 33 tests in a single process (parallelization threshold is 50)
Run options: --seed 9006

# Running:

F

Failure:
TasksControllerTest#test_should_destroy_task_&_related_permission(s)_where_user_!=_assignee [/home/runner/work/hubflo/hubflo/test/controllers/tasks_controller_test.rb:134]:
Expected response to be a <3XX: redirect>, but was a <500: Internal Server Error>

rails test test/controllers/tasks_controller_test.rb:128

Edit with answers

So I used the rescue_from option from the answer The error was

#<Searchkick::ImportError: {"type"=>"index_not_found_exception", "reason"=>"no such index [companies_test]", "resource.type"=>"index_expression", "resource.id"=>"companies_test", "index_uuid"=>"_na_", "index"=>"companies_test"} on item with id '650928[31](https://github.com/Hubflo-sas/hubflo/runs/5795678731?check_suite_focus=true#step:16:31)2'>
F

Failure:
CompaniesControllerTest#test_should_destroy_company [/home/runner/work/hubflo/hubflo/test/controllers/companies_controller_test.rb:110]:
Expected response to be a <3XX: redirect>, but was a <500: Internal Server Error>

rails test test/controllers/companies_controller_test.rb:104

I had to run this command before running the tests bundle exec rake searchkick:reindex:all


Solution

  • <500: Internal Server Error> indicates that some exception has been raised in your controller that wasn't properly rescued. If you can't reproduce the issue on your development environment you can:

    1. Either add a begin..rescue in your controller's action, then inside the rescue do: p $ERROR_INFO.message ( $ERROR_INFO is a special variable that contains the last exception rescued, it's a more readable way to use $!
    def my_controller_action
      # ... (business logic)
    rescue
     p $ERROR_INFO.message
    end
    

    you can also add a global rescue_from in you ApplicationController

    class ApplicationController
      rescue_from Exception, with: :my_exception_handler
    
    private
      def my_exception_handler
        # exception handling logic ( logging to Sentry for example etc.)
      end
    end
    

    and here is a more detailed example from the rails documentation

    1. Or, with rails minitest, you can run rails test --backtrace (source here), there are more interesting options in there that you might want to use in your CI, or even when developing locally.