Search code examples
ruby-on-railsrubyelasticsearchrspecfacet

RSPEC can't find elasticsearch when reinstalled manually (without homebrew)


I've been researching this problem for a couple of weeks now. A rails app I'm working on uses elasticsearch 1.5. I was managing elasticsearch with homebrew, and it upgraded my elasticsearch to 2.x. The app didn't function with this newest version of elasticsearch, and the search function completely stopped working (due to the use of facets being deprecated). While a migration to aggregations is planned for the coming months, I had other problems to work on, so I brew uninstalled the newer version of elasticsearch. Our app's version wasn't available through homebrew, so I found it online and downloaded and installed it using a zip. The search function works with the app again, but now rspec can't find elasticsearch and won't function at all. It says elasticsearch is starting but then throws an error:

Starting 1 Elasticsearch nodes...sh: elasticsearch: command not found. 

& then this error:

/.rvm/gems/ruby-2.0.0-p643/gems/elasticsearch-extensions-0.0.18
    /lib/elasticsearch/extensions/test
    /cluster.rb:240:in `sleep': execution expired (Timeout::Error)

In order to start working on the migration to aggregations from facets, I have to get rspec working. Thanks in advance for any help. I've already set a bash alias to my location of elasticsearch. My guess is that rspec is not finding the manually installed version, but I'm unclear about where to deal with this.

spec_helper code:

def start_es_server
  Elasticsearch::Extensions::Test::Cluster.start(nodes: 1) unless   Elasticsearch::Extensions::Test::Cluster.running?

  # create index(s) to test against.
  create_es_index(Item)
end

def stop_es_server
  Elasticsearch::Extensions::Test::Cluster.stop if Elasticsearch::Extensions::Test::Cluster.running?
end

RSpec.configure do |config|
  config.include Capybara::DSL
  config.mock_with :rspec
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  config.raise_errors_for_deprecations!  

  config.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }
  config.mock_with :rspec do |mocks|
    mocks.syntax = [:should, :expect]
    mocks.verify_partial_doubles = true
  end

Solution

  • I found an answer to this question by looking into the https://github.com/elastic/elasticsearch-ruby/blob/b3cfdcbde678c2704c0a557a163782b9e027d144/elasticsearch-extensions/lib/elasticsearch/extensions/test/cluster.rb . The list of optional arguments that can be passed to the start method include one called :command which defaults to 'elasticsearch.' This can be overridden with a direct path to the location of your elasticsearch:

    elasticsearch = '/usr/local/elasticsearch-1.5.2/bin/elasticsearch'
    Elasticsearch::Extensions::Test::Cluster.start(nodes: 1, command: elasticsearch) unless Elasticsearch::Extensions::Test::Cluster.running?