File: test_manylinux.py

package info (click to toggle)
python-auditwheel 5.3.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 816 kB
  • sloc: python: 4,270; ansic: 205; cpp: 58; makefile: 20; f90: 12
file content (1046 lines) | stat: -rw-r--r-- 39,295 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
import glob
import io
import logging
import os
import os.path as op
import re
import shutil
import sys
import zipfile
from contextlib import contextmanager
from subprocess import CalledProcessError

import docker
import pytest
from elftools.elf.elffile import ELFFile

from auditwheel.policy import get_arch_name, get_priority_by_name

logger = logging.getLogger(__name__)


ENCODING = "utf-8"
PLATFORM = get_arch_name()
MANYLINUX1_IMAGE_ID = f"quay.io/pypa/manylinux1_{PLATFORM}:latest"
MANYLINUX2010_IMAGE_ID = f"quay.io/pypa/manylinux2010_{PLATFORM}:latest"
MANYLINUX2014_IMAGE_ID = f"quay.io/pypa/manylinux2014_{PLATFORM}:latest"
MANYLINUX_2_24_IMAGE_ID = f"quay.io/pypa/manylinux_2_24_{PLATFORM}:latest"
MANYLINUX_2_28_IMAGE_ID = f"quay.io/pypa/manylinux_2_28_{PLATFORM}:latest"
if PLATFORM in {"i686", "x86_64"}:
    MANYLINUX_IMAGES = {
        "manylinux_2_5": MANYLINUX1_IMAGE_ID,
        "manylinux_2_12": MANYLINUX2010_IMAGE_ID,
        "manylinux_2_17": MANYLINUX2014_IMAGE_ID,
        "manylinux_2_24": MANYLINUX_2_24_IMAGE_ID,
        "manylinux_2_28": MANYLINUX_2_28_IMAGE_ID,
    }
    POLICY_ALIASES = {
        "manylinux_2_5": ["manylinux1"],
        "manylinux_2_12": ["manylinux2010"],
        "manylinux_2_17": ["manylinux2014"],
    }
else:
    MANYLINUX_IMAGES = {
        "manylinux_2_17": MANYLINUX2014_IMAGE_ID,
        "manylinux_2_24": MANYLINUX_2_24_IMAGE_ID,
    }
    if PLATFORM in {"aarch64", "ppc64le"}:
        MANYLINUX_IMAGES["manylinux_2_28"] = MANYLINUX_2_28_IMAGE_ID
    POLICY_ALIASES = {
        "manylinux_2_17": ["manylinux2014"],
    }
DOCKER_CONTAINER_NAME = "auditwheel-test-anylinux"
PYTHON_MAJ_MIN = [str(i) for i in sys.version_info[:2]]
PYTHON_ABI_MAJ_MIN = "".join(PYTHON_MAJ_MIN)
PYTHON_ABI_FLAGS = "m" if sys.version_info.minor < 8 else ""
PYTHON_ABI = f"cp{PYTHON_ABI_MAJ_MIN}-cp{PYTHON_ABI_MAJ_MIN}{PYTHON_ABI_FLAGS}"
MANYLINUX_PYTHON_IMAGE_ID = f'python:{".".join(PYTHON_MAJ_MIN)}-slim'
MUSLLINUX_IMAGES = {
    "musllinux_1_1": f"quay.io/pypa/musllinux_1_1_{PLATFORM}:latest",
}
MUSLLINUX_PYTHON_IMAGE_ID = f'python:{".".join(PYTHON_MAJ_MIN)}-alpine'
DEVTOOLSET = {
    "manylinux_2_5": "devtoolset-2",
    "manylinux_2_12": "devtoolset-8",
    "manylinux_2_17": "devtoolset-10",
    "manylinux_2_24": "devtoolset-not-present",
    "manylinux_2_28": "gcc-toolset-11",
    "musllinux_1_1": "devtoolset-not-present",
}
PATH_DIRS = [
    f"/opt/python/{PYTHON_ABI}/bin",
    "/opt/rh/{devtoolset}/root/usr/bin",
    "/usr/local/sbin",
    "/usr/local/bin",
    "/usr/sbin",
    "/usr/bin",
    "/sbin",
    "/bin",
]
PATH = {k: ":".join(PATH_DIRS).format(devtoolset=v) for k, v in DEVTOOLSET.items()}
WHEEL_CACHE_FOLDER = op.expanduser("~/.cache/auditwheel_tests")
NUMPY_VERSION_MAP = {
    "36": "1.19.2",
    "37": "1.19.2",
    "38": "1.19.2",
    "39": "1.19.2",
    "310": "1.21.4",
    "313": "2.3.3",
}
NUMPY_VERSION = NUMPY_VERSION_MAP[PYTHON_ABI_MAJ_MIN]
ORIGINAL_NUMPY_WHEEL = f"numpy-{NUMPY_VERSION}-{PYTHON_ABI}-linux_{PLATFORM}.whl"
ORIGINAL_SIX_WHEEL = "six-1.11.0-py2.py3-none-any.whl"
SHOW_RE = re.compile(
    r'[\s](?P<wheel>\S+) is consistent with the following platform tag: "(?P<tag>\S+)"',
    flags=re.DOTALL,
)
TAG_RE = re.compile(r"^manylinux_(?P<major>[0-9]+)_(?P<minor>[0-9]+)_(?P<arch>\S+)$")


