Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
bcd4674e
Commit
bcd4674e
authored
Aug 13, 2020
by
Ryan Cobb
Committed by
Mayra Cabrera
Aug 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update metric dashboard validator
Update metric dashboard validator to use dashboard path
parent
4f6d51a3
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
202 additions
and
55 deletions
+202
-55
lib/gitlab/metrics/dashboard/validator.rb
lib/gitlab/metrics/dashboard/validator.rb
+6
-6
lib/gitlab/metrics/dashboard/validator/client.rb
lib/gitlab/metrics/dashboard/validator/client.rb
+10
-3
lib/gitlab/metrics/dashboard/validator/post_schema_validator.rb
...tlab/metrics/dashboard/validator/post_schema_validator.rb
+28
-7
locale/gitlab.pot
locale/gitlab.pot
+3
-0
spec/lib/gitlab/metrics/dashboard/validator/client_spec.rb
spec/lib/gitlab/metrics/dashboard/validator/client_spec.rb
+1
-1
spec/lib/gitlab/metrics/dashboard/validator/post_schema_validator_spec.rb
...metrics/dashboard/validator/post_schema_validator_spec.rb
+65
-7
spec/lib/gitlab/metrics/dashboard/validator_spec.rb
spec/lib/gitlab/metrics/dashboard/validator_spec.rb
+89
-31
No files found.
lib/gitlab/metrics/dashboard/validator.rb
View file @
bcd4674e
...
@@ -7,20 +7,20 @@ module Gitlab
...
@@ -7,20 +7,20 @@ module Gitlab
DASHBOARD_SCHEMA_PATH
=
'lib/gitlab/metrics/dashboard/validator/schemas/dashboard.json'
.
freeze
DASHBOARD_SCHEMA_PATH
=
'lib/gitlab/metrics/dashboard/validator/schemas/dashboard.json'
.
freeze
class
<<
self
class
<<
self
def
validate
(
content
,
schema_path
=
DASHBOARD_SCHEMA_PATH
,
project:
nil
)
def
validate
(
content
,
schema_path
=
DASHBOARD_SCHEMA_PATH
,
dashboard_path:
nil
,
project:
nil
)
errors
=
_validate
(
content
,
schema_path
,
project:
project
)
errors
=
_validate
(
content
,
schema_path
,
dashboard_path:
dashboard_path
,
project:
project
)
errors
.
empty?
errors
.
empty?
end
end
def
validate!
(
content
,
schema_path
=
DASHBOARD_SCHEMA_PATH
,
project:
nil
)
def
validate!
(
content
,
schema_path
=
DASHBOARD_SCHEMA_PATH
,
dashboard_path:
nil
,
project:
nil
)
errors
=
_validate
(
content
,
schema_path
,
project:
project
)
errors
=
_validate
(
content
,
schema_path
,
dashboard_path:
dashboard_path
,
project:
project
)
errors
.
empty?
||
raise
(
errors
.
first
)
errors
.
empty?
||
raise
(
errors
.
first
)
end
end
private
private
def
_validate
(
content
,
schema_path
,
project
)
def
_validate
(
content
,
schema_path
,
dashboard_path:
nil
,
project:
nil
)
client
=
Client
.
new
(
content
,
schema
_path
,
project:
project
)
client
=
Validator
::
Client
.
new
(
content
,
schema_path
,
dashboard_path:
dashboard
_path
,
project:
project
)
client
.
execute
client
.
execute
end
end
end
end
...
...
lib/gitlab/metrics/dashboard/validator/client.rb
View file @
bcd4674e
...
@@ -8,9 +8,12 @@ module Gitlab
...
@@ -8,9 +8,12 @@ module Gitlab
# @param content [Hash] Representing a raw, unprocessed
# @param content [Hash] Representing a raw, unprocessed
# dashboard object
# dashboard object
# @param schema_path [String] Representing path to dashboard schema file
# @param schema_path [String] Representing path to dashboard schema file
def
initialize
(
content
,
schema_path
,
project:
nil
)
# @param dashboard_path[String] Representing path to dashboard content file
# @param project [Project] Project to validate dashboard against
def
initialize
(
content
,
schema_path
,
dashboard_path:
nil
,
project:
nil
)
@content
=
content
@content
=
content
@schema_path
=
schema_path
@schema_path
=
schema_path
@dashboard_path
=
dashboard_path
@project
=
project
@project
=
project
end
end
...
@@ -23,14 +26,18 @@ module Gitlab
...
@@ -23,14 +26,18 @@ module Gitlab
private
private
attr_reader
:content
,
:schema_path
,
:project
attr_reader
:content
,
:schema_path
,
:project
,
:dashboard_path
def
custom_formats
def
custom_formats
@custom_formats
||=
CustomFormats
.
new
@custom_formats
||=
CustomFormats
.
new
end
end
def
post_schema_validator
def
post_schema_validator
@post_schema_validator
||=
PostSchemaValidator
.
new
(
project:
project
,
metric_ids:
custom_formats
.
metric_ids_cache
)
PostSchemaValidator
.
new
(
project:
project
,
metric_ids:
custom_formats
.
metric_ids_cache
,
dashboard_path:
dashboard_path
)
end
end
def
schemer
def
schemer
...
...
lib/gitlab/metrics/dashboard/validator/post_schema_validator.rb
View file @
bcd4674e
...
@@ -5,25 +5,46 @@ module Gitlab
...
@@ -5,25 +5,46 @@ module Gitlab
module
Dashboard
module
Dashboard
module
Validator
module
Validator
class
PostSchemaValidator
class
PostSchemaValidator
def
initialize
(
project:
nil
,
metric_ids:
[])
def
initialize
(
metric_ids
:,
project:
nil
,
dashboard_path:
nil
)
@project
=
project
@metric_ids
=
metric_ids
@metric_ids
=
metric_ids
@project
=
project
@dashboard_path
=
dashboard_path
end
end
def
validate
def
validate
[
uniq_metric_ids_across_project
].
compact
errors
=
[]
errors
<<
uniq_metric_ids
errors
.
compact
end
end
private
private
attr_reader
:project
,
:metric_ids
attr_reader
:project
,
:metric_ids
,
:dashboard_path
def
uniq_metric_ids
return
Validator
::
Errors
::
DuplicateMetricIds
.
new
if
metric_ids
.
uniq!
uniq_metric_ids_across_project
if
project
.
present?
||
dashboard_path
.
present?
end
# rubocop: disable CodeReuse/ActiveRecord
def
uniq_metric_ids_across_project
def
uniq_metric_ids_across_project
# TODO: modify this method to check metric identifier uniqueness across project once we start
return
ArgumentError
.
new
(
_
(
'Both project and dashboard_path are required'
))
unless
# recording dashboard_path https://gitlab.com/gitlab-org/gitlab/-/merge_requests/38237
dashboard_path
.
present?
&&
project
.
present?
# If PrometheusMetric identifier is not unique across project and dashboard_path,
# we need to error because we don't know if the user is trying to create a new metric
# or update an existing one.
identifier_on_other_dashboard
=
PrometheusMetric
.
where
(
project:
project
,
identifier:
metric_ids
).
where
.
not
(
dashboard_path:
dashboard_path
).
exists?
Validator
::
Errors
::
DuplicateMetricIds
.
new
if
metric_ids
.
uniq!
Validator
::
Errors
::
DuplicateMetricIds
.
new
if
identifier_on_other_dashboard
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
end
end
end
...
...
locale/gitlab.pot
View file @
bcd4674e
...
@@ -3932,6 +3932,9 @@ msgstr ""
...
@@ -3932,6 +3932,9 @@ msgstr ""
msgid "Boards|View scope"
msgid "Boards|View scope"
msgstr ""
msgstr ""
msgid "Both project and dashboard_path are required"
msgstr ""
msgid "Branch"
msgid "Branch"
msgstr ""
msgstr ""
...
...
spec/lib/gitlab/metrics/dashboard/validator/client_spec.rb
View file @
bcd4674e
...
@@ -22,7 +22,7 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Client do
...
@@ -22,7 +22,7 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Client do
let
(
:dashboard
)
{
load_dashboard_yaml
(
fixture_file
(
'lib/gitlab/metrics/dashboard/invalid_dashboard.yml'
))
}
let
(
:dashboard
)
{
load_dashboard_yaml
(
fixture_file
(
'lib/gitlab/metrics/dashboard/invalid_dashboard.yml'
))
}
it
'returns array of error objects'
do
it
'returns array of error objects'
do
expect
(
subject
.
execute
).
to
all
(
be_a
(
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
Errors
::
SchemaValidationError
)
)
expect
(
subject
.
execute
).
to
include
(
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
Errors
::
SchemaValidationError
)
end
end
end
end
end
end
...
...
spec/lib/gitlab/metrics/dashboard/validator/post_schema_validator_spec.rb
View file @
bcd4674e
...
@@ -4,17 +4,75 @@ require 'spec_helper'
...
@@ -4,17 +4,75 @@ require 'spec_helper'
RSpec
.
describe
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
PostSchemaValidator
do
RSpec
.
describe
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
PostSchemaValidator
do
describe
'#validate'
do
describe
'#validate'
do
context
'unique metric ids'
do
context
'with no project and dashboard_path provided'
do
it
'returns blank array'
do
context
'unique local metric_ids'
do
it
'returns empty array'
do
expect
(
described_class
.
new
(
metric_ids:
[
1
,
2
,
3
]).
validate
).
to
eq
([])
expect
(
described_class
.
new
(
metric_ids:
[
1
,
2
,
3
]).
validate
).
to
eq
([])
end
end
end
end
context
'duplicate metric
ids'
do
context
'duplicate local metrics_
ids'
do
it
'raise
s error'
do
it
'return
s error'
do
expect
(
described_class
.
new
(
metric_ids:
[
1
,
1
]).
validate
)
expect
(
described_class
.
new
(
metric_ids:
[
1
,
1
]).
validate
)
.
to
eq
([
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
Errors
::
DuplicateMetricIds
])
.
to
eq
([
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
Errors
::
DuplicateMetricIds
])
end
end
end
end
end
end
context
'with project and dashboard_path'
do
let
(
:project
)
{
create
(
:project
)
}
subject
do
described_class
.
new
(
project:
project
,
metric_ids:
[
'some_identifier'
],
dashboard_path:
'test/path.yml'
).
validate
end
context
'with unique metric identifiers'
do
before
do
create
(
:prometheus_metric
,
project:
project
,
identifier:
'some_other_identifier'
,
dashboard_path:
'test/path.yml'
)
end
it
'returns empty array'
do
expect
(
subject
).
to
eq
([])
end
end
context
'duplicate metric identifiers in database'
do
context
'with different dashboard_path'
do
before
do
create
(
:prometheus_metric
,
project:
project
,
identifier:
'some_identifier'
,
dashboard_path:
'some/other/path.yml'
)
end
it
'returns error'
do
expect
(
subject
).
to
include
(
Gitlab
::
Metrics
::
Dashboard
::
Validator
::
Errors
::
DuplicateMetricIds
)
end
end
context
'with same dashboard_path'
do
before
do
create
(
:prometheus_metric
,
project:
project
,
identifier:
'some_identifier'
,
dashboard_path:
'test/path.yml'
)
end
it
'returns empty array'
do
expect
(
subject
).
to
eq
([])
end
end
end
end
end
end
end
spec/lib/gitlab/metrics/dashboard/validator_spec.rb
View file @
bcd4674e
...
@@ -9,27 +9,57 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do
...
@@ -9,27 +9,57 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do
let_it_be
(
:invalid_dashboard
)
{
load_dashboard_yaml
(
fixture_file
(
'lib/gitlab/metrics/dashboard/invalid_dashboard.yml'
))
}
let_it_be
(
:invalid_dashboard
)
{
load_dashboard_yaml
(
fixture_file
(
'lib/gitlab/metrics/dashboard/invalid_dashboard.yml'
))
}
let_it_be
(
:duplicate_id_dashboard
)
{
load_dashboard_yaml
(
fixture_file
(
'lib/gitlab/metrics/dashboard/duplicate_id_dashboard.yml'
))
}
let_it_be
(
:duplicate_id_dashboard
)
{
load_dashboard_yaml
(
fixture_file
(
'lib/gitlab/metrics/dashboard/duplicate_id_dashboard.yml'
))
}
let_it_be
(
:project
)
{
create
(
:project
)
}
describe
'#validate'
do
describe
'#validate'
do
context
'valid dashboard'
do
context
'valid dashboard
schema
'
do
it
'returns true'
do
it
'returns true'
do
expect
(
described_class
.
validate
(
valid_dashboard
)).
to
be
true
expect
(
described_class
.
validate
(
valid_dashboard
)).
to
be
true
end
end
end
context
'invalid dashboard'
do
context
'with duplicate metric_ids'
do
context
'invalid schema'
do
it
'returns false'
do
it
'returns false'
do
expect
(
described_class
.
validate
(
inval
id_dashboard
)).
to
be
false
expect
(
described_class
.
validate
(
duplicate_
id_dashboard
)).
to
be
false
end
end
end
end
context
'duplicate metric ids'
do
context
'with dashboard_path and project'
do
context
'with no project given'
do
subject
{
described_class
.
validate
(
valid_dashboard
,
dashboard_path:
'test/path.yml'
,
project:
project
)
}
it
'checks against given dashboard and returns false'
do
expect
(
described_class
.
validate
(
duplicate_id_dashboard
)).
to
be
false
context
'with no conflicting metric identifiers in db'
do
it
{
is_expected
.
to
be
true
}
end
context
'with metric identifier present in current dashboard'
do
before
do
create
(
:prometheus_metric
,
identifier:
'metric_a1'
,
dashboard_path:
'test/path.yml'
,
project:
project
)
end
it
{
is_expected
.
to
be
true
}
end
context
'with metric identifier present in another dashboard'
do
before
do
create
(
:prometheus_metric
,
identifier:
'metric_a1'
,
dashboard_path:
'some/other/dashboard/path.yml'
,
project:
project
)
end
it
{
is_expected
.
to
be
false
}
end
end
end
end
end
end
context
'invalid dashboard schema'
do
it
'returns false'
do
expect
(
described_class
.
validate
(
invalid_dashboard
)).
to
be
false
end
end
end
end
end
...
@@ -43,16 +73,53 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do
...
@@ -43,16 +73,53 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do
end
end
end
end
context
'valid dashboard'
do
context
'valid dashboard
schema
'
do
it
'returns true'
do
it
'returns true'
do
expect
(
described_class
.
validate!
(
valid_dashboard
)).
to
be
true
expect
(
described_class
.
validate!
(
valid_dashboard
)).
to
be
true
end
end
context
'with duplicate metric_ids'
do
subject
{
described_class
.
validate!
(
duplicate_id_dashboard
)
}
it_behaves_like
'validation failed'
,
'metric_id must be unique across a project'
end
end
context
'invalid dashboard'
do
context
'with dashboard_path and project'
do
subject
{
described_class
.
validate!
(
valid_dashboard
,
dashboard_path:
'test/path.yml'
,
project:
project
)
}
context
'with no conflicting metric identifiers in db'
do
it
{
is_expected
.
to
be
true
}
end
context
'with metric identifier present in current dashboard'
do
before
do
create
(
:prometheus_metric
,
identifier:
'metric_a1'
,
dashboard_path:
'test/path.yml'
,
project:
project
)
end
it
{
is_expected
.
to
be
true
}
end
context
'with metric identifier present in another dashboard'
do
before
do
create
(
:prometheus_metric
,
identifier:
'metric_a1'
,
dashboard_path:
'some/other/dashboard/path.yml'
,
project:
project
)
end
it_behaves_like
'validation failed'
,
'metric_id must be unique across a project'
end
end
end
context
'invalid dashboard schema'
do
subject
{
described_class
.
validate!
(
invalid_dashboard
)
}
subject
{
described_class
.
validate!
(
invalid_dashboard
)
}
context
'invalid schema'
do
context
'wrong property type'
do
context
'wrong property type'
do
it_behaves_like
'validation failed'
,
"'this_should_be_a_int' at /panel_groups/0/panels/0/weight is not of type: number"
it_behaves_like
'validation failed'
,
"'this_should_be_a_int' at /panel_groups/0/panels/0/weight is not of type: number"
end
end
...
@@ -75,14 +142,5 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do
...
@@ -75,14 +142,5 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do
it_behaves_like
'validation failed'
,
'/panel_groups/0/panels/0 is missing required keys: metrics'
it_behaves_like
'validation failed'
,
'/panel_groups/0/panels/0 is missing required keys: metrics'
end
end
end
end
context
'duplicate metric ids'
do
context
'with no project given'
do
subject
{
described_class
.
validate!
(
duplicate_id_dashboard
)
}
it_behaves_like
'validation failed'
,
'metric_id must be unique across a project'
end
end
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment