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
|
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::BackgroundMigration::AddGitlabInstanceAdministrationProject, :migration, schema: 20190725080128 do
let(:application_settings) { table(:application_settings) }
let(:users) { table(:users) }
let(:projects) { table(:projects) }
let(:namespaces) { table(:namespaces) }
let(:members) { table(:members) }
let(:service_class) do
Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService
end
let(:prometheus_settings) do
{
enable: true,
listen_address: 'localhost:9090'
}
end
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
stub_config(prometheus: prometheus_settings)
end
describe 'perform' do
context 'without application_settings' do
it 'does not fail' do
subject.perform
expect(Project.count).to eq(0)
end
end
context 'without admin users' do
let!(:application_setting) { application_settings.create! }
it 'does not fail' do
subject.perform
expect(Project.count).to eq(0)
end
end
context 'with admin users' do
let(:project) { Project.last }
let(:group) { Group.last }
let!(:application_setting) { application_settings.create! }
let!(:user) { users.create!(admin: true, email: 'admin1@example.com', projects_limit: 10, state: :active) }
before do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
end
shared_examples 'has prometheus service' do |listen_address|
it do
subject.perform
prometheus = project.prometheus_service
expect(prometheus).to be_persisted
expect(prometheus).not_to eq(nil)
expect(prometheus.api_url).to eq(listen_address)
expect(prometheus.active).to eq(true)
expect(prometheus.manual_configuration).to eq(true)
end
end
it_behaves_like 'has prometheus service', 'http://localhost:9090'
it 'creates GitLab Instance Administrator group' do
subject.perform
expect(group).to be_persisted
expect(group.name).to eq('GitLab Instance Administrators')
expect(group.path).to start_with('gitlab-instance-administrators')
expect(group.path.split('-').last.length).to eq(8)
expect(group.visibility_level).to eq(service_class::VISIBILITY_LEVEL)
end
it 'creates project with internal visibility' do
subject.perform
expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
expect(project).to be_persisted
end
it 'creates project with correct name and description' do
subject.perform
path = 'administration/monitoring/gitlab_instance_administration_project/index'
docs_path = Rails.application.routes.url_helpers.help_page_path(path)
expect(project.name).to eq(service_class::PROJECT_NAME)
expect(project.description).to eq(
'This project is automatically generated and will be used to help monitor this GitLab instance. ' \
"[More information](#{docs_path})"
)
expect(File).to exist("doc/#{path}.md")
end
it 'adds all admins as maintainers' do
admin1 = users.create!(admin: true, email: 'admin2@example.com', projects_limit: 10, state: :active)
admin2 = users.create!(admin: true, email: 'admin3@example.com', projects_limit: 10, state: :active)
users.create!(email: 'nonadmin1@example.com', projects_limit: 10, state: :active)
subject.perform
expect(project.owner).to eq(group)
expect(group.members.collect(&:user).collect(&:id)).to contain_exactly(user.id, admin1.id, admin2.id)
expect(group.members.collect(&:access_level)).to contain_exactly(
Gitlab::Access::OWNER,
Gitlab::Access::MAINTAINER,
Gitlab::Access::MAINTAINER
)
end
it 'saves the project id' do
subject.perform
application_setting.reload
expect(application_setting.instance_administration_project_id).to eq(project.id)
end
it 'does not fail when a project already exists' do
group = namespaces.create!(
path: 'gitlab-instance-administrators',
name: 'GitLab Instance Administrators',
type: 'Group'
)
project = projects.create!(
namespace_id: group.id,
name: 'GitLab Instance Administration'
)
admin1 = users.create!(admin: true, email: 'admin4@example.com', projects_limit: 10, state: :active)
admin2 = users.create!(admin: true, email: 'admin5@example.com', projects_limit: 10, state: :active)
members.create!(
user_id: admin1.id,
source_id: group.id,
source_type: 'Namespace',
type: 'GroupMember',
access_level: GroupMember::MAINTAINER,
notification_level: NotificationSetting.levels[:global]
)
members.create!(
user_id: admin2.id,
source_id: group.id,
source_type: 'Namespace',
type: 'GroupMember',
access_level: GroupMember::MAINTAINER,
notification_level: NotificationSetting.levels[:global]
)
stub_application_setting(instance_administration_project: project)
subject.perform
expect(Project.last.id).to eq(project.id)
expect(Group.last.id).to eq(group.id)
end
context 'when local requests from hooks and services are not allowed' do
before do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
end
it_behaves_like 'has prometheus service', 'http://localhost:9090'
it 'does not overwrite the existing whitelist' do
application_setting.update!(outbound_local_requests_whitelist: ['example.com'])
subject.perform
application_setting.reload
expect(application_setting.outbound_local_requests_whitelist).to contain_exactly(
'example.com', 'localhost'
)
end
end
context 'with non default prometheus address' do
let(:prometheus_settings) do
{
enable: true,
listen_address: 'https://localhost:9090'
}
end
it_behaves_like 'has prometheus service', 'https://localhost:9090'
end
context 'when prometheus setting is not present in gitlab.yml' do
before do
allow(Gitlab.config).to receive(:prometheus).and_raise(Settingslogic::MissingSetting)
end
it 'does not fail' do
subject.perform
expect(project.prometheus_service).to be_nil
end
end
context 'when prometheus setting is disabled in gitlab.yml' do
let(:prometheus_settings) do
{
enable: false,
listen_address: 'localhost:9090'
}
end
it 'does not configure prometheus' do
subject.perform
expect(project.prometheus_service).to be_nil
end
end
context 'when prometheus listen address is blank in gitlab.yml' do
let(:prometheus_settings) { { enable: true, listen_address: '' } }
it 'does not configure prometheus' do
subject.perform
expect(project.prometheus_service).to be_nil
end
end
end
end
end
|