def find_src_folder():
    candidate = op.abspath(op.join(op.dirname(__file__), "../.."))
    contents = os.listdir(candidate)
    if "setup.py" in contents and "src" in contents:
        return candidate


def docker_start(image, volumes={}, env_variables={}):
    """Start a long waiting idle program in container

    Return the container object to be used for 'docker exec' commands.
    """
    # Make sure to use the latest public version of the docker image
    client = docker.from_env()

    dvolumes = {host: {"bind": ctr, "mode": "rw"} for (ctr, host) in volumes.items()}

    logger.info("Starting container with image %r", image)
    con = client.containers.run(
        image,
        ["sleep", "10000"],
        detach=True,
        volumes=dvolumes,
        environment=env_variables,
    )
    logger.info("Started container %s", con.id[:12])
    return con


@contextmanager
def docker_container_ctx(image, io_dir=None, env_variables={}):
    src_folder = find_src_folder()
    if src_folder is None:
        pytest.skip("Can only be run from the source folder")
    vols = {"/auditwheel_src": src_folder}
    if io_dir is not None:
        vols["/io"] = io_dir

    for key in env_variables:
        if key.startswith("COV_CORE_"):
            env_variables[key] = env_variables[key].replace(
                src_folder, "/auditwheel_src"
            )

    container = docker_start(image, vols, env_variables)
    try:
        yield container
    finally:
        container.remove(force=True)


def docker_exec(container, cmd, expected_retcode=0):
    logger.info("docker exec %s: %r", container.id[:12], cmd)
    ec, output = container.exec_run(cmd)
    output = output.decode(ENCODING)
    if ec != expected_retcode:
        print(output)
        raise CalledProcessError(ec, cmd, output=output)
    return output


@contextmanager
def tmp_docker_image(base, commands, setup_env={}):
    """Make a temporary docker image for tests

    Pulls the *base* image, runs *commands* inside it with *setup_env*, and
    commits the result as a new image. The image is removed on exiting the
    context.

    Making temporary images like this avoids each test having to re-run the
    same container setup steps.
    """
    with docker_container_ctx(base, env_variables=setup_env) as con:
        for cmd in commands:
            docker_exec(con, cmd)
        image = con.commit()

    logger.info("Made image %s based on %s", image.short_id, base)
    try:
        yield image.short_id
    finally:
        client = image.client
        client.images.remove(image.id)


def assert_show_output(manylinux_ctr, wheel, expected_tag, strict):
    output = docker_exec(manylinux_ctr, f"auditwheel show /io/{wheel}")
    output = output.replace("\n", " ")
    match = SHOW_RE.match(output)
    assert match
    assert match["wheel"] == wheel
    if strict or "musllinux" in expected_tag:
        assert match["tag"] == expected_tag
    else:
        expected_match = TAG_RE.match(expected_tag)
        assert expected_match, f"No match for tag {expected_tag}"
        expected_glibc = (int(expected_match["major"]), int(expected_match["minor"]))
        actual_match = TAG_RE.match(match["tag"])
        assert actual_match, f"No match for tag {match['tag']}"
        actual_glibc = (int(actual_match["major"]), int(actual_match["minor"]))
        assert expected_match["arch"] == actual_match["arch"]
        assert actual_glibc <= expected_glibc


