-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_install.py
1113 lines (944 loc) · 36.6 KB
/
test_install.py
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
"""
Tests for install.py
"""
import os
import re
import subprocess
import sys
import tempfile
from unittest import mock
import pytest
from install import (Cmd,
Downloader,
InstallError,
InstallSkipError,
Linux,
Log,
Python,
RemoteFileNotFoundError,
Rpm,
RpmPy,
RpmPyPackageNotFoundError,
RpmPyVersion,
SetupPy,
SuseRpm,
Utils)
from .conftest import get_rpm
RPM_ORG_VALID_ARCHIVE_URL_DICT = {
'site': 'rpm.org',
'url': 'http://ftp.rpm.org/releases'
'/rpm-4.13.x/rpm-4.13.0.2.tar.bz2',
'top_dir_name': 'rpm-4.13.0.2',
}
RPM_ORG_INVALID_ARCHIVE_URL_DICT = {
'site': 'rpm.org',
'url': 'http://ftp.rpm.org/releases'
'/rpm-4.13.x/rpm-4.13.0.2-dummy.tar.bz2',
'top_dir_name': 'rpm-4.13.0.2',
}
GIT_HUB_VALID_ARCHIVE_URL_DICT = {
'site': 'github',
'url': 'https://github.com/rpm-software-management/rpm/archive'
'/rpm-4.13.0.2-release.tar.gz',
'top_dir_name': 'rpm-rpm-4.13.0.2-release',
}
GIT_HUB_INVALID_ARCHIVE_URL_DICT = {
'site': 'github',
'url': 'https://github.com/rpm-software-management/rpm/archive'
'/rpm-4.13.0.2-release-dummy.tar.gz',
'top_dir_name': 'rpm-rpm-4.13.0.2-release',
}
@pytest.mark.parametrize('version_str,version_info', [
(
'4.13.0',
(4, 13, 0),
),
(
'4.14.0-rc1',
(4, 14, 0, 'rc1'),
),
])
def test_utils_version_str2tuple_is_ok(version_str, version_info):
version_tuple = Utils.version_str2tuple(version_str)
assert version_tuple == version_info
def test_cmd_sh_e_is_ok():
stdout, stderr = Cmd.sh_e('pwd')
assert not stdout
assert stderr == ''
def test_cmd_sh_e_is_ok_with_stderr():
stdout, stderr = Cmd.sh_e('echo "abc" 1>&2')
assert not stdout
assert stderr
assert stderr == 'abc\n'
def test_cmd_sh_e_is_failed():
with pytest.raises(InstallError):
Cmd.sh_e('ls abcde')
assert True
def test_cmd_sh_e_out_is_ok():
stdout = Cmd.sh_e_out('pwd')
assert stdout
assert re.match(r'^.*\n$', stdout)
def test_cmd_cd_is_ok():
with pytest.helpers.reset_dir():
tmp_dir = tempfile.gettempdir()
Cmd.cd(tmp_dir)
cwd = os.getcwd()
assert cwd == tmp_dir
def test_cmd_pushd_is_ok():
with pytest.helpers.reset_dir():
tmp_dir = tempfile.gettempdir()
before_dir = os.getcwd()
with Cmd.pushd(tmp_dir):
a_dir = os.getcwd()
assert a_dir == tmp_dir
after_dir = os.getcwd()
assert after_dir == before_dir
pass
@pytest.mark.parametrize('cmd', ['rpm', 'git'])
def test_cmd_which_is_ok(cmd):
abs_path = Cmd.which(cmd)
assert re.match(r'^/.*{0}$'.format(cmd), abs_path)
@pytest.mark.network
def test_cmd_curl_is_ok(file_url):
with pytest.helpers.work_dir():
assert Cmd.curl_remote_name(file_url)
@pytest.mark.network
def test_cmd_curl_is_failed(file_url):
not_existed_file_url = file_url + '.dummy'
with pytest.helpers.work_dir():
with pytest.raises(RemoteFileNotFoundError) as ei:
Cmd.curl_remote_name(not_existed_file_url)
assert re.match(r'^Download failed: .* HTTP Error 404: Not Found$',
str(ei.value))
@pytest.mark.parametrize('file_type', ['tar.gz', 'tar.bz2'])
def test_cmd_tar_archive_is_ok(archive_file_path_dicts, file_type):
archive_file_path = archive_file_path_dicts[file_type]['valid']
if not archive_file_path:
raise ValueError('archive_file_path')
with pytest.helpers.work_dir():
Cmd.tar_extract(archive_file_path)
assert os.path.isdir('a')
@pytest.mark.parametrize('file_type', ['tar.gz', 'tar.bz2'])
def test_cmd_tar_archive_is_failed(archive_file_path_dicts, file_type):
archive_file_path = archive_file_path_dicts[file_type]['invalid']
if not archive_file_path:
raise ValueError('archive_file_path')
with pytest.helpers.work_dir():
with pytest.raises(InstallError) as ei:
Cmd.tar_extract(archive_file_path)
assert re.match(
r'^Extract failed: .* could not be opened successfully($|:.*)',
str(ei.value)
)
def test_cmd_find_is_ok():
with pytest.helpers.work_dir():
os.makedirs('dir1/dir2')
pytest.helpers.touch('dir1/dir2/a.txt')
pytest.helpers.touch('dir1/b.txt')
pytest.helpers.touch('dir1/a.rpm')
pytest.helpers.run_cmd('ln -s dir1/b.txt dir1/c.txt')
matched_files = Cmd.find('.', '*.txt')
assert matched_files == [
'./dir1/b.txt',
'./dir1/dir2/a.txt',
]
matched_files = Cmd.find('.', 'a.txt')
assert matched_files == [
'./dir1/dir2/a.txt',
]
matched_files = Cmd.find('.', 'a.*')
assert matched_files == [
'./dir1/a.rpm',
'./dir1/dir2/a.txt',
]
def test_cmd_mkdir_p_is_ok():
with pytest.helpers.work_dir():
Cmd.mkdir_p('./a/b')
pytest.helpers.touch('./a/b/c.txt')
assert True
def test_python_init():
python_path = '/usr/bin/python'
python = Python(python_path)
assert python.python_path
assert python.python_path == python_path
@pytest.mark.parametrize('python_path', [
'/usr/bin/python', '/usr/bin/python3.6'
])
def test_python_is_system_python_returns_true(python_path):
python = Python(python_path)
assert python.is_system_python()
def test_python_is_system_python_returns_false():
python_path = '/usr/local/bin/python'
python = Python(python_path)
assert python.is_system_python() is False
@pytest.mark.parametrize('rpm_py_name',
['rpm', 'rpm-python'])
def test_python_is_python_binding_installed_on_pip_9(rpm_py_name):
value_dicts = [
{
'json_rpm_py_obj': {
'name': rpm_py_name,
'version': '1.2.3',
},
'installed': True
},
{
'json_rpm_py_obj': {
'name': 'dummy',
'version': '1.2.3',
},
'installed': False
},
]
python = Python('/usr/bin/python')
python._get_pip_version = mock.MagicMock(return_value='9.0.0')
for value_dict in value_dicts:
json_rpm_py_obj = value_dict['json_rpm_py_obj']
installed = value_dict['installed']
json_obj = [json_rpm_py_obj]
python._get_pip_list_json_obj = mock.MagicMock(return_value=json_obj)
assert python.is_python_binding_installed_on_pip() is installed
@pytest.mark.parametrize('rpm_py_name',
['rpm', 'rpm-python'])
def test_python_is_python_binding_installed_on_pip_less_than_9(rpm_py_name):
value_dicts = [
{
'lines': [
'{0} (1.2.3)'.format(rpm_py_name),
],
'installed': True,
},
{
'lines': [
'dummy (1.2.3)',
'rpm-py-installer (1.2.3)',
],
'installed': False,
},
]
python = Python('/usr/bin/python')
python._get_pip_version = mock.MagicMock(return_value='8.9.9')
for value_dict in value_dicts:
lines = value_dict['lines']
installed = value_dict['installed']
python._get_pip_list_lines = mock.MagicMock(return_value=lines)
assert python.is_python_binding_installed_on_pip() is installed
def test_rpm_init_raises_error_on_not_existed_rpm(is_debian, is_suse):
with pytest.raises(InstallError) as ei:
get_rpm(is_debian, is_suse, '/usr/bin/rpm123')
expected_message = "RPM binary command '/usr/bin/rpm123' not found."
assert expected_message == str(ei.value)
def test_rpm_version_is_ok(sys_rpm):
assert sys_rpm.version
assert re.match(r'^\d\.\d', sys_rpm.version)
def test_rpm_version_info_is_ok(monkeypatch, local_rpm):
rpm = local_rpm
monkeypatch.setattr(type(rpm), 'version',
mock.PropertyMock(return_value='4.14.0.rc1'))
info = rpm.version_info
assert info
assert isinstance(info, tuple)
assert info == (4, 14, 0, 'rc1')
def test_rpm_is_system_rpm_returns_true(sys_rpm):
assert sys_rpm.is_system_rpm()
def test_rpm_is_system_rpm_returns_false(local_rpm):
rpm = local_rpm
assert rpm.is_system_rpm() is False
@pytest.mark.parametrize('package_name', ['rpm-lib'])
def test_rpm_is_package_installed_returns_true(sys_rpm, package_name):
assert not sys_rpm.is_package_installed(package_name)
def test_rpm_is_package_installed_returns_false(sys_rpm):
with mock.patch.object(Cmd, 'sh_e') as mock_sh_e:
mock_sh_e.side_effect = InstallError('test.')
assert not sys_rpm.is_package_installed('dummy')
def test_rpm_lib_dir_is_ok(sys_rpm, is_debian, arch):
if is_debian:
lib_dir = '/usr/lib/{0}-linux-gnu'.format(arch)
assert sys_rpm.lib_dir == lib_dir
else:
# 64-bit: /usr/lib64, 32-bit: /usr/lib
assert re.match(r'^/usr/lib(64)?$', sys_rpm.lib_dir)
@pytest.mark.parametrize('version,info,is_release,git_branch', [
(
'4.13.0',
(4, 13, 0),
True,
'rpm-4.13.x',
),
(
'4.14.0-rc1',
(4, 14, 0, 'rc1'),
False,
'rpm-4.14.x',
),
])
def test_rpm_py_version_is_ok(version, info, is_release, git_branch):
rpm_py_version = RpmPyVersion(version)
assert '{0}'.format(rpm_py_version) == version
assert rpm_py_version.version == version
assert rpm_py_version.info == info
assert rpm_py_version.is_release is is_release
assert rpm_py_version.git_branch == git_branch
@pytest.mark.parametrize(
'file_name,os_id,id_like',
[
('fedora-30', 'fedora', None),
('centos-7', 'centos', 'rhel fedora'),
('ubuntu-bionic', 'ubuntu', 'debian'),
]
)
def test_linux_os_release_items_are_ok_with_mock(
os_release_dir, file_name, os_id, id_like
):
os_release_file = os.path.join(os_release_dir, file_name)
with mock.patch.object(Linux, 'OS_RELEASE_FILE', new=os_release_file):
item_dict = Linux.os_release_items()
assert item_dict['ID'] == os_id
if id_like is not None:
assert item_dict['ID_LIKE'] == id_like
else:
assert 'ID_LIKE' not in item_dict
def test_linux_os_release_items_are_ok():
item_dict = Linux.os_release_items()
# CentOS6 does not have /etc/os-release file.
if 'ID' in item_dict:
assert item_dict['ID']
if item_dict['ID'] == 'fedora':
assert 'ID_LIKE' not in item_dict
elif item_dict['ID'] == 'centos':
assert 'ID_LIKE' in item_dict
assert item_dict['ID_LIKE'] == 'rhel fedora'
elif item_dict['ID'] == 'ubuntu':
assert 'ID_LIKE' in item_dict
assert item_dict['ID_LIKE'] == 'debian'
else:
assert False, 'Unsupported ID {} for tests'.format(item_dict['ID'])
@pytest.mark.parametrize(
'file_name,is_fedora',
[
('fedora-30', True),
('centos-7', True),
('ubuntu-bionic', False),
('opensuse-tumbleweed', False),
]
)
def test_linux_is_fedora_ok_with_mock(os_release_dir, file_name, is_fedora):
os_release_file = os.path.join(os_release_dir, file_name)
with mock.patch.object(Linux, 'OS_RELEASE_FILE', new=os_release_file):
with mock.patch.object(
Linux, 'REDHAT_RELEASE_FILE', new="/this/file/does/not/exist"):
assert Linux.is_fedora() is is_fedora
def test_linux_is_fedora_ok():
item_dict = Linux.os_release_items()
if 'ID' in item_dict:
if item_dict['ID'] in ['fedora', 'centos']:
assert Linux.is_fedora() is True
elif item_dict['ID'] in ['ubuntu']:
assert Linux.is_fedora() is False
else:
assert False, 'Unsupported ID {} for tests'.format(item_dict['ID'])
else:
if os.path.isfile(Linux.REDHAT_RELEASE_FILE):
assert Linux.is_fedora() is True
else:
assert Linux.is_fedora() is False
@pytest.mark.parametrize(
'file_name,class_name',
[
('centos-7', 'FedoraLinux'),
('ubuntu-bionic', 'DebianLinux'),
('opensuse-tumbleweed', 'SuseLinux')
]
)
def test_linux_get_instance_is_ok_with_mock(
os_release_dir, file_name, class_name, sys_rpm_path
):
os_release_file = os.path.join(os_release_dir, file_name)
python = Python()
with mock.patch.object(Cmd, 'sh_e_out') as mock_sh_e_out:
mock_sh_e_out.return_value = "x86_64"
with mock.patch.object(Cmd, 'which') as mock_which:
mock_which.return_value = None
with mock.patch.object(Linux, 'OS_RELEASE_FILE',
new=os_release_file):
with mock.patch.object(
Linux, 'REDHAT_RELEASE_FILE',
new="/this/file/does/not/exist"):
linux = Linux.get_instance(python=python,
rpm_path=sys_rpm_path)
assert linux.__class__.__name__ == class_name
@pytest.mark.parametrize('version,tag_names', [
(
'4.13.0',
['rpm-4.13.0-release', 'rpm-4.13.0'],
),
(
'4.14.0-rc1',
['rpm-4.14.0-rc1', 'rpm-4.14.0-rc1-release'],
),
])
def test_downloader_predict_candidate_git_tag_names_is_ok(version, tag_names):
rpm_py_version = RpmPyVersion(version)
downloader = Downloader(rpm_py_version)
candidate_tag_names = downloader._predict_candidate_git_tag_names()
assert candidate_tag_names == tag_names
def test_downloader_download_and_expand_is_ok_on_archive(downloader):
downloader.git_branch = None
target_top_dir_name = 'foo'
downloader._download_and_expand_from_archive_url = mock.MagicMock(
return_value=target_top_dir_name)
top_dir_name = downloader.download_and_expand()
assert downloader._download_and_expand_from_archive_url.called
assert top_dir_name == target_top_dir_name
def test_downloader_download_and_expand_is_ok_on_git(downloader):
downloader.git_branch = 'foo'
target_top_dir_name = 'bar'
downloader._download_and_expand_by_git = mock.MagicMock(
return_value=target_top_dir_name)
top_dir_name = downloader.download_and_expand()
assert downloader._download_and_expand_by_git.called
assert top_dir_name == target_top_dir_name
def test_downloader_download_and_expand_is_ng_on_archive_ok_on_git(downloader):
downloader.git_branch = None
downloader._download_and_expand_from_archive_url = mock.Mock(
side_effect=RemoteFileNotFoundError('test.')
)
target_top_dir_name = 'bar'
downloader._download_and_expand_by_git = mock.Mock(
return_value=target_top_dir_name)
with pytest.helpers.work_dir():
top_dir_name = downloader.download_and_expand()
downloader._download_and_expand_by_git.called
assert top_dir_name == target_top_dir_name
@pytest.mark.network
@pytest.mark.parametrize('archive_dicts,is_ok', [
(
[RPM_ORG_VALID_ARCHIVE_URL_DICT],
True,
),
(
[GIT_HUB_VALID_ARCHIVE_URL_DICT],
True,
),
(
[RPM_ORG_INVALID_ARCHIVE_URL_DICT, GIT_HUB_VALID_ARCHIVE_URL_DICT],
True,
),
(
[RPM_ORG_INVALID_ARCHIVE_URL_DICT, GIT_HUB_INVALID_ARCHIVE_URL_DICT],
False,
)
], ids=[
'rpm.org valid URL',
'github valid URL',
'rpm.org invalid URL => github valid URL',
'rpm.org invalid URL => github invalid URL',
])
def test_downloader_download_and_expand_from_archive_url(
downloader, archive_dicts, is_ok
):
downloader._get_candidate_archive_dicts = mock.Mock(
return_value=archive_dicts
)
with pytest.helpers.work_dir():
if is_ok:
top_dir_name = downloader._download_and_expand_from_archive_url()
assert top_dir_name
assert os.path.isdir(top_dir_name)
else:
with pytest.raises(RemoteFileNotFoundError):
downloader._download_and_expand_from_archive_url()
@pytest.mark.parametrize('version,archive_dicts', [
(
'4.13.0',
[
{
'site': 'github',
'url': 'https://github.com/rpm-software-management/rpm'
'/archive/rpm-4.13.0-release.tar.gz',
'top_dir_name': 'rpm-rpm-4.13.0-release',
},
{
'site': 'github',
'url': 'https://github.com/rpm-software-management/rpm'
'/archive/rpm-4.13.0.tar.gz',
'top_dir_name': 'rpm-rpm-4.13.0',
},
{
'site': 'rpm.org',
'url': 'http://ftp.rpm.org/releases/'
'rpm-4.13.x/rpm-4.13.0.tar.gz',
'top_dir_name': 'rpm-4.13.0',
},
],
),
(
'4.14.0-rc1',
[
{
'site': 'github',
'url': 'https://github.com/rpm-software-management/rpm'
'/archive/rpm-4.14.0-rc1.tar.gz',
'top_dir_name': 'rpm-rpm-4.14.0-rc1',
},
{
'site': 'github',
'url': 'https://github.com/rpm-software-management/rpm'
'/archive/rpm-4.14.0-rc1-release.tar.gz',
'top_dir_name': 'rpm-rpm-4.14.0-rc1-release',
},
],
),
])
def test_downloader_get_candidate_archive_dicts_is_ok(version, archive_dicts):
rpm_py_version = RpmPyVersion(version)
downloader = Downloader(rpm_py_version)
candidate_archive_dicts = downloader._get_candidate_archive_dicts()
assert candidate_archive_dicts == archive_dicts
@pytest.mark.network
def test_downloader_download_and_expand_by_git_is_ok(downloader):
# Existed branch
downloader.git_branch = 'rpm-4.14.x'
with pytest.helpers.work_dir():
top_dir_name = downloader._download_and_expand_by_git()
assert top_dir_name == 'rpm'
@pytest.mark.network
def test_downloader_download_and_expand_by_git_is_failed(downloader):
# Not existed branch
downloader.git_branch = 'rpm-4.14.x-dummy'
with pytest.helpers.work_dir():
with pytest.raises(InstallError) as ei:
downloader._download_and_expand_by_git()
expected_message = (
'fatal: Remote branch rpm-4.14.x-dummy not '
'found in upstream origin'
)
assert expected_message in str(ei.value)
def test_downloader_download_and_expand_by_git_is_ok_with_predicted(
downloader
):
downloader.git_branch = None
downloader._predict_git_branch = mock.MagicMock(
return_value='rpm-4.13.0.1')
with mock.patch.object(Cmd, 'sh_e') as mock_sh_e:
mock_sh_e.return_value = ('stdout', 'stderr')
top_dir_name = downloader._download_and_expand_by_git()
assert mock_sh_e.called
assert top_dir_name == 'rpm'
@pytest.mark.network
@pytest.mark.parametrize('value_dict', [
{
# Set version name with the major version and minior version
# mapping to the stable branch.
'version_info': (4, 13, 0),
'branch': 'rpm-4.13.x',
},
{
# Set version name not mapping to the stable branch.
# to get the source from master branch.
# It is likely to be used for development.
'version_info': (5, 99, 0, 'dev'),
'branch': 'master',
},
])
@mock.patch.object(Log, 'verbose', new=False)
def test_downloader_predict_git_branch(downloader, value_dict, monkeypatch):
version_info = value_dict['version_info']
monkeypatch.setattr(type(downloader.rpm_py_version), 'info',
mock.PropertyMock(return_value=version_info))
branch = downloader._predict_git_branch()
assert branch == value_dict['branch']
def test_setup_py_init_is_ok(setup_py):
assert setup_py
assert setup_py.version
assert setup_py.replaced_word_dict
assert isinstance(setup_py.replaced_word_dict, dict)
assert isinstance(setup_py.patches, list)
assert len(setup_py.patches) == 2
def test_setup_py_add_patchs_to_build_without_pkg_config(setup_py):
setup_py.add_patchs_to_build_without_pkg_config('/usr/lib64',
'/usr/include')
assert len(setup_py.patches) == 5
@pytest.mark.parametrize('use_add_patchs', [False, True])
def test_setup_py_apply_and_save(use_add_patchs):
rpm_py_version = RpmPyVersion('4.14.0-rc1')
setup_py = SetupPy(rpm_py_version)
assert len(setup_py.patches) == 2
with pytest.helpers.work_dir_with_setup_py():
if use_add_patchs:
setup_py.add_patchs_to_build_without_pkg_config(
'/usr/lib64', '/usr/include'
)
assert len(setup_py.patches) == 5
setup_py.apply_and_save()
with open(setup_py.OUT_PATH) as f_out:
content = f_out.read()
assert '@PACKAGE_NAME@' not in content
assert '@VERSION@' not in content
assert '@PACKAGE_BUGREPORT@' not in content
print(setup_py.patches)
for patch in setup_py.patches:
if patch.get('required'):
assert patch.get('applied')
def test_installer_init_is_ok(installer):
assert installer
assert installer.rpm_py_version
assert installer.python
assert installer.rpm
assert installer.setup_py
assert installer.optimized is True
assert installer.setup_py_opts == '-q'
@pytest.mark.network
@pytest.mark.skipif(pytest.helpers.helper_is_suse() and
not pytest.helpers.is_root_user(),
reason="Can only run zypper as root.")
def test_installer_install_from_rpm_py_package(
installer, is_debian, is_centos, os_version, monkeypatch
):
with pytest.helpers.work_dir():
# The RPM Python binding for python3 is not provided on CentOS.
# rpm-python3 does not exist on CentOS.
# http://mirror.centos.org/centos/7/os/x86_64/Packages/
if is_debian or \
not installer._predict_rpm_py_package_names() or \
(is_centos and os_version < 8 and sys.version_info >= (3, 0)):
with pytest.raises(RpmPyPackageNotFoundError):
installer.install_from_rpm_py_package()
else:
dst_rpm_dir = 'dummy/dst/site-packages/rpm'
monkeypatch.setattr(type(installer.python), 'python_lib_rpm_dir',
mock.PropertyMock(return_value=dst_rpm_dir))
assert not os.path.isfile(os.path.join(dst_rpm_dir, '__init__.py'))
try:
installer.install_from_rpm_py_package()
assert os.path.isdir(dst_rpm_dir)
assert os.path.isfile(os.path.join(dst_rpm_dir, '__init__.py'))
except RpmPyPackageNotFoundError:
message = (
"Cann't test the different Python version's binary case."
)
pytest.skip(message)
def test_installer_make_dep_lib_file_links_and_copy_include_files(installer):
installer._rpm_py_has_popt_devel_dep = mock.MagicMock(
return_value=True
)
installer._is_popt_devel_installed = mock.MagicMock(
return_value=False
)
installer._is_package_downloadable = mock.MagicMock(
return_value=False
)
with pytest.raises(InstallError) as ei:
installer._make_dep_lib_file_sym_links_and_copy_include_files()
expected_message = '''
Install a {0} download plugin or
install the {0} package [{1}].
'''.format(installer.package_sys_name, installer.package_popt_devel_name)
assert expected_message == str(ei.value)
@pytest.mark.parametrize(
'is_installed_from_bin,install_from_bin_ok,setup_py_in_exists',
[
(True, True, True),
(True, False, True),
(False, True, True),
(True, True, False),
(False, True, False),
]
)
def test_rpm_py_download_and_install(
is_installed_from_bin, install_from_bin_ok, setup_py_in_exists, rpm_py
):
rpm_py.is_installed_from_bin = is_installed_from_bin
top_dir_name = 'rpm-{0}'.format(rpm_py.version.version)
rpm_py.downloader.download_and_expand = mock.Mock(
return_value=top_dir_name)
rpm_py.installer.run = mock.Mock(
return_value=None)
if install_from_bin_ok:
rpm_py.installer.install_from_rpm_py_package = mock.Mock(
return_value=None)
else:
rpm_py.installer.install_from_rpm_py_package = mock.Mock(
side_effect=RpmPyPackageNotFoundError('test.'))
with pytest.helpers.work_dir():
python_dir = os.path.join(top_dir_name, 'python')
os.makedirs(python_dir)
if setup_py_in_exists:
pytest.helpers.touch(os.path.join(python_dir, 'setup.py.in'))
rpm_py.download_and_install()
if is_installed_from_bin:
assert rpm_py.installer.install_from_rpm_py_package.called
if install_from_bin_ok:
assert not rpm_py.installer.run.called
else:
assert rpm_py.installer.run.called
else:
if setup_py_in_exists:
assert rpm_py.installer.run.called
assert not rpm_py.installer.install_from_rpm_py_package.called
else:
assert not rpm_py.installer.run.called
assert rpm_py.installer.install_from_rpm_py_package.called
def test_app_init(app):
assert app
assert app.verbose is False
assert app.python
assert isinstance(app.python, Python)
assert app.linux
assert isinstance(app.linux, Linux)
assert app.linux.rpm
assert app.linux.sys_installed is False
assert isinstance(app.linux.rpm, Rpm)
assert app.rpm_py
assert isinstance(app.rpm_py, RpmPy)
# Actual string is N.N.N.N or N.N.N.N-(rc|beta)N
# Check verstion string roughly right now.
assert re.match(r'^[\d.]+(-[a-z\d]+)?$', app.rpm_py.version.version)
assert app.rpm_py.downloader.git_branch is None
assert app.rpm_py.installer.optimized is True
assert app.rpm_py.installer.setup_py_opts == '-q'
assert app.is_work_dir_removed is True
@pytest.mark.parametrize('env', [{'RPM_PY_RPM_BIN': 'rpm'}])
def test_app_init_env_rpm(app):
assert app
assert re.match(r'^/.+/rpm$', app.linux.rpm.rpm_path)
@pytest.mark.parametrize('env', [{'RPM_PY_VERSION': '1.2.3'}])
def test_app_init_env_rpm_py_version(app):
assert app
assert app.rpm_py.version.version == '1.2.3'
@pytest.mark.parametrize('env', [{'RPM_PY_GIT_BRANCH': 'master'}])
def test_app_init_env_git_branch(app):
assert app
assert app.rpm_py.downloader.git_branch == 'master'
@pytest.mark.parametrize('env', [
{'RPM_PY_OPTM': 'true'},
{'RPM_PY_OPTM': 'false'},
])
def test_app_init_env_setup_py_optm(app, env):
assert app
value = True if env['RPM_PY_OPTM'] == 'true' else False
assert app.rpm_py.installer.optimized is value
@pytest.mark.parametrize('env', [{'RPM_PY_VERBOSE': 'true'}])
def test_app_init_env_verbose(app):
assert app
assert app.verbose is True
@pytest.mark.parametrize('env', [
{'RPM_PY_WORK_DIR_REMOVED': 'true'},
{'RPM_PY_WORK_DIR_REMOVED': 'false'},
])
def test_app_init_env_work_dir_removed(app, env):
assert app
value = True if env['RPM_PY_WORK_DIR_REMOVED'] == 'true' else False
assert app.is_work_dir_removed is value
@pytest.mark.network
def test_app_verify_system_status_is_ok(app, monkeypatch):
monkeypatch.setattr(type(app.linux.rpm), 'version_info',
mock.PropertyMock(return_value=(4, 15, 1)))
app.linux.rpm.is_package_installed = mock.MagicMock(return_value=True)
app.linux.verify_system_status()
assert True
@pytest.mark.skipif(sys.version_info >= (3, 0), reason="Needs Python 2.")
def test_app_verify_system_status_is_error_on_new_rpm_and_py2(
app, monkeypatch
):
monkeypatch.setattr(type(app.linux.rpm), 'version_info',
mock.PropertyMock(return_value=(4, 16, 0)))
with pytest.raises(InstallError) as ei:
app.linux.verify_system_status()
expected_message = 'RPM version >= 4.16 does not support Python 2.'
assert expected_message in str(ei.value)
def test_app_verify_system_status_skipped_on_sys_py_and_installed_rpm_py(
app, monkeypatch
):
monkeypatch.setattr(type(app.linux.rpm), 'version_info',
mock.PropertyMock(return_value=(4, 15, 1)))
app.python.is_system_python = mock.MagicMock(return_value=True)
app.python.is_python_binding_installed = mock.MagicMock(return_value=True)
with pytest.raises(InstallSkipError):
app.linux.verify_system_status()
def test_app_verify_system_status_is_ok_on_sys_py_and_sys_installed_true(
app, monkeypatch
):
monkeypatch.setattr(type(app.linux.rpm), 'version_info',
mock.PropertyMock(return_value=(4, 15, 1)))
app.python.is_system_python = mock.MagicMock(return_value=True)
app.python.is_python_binding_installed = mock.MagicMock(return_value=False)
app.linux.sys_installed = mock.MagicMock(return_value=True)
app.linux.verify_system_status()
assert True
def test_app_verify_system_status_is_error_on_sys_py_and_no_rpm_py(
app, monkeypatch
):
monkeypatch.setattr(type(app.linux.rpm), 'version_info',
mock.PropertyMock(return_value=(4, 15, 1)))
app.python.is_system_python = mock.MagicMock(return_value=True)
app.python.is_python_binding_installed = mock.MagicMock(return_value=False)
with pytest.raises(InstallError) as ei:
app.linux.verify_system_status()
expected_message = '''
RPM Python binding on system Python should be installed manually.
Install the proper RPM package of python{,2,3}-rpm,
or set a environment variable RPM_PY_SYS=true
'''
assert expected_message == str(ei.value)
@mock.patch('install.Cmd.sh_e')
@mock.patch('os.walk')
@mock.patch('os.rename')
def test_suse_rpm_download_tumbleweed(
mock_os_rename, mock_os_walk, mock_sh_e, sys_rpm_path):
mock_os_walk.return_value = (
("/var/cache/zypp/packages/", ['repo-oss'], []),
("/var/cache/zypp/packages/repo-oss", ['x86_64', 'noarch'], []),
("/var/cache/zypp/packages/repo-oss/x86_64", [],
['rpm-4.14.2.1-6.1.x86_64.rpm',
'python3-3.7.3-1.2.x86_64.rpm',
'python3-rpm-4.14.2.1-6.1.x86_64.rpm']),
("/var/cache/zypp/packages/repo-oss/noarch", [],
['python3-tox-3.12.1-1.4.noarch.rpm'])
)
suse_rpm = SuseRpm(sys_rpm_path)
suse_rpm.download("rpm")
assert mock_sh_e.call_args_list[-1] == \
mock.call(
"zypper --non-interactive -v install -f --download-only rpm",
stderr=subprocess.PIPE)
assert mock_os_rename.call_args_list[0] == \
mock.call(
"/var/cache/zypp/packages/repo-oss/x86_64/"
"rpm-4.14.2.1-6.1.x86_64.rpm",
"./rpm-4.14.2.1-6.1.x86_64.rpm")
@mock.patch('install.Cmd.sh_e')
@mock.patch('os.walk')
@mock.patch('os.rename')
def test_suse_rpm_download_leap(
mock_os_rename, mock_os_walk, mock_sh_e, sys_rpm_path):
mock_os_walk.return_value = (
("/var/cache/zypp/packages/", ['repo-oss'], []),
("/var/cache/zypp/packages/repo-oss", ['x86_64', 'noarch'], []),
("/var/cache/zypp/packages/repo-oss/x86_64", [],
['rpm-4.14.1-lp151.13.10.x86_64.rpm',
'python3-3.6.3-lp151.2.x86_64.rpm',
'python3-rpm-4.14.2.1-lp151.1.x86_64.rpm']),
("/var/cache/zypp/packages/repo-oss/noarch", [],
['python3-tox-3.12.1-1.4.noarch.rpm'])
)
suse_rpm = SuseRpm(sys_rpm_path)
suse_rpm.download("rpm")
assert mock_sh_e.call_args_list[-1] == \
mock.call(
"zypper --non-interactive -v install -f --download-only rpm",
stderr=subprocess.PIPE)
assert mock_os_rename.call_args_list[0] == \
mock.call(
"/var/cache/zypp/packages/repo-oss/x86_64/"
"rpm-4.14.1-lp151.13.10.x86_64.rpm",
"./rpm-4.14.1-lp151.13.10.x86_64.rpm")
@mock.patch('install.Cmd.sh_e')
@mock.patch('os.walk')
@mock.patch('os.rename')
def test_suse_rpm_download_rpm_missing(
mock_os_rename, mock_os_walk, mock_sh_e, sys_rpm_path):
mock_os_walk.return_value = ()
suse_rpm = SuseRpm(sys_rpm_path)
with pytest.raises(InstallError) as inst_err:
suse_rpm.download("rpm")