Nona
Nona

Reputation: 5462

How to run elasticsearch server in rspec

Running on Linux Mint 16, I followed this guide here: http://pivotallabs.com/rspec-elasticsearchruby-elasticsearchmodel/ to setup elasticsearch with Ruby on Rails application.

When I run rspec, when it hits this line spec_helper.rb:

config.before :each, elasticsearch: true do
    Elasticsearch::Extensions::Test::Cluster.start(port: 9200) unless Elasticsearch::Extensions::Test::Cluster.running?
  end 

I received the following error:

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

I thought it might be a path issue....

So I added the following to ~/.bashrc:

export PATH="/etc/init.d:$PATH" since sudo /etc/init.d/elasticsearch start starts the elasticsearch service.

Then I issued command source ~/.bashrc

This got rid of the sh: 1: elasticsearch: not found message and instead the message from the error triggered in spec_helper.rb was:

............Starting 2 Elasticsearch nodes..
[!!!] Process failed to start (see output above)
F........

Below is the config block in my spec_helper.rb file:

config.before :each, elasticsearch: true do
    Article.__elasticsearch__.client = Elasticsearch::Client.new host: 'http://localhost:9200'
    Article.__elasticsearch__.create_index!(force: true)
    Article.__elasticsearch__.refresh_index!
    Elasticsearch::Extensions::Test::Cluster.start(port: 9200) unless Elasticsearch::Extensions::Test::Cluster.running?
  end

  config.after :suite do
    Elasticsearch::Extensions::Test::Cluster.stop(port: 9200) if Elasticsearch::Extensions::Test::Cluster.running?
  end

Any ideas on what the issue might be?

EDIT: If I change to port 9250 as suggested by commenter below:

config.before :each, elasticsearch: true do
   Article.__elasticsearch__.client = Elasticsearch::Client.new host: 'http://localhost:9250'
    Article.__elasticsearch__.create_index!(force: true)
    Article.__elasticsearch__.refresh_index!
    Elasticsearch::Extensions::Test::Cluster.start(port: 9250) unless Elasticsearch::Extensions::Test::Cluster.running?
  end

  config.after :suite do
    Elasticsearch::Extensions::Test::Cluster.stop(port: 9250) if Elasticsearch::Extensions::Test::Cluster.running?
  end

I get this new error:

An error occurred in an after hook
    Faraday::ConnectionFailed: Connection refused - connect(2) for "localhost" port 9250
    occurred at /home/nona/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/net/http.rb:879:in `initialize'

F........

Upvotes: 2

Views: 3521

Answers (1)

mdmoskwa
mdmoskwa

Reputation: 138

I remember having a similar problem, and I solved it with the following line in my test_helper:

ENV["TEST_CLUSTER_NODES"] = "1" # need to set so we trigger correct ES defaults

I figured it out by looking at the Elasticsearch::Extensions::Test::Cluster source and realizing that Elasticsearch::Extensions::Test::Cluster.running? was returning true (and therefore not starting the cluster) unless that TEST_CLUSTER_NODES value is set.

Upvotes: 3

Related Questions