diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-06 19:35:28 +0200 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-06 19:35:28 +0200 |
commit | 969b812433b6030b15b591ec5862daae1b707025 (patch) | |
tree | 478affbb74c02b08ff97cc9e94b00038c9b89a0c | |
parent | ccf89acc7145bb129f5666108854daa71a022827 (diff) | |
download | gitlab-ce-969b812433b6030b15b591ec5862daae1b707025.tar.gz |
Add schema matcher for non response objects + use schema to test additional metrics compliance
4 files changed, 88 insertions, 71 deletions
diff --git a/spec/fixtures/api/schemas/prometheus/additional_metrics_query_result.json b/spec/fixtures/api/schemas/prometheus/additional_metrics_query_result.json new file mode 100644 index 00000000000..47b5d283b8c --- /dev/null +++ b/spec/fixtures/api/schemas/prometheus/additional_metrics_query_result.json @@ -0,0 +1,58 @@ +{ + "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 diff --git a/spec/support/api/schema_matcher.rb b/spec/support/api/schema_matcher.rb index e42d727672b..dff0dfba675 100644 --- a/spec/support/api/schema_matcher.rb +++ b/spec/support/api/schema_matcher.rb @@ -1,8 +1,16 @@ +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| match do |response| - schema_directory = "#{Dir.pwd}/spec/fixtures/api/schemas" - schema_path = "#{schema_directory}/#{schema}.json" + JSON::Validator.validate!(schema_path(schema), response.body, options) + 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 diff --git a/spec/support/prometheus/additional_metrics_shared_examples.rb b/spec/support/prometheus/additional_metrics_shared_examples.rb index 0581eab95a0..016e16fc8d4 100644 --- a/spec/support/prometheus/additional_metrics_shared_examples.rb +++ b/spec/support/prometheus/additional_metrics_shared_examples.rb @@ -34,7 +34,7 @@ RSpec.shared_examples 'additional metrics query' do priority: 1, 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_b', label: 'label', unit: 'unit', result: query_range_result } ] @@ -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) end end @@ -66,53 +67,16 @@ RSpec.shared_examples 'additional metrics query' do end it 'return group data both queries' do - expected = [ - { - 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']] - } - ] - } - ] - } - ] - } - ] + 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 }] } - 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 @@ -123,27 +87,14 @@ RSpec.shared_examples 'additional metrics query' do end it 'return group data only for query with results' do - expected = [ - { - group: 'group_a', - priority: 1, - metrics: [ - { - title: 'title', - weight: nil, - y_label: 'Values', - queries: [ - { - query_range: 'query_range_a', - result: query_range_result - } - ] - } - ] - } - ] + queries_with_result = { 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 diff --git a/spec/support/prometheus/metric_builders.rb b/spec/support/prometheus/metric_builders.rb index cc733bfe1b4..18378ec0145 100644 --- a/spec/support/prometheus/metric_builders.rb +++ b/spec/support/prometheus/metric_builders.rb @@ -9,7 +9,7 @@ module Prometheus end 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 def simple_metrics(added_metric_name: 'metric_a') |