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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
72003273
Commit
72003273
authored
Jan 04, 2018
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup PrometheusService tests
parent
e308bb0c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
49 deletions
+52
-49
app/models/project_services/prometheus_service.rb
app/models/project_services/prometheus_service.rb
+0
-4
lib/gitlab/prometheus/queries/environment_query.rb
lib/gitlab/prometheus/queries/environment_query.rb
+14
-12
spec/models/project_services/prometheus_service_spec.rb
spec/models/project_services/prometheus_service_spec.rb
+38
-33
No files found.
app/models/project_services/prometheus_service.rb
View file @
72003273
...
...
@@ -76,10 +76,6 @@ class PrometheusService < MonitoringService
{
success:
false
,
result:
err
}
end
def
with_reactive_cache
(
cl
,
*
args
)
yield
calculate_reactive_cache
(
cl
,
*
args
)
end
def
environment_metrics
(
environment
)
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
EnvironmentQuery
.
name
,
environment
.
id
,
&
method
(
:rename_data_to_metrics
))
end
...
...
lib/gitlab/prometheus/queries/environment_query.rb
View file @
72003273
...
...
@@ -2,20 +2,22 @@ module Gitlab
module
Prometheus
module
Queries
class
EnvironmentQuery
<
BaseQuery
def
query
environment_slug
=
environment
.
slug
timeframe_start
=
8
.
hours
.
ago
.
to_f
timeframe_end
=
Time
.
now
.
to_f
def
query
(
environment_id
)
::
Environment
.
find_by
(
id:
environment_id
).
try
do
|
environment
|
environment_slug
=
environment
.
slug
timeframe_start
=
8
.
hours
.
ago
.
to_f
timeframe_end
=
Time
.
now
.
to_f
memory_query
=
raw_memory_usage_query
(
environment_slug
)
cpu_query
=
raw_cpu_usage_query
(
environment_slug
)
memory_query
=
raw_memory_usage_query
(
environment_slug
)
cpu_query
=
raw_cpu_usage_query
(
environment_slug
)
{
memory_values:
client_query_range
(
memory_query
,
start:
timeframe_start
,
stop:
timeframe_end
),
memory_current:
client_query
(
memory_query
,
time:
timeframe_end
),
cpu_values:
client_query_range
(
cpu_query
,
start:
timeframe_start
,
stop:
timeframe_end
),
cpu_current:
client_query
(
cpu_query
,
time:
timeframe_end
)
}
{
memory_values:
client_query_range
(
memory_query
,
start:
timeframe_start
,
stop:
timeframe_end
),
memory_current:
client_query
(
memory_query
,
time:
timeframe_end
),
cpu_values:
client_query_range
(
cpu_query
,
start:
timeframe_start
,
stop:
timeframe_end
),
cpu_current:
client_query
(
cpu_query
,
time:
timeframe_end
)
}
end
end
end
end
...
...
spec/models/project_services/prometheus_service_spec.rb
View file @
72003273
...
...
@@ -8,24 +8,22 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let
(
:service
)
{
project
.
prometheus_service
}
let
(
:environment_query
)
{
Gitlab
::
Prometheus
::
Queries
::
EnvironmentQuery
}
subject
{
project
.
prometheus_service
}
describe
"Associations"
do
it
{
is_expected
.
to
belong_to
:project
}
end
describe
'Validations'
do
context
'when
service is active
'
do
context
'when
manual_configuration is enabled
'
do
before
do
subject
.
active
=
true
subject
.
manual_configuration
=
true
end
it
{
is_expected
.
to
validate_presence_of
(
:api_url
)
}
end
context
'when
service is inactive
'
do
context
'when
manual configuration is disabled
'
do
before
do
subject
.
active
=
false
subject
.
manual_configuration
=
false
end
it
{
is_expected
.
not_to
validate_presence_of
(
:api_url
)
}
...
...
@@ -33,12 +31,17 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
end
describe
'#test'
do
before
do
service
.
manual_configuration
=
true
end
let!
(
:req_stub
)
{
stub_prometheus_request
(
prometheus_query_url
(
'1'
),
body:
prometheus_value_body
(
'vector'
))
}
context
'success'
do
it
'reads the discovery endpoint'
do
expect
(
service
.
test
[
:result
]).
to
eq
(
'Checked API endpoint'
)
expect
(
service
.
test
[
:success
]).
to
be_truthy
expect
(
req_stub
).
to
have_been_requested
expect
(
req_stub
).
to
have_been_requested
.
twice
end
end
...
...
@@ -85,7 +88,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let
(
:fake_deployment_time
)
{
10
}
before
do
stub_reactive_cache
(
service
,
prometheus_data
,
deployment_query
,
deployment
.
id
)
stub_reactive_cache
(
service
,
prometheus_data
,
deployment_query
,
deployment
.
environment
.
id
,
deployment
.
id
)
end
it
'returns reactive data'
do
...
...
@@ -98,13 +101,17 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
describe
'#calculate_reactive_cache'
do
let
(
:environment
)
{
create
(
:environment
,
slug:
'env-slug'
)
}
around
do
|
example
|
Timecop
.
freeze
{
example
.
run
}
before
do
service
.
manual_configuration
=
true
service
.
active
=
true
end
subject
do
service
.
calculate_reactive_cache
(
environment_query
.
to_s
,
environment
.
id
)
service
.
calculate_reactive_cache
(
environment_query
.
name
,
environment
.
id
)
end
around
do
|
example
|
Timecop
.
freeze
{
example
.
run
}
end
context
'when service is inactive'
do
...
...
@@ -157,7 +164,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let
(
:proxy_client
)
{
double
(
'proxy_client'
)
}
before
do
s
ubject
.
manual_configuration
=
false
s
ervice
.
manual_configuration
=
false
end
context
'with cluster for all environments with prometheus installed'
do
...
...
@@ -165,10 +172,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
context
'without environment supplied'
do
it
'returns client handling all environments'
do
expect
(
s
ubject
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_all
).
and_return
(
proxy_client
).
twice
expect
(
s
ervice
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_all
).
and_return
(
proxy_client
).
twice
expect
(
s
ubject
.
client
).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ubject
.
client
.
rest_client
).
to
eq
(
proxy_client
)
expect
(
s
ervice
.
client
).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ervice
.
client
.
rest_client
).
to
eq
(
proxy_client
)
end
end
...
...
@@ -176,10 +183,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!
(
:environment
)
{
create
(
:environment
,
project:
project
,
name:
'dev'
)
}
it
'returns dev cluster client'
do
expect
(
s
ubject
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_dev
).
and_return
(
proxy_client
).
twice
expect
(
s
ervice
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_dev
).
and_return
(
proxy_client
).
twice
expect
(
s
ubject
.
client
(
environment
.
id
)).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ubject
.
client
(
environment
.
id
).
rest_client
).
to
eq
(
proxy_client
)
expect
(
s
ervice
.
client
(
environment
.
id
)).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ervice
.
client
(
environment
.
id
).
rest_client
).
to
eq
(
proxy_client
)
end
end
...
...
@@ -187,10 +194,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!
(
:environment
)
{
create
(
:environment
,
project:
project
,
name:
'prod'
)
}
it
'returns dev cluster client'
do
expect
(
s
ubject
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_all
).
and_return
(
proxy_client
).
twice
expect
(
s
ervice
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_all
).
and_return
(
proxy_client
).
twice
expect
(
s
ubject
.
client
(
environment
.
id
)).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ubject
.
client
(
environment
.
id
).
rest_client
).
to
eq
(
proxy_client
)
expect
(
s
ervice
.
client
(
environment
.
id
)).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ervice
.
client
(
environment
.
id
).
rest_client
).
to
eq
(
proxy_client
)
end
end
end
...
...
@@ -198,7 +205,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
context
'with cluster for all environments without prometheus installed'
do
context
'without environment supplied'
do
it
'raises PrometheusError because cluster was not found'
do
expect
{
s
ubject
.
client
}.
to
raise_error
(
Gitlab
::
PrometheusError
,
/couldn't find cluster with Prometheus installed/
)
expect
{
s
ervice
.
client
}.
to
raise_error
(
Gitlab
::
PrometheusError
,
/couldn't find cluster with Prometheus installed/
)
end
end
...
...
@@ -206,10 +213,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!
(
:environment
)
{
create
(
:environment
,
project:
project
,
name:
'dev'
)
}
it
'returns dev cluster client'
do
expect
(
s
ubject
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_dev
).
and_return
(
proxy_client
).
twice
expect
(
s
ervice
).
to
receive
(
:client_from_cluster
).
with
(
cluster_for_dev
).
and_return
(
proxy_client
).
twice
expect
(
s
ubject
.
client
(
environment
.
id
)).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ubject
.
client
(
environment
.
id
).
rest_client
).
to
eq
(
proxy_client
)
expect
(
s
ervice
.
client
(
environment
.
id
)).
to
be_instance_of
(
Gitlab
::
PrometheusClient
)
expect
(
s
ervice
.
client
(
environment
.
id
).
rest_client
).
to
eq
(
proxy_client
)
end
end
...
...
@@ -217,7 +224,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!
(
:environment
)
{
create
(
:environment
,
project:
project
,
name:
'prod'
)
}
it
'raises PrometheusError because cluster was not found'
do
expect
{
s
ubject
.
client
}.
to
raise_error
(
Gitlab
::
PrometheusError
,
/couldn't find cluster with Prometheus installed/
)
expect
{
s
ervice
.
client
}.
to
raise_error
(
Gitlab
::
PrometheusError
,
/couldn't find cluster with Prometheus installed/
)
end
end
end
...
...
@@ -225,14 +232,12 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
end
describe
'#prometheus_installed?'
do
subject
{
project
.
prometheus_service
}
context
'clusters with installed prometheus'
do
let!
(
:cluster
)
{
create
(
:cluster
,
projects:
[
project
])
}
let!
(
:prometheus
)
{
create
(
:clusters_applications_prometheus
,
:installed
,
cluster:
cluster
)
}
it
'returns true'
do
expect
(
s
ubject
.
prometheus_installed?
).
to
be
(
true
)
expect
(
s
ervice
.
prometheus_installed?
).
to
be
(
true
)
end
end
...
...
@@ -241,7 +246,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!
(
:prometheus
)
{
create
(
:clusters_applications_prometheus
,
cluster:
cluster
)
}
it
'returns false'
do
expect
(
s
ubject
.
prometheus_installed?
).
to
be
(
false
)
expect
(
s
ervice
.
prometheus_installed?
).
to
be
(
false
)
end
end
...
...
@@ -249,13 +254,13 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let
(
:cluster
)
{
create
(
:cluster
,
projects:
[
project
])
}
it
'returns false'
do
expect
(
s
ubject
.
prometheus_installed?
).
to
be
(
false
)
expect
(
s
ervice
.
prometheus_installed?
).
to
be
(
false
)
end
end
context
'no clusters'
do
it
'returns false'
do
expect
(
s
ubject
.
prometheus_installed?
).
to
be
(
false
)
expect
(
s
ervice
.
prometheus_installed?
).
to
be
(
false
)
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