This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_dsl_methods.rb
88 lines (73 loc) · 2.54 KB
/
resource_dsl_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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