mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 22:57:16 -04:00
#6828 port some tests to json-schema instead of self-made json matching
Fixes #7419
This commit is contained in:
parent
153eea67b5
commit
a10a7eb8c6
7 changed files with 93 additions and 38 deletions
|
@ -1,8 +1,8 @@
|
|||
# encoding: utf-8
|
||||
require "spec_helper"
|
||||
require "json-schema"
|
||||
require "sinatra"
|
||||
require "logstash/api/modules/logging"
|
||||
require "logstash/json"
|
||||
|
||||
describe LogStash::Api::Modules::Logging do
|
||||
include_context "api setup"
|
||||
|
@ -12,20 +12,26 @@ describe LogStash::Api::Modules::Logging do
|
|||
context "when setting a logger's log level" do
|
||||
it "should return a positive acknowledgement on success" do
|
||||
put '/', '{"logger.logstash": "ERROR"}'
|
||||
payload = LogStash::Json.load(last_response.body)
|
||||
expect(payload['acknowledged']).to eq(true)
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{ "properties" => { "acknowledged" => { "enum" => [true] } } },
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
|
||||
it "should throw error when level is invalid" do
|
||||
put '/', '{"logger.logstash": "invalid"}'
|
||||
payload = LogStash::Json.load(last_response.body)
|
||||
expect(payload['error']).to eq("invalid level[invalid] for logger[logstash]")
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{ "properties" => { "error" => { "enum" => ["invalid level[invalid] for logger[logstash]"] } } },
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
|
||||
it "should throw error when key logger is invalid" do
|
||||
put '/', '{"invalid" : "ERROR"}'
|
||||
payload = LogStash::Json.load(last_response.body)
|
||||
expect(payload['error']).to eq("unrecognized option [invalid]")
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{ "properties" => { "error" => { "enum" => ["unrecognized option [invalid]"] } } },
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# encoding: utf-8
|
||||
require "spec_helper"
|
||||
require "json-schema"
|
||||
require "sinatra"
|
||||
require "logstash/api/modules/plugins"
|
||||
require "logstash/json"
|
||||
|
||||
describe LogStash::Api::Modules::Plugins do
|
||||
include_context "api setup"
|
||||
|
@ -14,19 +14,37 @@ describe LogStash::Api::Modules::Plugins do
|
|||
get "/"
|
||||
end
|
||||
|
||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
||||
|
||||
describe "retrieving plugins" do
|
||||
it "should return OK" do
|
||||
expect(last_response).to be_ok
|
||||
end
|
||||
|
||||
it "should return a list of plugins" do
|
||||
expect(payload["plugins"]).to be_a(Array)
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{
|
||||
"properties" => {
|
||||
"plugins" => {
|
||||
"type" => "array"
|
||||
},
|
||||
"required" => ["plugins"]
|
||||
}
|
||||
},
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
|
||||
it "should return the total number of plugins" do
|
||||
expect(payload["total"]).to be_a(Numeric)
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{
|
||||
"properties" => {
|
||||
"total" => {
|
||||
"type" => "number"
|
||||
},
|
||||
"required" => ["total"]
|
||||
}
|
||||
},
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# encoding: utf-8
|
||||
require "spec_helper"
|
||||
require "json-schema"
|
||||
require "sinatra"
|
||||
require "logstash/api/modules/node"
|
||||
require "logstash/json"
|
||||
|
||||
describe LogStash::Api::Modules::Node do
|
||||
include_context "api setup"
|
||||
|
@ -19,7 +19,7 @@ describe LogStash::Api::Modules::Node do
|
|||
end
|
||||
|
||||
it "should return a JSON object" do
|
||||
expect{ LogStash::Json.load(last_response.body) }.not_to raise_error
|
||||
expect(JSON::Validator.validate({}, last_response.body)).to eq(true)
|
||||
end
|
||||
|
||||
context "#threads count" do
|
||||
|
@ -28,14 +28,21 @@ describe LogStash::Api::Modules::Node do
|
|||
get "/hot_threads?threads=5"
|
||||
end
|
||||
|
||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
||||
|
||||
it "should return a json payload content type" do
|
||||
expect(last_response.content_type).to eq("application/json")
|
||||
end
|
||||
|
||||
it "should return information for <= # requested threads" do
|
||||
expect(payload["hot_threads"]["threads"].count).to be <= 5
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{
|
||||
"properties" => {
|
||||
"hot_threads" => {
|
||||
"properties" => { "threads" => { "type" => "array", "maxItems" => 5 } }
|
||||
}
|
||||
}
|
||||
},
|
||||
last_response.body
|
||||
)).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -51,14 +58,12 @@ describe LogStash::Api::Modules::Node do
|
|||
get path
|
||||
end
|
||||
|
||||
let(:payload) { last_response.body }
|
||||
|
||||
it "should return a text/plain content type" do
|
||||
expect(last_response.content_type).to eq("text/plain;charset=utf-8")
|
||||
end
|
||||
|
||||
it "should return a plain text payload" do
|
||||
expect{ JSON.parse(payload) }.to raise_error
|
||||
expect {JSON::Validator.fully_validate({}, payload)}.to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -76,10 +81,8 @@ describe LogStash::Api::Modules::Node do
|
|||
@threads.each { |t| t.kill } rescue nil
|
||||
end
|
||||
|
||||
let(:payload) { last_response.body }
|
||||
|
||||
it "should return information for <= # requested threads" do
|
||||
expect(payload.scan(/thread name/).size).to eq(2)
|
||||
expect(last_response.body.scan(/thread name/).size).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -97,10 +100,8 @@ describe LogStash::Api::Modules::Node do
|
|||
expect(last_response.content_type).to eq("application/json")
|
||||
end
|
||||
|
||||
let(:payload) { last_response.body }
|
||||
|
||||
it "should return a json payload" do
|
||||
expect{ JSON.parse(payload) }.not_to raise_error
|
||||
expect(JSON::Validator.validate({}, last_response.body)).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,6 @@ require "spec_helper"
|
|||
|
||||
require "sinatra"
|
||||
require "logstash/api/modules/node_stats"
|
||||
require "logstash/json"
|
||||
|
||||
describe LogStash::Api::Modules::NodeStats do
|
||||
include_context "api setup"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# encoding: utf-8
|
||||
require "spec_helper"
|
||||
require "json-schema"
|
||||
require "sinatra"
|
||||
require "logstash/api/modules/plugins"
|
||||
require "logstash/json"
|
||||
|
@ -12,8 +13,6 @@ describe LogStash::Api::Modules::Plugins do
|
|||
get "/"
|
||||
end
|
||||
|
||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
||||
|
||||
it "respond to plugins resource" do
|
||||
expect(last_response).to be_ok
|
||||
end
|
||||
|
@ -24,15 +23,31 @@ describe LogStash::Api::Modules::Plugins do
|
|||
|
||||
context "#schema" do
|
||||
it "return the expected schema" do
|
||||
expect(payload.keys).to include("plugins", "total")
|
||||
payload["plugins"].each do |plugin|
|
||||
expect(plugin.keys).to include("name", "version")
|
||||
end
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{
|
||||
"properties" => {
|
||||
"plugins" => {
|
||||
"type" => "array",
|
||||
"items" => [
|
||||
{
|
||||
"type" => "object",
|
||||
"required" => ["version", "name"]
|
||||
}
|
||||
]
|
||||
},
|
||||
"total" => { "type" => "number" }
|
||||
},
|
||||
"required" => ["plugins", "total"]
|
||||
},
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context "#values" do
|
||||
|
||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
||||
|
||||
it "return totals of plugins" do
|
||||
expect(payload["total"]).to eq(payload["plugins"].count)
|
||||
end
|
||||
|
@ -46,9 +61,25 @@ describe LogStash::Api::Modules::Plugins do
|
|||
end
|
||||
|
||||
it "return non empty version values" do
|
||||
payload["plugins"].each do |plugin|
|
||||
expect(plugin["version"]).not_to be_empty
|
||||
end
|
||||
expect(JSON::Validator.fully_validate(
|
||||
{ "properties" => { "plugins" => {
|
||||
"type" => "array",
|
||||
"items" => [
|
||||
{
|
||||
"type" => "object",
|
||||
"properties" => {
|
||||
"version" => {
|
||||
"type" => "string",
|
||||
"minLength" => 1
|
||||
}
|
||||
},
|
||||
"required" => ["version"]
|
||||
}
|
||||
],
|
||||
"minItems" => 1
|
||||
} } },
|
||||
last_response.body)
|
||||
).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,6 @@ require "spec_helper"
|
|||
|
||||
require "sinatra"
|
||||
require "logstash/api/modules/root"
|
||||
require "logstash/json"
|
||||
|
||||
describe LogStash::Api::Modules::Root do
|
||||
include_context "api setup"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "logstash/api/rack_app"
|
||||
require "json-schema"
|
||||
require "rack/test"
|
||||
|
||||
describe LogStash::Api::RackApp do
|
||||
|
@ -55,7 +56,7 @@ describe LogStash::Api::RackApp do
|
|||
end
|
||||
|
||||
it "should return valid JSON" do
|
||||
expect { LogStash::Json.load(last_response.body) }.not_to raise_error
|
||||
expect(JSON::Validator.validate({}, last_response.body)).to eq(true)
|
||||
end
|
||||
|
||||
it "should log the error" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue