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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::DeploymentsController do
include ApiHelpers
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
let(:environment) { create(:environment, name: 'production', project: project) }
before do
project.add_maintainer(user)
sign_in(user)
end
describe 'GET #index' do
it 'returns list of deployments from last 8 hours' do
create(:deployment, :success, environment: environment, created_at: 9.hours.ago)
create(:deployment, :success, environment: environment, created_at: 7.hours.ago)
create(:deployment, :success, environment: environment)
get :index, params: deployment_params(after: 8.hours.ago)
expect(response).to be_ok
expect(json_response['deployments'].count).to eq(2)
end
it 'returns a list with deployments information' do
create(:deployment, :success, environment: environment)
get :index, params: deployment_params
expect(response).to be_ok
expect(response).to match_response_schema('deployments')
end
context 'anonymous user' do
let(:anonymous_user) { create(:user) }
before do
sign_in(anonymous_user)
end
context 'project and metrics dashboard are public' do
before do
project.update!(
visibility_level: Gitlab::VisibilityLevel::PUBLIC,
project_feature_attributes: {
metrics_dashboard_access_level: Gitlab::VisibilityLevel::PUBLIC
}
)
end
it 'returns a list with deployments information' do
create(:deployment, :success, environment: environment)
get :index, params: deployment_params
expect(response).to be_ok
end
end
context 'project and metrics dashboard are private' do
before do
project.update!(
visibility_level: Gitlab::VisibilityLevel::PRIVATE,
project_feature_attributes: {
metrics_dashboard_access_level: Gitlab::VisibilityLevel::PRIVATE
}
)
end
it 'responds with not found' do
create(:deployment, :success, environment: environment)
get :index, params: deployment_params
expect(response).to be_not_found
end
end
end
end
describe 'GET #metrics' do
let(:deployment) { create(:deployment, :success, project: project, environment: environment) }
context 'when metrics are disabled' do
it 'responds with not found' do
get :metrics, params: deployment_params(id: deployment.to_param)
expect(response).to be_not_found
end
end
context 'when metrics are enabled' do
context 'when environment has no metrics' do
before do
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
expect(deployment_metrics).to receive(:metrics).and_return(nil)
end
end
it 'returns an empty 204 response' do
get :metrics, params: deployment_params(id: deployment.to_param)
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to eq('')
end
end
context 'when environment has some metrics' do
let(:empty_metrics) do
{
success: true,
metrics: {},
last_update: 42
}
end
it 'returns a metrics JSON document' do
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
expect(deployment_metrics).to receive(:metrics).and_return(empty_metrics)
end
get :metrics, params: deployment_params(id: deployment.to_param)
expect(response).to be_ok
expect(json_response['success']).to be(true)
expect(json_response['metrics']).to eq({})
expect(json_response['last_update']).to eq(42)
end
it 'returns a 404 if the deployment failed' do
failed_deployment = create(
:deployment,
:failed,
project: project,
environment: environment
)
get :metrics, params: deployment_params(id: failed_deployment.to_param)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
describe 'GET #additional_metrics' do
let(:deployment) { create(:deployment, :success, project: project, environment: environment) }
context 'when metrics are disabled' do
it 'responds with not found' do
get :metrics, params: deployment_params(id: deployment.to_param)
expect(response).to be_not_found
end
end
context 'when metrics are enabled' do
context 'when environment has no metrics' do
before do
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
expect(deployment_metrics).to receive(:additional_metrics).and_return({})
end
end
it 'returns a empty response 204 response' do
get :additional_metrics, params: deployment_params(id: deployment.to_param, format: :json)
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to eq('')
end
end
context 'when environment has some metrics' do
let(:empty_metrics) do
{
success: true,
metrics: {},
last_update: 42
}
end
before do
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
allow(deployment_metrics).to receive(:has_metrics?).and_return(true)
expect(deployment_metrics).to receive(:additional_metrics).and_return(empty_metrics)
end
end
it 'returns a metrics JSON document' do
get :additional_metrics, params: deployment_params(id: deployment.to_param, format: :json)
expect(response).to be_ok
expect(json_response['success']).to be(true)
expect(json_response['metrics']).to eq({})
expect(json_response['last_update']).to eq(42)
end
end
end
end
def deployment_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace,
project_id: project,
environment_id: environment.id)
end
end
|