def build_numpy(container, policy, output_dir):
    """Helper to build numpy from source using the specified container, into
    output_dir."""

    if policy.startswith("musllinux_"):
        docker_exec(container, "apk add openblas-dev")
    elif policy.startswith("manylinux_2_24_"):
        docker_exec(container, "apt-get update")
        docker_exec(
            container, "apt-get install -y --no-install-recommends libatlas-dev"
        )
    else:
        docker_exec(container, "yum install -y atlas atlas-devel")

    if op.exists(op.join(WHEEL_CACHE_FOLDER, policy, ORIGINAL_NUMPY_WHEEL)):
        # If numpy has already been built and put in cache, let's reuse this.
        shutil.copy2(
            op.join(WHEEL_CACHE_FOLDER, policy, ORIGINAL_NUMPY_WHEEL),
            op.join(output_dir, ORIGINAL_NUMPY_WHEEL),
        )
    else:
        # otherwise build the original linux_x86_64 numpy wheel from source
        # and put the result in the cache folder to speed-up future build.
        # This part of the build is independent of the auditwheel code-base
        # so it's safe to put it in cache.

        docker_exec(
            container,
            f"pip wheel -w /io --no-binary=:all: numpy=={NUMPY_VERSION}",
        )
        os.makedirs(op.join(WHEEL_CACHE_FOLDER, policy), exist_ok=True)
        shutil.copy2(
            op.join(output_dir, ORIGINAL_NUMPY_WHEEL),
            op.join(WHEEL_CACHE_FOLDER, policy, ORIGINAL_NUMPY_WHEEL),
        )
    orig_wheel, *_ = os.listdir(output_dir)
    return orig_wheel


