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
|
# encoding: utf-8
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
require "json-schema"
|
||||||
require "sinatra"
|
require "sinatra"
|
||||||
require "logstash/api/modules/logging"
|
require "logstash/api/modules/logging"
|
||||||
require "logstash/json"
|
|
||||||
|
|
||||||
describe LogStash::Api::Modules::Logging do
|
describe LogStash::Api::Modules::Logging do
|
||||||
include_context "api setup"
|
include_context "api setup"
|
||||||
|
@ -12,20 +12,26 @@ describe LogStash::Api::Modules::Logging do
|
||||||
context "when setting a logger's log level" do
|
context "when setting a logger's log level" do
|
||||||
it "should return a positive acknowledgement on success" do
|
it "should return a positive acknowledgement on success" do
|
||||||
put '/', '{"logger.logstash": "ERROR"}'
|
put '/', '{"logger.logstash": "ERROR"}'
|
||||||
payload = LogStash::Json.load(last_response.body)
|
expect(JSON::Validator.fully_validate(
|
||||||
expect(payload['acknowledged']).to eq(true)
|
{ "properties" => { "acknowledged" => { "enum" => [true] } } },
|
||||||
|
last_response.body)
|
||||||
|
).to be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should throw error when level is invalid" do
|
it "should throw error when level is invalid" do
|
||||||
put '/', '{"logger.logstash": "invalid"}'
|
put '/', '{"logger.logstash": "invalid"}'
|
||||||
payload = LogStash::Json.load(last_response.body)
|
expect(JSON::Validator.fully_validate(
|
||||||
expect(payload['error']).to eq("invalid level[invalid] for logger[logstash]")
|
{ "properties" => { "error" => { "enum" => ["invalid level[invalid] for logger[logstash]"] } } },
|
||||||
|
last_response.body)
|
||||||
|
).to be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should throw error when key logger is invalid" do
|
it "should throw error when key logger is invalid" do
|
||||||
put '/', '{"invalid" : "ERROR"}'
|
put '/', '{"invalid" : "ERROR"}'
|
||||||
payload = LogStash::Json.load(last_response.body)
|
expect(JSON::Validator.fully_validate(
|
||||||
expect(payload['error']).to eq("unrecognized option [invalid]")
|
{ "properties" => { "error" => { "enum" => ["unrecognized option [invalid]"] } } },
|
||||||
|
last_response.body)
|
||||||
|
).to be_empty
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
require "json-schema"
|
||||||
require "sinatra"
|
require "sinatra"
|
||||||
require "logstash/api/modules/plugins"
|
require "logstash/api/modules/plugins"
|
||||||
require "logstash/json"
|
|
||||||
|
|
||||||
describe LogStash::Api::Modules::Plugins do
|
describe LogStash::Api::Modules::Plugins do
|
||||||
include_context "api setup"
|
include_context "api setup"
|
||||||
|
@ -14,19 +14,37 @@ describe LogStash::Api::Modules::Plugins do
|
||||||
get "/"
|
get "/"
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
|
||||||
|
|
||||||
describe "retrieving plugins" do
|
describe "retrieving plugins" do
|
||||||
it "should return OK" do
|
it "should return OK" do
|
||||||
expect(last_response).to be_ok
|
expect(last_response).to be_ok
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a list of plugins" do
|
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
|
end
|
||||||
|
|
||||||
it "should return the total number of plugins" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
require "json-schema"
|
||||||
require "sinatra"
|
require "sinatra"
|
||||||
require "logstash/api/modules/node"
|
require "logstash/api/modules/node"
|
||||||
require "logstash/json"
|
|
||||||
|
|
||||||
describe LogStash::Api::Modules::Node do
|
describe LogStash::Api::Modules::Node do
|
||||||
include_context "api setup"
|
include_context "api setup"
|
||||||
|
@ -19,7 +19,7 @@ describe LogStash::Api::Modules::Node do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a JSON object" do
|
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
|
end
|
||||||
|
|
||||||
context "#threads count" do
|
context "#threads count" do
|
||||||
|
@ -28,14 +28,21 @@ describe LogStash::Api::Modules::Node do
|
||||||
get "/hot_threads?threads=5"
|
get "/hot_threads?threads=5"
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
|
||||||
|
|
||||||
it "should return a json payload content type" do
|
it "should return a json payload content type" do
|
||||||
expect(last_response.content_type).to eq("application/json")
|
expect(last_response.content_type).to eq("application/json")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return information for <= # requested threads" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -51,14 +58,12 @@ describe LogStash::Api::Modules::Node do
|
||||||
get path
|
get path
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:payload) { last_response.body }
|
|
||||||
|
|
||||||
it "should return a text/plain content type" do
|
it "should return a text/plain content type" do
|
||||||
expect(last_response.content_type).to eq("text/plain;charset=utf-8")
|
expect(last_response.content_type).to eq("text/plain;charset=utf-8")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a plain text payload" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -76,10 +81,8 @@ describe LogStash::Api::Modules::Node do
|
||||||
@threads.each { |t| t.kill } rescue nil
|
@threads.each { |t| t.kill } rescue nil
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:payload) { last_response.body }
|
|
||||||
|
|
||||||
it "should return information for <= # requested threads" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -97,10 +100,8 @@ describe LogStash::Api::Modules::Node do
|
||||||
expect(last_response.content_type).to eq("application/json")
|
expect(last_response.content_type).to eq("application/json")
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:payload) { last_response.body }
|
|
||||||
|
|
||||||
it "should return a json payload" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,6 @@ require "spec_helper"
|
||||||
|
|
||||||
require "sinatra"
|
require "sinatra"
|
||||||
require "logstash/api/modules/node_stats"
|
require "logstash/api/modules/node_stats"
|
||||||
require "logstash/json"
|
|
||||||
|
|
||||||
describe LogStash::Api::Modules::NodeStats do
|
describe LogStash::Api::Modules::NodeStats do
|
||||||
include_context "api setup"
|
include_context "api setup"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
require "json-schema"
|
||||||
require "sinatra"
|
require "sinatra"
|
||||||
require "logstash/api/modules/plugins"
|
require "logstash/api/modules/plugins"
|
||||||
require "logstash/json"
|
require "logstash/json"
|
||||||
|
@ -12,8 +13,6 @@ describe LogStash::Api::Modules::Plugins do
|
||||||
get "/"
|
get "/"
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:payload) { LogStash::Json.load(last_response.body) }
|
|
||||||
|
|
||||||
it "respond to plugins resource" do
|
it "respond to plugins resource" do
|
||||||
expect(last_response).to be_ok
|
expect(last_response).to be_ok
|
||||||
end
|
end
|
||||||
|
@ -24,15 +23,31 @@ describe LogStash::Api::Modules::Plugins do
|
||||||
|
|
||||||
context "#schema" do
|
context "#schema" do
|
||||||
it "return the expected schema" do
|
it "return the expected schema" do
|
||||||
expect(payload.keys).to include("plugins", "total")
|
expect(JSON::Validator.fully_validate(
|
||||||
payload["plugins"].each do |plugin|
|
{
|
||||||
expect(plugin.keys).to include("name", "version")
|
"properties" => {
|
||||||
end
|
"plugins" => {
|
||||||
|
"type" => "array",
|
||||||
|
"items" => [
|
||||||
|
{
|
||||||
|
"type" => "object",
|
||||||
|
"required" => ["version", "name"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"total" => { "type" => "number" }
|
||||||
|
},
|
||||||
|
"required" => ["plugins", "total"]
|
||||||
|
},
|
||||||
|
last_response.body)
|
||||||
|
).to be_empty
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "#values" do
|
context "#values" do
|
||||||
|
|
||||||
|
let(:payload) { LogStash::Json.load(last_response.body) }
|
||||||
|
|
||||||
it "return totals of plugins" do
|
it "return totals of plugins" do
|
||||||
expect(payload["total"]).to eq(payload["plugins"].count)
|
expect(payload["total"]).to eq(payload["plugins"].count)
|
||||||
end
|
end
|
||||||
|
@ -46,9 +61,25 @@ describe LogStash::Api::Modules::Plugins do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "return non empty version values" do
|
it "return non empty version values" do
|
||||||
payload["plugins"].each do |plugin|
|
expect(JSON::Validator.fully_validate(
|
||||||
expect(plugin["version"]).not_to be_empty
|
{ "properties" => { "plugins" => {
|
||||||
end
|
"type" => "array",
|
||||||
|
"items" => [
|
||||||
|
{
|
||||||
|
"type" => "object",
|
||||||
|
"properties" => {
|
||||||
|
"version" => {
|
||||||
|
"type" => "string",
|
||||||
|
"minLength" => 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required" => ["version"]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"minItems" => 1
|
||||||
|
} } },
|
||||||
|
last_response.body)
|
||||||
|
).to be_empty
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,6 @@ require "spec_helper"
|
||||||
|
|
||||||
require "sinatra"
|
require "sinatra"
|
||||||
require "logstash/api/modules/root"
|
require "logstash/api/modules/root"
|
||||||
require "logstash/json"
|
|
||||||
|
|
||||||
describe LogStash::Api::Modules::Root do
|
describe LogStash::Api::Modules::Root do
|
||||||
include_context "api setup"
|
include_context "api setup"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require "logstash/api/rack_app"
|
require "logstash/api/rack_app"
|
||||||
|
require "json-schema"
|
||||||
require "rack/test"
|
require "rack/test"
|
||||||
|
|
||||||
describe LogStash::Api::RackApp do
|
describe LogStash::Api::RackApp do
|
||||||
|
@ -55,7 +56,7 @@ describe LogStash::Api::RackApp do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return valid JSON" do
|
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
|
end
|
||||||
|
|
||||||
it "should log the error" do
|
it "should log the error" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue