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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PerformanceMonitoring::PrometheusDashboard do
let(:json_content) do
{
"dashboard" => "Dashboard Title",
"templating" => {
"variables" => {
"variable1" => %w(value1 value2 value3)
}
},
"panel_groups" => [{
"group" => "Group Title",
"panels" => [{
"type" => "area-chart",
"title" => "Chart Title",
"y_label" => "Y-Axis",
"metrics" => [{
"id" => "metric_of_ages",
"unit" => "count",
"label" => "Metric of Ages",
"query_range" => "http_requests_total"
}]
}]
}]
}
end
describe '.from_json' do
subject { described_class.from_json(json_content) }
it 'creates a PrometheusDashboard object' do
expect(subject).to be_a PerformanceMonitoring::PrometheusDashboard
expect(subject.dashboard).to eq(json_content['dashboard'])
expect(subject.panel_groups).to all(be_a PerformanceMonitoring::PrometheusPanelGroup)
end
describe 'validations' do
shared_examples 'validation failed' do |errors_messages|
it 'raises error with corresponding messages', :aggregate_failures do
expect { subject }.to raise_error do |error|
expect(error).to be_kind_of(ActiveModel::ValidationError)
expect(error.model.errors.messages).to eq(errors_messages)
end
end
end
context 'dashboard content is missing' do
let(:json_content) { nil }
it_behaves_like 'validation failed', panel_groups: ["should be an array of panel_groups objects"], dashboard: ["can't be blank"]
end
context 'dashboard content is NOT a hash' do
let(:json_content) { YAML.safe_load("'test'") }
it_behaves_like 'validation failed', panel_groups: ["should be an array of panel_groups objects"], dashboard: ["can't be blank"]
end
context 'content is an array' do
let(:json_content) { [{ "dashboard" => "Dashboard Title" }] }
it_behaves_like 'validation failed', panel_groups: ["should be an array of panel_groups objects"], dashboard: ["can't be blank"]
end
context 'dashboard definition is missing panels_groups and dashboard keys' do
let(:json_content) do
{
"dashboard" => nil
}
end
it_behaves_like 'validation failed', panel_groups: ["should be an array of panel_groups objects"], dashboard: ["can't be blank"]
end
context 'group definition is missing panels and group keys' do
let(:json_content) do
{
"dashboard" => "Dashboard Title",
"templating" => {
"variables" => {
"variable1" => %w(value1 value2 value3)
}
},
"panel_groups" => [{ "group" => nil }]
}
end
it_behaves_like 'validation failed', panels: ["should be an array of panels objects"], group: ["can't be blank"]
end
context 'panel definition is missing metrics and title keys' do
let(:json_content) do
{
"dashboard" => "Dashboard Title",
"templating" => {
"variables" => {
"variable1" => %w(value1 value2 value3)
}
},
"panel_groups" => [{
"group" => "Group Title",
"panels" => [{
"type" => "area-chart",
"y_label" => "Y-Axis"
}]
}]
}
end
it_behaves_like 'validation failed', metrics: ["should be an array of metrics objects"], title: ["can't be blank"]
end
context 'metrics definition is missing unit, query and query_range keys' do
let(:json_content) do
{
"dashboard" => "Dashboard Title",
"templating" => {
"variables" => {
"variable1" => %w(value1 value2 value3)
}
},
"panel_groups" => [{
"group" => "Group Title",
"panels" => [{
"type" => "area-chart",
"title" => "Chart Title",
"y_label" => "Y-Axis",
"metrics" => [{
"id" => "metric_of_ages",
"label" => "Metric of Ages",
"query_range" => nil
}]
}]
}]
}
end
it_behaves_like 'validation failed', unit: ["can't be blank"], query_range: ["can't be blank"], query: ["can't be blank"]
end
# for each parent entry validation first is done to its children,
# whole execution is stopped on first encountered error
# which is the one that is reported
context 'multiple offences on different levels' do
let(:json_content) do
{
"dashboard" => nil,
"panel_groups" => [{
"group" => nil,
"panels" => [{
"type" => "area-chart",
"title" => nil,
"y_label" => "Y-Axis",
"metrics" => [{
"id" => "metric_of_ages",
"label" => "Metric of Ages",
"query_range" => 'query'
}, {
"id" => "metric_of_ages",
"unit" => "count",
"label" => "Metric of Ages",
"query_range" => nil
}]
}]
}, {
"group" => 'group',
"panels" => nil
}]
}
end
it_behaves_like 'validation failed', unit: ["can't be blank"]
end
end
end
describe '.find_for' do
let(:project) { build_stubbed(:project) }
let(:user) { build_stubbed(:user) }
let(:environment) { build_stubbed(:environment, project: project) }
let(:path) { ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH }
context 'dashboard has been found' do
it 'uses dashboard finder to find and load dashboard data and returns dashboard instance', :aggregate_failures do
expect(Gitlab::Metrics::Dashboard::Finder).to receive(:find).with(project, user, environment: environment, dashboard_path: path).and_return(status: :success, dashboard: json_content)
dashboard_instance = described_class.find_for(project: project, user: user, path: path, options: { environment: environment })
expect(dashboard_instance).to be_instance_of described_class
expect(dashboard_instance.environment).to eq environment
expect(dashboard_instance.path).to eq path
end
end
context 'dashboard has NOT been found' do
it 'returns nil' do
allow(Gitlab::Metrics::Dashboard::Finder).to receive(:find).and_return(http_status: :not_found)
dashboard_instance = described_class.find_for(project: project, user: user, path: path, options: { environment: environment })
expect(dashboard_instance).to be_nil
end
end
context 'dashboard has invalid schema', :aggregate_failures do
it 'still returns dashboard object' do
expect(Gitlab::Metrics::Dashboard::Finder).to receive(:find).and_return(http_status: :unprocessable_entity)
dashboard_instance = described_class.find_for(project: project, user: user, path: path, options: { environment: environment })
expect(dashboard_instance).to be_instance_of described_class
expect(dashboard_instance.environment).to eq environment
expect(dashboard_instance.path).to eq path
end
end
end
describe '#schema_validation_warnings' do
context 'when schema is valid' do
it 'returns nil' do
expect(described_class).to receive(:from_json)
expect(described_class.new.schema_validation_warnings).to be_nil
end
end
context 'when schema is invalid' do
it 'returns array with errors messages' do
instance = described_class.new
instance.errors.add(:test, 'test error')
expect(described_class).to receive(:from_json).and_raise(ActiveModel::ValidationError.new(instance))
expect(described_class.new.schema_validation_warnings).to eq ['test: test error']
end
end
end
describe '#to_yaml' do
subject { prometheus_dashboard.to_yaml }
let(:prometheus_dashboard) { described_class.from_json(json_content) }
let(:expected_yaml) do
"---\npanel_groups:\n- panels:\n - metrics:\n - id: metric_of_ages\n unit: count\n label: Metric of Ages\n query: \n query_range: http_requests_total\n type: area-chart\n title: Chart Title\n y_label: Y-Axis\n weight: \n group: Group Title\n priority: \ndashboard: Dashboard Title\n"
end
it { is_expected.to eq(expected_yaml) }
end
end
|