class Anylinux:
    @pytest.fixture()
    def io_folder(self, tmp_path):
        d = tmp_path / "io"
        d.mkdir(exist_ok=True)
        return str(d)

    @pytest.fixture()
    def docker_python(self, docker_python_img, io_folder):
        with docker_container_ctx(docker_python_img, io_folder) as container:
            yield container

    @pytest.fixture()
    def any_manylinux_container(self, any_manylinux_img, io_folder):
        policy, manylinux_img = any_manylinux_img
        env = {"PATH": PATH[policy]}
        for key in os.environ:
            if key.startswith("COV_CORE_"):
                env[key] = os.environ[key]

        with docker_container_ctx(manylinux_img, io_folder, env) as container:
            platform_tag = ".".join(
                [f"{p}_{PLATFORM}" for p in [policy] + POLICY_ALIASES.get(policy, [])]
            )
            yield f"{policy}_{PLATFORM}", platform_tag, container

    def test_build_repair_numpy(
        self, any_manylinux_container, docker_python, io_folder
    ):
        # Integration test: repair numpy built from scratch
        policy, tag, manylinux_ctr = any_manylinux_container

        # First build numpy from source as a naive linux wheel that is tied
        # to system libraries (atlas, libgfortran...)
        orig_wheel = build_numpy(manylinux_ctr, policy, io_folder)
        assert orig_wheel == ORIGINAL_NUMPY_WHEEL
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --only-plat -w /io /io/{orig_wheel}"
        )
        docker_exec(manylinux_ctr, repair_command)
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"numpy-{NUMPY_VERSION}-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, False)

        # Check that the repaired numpy wheel can be installed and executed
        # on a modern linux image.
        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        output = docker_exec(
            docker_python,
            "python /auditwheel_src/tests/integration/quick_check_numpy.py",
        )
        assert output.strip() == "ok"

        # Check that numpy f2py works with a more recent version of gfortran
        if policy.startswith("musllinux_"):
            docker_exec(docker_python, "apk add musl-dev gfortran")
        else:
            docker_exec(docker_python, "apt-get update -yqq")
            docker_exec(docker_python, "apt-get install -y gfortran")
        docker_exec(
            docker_python,
            "python -m numpy.f2py -c /auditwheel_src/tests/integration/foo.f90 -m foo",
        )

        # Check that the 2 fortran runtimes are well isolated and can be loaded
        # at once in the same Python program:
        docker_exec(docker_python, ["python", "-c", "'import numpy; import foo'"])

    @pytest.mark.skipif(
        PLATFORM != "x86_64", reason="Only needs checking on one platform"
    )
    def test_repair_exclude(self, any_manylinux_container, io_folder):
        """Test the --exclude argument to avoid grafting certain libraries."""

        policy, tag, manylinux_ctr = any_manylinux_container

        orig_wheel = build_numpy(manylinux_ctr, policy, io_folder)
        assert orig_wheel == ORIGINAL_NUMPY_WHEEL
        assert "manylinux" not in orig_wheel

        # Exclude libgfortran from grafting into the wheel
        excludes = {
            "manylinux_2_5_x86_64": ["libgfortran.so.1", "libgfortran.so.3"],
            "manylinux_2_12_x86_64": ["libgfortran.so.3", "libgfortran.so.5"],
            "manylinux_2_17_x86_64": ["libgfortran.so.3", "libgfortran.so.5"],
            "manylinux_2_24_x86_64": ["libgfortran.so.3"],
            "manylinux_2_28_x86_64": ["libgfortran.so.5"],
            "musllinux_1_1_x86_64": ["libgfortran.so.5"],
        }[policy]

        repair_command = [
            "auditwheel",
            "repair",
            "--plat",
            policy,
            "--only-plat",
            "-w",
            "/io",
        ]
        for exclude in excludes:
            repair_command.extend(["--exclude", exclude])
        repair_command.append(f"/io/{orig_wheel}")
        output = docker_exec(manylinux_ctr, repair_command)

        for exclude in excludes:
            assert f"Excluding {exclude}" in output
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"numpy-{NUMPY_VERSION}-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames

        # Make sure we don't have libgfortran in the result
        contents = zipfile.ZipFile(os.path.join(io_folder, repaired_wheel)).namelist()
        assert not any(x for x in contents if "/libgfortran" in x)

    def test_build_wheel_with_binary_executable(
        self, any_manylinux_container, docker_python, io_folder
    ):
        # Test building a wheel that contains a binary executable (e.g., a program)

        policy, tag, manylinux_ctr = any_manylinux_container
        if policy.startswith("musllinux_"):
            docker_exec(manylinux_ctr, "apk add gsl-dev")
        elif policy.startswith("manylinux_2_24_"):
            docker_exec(manylinux_ctr, "apt-get update")
            docker_exec(
                manylinux_ctr, "apt-get install -y --no-install-recommends libgsl-dev"
            )
        else:
            docker_exec(manylinux_ctr, "yum install -y gsl-devel")

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/testpackage && "
                "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                "python -m pip wheel --no-deps -w /io .",
            ],
        )

        filenames = os.listdir(io_folder)
        assert filenames == ["testpackage-0.0.1-py3-none-any.whl"]
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the appropriate manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --only-plat -w /io /io/{orig_wheel}"
        )
        docker_exec(manylinux_ctr, repair_command)
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"testpackage-0.0.1-py3-none-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, False)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        output = docker_exec(
            docker_python,
            ["python", "-c", "from testpackage import runit; print(runit(1.5))"],
        )
        assert output.strip() == "2.25"

    def test_build_repair_pure_wheel(self, any_manylinux_container, io_folder):
        policy, tag, manylinux_ctr = any_manylinux_container

        if op.exists(op.join(WHEEL_CACHE_FOLDER, policy, ORIGINAL_SIX_WHEEL)):
            # If six has already been built and put in cache, let's reuse this.
            shutil.copy2(
                op.join(WHEEL_CACHE_FOLDER, policy, ORIGINAL_SIX_WHEEL),
                op.join(io_folder, ORIGINAL_SIX_WHEEL),
            )
            logger.info(f"Copied six wheel from {WHEEL_CACHE_FOLDER} to {io_folder}")
        else:
            docker_exec(manylinux_ctr, "pip wheel -w /io --no-binary=:all: six==1.11.0")
            os.makedirs(op.join(WHEEL_CACHE_FOLDER, policy), exist_ok=True)
            shutil.copy2(
                op.join(io_folder, ORIGINAL_SIX_WHEEL),
                op.join(WHEEL_CACHE_FOLDER, policy, ORIGINAL_SIX_WHEEL),
            )

        filenames = os.listdir(io_folder)
        assert filenames == [ORIGINAL_SIX_WHEEL]
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the manylinux container
        repair_command = f"auditwheel repair --plat {policy} -w /io /io/{orig_wheel}"
        output = docker_exec(manylinux_ctr, repair_command, expected_retcode=1)
        assert "This does not look like a platform wheel" in output

        output = docker_exec(
            manylinux_ctr, f"auditwheel show /io/{orig_wheel}", expected_retcode=1
        )
        assert "This does not look like a platform wheel" in output

    @pytest.mark.parametrize("dtag", ["rpath", "runpath"])
    def test_build_wheel_depending_on_library_with_rpath(
        self, any_manylinux_container, docker_python, io_folder, dtag
    ):
        # Test building a wheel that contains an extension depending on a library
        # with RPATH or RUNPATH set.
        # Following checks are performed:
        # - check if RUNPATH is replaced by RPATH
        # - check if RPATH location is correct, i.e. it is inside .libs directory
        #   where all gathered libraries are put

        policy, tag, manylinux_ctr = any_manylinux_container

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                (
                    "cd /auditwheel_src/tests/integration/testrpath &&"
                    "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                    f"DTAG={dtag} python setup.py bdist_wheel -d /io"
                ),
            ],
        )
        with open(
            op.join(op.dirname(__file__), "testrpath", "a", "liba.so"), "rb"
        ) as f:
            elf = ELFFile(f)
            dynamic = elf.get_section_by_name(".dynamic")
            tags = {t.entry.d_tag for t in dynamic.iter_tags()}
            assert f"DT_{dtag.upper()}" in tags
        filenames = os.listdir(io_folder)
        assert filenames == [f"testrpath-0.0.1-{PYTHON_ABI}-linux_{PLATFORM}.whl"]
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the appropriate manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --only-plat -w /io /io/{orig_wheel}"
        )
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "LD_LIBRARY_PATH="
                "/auditwheel_src/tests/integration/testrpath/a:$LD_LIBRARY_PATH "
                + repair_command,
            ],
        )
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"testrpath-0.0.1-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, False)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        output = docker_exec(
            docker_python,
            [
                "python",
                "-c",
                "from testrpath import testrpath; print(testrpath.func())",
            ],
        )
        assert output.strip() == "11"
        with zipfile.ZipFile(os.path.join(io_folder, repaired_wheel)) as w:
            for name in w.namelist():
                if "testrpath/.libs/lib" in name:
                    with w.open(name) as f:
                        elf = ELFFile(io.BytesIO(f.read()))
                        dynamic = elf.get_section_by_name(".dynamic")
                        assert (
                            len(
                                [
                                    t
                                    for t in dynamic.iter_tags()
                                    if t.entry.d_tag == "DT_RUNPATH"
                                ]
                            )
                            == 0
                        )
                        if ".libs/liba" in name:
                            rpath_tags = [
                                t
                                for t in dynamic.iter_tags()
                                if t.entry.d_tag == "DT_RPATH"
                            ]
                            assert len(rpath_tags) == 1
                            assert rpath_tags[0].rpath == "$ORIGIN/."

    def test_build_repair_multiple_top_level_modules_wheel(
        self, any_manylinux_container, docker_python, io_folder
    ):

        policy, tag, manylinux_ctr = any_manylinux_container

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/multiple_top_level && "
                "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                "make clean all && "
                "pip wheel . -w /io",
            ],
        )

        filenames = os.listdir(io_folder)
        assert filenames == [
            f"multiple_top_level-1.0-{PYTHON_ABI}-linux_{PLATFORM}.whl"
        ]
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the appropriate manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --only-plat -w /io /io/{orig_wheel}"
        )
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                (
                    "LD_LIBRARY_PATH="
                    "/auditwheel_src/tests/integration/multiple_top_level/lib-src/a:"
                    "/auditwheel_src/tests/integration/multiple_top_level/lib-src/b:"
                    "$LD_LIBRARY_PATH "
                )
                + repair_command,
            ],
        )
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"multiple_top_level-1.0-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, False)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        for mod, func, expected in [
            ("example_a", "example_a", "11"),
            ("example_b", "example_b", "110"),
        ]:
            output = docker_exec(
                docker_python,
                [
                    "python",
                    "-c",
                    f"from {mod} import {func}; print({func}())",
                ],
            ).strip()
            assert output.strip() == expected
        with zipfile.ZipFile(os.path.join(io_folder, repaired_wheel)) as w:
            for lib_name in ["liba", "libb"]:
                assert any(
                    re.match(rf"multiple_top_level.libs/{lib_name}.*\.so", name)
                    for name in w.namelist()
                )

    def test_build_repair_wheel_with_internal_rpath(
        self, any_manylinux_container, docker_python, io_folder
    ):
        policy, tag, manylinux_ctr = any_manylinux_container

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/internal_rpath && "
                "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                "make clean all && "
                "mv lib-src/a/liba.so internal_rpath && "
                "pip wheel . -w /io",
            ],
        )

        filenames = os.listdir(io_folder)
        assert filenames == [f"internal_rpath-1.0-{PYTHON_ABI}-linux_{PLATFORM}.whl"]
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the appropriate manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --only-plat -w /io /io/{orig_wheel}"
        )
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                (
                    "LD_LIBRARY_PATH="
                    "/auditwheel_src/tests/integration/internal_rpath/lib-src/b:"
                    "$LD_LIBRARY_PATH "
                )
                + repair_command,
            ],
        )
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"internal_rpath-1.0-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, False)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        for mod, func, expected in [
            ("example_a", "example_a", "11"),
            ("example_b", "example_b", "10"),
        ]:
            output = docker_exec(
                docker_python,
                [
                    "python",
                    "-c",
                    f"from internal_rpath.{mod} import {func}; print({func}())",
                ],
            ).strip()
            assert output.strip() == expected
        with zipfile.ZipFile(os.path.join(io_folder, repaired_wheel)) as w:
            for lib_name in ["libb"]:
                assert any(
                    re.match(rf"internal_rpath.libs/{lib_name}.*\.so", name)
                    for name in w.namelist()
                )

    def test_strip_wheel(self, any_manylinux_container, docker_python, io_folder):
        policy, tag, manylinux_ctr = any_manylinux_container
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/sample_extension && "
                "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                "python -m pip wheel --no-deps -w /io .",
            ],
        )

        orig_wheel, *_ = os.listdir(io_folder)
        assert orig_wheel.startswith("sample_extension-0.1.0")

        # Repair the wheel using the appropriate manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --strip -w /io /io/{orig_wheel}"
        )
        docker_exec(manylinux_ctr, repair_command)

        repaired_wheel, *_ = glob.glob(f"{io_folder}/*{policy}*.whl")
        repaired_wheel = os.path.basename(repaired_wheel)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        output = docker_exec(
            docker_python,
            [
                "python",
                "-c",
                "from sample_extension import test_func; print(test_func(1))",
            ],
        )
        assert output.strip() == "2"

    def test_nonpy_rpath(self, any_manylinux_container, docker_python, io_folder):
        # Tests https://github.com/pypa/auditwheel/issues/136
        policy, tag, manylinux_ctr = any_manylinux_container
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/nonpy_rpath && "
                "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                "python -m pip wheel --no-deps -w /io .",
            ],
        )

        orig_wheel, *_ = os.listdir(io_folder)
        assert orig_wheel.startswith("nonpy_rpath-0.1.0")
        assert "manylinux" not in orig_wheel

        # Repair the wheel using the appropriate manylinux container
        repair_command = (
            f"auditwheel repair --plat {policy} --only-plat -w /io /io/{orig_wheel}"
        )
        docker_exec(manylinux_ctr, ["bash", "-c", repair_command])
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"nonpy_rpath-0.1.0-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, False)

        # Test the resulting wheel outside the manylinux container
        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        docker_exec(
            docker_python,
            [
                "python",
                "-c",
                "import nonpy_rpath\n"
                "assert nonpy_rpath.crypt_something().startswith('*')",
            ],
        )

    def test_glibcxx_3_4_25(self, any_manylinux_container, docker_python, io_folder):
        policy, tag, manylinux_ctr = any_manylinux_container
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/test_glibcxx_3_4_25 && "
                "if [ -d ./build ]; then rm -rf ./build ./*.egg-info; fi && "
                "python -m pip wheel --no-deps -w /io .",
            ],
        )

        orig_wheel, *_ = os.listdir(io_folder)
        assert orig_wheel.startswith("testentropy-0.0.1")

        # Repair the wheel using the appropriate manylinux container
        repair_command = f"auditwheel repair --plat {policy} -w /io /io/{orig_wheel}"
        if policy.startswith("manylinux_2_28_"):
            with pytest.raises(CalledProcessError):
                docker_exec(manylinux_ctr, repair_command)
            # TODO if a "permissive" mode is implemented, add the relevant flag to the
            # repair_command here and drop the return statement below
            return

        docker_exec(manylinux_ctr, repair_command)

        repaired_wheel, *_ = glob.glob(f"{io_folder}/*{policy}*.whl")
        repaired_wheel = os.path.basename(repaired_wheel)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        docker_exec(
            docker_python,
            [
                "python",
                "-c",
                "from testentropy import run; exit(run())",
            ],
        )


