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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
# frozen_string_literal: true
require 'spec_helper'
require 'mime/types'
RSpec.describe API::Ml::Mlflow, feature_category: :mlops do
include SessionHelpers
include ApiHelpers
include HttpBasicAuthHelpers
let_it_be(:project) { create(:project, :private) }
let_it_be(:developer) { create(:user).tap { |u| project.add_developer(u) } }
let_it_be(:another_project) { build(:project).tap { |p| p.add_developer(developer) } }
let_it_be(:experiment) do
create(:ml_experiments, :with_metadata, project: project)
end
let_it_be(:candidate) do
create(:ml_candidates,
:with_metrics_and_params, :with_metadata,
user: experiment.user, start_time: 1234, experiment: experiment, project: project)
end
let_it_be(:tokens) do
{
write: create(:personal_access_token, scopes: %w[read_api api], user: developer),
read: create(:personal_access_token, scopes: %w[read_api], user: developer),
no_access: create(:personal_access_token, scopes: %w[read_user], user: developer),
different_user: create(:personal_access_token, scopes: %w[read_api api], user: build(:user))
}
end
let(:current_user) { developer }
let(:ff_value) { true }
let(:access_token) { tokens[:write] }
let(:headers) do
{ 'Authorization' => "Bearer #{access_token.token}" }
end
let(:project_id) { project.id }
let(:default_params) { {} }
let(:params) { default_params }
let(:request) { get api(route), params: params, headers: headers }
before do
stub_feature_flags(ml_experiment_tracking: ff_value)
request
end
shared_examples 'Not Found' do |message|
it "is Not Found" do
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq(message) if message.present?
end
end
shared_examples 'Not Found - Resource Does Not Exist' do
it "is Resource Does Not Exist" do
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response).to include({ "error_code" => 'RESOURCE_DOES_NOT_EXIST' })
end
end
shared_examples 'Requires api scope' do
context 'when user has access but token has wrong scope' do
let(:access_token) { tokens[:read] }
it { expect(response).to have_gitlab_http_status(:forbidden) }
end
end
shared_examples 'Requires read_api scope' do
context 'when user has access but token has wrong scope' do
let(:access_token) { tokens[:no_access] }
it { expect(response).to have_gitlab_http_status(:forbidden) }
end
end
shared_examples 'Bad Request' do |error_code = nil|
it "is Bad Request" do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to include({ 'error_code' => error_code }) if error_code.present?
end
end
shared_examples 'shared error cases' do
context 'when not authenticated' do
let(:headers) { {} }
it "is Unauthorized" do
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when user does not have access' do
let(:access_token) { tokens[:different_user] }
it_behaves_like 'Not Found'
end
context 'when ff is disabled' do
let(:ff_value) { false }
it_behaves_like 'Not Found'
end
end
shared_examples 'run_id param error cases' do
context 'when run id is not passed' do
let(:params) { {} }
it_behaves_like 'Bad Request'
end
context 'when run_id is invalid' do
let(:params) { default_params.merge(run_id: non_existing_record_iid.to_s) }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
context 'when run_id is not in in the project' do
let(:project_id) { another_project.id }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
end
shared_examples 'Bad Request on missing required' do |keys|
keys.each do |key|
context "when \"#{key}\" is missing" do
let(:params) { default_params.tap { |p| p.delete(key) } }
it_behaves_like 'Bad Request'
end
end
end
describe 'GET /projects/:id/ml/mlflow/api/2.0/mlflow/experiments/get' do
let(:experiment_iid) { experiment.iid.to_s }
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/get?experiment_id=#{experiment_iid}" }
it 'returns the experiment', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('ml/get_experiment')
expect(json_response).to include({
'experiment' => {
'experiment_id' => experiment_iid,
'name' => experiment.name,
'lifecycle_stage' => 'active',
'artifact_location' => 'not_implemented',
'tags' => [
{
'key' => experiment.metadata[0].name,
'value' => experiment.metadata[0].value
},
{
'key' => experiment.metadata[1].name,
'value' => experiment.metadata[1].value
}
]
}
})
end
describe 'Error States' do
context 'when has access' do
context 'and experiment does not exist' do
let(:experiment_iid) { non_existing_record_iid.to_s }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
context 'and experiment_id is not passed' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/get" }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires read_api scope'
end
end
describe 'GET /projects/:id/ml/mlflow/api/2.0/mlflow/experiments/list' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/list" }
it 'returns the experiments' do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('ml/list_experiments')
expect(json_response).to include({
'experiments' => [
'experiment_id' => experiment.iid.to_s,
'name' => experiment.name,
'lifecycle_stage' => 'active',
'artifact_location' => 'not_implemented',
'tags' => [
{
'key' => experiment.metadata[0].name,
'value' => experiment.metadata[0].value
},
{
'key' => experiment.metadata[1].name,
'value' => experiment.metadata[1].value
}
]
]
})
end
context 'when there are no experiments' do
let(:project_id) { another_project.id }
it 'returns an empty list' do
expect(json_response).to include({ 'experiments' => [] })
end
end
describe 'Error States' do
it_behaves_like 'shared error cases'
it_behaves_like 'Requires read_api scope'
end
end
describe 'GET /projects/:id/ml/mlflow/api/2.0/mlflow/experiments/get-by-name' do
let(:experiment_name) { experiment.name }
let(:route) do
"/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/get-by-name?experiment_name=#{experiment_name}"
end
it 'returns the experiment', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('ml/get_experiment')
expect(json_response).to include({
'experiment' => {
'experiment_id' => experiment.iid.to_s,
'name' => experiment_name,
'lifecycle_stage' => 'active',
'artifact_location' => 'not_implemented',
'tags' => [
{
'key' => experiment.metadata[0].name,
'value' => experiment.metadata[0].value
},
{
'key' => experiment.metadata[1].name,
'value' => experiment.metadata[1].value
}
]
}
})
end
describe 'Error States' do
context 'when has access but experiment does not exist' do
let(:experiment_name) { "random_experiment" }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
context 'when has access but experiment_name is not passed' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/get-by-name" }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires read_api scope'
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/experiments/create' do
let(:route) do
"/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/create"
end
let(:params) { { name: 'new_experiment' } }
let(:request) { post api(route), params: params, headers: headers }
it 'creates the experiment', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to include('experiment_id')
end
describe 'Error States' do
context 'when experiment name is not passed' do
let(:params) { {} }
it_behaves_like 'Bad Request'
end
context 'when experiment name already exists' do
let(:existing_experiment) do
create(:ml_experiments, user: current_user, project: project)
end
let(:params) { { name: existing_experiment.name } }
it_behaves_like 'Bad Request', 'RESOURCE_ALREADY_EXISTS'
end
context 'when project does not exist' do
let(:route) { "/projects/#{non_existing_record_id}/ml/mlflow/api/2.0/mlflow/experiments/create" }
it_behaves_like 'Not Found', '404 Project Not Found'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/experiments/set-experiment-tag' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/experiments/set-experiment-tag" }
let(:default_params) { { experiment_id: experiment.iid.to_s, key: 'some_key', value: 'value' } }
let(:params) { default_params }
let(:request) { post api(route), params: params, headers: headers }
it 'logs the tag', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_empty
expect(experiment.reload.metadata.map(&:name)).to include('some_key')
end
describe 'Error Cases' do
context 'when tag was already set' do
let(:params) { default_params.merge(key: experiment.metadata[0].name) }
it_behaves_like 'Bad Request'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
it_behaves_like 'Bad Request on missing required', [:key, :value]
end
end
describe 'Runs' do
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/runs/create' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/create" }
let(:params) do
{
experiment_id: experiment.iid.to_s,
start_time: Time.now.to_i,
run_name: "A new Run",
tags: [
{ key: 'hello', value: 'world' }
]
}
end
let(:request) { post api(route), params: params, headers: headers }
it 'creates the run', :aggregate_failures do
expected_properties = {
'experiment_id' => params[:experiment_id],
'user_id' => current_user.id.to_s,
'run_name' => "A new Run",
'start_time' => params[:start_time],
'status' => 'RUNNING',
'lifecycle_stage' => 'active'
}
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('ml/run')
expect(json_response['run']).to include('info' => hash_including(**expected_properties),
'data' => {
'metrics' => [],
'params' => [],
'tags' => [{ 'key' => 'hello', 'value' => 'world' }]
})
end
describe 'Error States' do
context 'when experiment id is not passed' do
let(:params) { {} }
it_behaves_like 'Bad Request'
end
context 'when experiment id does not exist' do
let(:params) { { experiment_id: non_existing_record_iid.to_s } }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
context 'when experiment exists but is not part of the project' do
let(:project_id) { another_project.id }
it_behaves_like 'Not Found - Resource Does Not Exist'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
end
end
describe 'GET /projects/:id/ml/mlflow/api/2.0/mlflow/runs/get' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/get" }
let(:default_params) { { 'run_id' => candidate.eid } }
it 'gets the run', :aggregate_failures do
expected_properties = {
'experiment_id' => candidate.experiment.iid.to_s,
'user_id' => candidate.user.id.to_s,
'start_time' => candidate.start_time,
'artifact_uri' => "http://www.example.com/api/v4/projects/#{project_id}/packages/generic/ml_experiment_#{experiment.iid}/#{candidate.iid}/",
'status' => "RUNNING",
'lifecycle_stage' => "active"
}
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('ml/run')
expect(json_response['run']).to include(
'info' => hash_including(**expected_properties),
'data' => {
'metrics' => [
hash_including('key' => candidate.metrics[0].name),
hash_including('key' => candidate.metrics[1].name)
],
'params' => [
{ 'key' => candidate.params[0].name, 'value' => candidate.params[0].value },
{ 'key' => candidate.params[1].name, 'value' => candidate.params[1].value }
],
'tags' => [
{ 'key' => candidate.metadata[0].name, 'value' => candidate.metadata[0].value },
{ 'key' => candidate.metadata[1].name, 'value' => candidate.metadata[1].value }
]
})
end
describe 'Error States' do
it_behaves_like 'run_id param error cases'
it_behaves_like 'shared error cases'
it_behaves_like 'Requires read_api scope'
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/runs/update' do
let(:default_params) { { run_id: candidate.eid.to_s, status: 'FAILED', end_time: Time.now.to_i } }
let(:request) { post api(route), params: params, headers: headers }
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/update" }
it 'updates the run', :aggregate_failures do
expected_properties = {
'experiment_id' => candidate.experiment.iid.to_s,
'user_id' => candidate.user.id.to_s,
'start_time' => candidate.start_time,
'end_time' => params[:end_time],
'artifact_uri' => "http://www.example.com/api/v4/projects/#{project_id}/packages/generic/ml_experiment_#{experiment.iid}/#{candidate.iid}/",
'status' => 'FAILED',
'lifecycle_stage' => 'active'
}
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('ml/update_run')
expect(json_response).to include('run_info' => hash_including(**expected_properties))
end
describe 'Error States' do
context 'when status in invalid' do
let(:params) { default_params.merge(status: 'YOLO') }
it_behaves_like 'Bad Request'
end
context 'when end_time is invalid' do
let(:params) { default_params.merge(end_time: 's') }
it_behaves_like 'Bad Request'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
it_behaves_like 'run_id param error cases'
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/runs/log-metric' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/log-metric" }
let(:default_params) { { run_id: candidate.eid.to_s, key: 'some_key', value: 10.0, timestamp: Time.now.to_i } }
let(:request) { post api(route), params: params, headers: headers }
it 'logs the metric', :aggregate_failures do
candidate.metrics.reload
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_empty
expect(candidate.metrics.length).to eq(3)
end
describe 'Error Cases' do
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
it_behaves_like 'run_id param error cases'
it_behaves_like 'Bad Request on missing required', [:key, :value, :timestamp]
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/runs/log-parameter' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/log-parameter" }
let(:default_params) { { run_id: candidate.eid.to_s, key: 'some_key', value: 'value' } }
let(:request) { post api(route), params: params, headers: headers }
it 'logs the parameter', :aggregate_failures do
candidate.params.reload
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_empty
expect(candidate.params.length).to eq(3)
end
describe 'Error Cases' do
context 'when parameter was already logged' do
let(:params) { default_params.tap { |p| p[:key] = candidate.params[0].name } }
it_behaves_like 'Bad Request'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
it_behaves_like 'run_id param error cases'
it_behaves_like 'Bad Request on missing required', [:key, :value]
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/runs/set-tag' do
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/set-tag" }
let(:default_params) { { run_id: candidate.eid.to_s, key: 'some_key', value: 'value' } }
let(:request) { post api(route), params: params, headers: headers }
it 'logs the tag', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_empty
expect(candidate.reload.metadata.map(&:name)).to include('some_key')
end
describe 'Error Cases' do
context 'when tag was already logged' do
let(:params) { default_params.tap { |p| p[:key] = candidate.metadata[0].name } }
it_behaves_like 'Bad Request'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
it_behaves_like 'run_id param error cases'
it_behaves_like 'Bad Request on missing required', [:key, :value]
end
end
describe 'POST /projects/:id/ml/mlflow/api/2.0/mlflow/runs/log-batch' do
let(:candidate2) do
create(:ml_candidates, user: experiment.user, start_time: 1234, experiment: experiment, project: project)
end
let(:route) { "/projects/#{project_id}/ml/mlflow/api/2.0/mlflow/runs/log-batch" }
let(:default_params) do
{
run_id: candidate2.eid.to_s,
metrics: [
{ key: 'mae', value: 2.5, timestamp: 1552550804 },
{ key: 'rmse', value: 2.7, timestamp: 1552550804 }
],
params: [{ key: 'model_class', value: 'LogisticRegression' }],
tags: [{ key: 'tag1', value: 'tag.value.1' }]
}
end
let(:request) { post api(route), params: params, headers: headers }
it 'logs parameters and metrics', :aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_empty
expect(candidate2.params.size).to eq(1)
expect(candidate2.metadata.size).to eq(1)
expect(candidate2.metrics.size).to eq(2)
end
context 'when parameter was already logged' do
let(:params) do
default_params.tap { |p| p[:params] = [{ key: 'hello', value: 'a' }, { key: 'hello', value: 'b' }] }
end
it 'does not log', :aggregate_failures do
candidate.params.reload
expect(response).to have_gitlab_http_status(:ok)
expect(candidate2.params.size).to eq(1)
end
end
context 'when tag was already logged' do
let(:params) do
default_params.tap { |p| p[:tags] = [{ key: 'tag1', value: 'a' }, { key: 'tag1', value: 'b' }] }
end
it 'logs only 1', :aggregate_failures do
candidate.metadata.reload
expect(response).to have_gitlab_http_status(:ok)
expect(candidate2.metadata.size).to eq(1)
end
end
describe 'Error Cases' do
context 'when required metric key is missing' do
let(:params) { default_params.tap { |p| p[:metrics] = [p[:metrics][0].delete(:key)] } }
it_behaves_like 'Bad Request'
end
context 'when required param key is missing' do
let(:params) { default_params.tap { |p| p[:params] = [p[:params][0].delete(:key)] } }
it_behaves_like 'Bad Request'
end
it_behaves_like 'shared error cases'
it_behaves_like 'Requires api scope'
it_behaves_like 'run_id param error cases'
end
end
end
end
|