-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_api_v1.py
More file actions
3207 lines (2952 loc) · 116 KB
/
Copy pathtest_api_v1.py
File metadata and controls
3207 lines (2952 loc) · 116 KB
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: GPL-3.0-or-later
import json
from unittest import mock
from botocore.response import StreamingBody
from io import StringIO
import pytest
from sqlalchemy.exc import DisconnectionError
from iib.web.api_v1 import _get_unique_bundles
from iib.web.models import (
Image,
RequestAdd,
RequestRm,
RequestCreateEmptyIndex,
RequestFbcOperations,
)
def test_get_build(app, auth_env, client, db):
# flask_login.current_user is used in RequestAdd.from_json, which requires a request context
with app.test_request_context(environ_base=auth_env):
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': ['quay.io/namespace/bundle:1.0-3'],
'from_index': 'quay.io/namespace/repo:latest',
}
request = RequestAdd.from_json(data)
request.binary_image_resolved = Image.get_or_create(
'quay.io/namespace/binary_image@sha256:abcdef'
)
request.from_index_resolved = Image.get_or_create(
'quay.io/namespace/from_index@sha256:defghi'
)
request.index_image = Image.get_or_create('quay.io/namespace/index@sha256:fghijk')
request.internal_index_image_copy = Image.get_or_create(
'quay.io/namespace/internal_index:tag'
)
request.internal_index_image_copy_resolved = Image.get_or_create(
'quay.io/namespace/internal_index@sha256:something'
)
request.add_architecture('amd64')
request.add_architecture('s390x')
request.add_state('complete', 'Completed successfully')
request.add_build_tag('build-tag-1')
db.session.add(request)
db.session.commit()
rv = client.get('/api/v1/builds/1').json
for state in rv['state_history']:
# Set this to a stable timestamp so the tests are dependent on it
state['updated'] = '2020-02-12T17:03:00Z'
rv['updated'] = '2020-02-12T17:03:00Z'
rv['logs']['expiration'] = '2020-02-15T17:03:00Z'
expected = {
'arches': ['amd64', 's390x'],
'batch': 1,
'batch_annotations': None,
'binary_image': 'quay.io/namespace/binary_image:latest',
'binary_image_resolved': 'quay.io/namespace/binary_image@sha256:abcdef',
'build_tags': ['build-tag-1'],
'bundle_mapping': {},
'bundles': ['quay.io/namespace/bundle:1.0-3'],
'check_related_images': None,
'deprecation_list': [],
'distribution_scope': None,
'from_index': 'quay.io/namespace/repo:latest',
'from_index_resolved': 'quay.io/namespace/from_index@sha256:defghi',
'graph_update_mode': None,
'id': 1,
'index_image': 'quay.io/namespace/index@sha256:fghijk',
'index_image_resolved': None,
'internal_index_image_copy': 'quay.io/namespace/internal_index:tag',
'internal_index_image_copy_resolved': 'quay.io/namespace/internal_index@sha256:something',
'logs': {
'url': 'http://localhost/api/v1/builds/1/logs',
'expiration': '2020-02-15T17:03:00Z',
},
'omps_operator_version': {},
'organization': None,
'removed_operators': [],
'request_type': 'add',
'state': 'complete',
'state_history': [
{
'state': 'complete',
'state_reason': 'Completed successfully',
'updated': '2020-02-12T17:03:00Z',
},
{
'state': 'in_progress',
'state_reason': 'The request was initiated',
'updated': '2020-02-12T17:03:00Z',
},
],
'state_reason': 'Completed successfully',
'updated': '2020-02-12T17:03:00Z',
'user': 'tbrady@DOMAIN.LOCAL',
}
assert rv == expected
def test_get_builds(app, auth_env, client, db):
total_create_requests = 5
total_add_requests = 50
total_fbc_operations_requests = 5
total_requests = total_create_requests + total_add_requests + total_fbc_operations_requests
# flask_login.current_user is used in RequestAdd.from_json, which requires a request context
with app.test_request_context(environ_base=auth_env):
for i in range(total_add_requests):
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
if i % 5 == 0:
request.add_state('failed', 'Failed due to an unknown error')
db.session.add(request)
for i in range(total_create_requests):
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'from_index': f'quay.io/namespace/repo:{i}',
}
request2 = RequestCreateEmptyIndex.from_json(data)
db.session.add(request2)
for i in range(total_fbc_operations_requests):
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'from_index': f'quay.io/namespace/repo:{i}',
'fbc_fragment': f'quay.io/namespace/fbcfragment:{i}',
}
request2 = RequestFbcOperations.from_json(data)
db.session.add(request2)
db.session.commit()
rv_json = client.get('/api/v1/builds?page=2').json
# Verify the order_by is correct
assert rv_json['items'][0]['id'] == total_requests - app.config['IIB_MAX_PER_PAGE']
assert len(rv_json['items']) == app.config['IIB_MAX_PER_PAGE']
# This key is only present the verbose=true
assert 'state_history' not in rv_json['items'][0]
assert rv_json['meta']['page'] == 2
assert rv_json['meta']['pages'] == 3
assert rv_json['meta']['per_page'] == app.config['IIB_MAX_PER_PAGE']
assert rv_json['meta']['total'] == total_requests
rv_json = client.get('/api/v1/builds?state=failed&per_page=5').json
total_failed_requests = total_add_requests // 5
assert len(rv_json['items']) == 5
assert 'state=failed' in rv_json['meta']['next']
assert rv_json['meta']['page'] == 1
assert rv_json['meta']['pages'] == 2
assert rv_json['meta']['per_page'] == 5
assert rv_json['meta']['total'] == total_failed_requests
rv_json = client.get('/api/v1/builds?batch=3').json
assert len(rv_json['items']) == 1
assert 'batch=3' in rv_json['meta']['first']
assert rv_json['meta']['total'] == 1
rv_json = client.get('/api/v1/builds?verbose=true&per_page=1').json
# This key is only present the verbose=true
assert 'state_history' in rv_json['items'][0]
rv_json = client.get('/api/v1/builds?request_type=add').json
assert rv_json['meta']['total'] == total_add_requests
assert rv_json['items'][0]['request_type'] == 'add'
rv_json = client.get('/api/v1/builds?request_type=fbc-operations').json
assert rv_json['meta']['total'] == total_fbc_operations_requests
assert rv_json['items'][0]['request_type'] == 'fbc-operations'
rv_json = client.get('/api/v1/builds?request_type=create-empty-index').json
assert rv_json['meta']['total'] == total_create_requests
assert rv_json['items'][0]['request_type'] == 'create-empty-index'
rv_json = client.get('/api/v1/builds?user=tbrady@DOMAIN.LOCAL').json
assert rv_json['meta']['total'] == total_requests
assert rv_json['items'][0]['user'] == 'tbrady@DOMAIN.LOCAL'
def test_get_builds_empty(client, db):
rv_json = client.get('/api/v1/builds').json
assert rv_json['items'] == []
assert rv_json['meta']['total'] == 0
assert rv_json['meta']['pages'] == 0
assert rv_json['meta']['last'] == rv_json['meta']['first']
assert 'page=1' in rv_json['meta']['last']
def test_index_image_filter(
app, client, db, minimal_request_add, minimal_request_rm, minimal_request_fbc_operations
):
minimal_request_add.add_state('in_progress', 'Starting things up!')
minimal_request_add.index_image = Image.get_or_create('quay.io/namespace/index@sha256:fghijk')
minimal_request_add.add_state('complete', 'The request is complete')
minimal_request_rm.add_state('in_progress', 'Starting things up!')
minimal_request_rm.index_image = Image.get_or_create('quay.io/namespace/index@sha256:123456')
minimal_request_rm.add_state('complete', 'The request is complete')
minimal_request_fbc_operations.add_state('in_progress', 'Starting things up!')
minimal_request_fbc_operations.index_image = Image.get_or_create(
'quay.io/namespace/index@sha256:fbcop'
)
minimal_request_fbc_operations.add_state('complete', 'The request is complete')
db.session.commit()
rv_json = client.get('/api/v1/builds?index_image=quay.io/namespace/index@sha256:fghijk').json
assert rv_json['meta']['total'] == 1
rv_json = client.get('/api/v1/builds?index_image=quay.io/namespace/index@sha256:123456').json
assert rv_json['meta']['total'] == 1
rv_json = client.get('/api/v1/builds?index_image=quay.io/namespace/index@sha256:fbcop').json
assert rv_json['meta']['total'] == 1
rv = client.get('/api/v1/builds?index_image=quay.io/namespace/index@sha256:abc')
assert rv.json == {'error': ('quay.io/namespace/index@sha256:abc is not a valid index image')}
def _request_add_state_and_from_index(minimal_request, image_pull_spec):
minimal_request.add_state('in_progress', 'Starting things up!')
minimal_request.from_index = Image.get_or_create(image_pull_spec)
minimal_request.add_state('complete', 'The request is complete')
def test_from_index_filter(
app, client, db, minimal_request_add, minimal_request_rm, minimal_request_fbc_operations
):
_request_add_state_and_from_index(
minimal_request_add, 'quay.io/namespace/index@sha256:from_index'
)
_request_add_state_and_from_index(
minimal_request_rm, 'quay.io/namespace/from_index@sha256:123456'
)
_request_add_state_and_from_index(
minimal_request_fbc_operations,
'quay.io/namespace/from_index@sha256:fbcop',
)
db.session.commit()
rv_json = client.get('/api/v1/builds?from_index=quay.io/namespace/index@sha256:from_index').json
assert rv_json['meta']['total'] == 1
rv_json = client.get(
'/api/v1/builds?from_index=quay.io/namespace/from_index@sha256:123456'
).json
assert rv_json['meta']['total'] == 1
rv_json = client.get('/api/v1/builds?from_index=quay.io/namespace/from_index@sha256:fbcop').json
assert rv_json['meta']['total'] == 1
rv = client.get('/api/v1/builds?from_index=quay.io/namespace/index@sha256:abc')
assert rv.json == {
'error': 'from_index quay.io/namespace/index@sha256:abc is not a valid index image'
}
def test_from_index_startswith_filter(
app, client, db, minimal_request_add, minimal_request_rm, minimal_request_fbc_operations
):
_request_add_state_and_from_index(
minimal_request_add, 'quay.io/namespace/index@sha256:from_index'
)
_request_add_state_and_from_index(
minimal_request_rm, 'quay.io/namespace/from_index@sha256:123456'
)
_request_add_state_and_from_index(
minimal_request_fbc_operations,
'quay.io/namespace/from_index@sha256:fbcop',
)
db.session.commit()
rv_json = client.get('/api/v1/builds?from_index_startswith=quay.io/namespace/from_index').json
assert rv_json['meta']['total'] == 2
rv_json = client.get(
'/api/v1/builds?from_index_startswith=quay.io/namespace/index@sha256:from_index'
).json
assert rv_json['meta']['total'] == 1
rv_json = client.get('/api/v1/builds?from_index_startswith=quay.io/namespace').json
assert rv_json['meta']['total'] == 3
rv = client.get('/api/v1/builds?from_index_startswith=quay.io/namespace/index@sha256:abc')
assert rv.json == {
'error': "Can\'t find any from_index starting with quay.io/namespace/index@sha256:abc"
}
def test_get_builds_invalid_state(app, client, db):
rv = client.get('/api/v1/builds?state=is_it_lunch_yet%3F')
assert rv.status_code == 400
assert rv.json == {
'error': (
'is_it_lunch_yet? is not a valid build request state. Valid states are: complete, '
'failed, in_progress'
)
}
def test_get_builds_invalid_type(app, client, db):
rv = client.get('/api/v1/builds?request_type=wrong-type')
assert rv.status_code == 400
assert rv.json == {
'error': (
'wrong-type is not a valid build request type. Valid request_types are: '
'generic, add, rm, regenerate-bundle, merge-index-image, '
'create-empty-index, recursive-related-bundles, fbc-operations, add-deprecations'
)
}
@pytest.mark.parametrize('batch', (0, 'right_one'))
def test_get_builds_invalid_batch(batch, app, client, db):
rv = client.get(f'/api/v1/builds?batch={batch}')
assert rv.status_code == 400
assert rv.json == {'error': 'The batch must be a positive integer'}
@mock.patch('sqlalchemy.engine.base.Engine.connect')
def test_get_healthcheck_db_fail(mock_db_execute, app, client, db):
mock_db_execute.side_effect = DisconnectionError('DB failed')
rv = client.get('/api/v1/healthcheck')
assert rv.status_code == 500
assert rv.json == {'error': 'Database health check failed.'}
def test_get_healthcheck_ok(app, client, db):
rv = client.get('/api/v1/healthcheck')
assert rv.status_code == 200
assert rv.json == {'status': 'Health check OK'}
@pytest.mark.parametrize(
("bundles", "exp_bundles"),
(([1, 2, 3, 4], [1, 2, 3, 4]), ([], []), ([1, 2, 1, 3, 1, 4, 3], [1, 2, 3, 4])),
)
def test_get_unique_bundles(bundles, exp_bundles, app):
tmp_uniq = _get_unique_bundles(bundles)
assert tmp_uniq == exp_bundles
@pytest.mark.parametrize(
('logs_content', 'expired', 'finalized', 'expected'),
(
('foobar', False, True, {'status': 200, 'mimetype': 'text/plain', 'data': 'foobar'}),
('foobar', True, True, {'status': 200, 'mimetype': 'text/plain', 'data': 'foobar'}),
('', False, True, {'status': 200, 'mimetype': 'text/plain', 'data': ''}),
('', True, True, {'status': 200, 'mimetype': 'text/plain', 'data': ''}),
(
None,
True,
True,
{'status': 410, 'mimetype': 'application/json', 'json': {'error': mock.ANY}},
),
(
None,
False,
True,
{'status': 500, 'mimetype': 'application/json', 'json': {'error': mock.ANY}},
),
(
None,
False,
False,
{'status': 400, 'mimetype': 'application/json', 'json': {'error': mock.ANY}},
),
),
)
def test_get_build_logs(
client, db, minimal_request_add, tmpdir, logs_content, expired, finalized, expected
):
minimal_request_add.add_state('in_progress', 'Starting things up!')
db.session.commit()
client.application.config['IIB_REQUEST_LOGS_DIR'] = str(tmpdir)
if expired:
client.application.config['IIB_REQUEST_DATA_DAYS_TO_LIVE'] = -1
if finalized:
minimal_request_add.add_state('complete', 'The request is complete')
db.session.commit()
request_id = minimal_request_add.id
if logs_content is not None:
tmpdir.join(f'{request_id}.log').write(logs_content)
rv = client.get(f'/api/v1/builds/{request_id}/logs')
assert rv.status_code == expected['status']
assert rv.mimetype == expected['mimetype']
if 'data' in expected:
assert rv.data.decode('utf-8') == expected['data']
if 'json' in expected:
assert rv.json == expected['json']
def test_get_build_logs_not_configured(client, db, minimal_request_add):
minimal_request_add.add_state('complete', 'Wrapping things up!')
db.session.commit()
client.application.config['IIB_REQUEST_LOGS_DIR'] = None
request_id = minimal_request_add.id
rv = client.get(f'/api/v1/builds/{request_id}/logs')
assert rv.status_code == 404
assert rv.mimetype == 'application/json'
assert rv.json == {'error': mock.ANY}
rv = client.get(f'/api/v1/builds/{request_id}')
assert rv.status_code == 200
assert 'logs' not in rv.json
@pytest.mark.parametrize(
('logs_content', 'expired', 'expected'),
(
(
None,
False,
{'status': 404, 'mimetype': 'application/json', 'json': {'error': mock.ANY}},
),
(None, True, {'status': 410, 'mimetype': 'application/json', 'json': {'error': mock.ANY}}),
('foobar', False, {'status': 200, 'mimetype': 'application/json', 'data': 'foobar'}),
),
)
@mock.patch('iib.web.api_v1.get_object_from_s3_bucket')
def test_get_build_logs_s3_configured(
mock_gofs3b, logs_content, expired, expected, client, db, minimal_request_regenerate_bundle
):
minimal_request_regenerate_bundle.add_state('complete', 'The request is complete')
db.session.commit()
mock_gofs3b.return_value = None
if logs_content:
response_body = StreamingBody(StringIO(logs_content), len(logs_content))
mock_gofs3b.return_value = response_body
client.application.config['IIB_AWS_S3_BUCKET_NAME'] = 's3-bucket'
if expired:
client.application.config['IIB_REQUEST_DATA_DAYS_TO_LIVE'] = -1
request_id = minimal_request_regenerate_bundle.id
rv = client.get(f'/api/v1/builds/{request_id}/related_bundles')
assert rv.status_code == expected['status']
assert rv.mimetype == expected['mimetype']
if 'data' in expected:
assert rv.data.decode('utf-8') == expected['data']
if 'json' in expected:
assert rv.json == expected['json']
@pytest.mark.parametrize(
'data, error_msg',
(
(
{
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'binary_image': '',
'add_arches': ['s390x'],
},
'The "binary_image" value must be a non-empty string',
),
(
{
'bundles': ['some:thing'],
'from_index': 32,
'binary_image': 'binary:image',
'add_arches': ['s390x'],
},
'"from_index" must be a string',
),
(
{
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'add_arches': [1, 2, 3],
},
'Architectures should be specified as a non-empty array of strings',
),
(
{
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'overwrite_from_index': 123,
},
'The "overwrite_from_index" parameter must be a boolean',
),
(
{
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'check_related_images': 123,
},
'The "check_related_images" parameter must be a boolean',
),
(
{
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'check_related_images': "123",
},
'The "check_related_images" parameter must be a boolean',
),
(
{
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'check_related_images': "123",
},
'"check_related_images" must be specified only when bundles are specified',
),
(
{
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'overwrite_from_index': True,
'overwrite_from_index_token': True,
},
'The "overwrite_from_index_token" parameter must be a string',
),
(
{'bundles': ['some:thing'], 'binary_image': 'binary:image', 'cnr_token': True},
'"cnr_token" must be a string',
),
(
{'bundles': ['some:thing'], 'binary_image': 'binary:image', 'organization': True},
'"organization" must be a string',
),
(
{'bundles': ['some:thing'], 'binary_image': 'binary:image', 'force_backport': 'spam'},
'"force_backport" must be a boolean',
),
(
{'bundles': ['some:thing'], 'binary_image': 'binary:image', 'graph_update_mode': 123},
'"graph_update_mode" must be a string',
),
(
{'bundles': ['some:thing'], 'binary_image': 'binary:image', 'graph_update_mode': 'Hi'},
(
'"graph_update_mode" must be set to one of these: [\'replaces\', \'semver\''
', \'semver-skippatch\']'
),
),
),
)
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundles_invalid_params_format(mock_smfsc, data, error_msg, db, auth_env, client):
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert error_msg == rv.json['error']
mock_smfsc.assert_not_called()
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundles_overwrite_not_allowed(mock_smfsc, client, db):
data = {
'binary_image': 'binary:image',
'bundles': ['some:thing'],
'from_index': 'pull:spec',
'overwrite_from_index': True,
}
rv = client.post(f'/api/v1/builds/add', json=data, environ_base={'REMOTE_USER': 'tom_hanks'})
assert rv.status_code == 403
error_msg = 'You must set "overwrite_from_index_token" to use "overwrite_from_index"'
assert error_msg == rv.json['error']
mock_smfsc.assert_not_called()
@pytest.mark.parametrize('from_index', (None, 'some-random-index:v4.14', 'some-common-index:v4.15'))
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundles_graph_update_mode_not_allowed(
mock_smfsc, app, client, auth_env, db, from_index
):
graph_update_mode_list = ['some-unique-index', 'some-common-index:v1.14']
app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST'] = graph_update_mode_list
data = {
'binary_image': 'binary:image',
'bundles': ['some:thing'],
'from_index': from_index,
'graph_update_mode': 'semver',
'overwrite_from_index': True,
'overwrite_from_index_token': "somettoken",
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 403
error_msg = (
'"graph_update_mode" can only be used on the'
f' following index image: {graph_update_mode_list}'
)
assert error_msg == rv.json['error']
mock_smfsc.assert_not_called()
@pytest.mark.parametrize('from_index', ('some-common-index:v4.15', 'another-common-index:v4.15'))
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
@mock.patch('iib.web.api_v1.handle_add_request')
def test_add_bundles_graph_update_mode_allowed(
mock_har, mock_smfsc, app, client, auth_env, db, from_index
):
app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST'] = [
'some-common-index',
'another-common-index:v4.15',
]
data = {
'binary_image': 'binary:image',
'bundles': ['some:thing'],
'from_index': from_index,
'graph_update_mode': 'semver',
'overwrite_from_index': True,
'overwrite_from_index_token': "somettoken",
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 201
mock_har.apply_async.assert_called_once()
@mock.patch('iib.web.api_v1.db.session')
@mock.patch('iib.web.api_v1.flask.jsonify')
@mock.patch('iib.web.api_v1.RequestAdd')
@mock.patch('iib.web.api_v1.handle_add_request.apply_async')
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundles_unique_bundles(mock_smfsc, mock_har, mock_radd, mock_fj, mock_dbs, client):
data = {
'binary_image': 'binary:image',
'bundles': ['same:thing', 'same:thing'],
'from_index': 'pull:spec',
'overwrite_from_index': True,
'overwrite_from_index_token': "some_token",
}
mock_fj.return_value = '{"random": "json"}'
client.post(
'/api/v1/builds/add', json=data, environ_base={'REMOTE_USER': 'tbrady@DOMAIN.LOCAL'}
)
# check if duplicate bundles were removed from payload
assert mock_har.call_args[1]['args'][0] == ['same:thing']
@pytest.mark.parametrize(
'data, error_msg',
(
(
{'from_index': 'pull:spec', 'binary_image': 'binary:image', 'add_arches': ['s390x']},
'"operators" should be a non-empty array of strings',
),
(
{
'cnr_token': 'token',
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'operators': ['prometheus'],
},
'The following parameters are invalid: cnr_token',
),
(
{
'organization': 'organization',
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'operators': ['prometheus'],
},
'The following parameters are invalid: organization',
),
),
)
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_rm_operators_invalid_params_format(mock_smfsc, db, auth_env, client, data, error_msg):
rv = client.post(f'/api/v1/builds/rm', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert error_msg == rv.json['error']
mock_smfsc.assert_not_called()
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_rm_operators_overwrite_not_allowed(mock_smfsc, client, db):
data = {
'binary_image': 'binary:image',
'operators': ['prometheus'],
'from_index': 'pull:spec',
'overwrite_from_index': True,
}
rv = client.post(f'/api/v1/builds/rm', json=data, environ_base={'REMOTE_USER': 'tom_hanks'})
assert rv.status_code == 403
error_msg = 'You must set "overwrite_from_index_token" to use "overwrite_from_index"'
assert error_msg == rv.json['error']
mock_smfsc.assert_not_called()
@pytest.mark.parametrize(
'data, error_msg',
(
(
{'bundles': ['some:thing'], 'from_index': 'pull:spec', 'add_arches': ['s390x']},
'The "binary_image" value must be a non-empty string',
),
(
{'add_arches': ['s390x'], 'binary_image': 'binary:image'},
'"from_index" must be specified if no bundles are specified',
),
({'add_arches': ['s390x']}, '"from_index" must be specified if no bundles are specified'),
(
{
'bundles': ['some:thing'],
'binary_image': 'binary:image',
'add_arches': ['s390x'],
'overwrite_from_index_token': 'username:password',
},
(
'The "overwrite_from_index" parameter is required when the '
'"overwrite_from_index_token" parameter is used'
),
),
),
)
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundle_missing_required_param(mock_smfsc, data, error_msg, db, auth_env, client):
rv = client.post(f'/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert rv.json['error'] == error_msg
mock_smfsc.assert_not_called()
@pytest.mark.parametrize(
'data, error_msg',
(
(
{'operators': ['some:thing'], 'from_index': 'pull:spec', 'add_arches': ['s390x']},
'The "binary_image" value must be a non-empty string',
),
(
{'from_index': 'pull:spec', 'binary_image': 'binary:image', 'add_arches': ['s390x']},
'"operators" should be a non-empty array of strings',
),
(
{'operators': ['some:thing'], 'binary_image': 'pull:spec', 'add_arches': ['s390x']},
'Missing required parameter(s): from_index',
),
(
{
'operators': ['some:thing'],
'binary_image': 'pull:spec',
'from_index': 'pull:spec',
'overwrite_from_index_token': 'username:password',
},
(
'The "overwrite_from_index" parameter is required when the '
'"overwrite_from_index_token" parameter is used'
),
),
),
)
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_rm_operator_missing_required_param(mock_smfsc, data, error_msg, db, auth_env, client):
rv = client.post(f'/api/v1/builds/rm', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert rv.json['error'] == error_msg
mock_smfsc.assert_not_called()
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundle_invalid_param(mock_smfsc, db, auth_env, client):
data = {
'best_batsman': 'Virat Kohli',
'binary_image': 'binary:image',
'bundles': ['some:thing'],
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert rv.json['error'] == 'The following parameters are invalid: best_batsman'
mock_smfsc.assert_not_called()
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundle_from_invalid_distribution_scope(mock_smfsc, db, auth_env, client):
data = {
'binary_image': 'binary:image',
'add_arches': ['s390x'],
'organization': 'org',
'from_index': 'some:thing',
'distribution_scope': 'badvalue',
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert rv.json['error'] == (
'The "distribution_scope" value must be one of "dev", "stage", or "prod"'
)
mock_smfsc.assert_not_called()
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_rm_bundle_from_invalid_distribution_scope(mock_smfsc, db, auth_env, client):
data = {
'binary_image': 'binary:image',
'add_arches': ['s390x'],
'from_index': 'some:thing',
'distribution_scope': 'badvalue',
'operators': ['some:thing'],
}
rv = client.post('/api/v1/builds/rm', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert rv.json['error'] == (
'The "distribution_scope" value must be one of "dev", "stage", or "prod"'
)
mock_smfsc.assert_not_called()
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundle_from_index_and_add_arches_missing(mock_smfsc, db, auth_env, client):
data = {'bundles': ['some:thing'], 'binary_image': 'binary:image'}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 400
assert rv.json['error'] == 'One of "from_index" or "add_arches" must be specified'
mock_smfsc.assert_not_called()
@pytest.mark.parametrize(
(
'binary_image',
'overwrite_from_index',
'overwrite_from_index_token',
'bundles',
'from_index',
'distribution_scope',
'graph_update_mode',
),
(
('binary:image', False, None, ['some:thing'], None, None, None),
('binary:image', False, None, ['some:thing'], 'some:thing', None, 'semver'),
('binary:image', False, None, [], 'some:thing', 'Prod', 'semver-skippatch'),
('scratch', True, 'username:password', ['some:thing'], 'some:thing', 'StagE', 'replaces'),
('scratch', True, 'username:password', [], 'some:thing', 'DeV', 'semver'),
),
)
@mock.patch('iib.web.api_v1.handle_add_request')
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundle_success(
mock_smfsc,
mock_har,
app,
overwrite_from_index,
overwrite_from_index_token,
db,
auth_env,
client,
bundles,
from_index,
distribution_scope,
graph_update_mode,
binary_image,
):
app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST'] = [from_index]
data = {
'binary_image': binary_image,
'add_arches': ['s390x'],
'organization': 'org',
'cnr_token': 'token',
'overwrite_from_index': overwrite_from_index,
'overwrite_from_index_token': overwrite_from_index_token,
'from_index': from_index,
}
expected_distribution_scope = None
if distribution_scope:
data['distribution_scope'] = distribution_scope
expected_distribution_scope = distribution_scope.lower()
if bundles:
data['bundles'] = bundles
if graph_update_mode:
data['graph_update_mode'] = graph_update_mode
response_json = {
'arches': [],
'batch': 1,
'batch_annotations': None,
'binary_image': binary_image,
'binary_image_resolved': None,
'build_tags': [],
'bundle_mapping': {},
'bundles': bundles,
'check_related_images': None,
'deprecation_list': [],
'distribution_scope': expected_distribution_scope,
'from_index': from_index,
'from_index_resolved': None,
'graph_update_mode': graph_update_mode,
'id': 1,
'index_image': None,
'index_image_resolved': None,
'internal_index_image_copy': None,
'internal_index_image_copy_resolved': None,
'removed_operators': [],
'request_type': 'add',
'state': 'in_progress',
'logs': {
'url': 'http://localhost/api/v1/builds/1/logs',
'expiration': '2020-02-15T17:03:00Z',
},
'omps_operator_version': {},
'organization': 'org',
'state_history': [
{
'state': 'in_progress',
'state_reason': 'The request was initiated',
'updated': '2020-02-12T17:03:00Z',
}
],
'state_reason': 'The request was initiated',
'updated': '2020-02-12T17:03:00Z',
'user': 'tbrady@DOMAIN.LOCAL',
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
rv_json = rv.json
rv_json['state_history'][0]['updated'] = '2020-02-12T17:03:00Z'
rv_json['updated'] = '2020-02-12T17:03:00Z'
rv_json['logs']['expiration'] = '2020-02-15T17:03:00Z'
assert rv.status_code == 201
assert response_json == rv_json
assert 'cnr_token' not in rv_json
assert 'token' not in mock_har.apply_async.call_args[1]['argsrepr']
assert '*****' in mock_har.apply_async.call_args[1]['argsrepr']
mock_har.apply_async.assert_called_once()
mock_smfsc.assert_called_once_with(mock.ANY, new_batch_msg=True)
@pytest.mark.parametrize('force_backport', (False, True))
@mock.patch('iib.web.api_v1.handle_add_request')
def test_add_bundle_force_backport(mock_har, force_backport, db, auth_env, client):
data = {
'bundles': ['some:thing'],
'binary_image': 'binary:image',
'from_index': 'index:image',
'force_backport': force_backport,
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
assert rv.status_code == 201
mock_har.apply_async.assert_called_once()
# Eigth element in args is the force_backport parameter
assert mock_har.apply_async.call_args[1]['args'][7] == force_backport
@mock.patch('iib.web.api_v1.handle_add_request')
@mock.patch('iib.web.api_v1.messaging.send_message_for_state_change')
def test_add_bundle_overwrite_token_redacted(mock_smfsc, mock_har, app, auth_env, client, db):
token = 'username:password'
data = {
'bundles': ['some:thing'],
'binary_image': 'binary:image',
'add_arches': ['amd64'],
'overwrite_from_index': True,
'overwrite_from_index_token': token,
}
rv = client.post('/api/v1/builds/add', json=data, environ_base=auth_env)
rv_json = rv.json
assert rv.status_code == 201
mock_har.apply_async.assert_called_once()
# Tenth to last element in args is the overwrite_from_index parameter
assert mock_har.apply_async.call_args[1]['args'][-11] is True
# Ninth to last element in args is the overwrite_from_index_token parameter
assert mock_har.apply_async.call_args[1]['args'][-10] == token
assert 'overwrite_from_index_token' not in rv_json
assert token not in json.dumps(rv_json)
assert token not in mock_har.apply_async.call_args[1]['argsrepr']
assert '*****' in mock_har.apply_async.call_args[1]['argsrepr']
@pytest.mark.parametrize(
'user_to_queue, overwrite_from_index, expected_queue',
(
({'tbrady@DOMAIN.LOCAL': 'Buccaneers'}, False, 'Buccaneers'),
({'tbrady@DOMAIN.LOCAL': 'Buccaneers'}, True, 'Buccaneers'),
({'PARALLEL:tbrady@DOMAIN.LOCAL': 'Buccaneers'}, False, 'Buccaneers'),
({'SERIAL:tbrady@DOMAIN.LOCAL': 'Buccaneers'}, True, 'Buccaneers'),
(
{'tbrady@DOMAIN.LOCAL': 'Patriots', 'PARALLEL:tbrady@DOMAIN.LOCAL': 'Buccaneers'},
False,
'Buccaneers',
),
(
{
'tbrady@DOMAIN.LOCAL': {'all': 'all_queue', 'index:image': 'index_queue'},
'not.tbrady@DOMAIN.LOCAL': {'all', 'all_queue2'},
},
True,
'index_queue',
),
(