class TestManylinux(Anylinux):
    @pytest.fixture(scope="session")
    def docker_python_img(self):
        """The glibc Python base image with up-to-date pip"""
        with tmp_docker_image(
            MANYLINUX_PYTHON_IMAGE_ID, ["pip install -U pip"]
        ) as img_id:
            yield img_id

    @pytest.fixture(scope="session", params=MANYLINUX_IMAGES.keys())
    def any_manylinux_img(self, request):
        """Each manylinux image, with auditwheel installed.

        Plus up-to-date pip, setuptools and pytest-cov
        """
        policy = request.param
        if policy == "manylinux_2_5" and PYTHON_ABI_MAJ_MIN in {"310"}:
            pytest.skip(f"manylinux_2_5 images do not support cp{PYTHON_ABI_MAJ_MIN}")
        base = MANYLINUX_IMAGES[policy]
        env = {"PATH": PATH[policy]}
        with tmp_docker_image(
            base,
            [
                'git config --global --add safe.directory "/auditwheel_src"',
                "pip install -U pip setuptools pytest-cov",
                "pip install -U -e /auditwheel_src",
            ],
            env,
        ) as img_id:
            yield policy, img_id

    @pytest.mark.parametrize("with_dependency", ["0", "1"])
    def test_build_wheel_with_image_dependencies(
        self, with_dependency, any_manylinux_container, docker_python, io_folder
    ):
        # try to repair the wheel targeting different policies
        #
        # with_dependency == 0
        #   The python module has no dependencies that should be grafted-in and
        #   uses versioned symbols not available on policies pre-dating the policy
        #   matching the image being tested.
        # with_dependency == 1
        #   The python module itself does not use versioned symbols but has a
        #   dependency that should be grafted-in that uses versioned symbols not
        #   available on policies pre-dating the policy matching the image being
        #   tested.

        policy, tag, manylinux_ctr = any_manylinux_container

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/testdependencies && "
                f"WITH_DEPENDENCY={with_dependency} python setup.py -v build_ext -f "
                "bdist_wheel -d /io",
            ],
        )

        filenames = os.listdir(io_folder)
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        repair_command = (
            "LD_LIBRARY_PATH="
            "/auditwheel_src/tests/integration/testdependencies:$LD_LIBRARY_PATH "
            "auditwheel -v repair --plat {policy} -w /io /io/{orig_wheel}"
        )

        policy_priority = get_priority_by_name(policy)
        older_policies = [
            f"{p}_{PLATFORM}"
            for p in MANYLINUX_IMAGES.keys()
            if policy_priority < get_priority_by_name(f"{p}_{PLATFORM}")
        ]
        for target_policy in older_policies:
            # we shall fail to repair the wheel when targeting an older policy than
            # the one matching the image
            with pytest.raises(CalledProcessError):
                docker_exec(
                    manylinux_ctr,
                    [
                        "bash",
                        "-c",
                        repair_command.format(
                            policy=target_policy, orig_wheel=orig_wheel
                        ),
                    ],
                )

        # check all works properly when targeting the policy matching the image
        docker_exec(
            manylinux_ctr,
            ["bash", "-c", repair_command.format(policy=policy, orig_wheel=orig_wheel)],
        )
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        repaired_wheel = f"testdependencies-0.0.1-{PYTHON_ABI}-{tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, policy, True)

        # check the original wheel with a dependency was not compliant
        # and check the one without a dependency was already compliant
        if with_dependency == "1":
            expected = f"linux_{PLATFORM}"
        else:
            expected = policy
        assert_show_output(manylinux_ctr, orig_wheel, expected, True)

        docker_exec(docker_python, "pip install /io/" + repaired_wheel)
        docker_exec(
            docker_python,
            [
                "python",
                "-c",
                "from sys import exit; from testdependencies import run; exit(run())",
            ],
        )

    @pytest.mark.parametrize(
        "target_policy",
        [f"{p}_{PLATFORM}" for p in list(MANYLINUX_IMAGES.keys())]
        + [f"{p}_{PLATFORM}" for aliases in POLICY_ALIASES.values() for p in aliases],
    )
    @pytest.mark.parametrize("only_plat", [True, False])
    def test_build_wheel_compat(
        self,
        target_policy,
        only_plat,
        any_manylinux_container,
        docker_python,
        io_folder,
    ):
        # test building wheels with compatibility with older spec
        # check aliases for older spec

        policy, tag, manylinux_ctr = any_manylinux_container

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/testsimple "
                "&& python -m pip wheel --no-deps -w /io .",
            ],
        )

        filenames = os.listdir(io_folder)
        orig_wheel = filenames[0]
        assert "manylinux" not in orig_wheel

        if PLATFORM in {"x86_64", "i686"}:
            expect = f"manylinux_2_5_{PLATFORM}"
            expect_tag = f"manylinux_2_5_{PLATFORM}.manylinux1_{PLATFORM}"
        else:
            expect = f"manylinux_2_17_{PLATFORM}"
            expect_tag = f"manylinux_2_17_{PLATFORM}.manylinux2014_{PLATFORM}"

        target_tag = target_policy
        for pep600_policy, aliases in POLICY_ALIASES.items():
            policy_ = f"{pep600_policy}_{PLATFORM}"
            aliases_ = [f"{p}_{PLATFORM}" for p in aliases]
            if target_policy == policy_ or target_policy in aliases_:
                target_tag = f'{policy_}.{".".join(aliases_)}'

        only_plat_arg = "--only-plat" if only_plat else ""
        # we shall ba able to repair the wheel for all targets
        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                f"auditwheel -v repair --plat {target_policy} {only_plat_arg} -w /io"
                f" /io/{orig_wheel}",
            ],
        )
        filenames = os.listdir(io_folder)
        assert len(filenames) == 2
        if only_plat or target_tag == expect_tag:
            repaired_tag = target_tag
        else:
            repaired_tag = f"{expect_tag}.{target_tag}"
        repaired_wheel = f"testsimple-0.0.1-{PYTHON_ABI}-{repaired_tag}.whl"
        assert repaired_wheel in filenames
        assert_show_output(manylinux_ctr, repaired_wheel, expect, True)

        docker_exec(docker_python, f"pip install /io/{repaired_wheel}")
        docker_exec(
            docker_python,
            [
                "python",
                "-c",
                "from sys import exit; from testsimple import run; exit(run())",
            ],
        )

    def test_zlib_blacklist(self, any_manylinux_container, docker_python, io_folder):
        policy, tag, manylinux_ctr = any_manylinux_container
        if policy.startswith("manylinux_2_17_"):
            pytest.skip(f"{policy} image has no blacklist symbols in libz.so.1")

        docker_exec(
            manylinux_ctr,
            [
                "bash",
                "-c",
                "cd /auditwheel_src/tests/integration/testzlib "
                "&& python -m pip wheel --no-deps -w /io .",
            ],
        )

        orig_wheel, *_ = os.listdir(io_folder)
        assert orig_wheel.startswith("testzlib-0.0.1")

        # Repair the wheel using the appropriate manylinux container
        repair_command = f"auditwheel repair --plat {policy} -w /io /io/{orig_wheel}"
        with pytest.raises(CalledProcessError):
            docker_exec(manylinux_ctr, repair_command)

        # Check auditwheel show warns about the black listed symbols
        output = docker_exec(manylinux_ctr, f"auditwheel -v show /io/{orig_wheel}")
        assert "black-listed symbol dependencies" in output.replace("\n", " ")


class TestMusllinux(Anylinux):
    @pytest.fixture(scope="session")
    def docker_python_img(self):
        """The alpine Python base image with up-to-date pip"""
        with tmp_docker_image(
            MUSLLINUX_PYTHON_IMAGE_ID, ["pip install -U pip"]
        ) as img_id:
            yield img_id

    @pytest.fixture(scope="session", params=MUSLLINUX_IMAGES.keys())
    def any_manylinux_img(self, request):
        """Each musllinux image, with auditwheel installed.

        Plus up-to-date pip, setuptools and pytest-cov
        """
        policy = request.param
        base = MUSLLINUX_IMAGES[policy]
        env = {"PATH": PATH[policy]}
        with tmp_docker_image(
            base,
            [
                'git config --global --add safe.directory "/auditwheel_src"',
                "pip install -U pip setuptools pytest-cov",
                "pip install -U -e /auditwheel_src",
            ],
            env,
        ) as img_id:
            yield policy, img_id