logstash/spec/support/resource_dsl_methods.rb
Andrew Cholakian 78f70634ed Refactor API specs to play well with others
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 #7132
2017-05-18 13:45:27 +00:00

88 lines
2.5 KiB
Ruby

# Ruby doesn't have common class for boolean,
# And to simplify the ResourceDSLMethods check it make sense to have it.
module Boolean; end
class TrueClass
include Boolean
end
class FalseClass
include Boolean
end
module ResourceDSLMethods
# Convert a nested hash to a mapping of key paths to expected classes
def hash_to_mapping(h, path=[], mapping={})
h.each do |k,v|
if v.is_a?(Hash)
hash_to_mapping(v, path + [k], mapping)
else
full_path = path + [k]
mapping[full_path] = v
end
end
mapping
end
def test_api(expected, path)
context "GET #{path}" do
let(:payload) { LogStash::Json.load(last_response.body) }
before(:all) do
get path
end
it "should respond OK" do
expect(last_response).to be_ok
end
describe "the default metadata" do
it "should include the host" do
expect(payload["host"]).to eql(Socket.gethostname)
end
it "should include the version" do
expect(payload["version"]).to eql(LOGSTASH_CORE_VERSION)
end
it "should include the http address" do
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(@agent.name)
end
it "should include the node id" do
expect(payload["id"]).to eql(@agent.id)
end
end
hash_to_mapping(expected).each do |resource_path,klass|
dotted = resource_path.join(".")
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
expect(resource_path_value).to be_a(klass), "could not find '#{dotted}' in #{payload}"
end
end
end
yield if block_given? # Add custom expectations
end
def test_api_and_resources(expected, xopts={})
xopts[:exclude_from_root] ||= []
root_expectation = expected.clone
xopts[:exclude_from_root].each {|k| root_expectation.delete(k)}
test_api(root_expectation, "/")
expected.keys.each do |key|
test_api({key => expected[key]}, "/#{key}")
end
end
end