Commit 969b8124 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Add schema matcher for non response objects + use schema to test additional metrics compliance

parent ccf89acc
{
"items": {
"properties": {
"group": {
"type": "string"
},
"metrics": {
"items": {
"properties": {
"queries": {
"items": {
"properties": {
"query_range": {
"type": "string"
},
"query": {
"type": "string"
},
"result": {
"type": "any"
}
},
"type": "object"
},
"type": "array"
},
"title": {
"type": "string"
},
"weight": {
"type": "integer"
},
"y_label": {
"type": "string"
}
},
"type": "object"
},
"required": [
"metrics",
"title",
"weight"
],
"type": "array"
},
"priority": {
"type": "integer"
}
},
"type": "object"
},
"required": [
"group",
"priority",
"metrics"
],
"type": "array"
}
\ No newline at end of file
def schema_path(schema)
schema_directory = "#{Dir.pwd}/spec/fixtures/api/schemas"
"#{schema_directory}/#{schema}.json"
end
RSpec::Matchers.define :match_response_schema do |schema, **options| RSpec::Matchers.define :match_response_schema do |schema, **options|
match do |response| match do |response|
schema_directory = "#{Dir.pwd}/spec/fixtures/api/schemas" JSON::Validator.validate!(schema_path(schema), response.body, options)
schema_path = "#{schema_directory}/#{schema}.json" end
end
JSON::Validator.validate!(schema_path, response.body, options) RSpec::Matchers.define :match_schema do |schema, **options|
match do |data|
JSON::Validator.validate!(schema_path(schema), data, options)
end end
end end
...@@ -34,7 +34,7 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -34,7 +34,7 @@ RSpec.shared_examples 'additional metrics query' do
priority: 1, priority: 1,
metrics: [ metrics: [
{ {
title: 'title', weight: nil, y_label: 'Values', queries: [ title: 'title', weight: 1, y_label: 'Values', queries: [
{ query_range: 'query_range_a', result: query_range_result }, { query_range: 'query_range_a', result: query_range_result },
{ query_range: 'query_range_b', label: 'label', unit: 'unit', result: query_range_result } { query_range: 'query_range_b', label: 'label', unit: 'unit', result: query_range_result }
] ]
...@@ -43,6 +43,7 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -43,6 +43,7 @@ RSpec.shared_examples 'additional metrics query' do
} }
] ]
expect(query_result).to match_schema('prometheus/additional_metrics_query_result')
expect(query_result).to eq(expected) expect(query_result).to eq(expected)
end end
end end
...@@ -66,53 +67,16 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -66,53 +67,16 @@ RSpec.shared_examples 'additional metrics query' do
end end
it 'return group data both queries' do it 'return group data both queries' do
expected = [ queries_with_result_a = { queries: [{ query_range: 'query_range_a', result: query_range_result }] }
{ queries_with_result_b = { queries: [{ query_range: 'query_range_b', result: query_range_result }] }
group: 'group_a',
priority: 1,
metrics: [
{
title: 'title',
weight: nil,
y_label: 'Values',
queries: [
{
query_range: 'query_range_a',
result: [
{
metric: {},
values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']]
}
]
}
]
}
]
},
{
group: 'group_b',
priority: 1,
metrics: [
{
title: 'title_b',
weight: nil,
y_label: 'Values',
queries: [
{
query_range: 'query_range_b', result: [
{
metric: {},
values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']]
}
]
}
]
}
]
}
]
expect(query_result).to eq(expected) expect(query_result).to match_schema('prometheus/additional_metrics_query_result')
expect(query_result.count).to eq(2)
expect(query_result).to all(satisfy { |r| r[:metrics].count == 1 })
expect(query_result[0][:metrics].first).to include(queries_with_result_a)
expect(query_result[1][:metrics].first).to include(queries_with_result_b)
end end
end end
...@@ -123,27 +87,14 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -123,27 +87,14 @@ RSpec.shared_examples 'additional metrics query' do
end end
it 'return group data only for query with results' do it 'return group data only for query with results' do
expected = [ queries_with_result = { queries: [{ query_range: 'query_range_a', result: query_range_result }] }
{
group: 'group_a',
priority: 1,
metrics: [
{
title: 'title',
weight: nil,
y_label: 'Values',
queries: [
{
query_range: 'query_range_a',
result: query_range_result
}
]
}
]
}
]
expect(query_result).to eq(expected) expect(query_result).to match_schema('prometheus/additional_metrics_query_result')
expect(query_result.count).to eq(1)
expect(query_result).to all(satisfy { |r| r[:metrics].count == 1 })
expect(query_result.first[:metrics].first).to include(queries_with_result)
end end
end end
end end
......
...@@ -9,7 +9,7 @@ module Prometheus ...@@ -9,7 +9,7 @@ module Prometheus
end end
def simple_metric(title: 'title', required_metrics: [], queries: [simple_query]) def simple_metric(title: 'title', required_metrics: [], queries: [simple_query])
Gitlab::Prometheus::Metric.new(title, required_metrics, nil, nil, queries) Gitlab::Prometheus::Metric.new(title, required_metrics, 1, nil, queries)
end end
def simple_metrics(added_metric_name: 'metric_a') def simple_metrics(added_metric_name: 'metric_a')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment