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
Boxiang Sun
gitlab-ce
Commits
1427ad4f
Commit
1427ad4f
authored
Apr 02, 2019
by
Peter Leitzen
Committed by
rpereira2
Apr 05, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Streamline controller specs
parent
e1a167ee
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
87 deletions
+76
-87
app/controllers/projects/environments/prometheus_api_controller.rb
...ollers/projects/environments/prometheus_api_controller.rb
+2
-4
spec/controllers/projects/environments/prometheus_api_controller_spec.rb
...s/projects/environments/prometheus_api_controller_spec.rb
+74
-83
No files found.
app/controllers/projects/environments/prometheus_api_controller.rb
View file @
1427ad4f
...
@@ -25,10 +25,8 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon
...
@@ -25,10 +25,8 @@ class Projects::Environments::PrometheusApiController < Projects::ApplicationCon
if
result
[
:status
]
==
:success
if
result
[
:status
]
==
:success
render
status:
result
[
:http_status
],
json:
result
[
:body
]
render
status:
result
[
:http_status
],
json:
result
[
:body
]
else
else
render
status:
result
[
:http_status
]
||
:bad_request
,
json:
{
render
status:
result
[
:http_status
]
||
:bad_request
,
status:
result
[
:status
],
json:
{
status:
result
[
:status
],
message:
result
[
:message
]
}
message:
result
[
:message
]
}
end
end
end
end
...
...
spec/controllers/projects/environments/prometheus_api_controller_spec.rb
View file @
1427ad4f
...
@@ -14,121 +14,112 @@ describe Projects::Environments::PrometheusApiController do
...
@@ -14,121 +14,112 @@ describe Projects::Environments::PrometheusApiController do
describe
'GET #proxy'
do
describe
'GET #proxy'
do
let
(
:prometheus_proxy_service
)
{
instance_double
(
Prometheus
::
ProxyService
)
}
let
(
:prometheus_proxy_service
)
{
instance_double
(
Prometheus
::
ProxyService
)
}
let
(
:prometheus_response
)
{
{
status: :success
,
body:
response_body
}
}
let
(
:json_response_body
)
{
JSON
.
parse
(
response_body
)
}
let
(
:response_body
)
do
context
'with valid requests'
do
"{
\"
status
\"
:
\"
success
\"
,
\"
data
\"
:{
\"
resultType
\"
:
\"
scalar
\"
,
\"
result
\"
:[1553864609.117,
\"
1
\"
]}}"
before
do
end
allow
(
Prometheus
::
ProxyService
).
to
receive
(
:new
)
.
with
(
environment
,
'GET'
,
'query'
,
anything
)
before
do
.
and_return
(
prometheus_proxy_service
)
allow
(
Prometheus
::
ProxyService
).
to
receive
(
:new
)
.
with
(
environment
,
'GET'
,
'query'
,
anything
)
.
and_return
(
prometheus_proxy_service
)
allow
(
prometheus_proxy_service
).
to
receive
(
:execute
)
allow
(
prometheus_proxy_service
).
to
receive
(
:execute
)
.
and_return
(
prometheus_response
)
.
and_return
(
service_result
)
end
end
it
'returns prometheus response'
do
context
'with success result'
do
get
:proxy
,
params:
environment_params
let
(
:service_result
)
{
{
status: :success
,
body:
prometheus_body
}
}
let
(
:prometheus_body
)
{
'{"status":"success"}'
}
let
(
:prometheus_json_body
)
{
JSON
.
parse
(
prometheus_body
)
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
it
'returns prometheus response'
do
expect
(
json_response
).
to
eq
(
json_response_body
)
get
:proxy
,
params:
environment_params
end
it
'filters params'
do
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
get
:proxy
,
params:
environment_params
({
extra_param:
'dangerous value'
})
expect
(
json_response
).
to
eq
(
prometheus_json_body
)
end
expect
(
Prometheus
::
ProxyService
).
to
have_received
(
:new
)
it
'filters params'
do
.
with
(
environment
,
'GET'
,
'query'
,
ActionController
::
Parameters
.
new
({
'query'
=>
'1'
}).
permit!
)
get
:proxy
,
params:
environment_params
({
extra_param:
'dangerous value'
})
end
context
'Prometheus::ProxyService returns nil'
do
expect
(
Prometheus
::
ProxyService
).
to
have_received
(
:new
)
before
do
.
with
(
environment
,
'GET'
,
'query'
,
ActionController
::
Parameters
.
new
({
'query'
=>
'1'
}).
permit!
)
allow
(
prometheus_proxy_service
).
to
receive
(
:execute
)
end
.
and_return
(
nil
)
end
end
it
'returns 202 accepted'
do
context
'with nil result'
do
get
:proxy
,
params:
environment_params
let
(
:service_result
)
{
nil
}
expect
(
json_response
[
'status'
]).
to
eq
(
'processing'
)
expect
(
json_response
[
'message'
]).
to
eq
(
'Not ready yet. Try again later.'
)
expect
(
response
).
to
have_gitlab_http_status
(
:accepted
)
end
end
context
'Prometheus::ProxyService returns status success
'
do
it
'returns 202 accepted
'
do
let
(
:service_response
)
{
{
http_status:
404
,
status: :success
,
body:
'{"body": "value"}'
}
}
get
:proxy
,
params:
environment_params
before
do
expect
(
json_response
[
'status'
]).
to
eq
(
'processing'
)
allow
(
prometheus_proxy_service
).
to
receive
(
:execute
)
expect
(
json_response
[
'message'
]).
to
eq
(
'Not ready yet. Try again later.'
)
.
and_return
(
service_response
)
expect
(
response
).
to
have_gitlab_http_status
(
:accepted
)
end
end
end
it
'returns body
'
do
context
'with 404 result
'
do
get
:proxy
,
params:
environment_params
let
(
:service_result
)
{
{
http_status:
404
,
status: :success
,
body:
'{"body": "value"}'
}
}
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
it
'returns body'
do
expect
(
json_response
[
'body'
]).
to
eq
(
'value'
)
get
:proxy
,
params:
environment_params
end
end
context
'Prometheus::ProxyService returns status error'
do
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
before
do
expect
(
json_response
[
'body'
]).
to
eq
(
'value'
)
allow
(
prometheus_proxy_service
).
to
receive
(
:execute
)
end
.
and_return
(
service_response
)
end
end
context
'with http_status'
do
context
'with error result'
do
let
(
:service_response
)
do
context
'with http_status'
do
{
http_status: :service_unavailable
,
status: :error
,
message:
'error message'
}
let
(
:service_result
)
do
end
{
http_status: :service_unavailable
,
status: :error
,
message:
'error message'
}
end
it
'sets the http response status code'
do
it
'sets the http response status code'
do
get
:proxy
,
params:
environment_params
get
:proxy
,
params:
environment_params
expect
(
response
).
to
have_gitlab_http_status
(
:service_unavailable
)
expect
(
response
).
to
have_gitlab_http_status
(
:service_unavailable
)
expect
(
json_response
[
'status'
]).
to
eq
(
'error'
)
expect
(
json_response
[
'status'
]).
to
eq
(
'error'
)
expect
(
json_response
[
'message'
]).
to
eq
(
'error message'
)
expect
(
json_response
[
'message'
]).
to
eq
(
'error message'
)
end
end
end
end
context
'without http_status'
do
context
'without http_status'
do
let
(
:service_response
)
{
{
status: :error
,
message:
'error message'
}
}
let
(
:service_result
)
{
{
status: :error
,
message:
'error message'
}
}
it
'returns message
'
do
it
'returns bad_request
'
do
get
:proxy
,
params:
environment_params
get
:proxy
,
params:
environment_params
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
expect
(
json_response
[
'status'
]).
to
eq
(
'error'
)
expect
(
json_response
[
'status'
]).
to
eq
(
'error'
)
expect
(
json_response
[
'message'
]).
to
eq
(
'error message'
)
expect
(
json_response
[
'message'
]).
to
eq
(
'error message'
)
end
end
end
end
end
end
end
context
'with anonymous user'
do
context
'with inappropriate requests'
do
before
do
context
'with anonymous user'
do
sign_out
(
user
)
before
do
end
sign_out
(
user
)
end
it
'redirects to signin page'
do
it
'redirects to signin page'
do
get
:proxy
,
params:
environment_params
get
:proxy
,
params:
environment_params
expect
(
response
).
to
redirect_to
(
new_user_session_path
)
expect
(
response
).
to
redirect_to
(
new_user_session_path
)
end
end
end
end
context
'without correct permissions'
do
context
'without correct permissions'
do
before
do
before
do
project
.
team
.
truncate
project
.
team
.
truncate
end
end
it
'returns 404'
do
it
'returns 404'
do
get
:proxy
,
params:
environment_params
get
:proxy
,
params:
environment_params
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
end
end
end
end
end
end
end
...
@@ -142,6 +133,6 @@ describe Projects::Environments::PrometheusApiController do
...
@@ -142,6 +133,6 @@ describe Projects::Environments::PrometheusApiController do
project_id:
project
,
project_id:
project
,
proxy_path:
'query'
,
proxy_path:
'query'
,
query:
'1'
query:
'1'
}.
merge
(
params
)
}.
reverse_
merge
(
params
)
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