Skip to content

Commit

Permalink
Refactor API specs to play well with others
Browse files Browse the repository at this point in the history
This is a big chang, it:

1. Moves API specs out of their special hierarchy
2. Removes the API spec spec_helper
3. Reactivates that stats command spec (that was accidentally not being
run before due to it not having _spec as a suffix

This was required to fix the preceeding commit, where we added a
before(:each) hook to the spec_helper that wasn't being picked up in
some cases due to the existence of two spec helpers and a $LOAD_PATH
that could change.

Fixes elastic#7132
  • Loading branch information
andrewvc committed May 18, 2017
1 parent ff222b6 commit 78f7063
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 135 deletions.
106 changes: 0 additions & 106 deletions logstash-core/spec/api/spec_helper.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# encoding: utf-8
require_relative "../../spec_helper"
require "spec_helper"

describe LogStash::Api::Commands::Stats do
include_context "api setup"

let(:report_method) { :run }
subject(:report) { report_class.new.send(report_method) }
subject(:report) do
factory = ::LogStash::Api::CommandFactory.new(LogStash::Api::Service.new(@agent))

factory.build(:stats).send(report_method)
end

let(:report_class) { described_class }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8
require_relative "../spec_helper"
require "spec_helper"
require "logstash/api/errors"

describe LogStash::Api::ApiError do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8
require_relative "../../spec_helper"
require "spec_helper"
require "sinatra"
require "logstash/api/modules/logging"
require "logstash/json"
Expand All @@ -10,15 +10,6 @@
describe "#logging" do

context "when setting a logger's log level" do
before(:all) do
@runner = LogStashRunner.new
@runner.start
end

after(:all) do
@runner.stop
end

it "should return a positive acknowledgement on success" do
put '/', '{"logger.logstash": "ERROR"}'
payload = LogStash::Json.load(last_response.body)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8
require_relative "../../../support/shared_examples"
require_relative "../../spec_helper"
require "spec_helper"
require "sinatra"
require "logstash/api/modules/plugins"
require "logstash/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8
require_relative "../../spec_helper"
require_relative "../../../support/shared_examples"
require "spec_helper"
require "sinatra"
require "logstash/api/modules/node"
require "logstash/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
require_relative "../../spec_helper"
require_relative "../../../support/shared_examples"
require "spec_helper"

require "sinatra"
require "logstash/api/modules/node_stats"
require "logstash/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8
require_relative "../../spec_helper"
require_relative "../../../support/shared_examples"
require "spec_helper"
require "sinatra"
require "logstash/api/modules/plugins"
require "logstash/json"
Expand Down Expand Up @@ -40,7 +39,9 @@

it "return a list of available plugins" do
payload["plugins"].each do |plugin|
expect(plugin).to be_available?
expect do
Gem::Specification.find_by_name(plugin["name"])
end.not_to raise_error
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
require_relative "../../spec_helper"
require_relative "../../../support/shared_examples"
require "spec_helper"

require "sinatra"
require "logstash/api/modules/root"
require "logstash/json"
Expand Down
File renamed without changes.
12 changes: 10 additions & 2 deletions logstash-core/spec/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def clear_data_dir
end
end

def mock_settings(settings_values)
def mock_settings(settings_values={})
settings = LogStash::SETTINGS.clone

settings_values.each do |key, value|
Expand All @@ -32,6 +32,14 @@ def mock_settings(settings_values)
settings
end

def make_test_agent(settings=mock_settings)
sl = LogStash::Config::SourceLoader.new
sl.add_source(LogStash::Config::Source::Local.new(settings))
sl

::LogStash::Agent.new(settings, sl)
end

def mock_pipeline(pipeline_id, reloadable = true, config_hash = nil)
config_string = "input { stdin { id => '#{pipeline_id}' }}"
settings = mock_settings("pipeline.id" => pipeline_id.to_s,
Expand Down Expand Up @@ -90,4 +98,4 @@ def temporary_file(content, file_name = Time.now.to_i.to_s, directory = Stud::Te
end
end

SUPPORT_DIR = Pathname.new(::File.join(::File.dirname(__FILE__), "support"))
SUPPORT_DIR = Pathname.new(::File.join(::File.dirname(__FILE__), "support"))
21 changes: 21 additions & 0 deletions logstash-core/spec/support/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,24 @@
allow(pipeline).to receive(:agent).and_return(agent)
end
end

shared_context "api setup" do
before :all do
clear_data_dir
settings = mock_settings
config_string = "input { generator {id => 'api-generator-pipeline' count => 100 } } output { dummyoutput {} }"
settings.set("config.string", config_string)
@agent = make_test_agent(settings)
@agent.execute
end

after :all do
@agent.shutdown
end

include Rack::Test::Methods

def app()
described_class.new(nil, @agent)
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
require "flores/rspec"
require "flores/random"
require "pathname"
require "stud/task"
require "logstash/devutils/rspec/spec_helper"
require "support/resource_dsl_methods"
require "support/mocks_classes"
require "support/helpers"
require "support/shared_contexts"
require "support/shared_examples"
require 'rspec/expectations'
require "logstash/settings"
require 'rack/test'
require 'rspec'
require "json"


class JSONIOThingy < IO
def initialize; end
Expand Down Expand Up @@ -53,3 +66,4 @@ def puts(payload)
def installed_plugins
Gem::Specification.find_all.select { |spec| spec.metadata["logstash_plugin"] }.map { |plugin| plugin.name }
end

Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def test_api(expected, path)
end

it "should include the http address" do
expect(payload["http_address"]).to eql("#{Socket.gethostname}:#{::LogStash::WebServer::DEFAULT_PORTS.first}")
expect(payload["http_address"]).to eql("127.0.0.1:#{::LogStash::WebServer::DEFAULT_PORTS.first}")
end

it "should include the node name" do
expect(payload["name"]).to eql(@runner.agent.name)
expect(payload["name"]).to eql(@agent.name)
end

it "should include the node id" do
expect(payload["id"]).to eql(@runner.agent.id)
expect(payload["id"]).to eql(@agent.id)
end
end

Expand All @@ -63,6 +63,7 @@ def test_api(expected, path)
it "should set '#{dotted}' at '#{path}' to be a '#{klass}'" do
expect(last_response).to be_ok # fail early if need be
resource_path_value = resource_path.reduce(payload) do |acc,v|
expect(acc).to be_a(Hash), "Got a nil looking for #{resource_path} in #{payload}"
expect(acc.has_key?(v)).to eql(true), "Expected to find value '#{v}' in structure '#{acc}', but could not. Payload was '#{payload}'"
acc[v]
end
Expand Down

0 comments on commit 78f7063

Please sign in to comment.