summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_internal/commands/sanity/__init__.py
blob: 306951109111939f2b0db0083f9cca2f8d965bea (plain)
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
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
"""Execute Ansible sanity tests."""
from __future__ import annotations

import abc
import glob
import hashlib
import json
import os
import pathlib
import re
import collections
import typing as t

from ...constants import (
    CONTROLLER_PYTHON_VERSIONS,
    REMOTE_ONLY_PYTHON_VERSIONS,
    SUPPORTED_PYTHON_VERSIONS,
)

from ...encoding import (
    to_bytes,
)

from ...io import (
    read_json_file,
    write_json_file,
    write_text_file,
)

from ...util import (
    ApplicationError,
    SubprocessError,
    display,
    import_plugins,
    load_plugins,
    parse_to_list_of_dict,
    ANSIBLE_TEST_CONTROLLER_ROOT,
    ANSIBLE_TEST_TARGET_ROOT,
    is_binary_file,
    read_lines_without_comments,
    is_subdir,
    paths_to_dirs,
    get_ansible_version,
    str_to_version,
    cache,
    remove_tree,
)

from ...util_common import (
    intercept_python,
    handle_layout_messages,
    yamlcheck,
    create_result_directories,
)

from ...ansible_util import (
    ansible_environment,
)

from ...target import (
    walk_internal_targets,
    walk_sanity_targets,
    TestTarget,
)

from ...executor import (
    get_changes_filter,
    AllTargetsSkipped,
    Delegate,
)

from ...python_requirements import (
    PipInstall,
    collect_requirements,
    run_pip,
)

from ...config import (
    SanityConfig,
)

from ...test import (
    TestSuccess,
    TestFailure,
    TestSkipped,
    TestMessage,
    TestResult,
    calculate_best_confidence,
)

from ...data import (
    data_context,
)

from ...content_config import (
    get_content_config,
)

from ...host_configs import (
    DockerConfig,
    PosixConfig,
    PythonConfig,
    VirtualPythonConfig,
)

from ...host_profiles import (
    PosixProfile,
)

from ...provisioning import (
    prepare_profiles,
)

from ...pypi_proxy import (
    configure_pypi_proxy,
)

from ...venv import (
    create_virtual_environment,
)

COMMAND = 'sanity'
SANITY_ROOT = os.path.join(ANSIBLE_TEST_CONTROLLER_ROOT, 'sanity')
TARGET_SANITY_ROOT = os.path.join(ANSIBLE_TEST_TARGET_ROOT, 'sanity')

# NOTE: must match ansible.constants.DOCUMENTABLE_PLUGINS, but with 'module' replaced by 'modules'!
DOCUMENTABLE_PLUGINS = (
    'become', 'cache', 'callback', 'cliconf', 'connection', 'httpapi', 'inventory', 'lookup', 'netconf', 'modules', 'shell', 'strategy', 'vars'
)

created_venvs = []  # type: t.List[str]


def command_sanity(args):  # type: (SanityConfig) -> None
    """Run sanity tests."""
    create_result_directories(args)

    target_configs = t.cast(t.List[PosixConfig], args.targets)
    target_versions = {target.python.version: target for target in target_configs}  # type: t.Dict[str, PosixConfig]

    handle_layout_messages(data_context().content.sanity_messages)

    changes = get_changes_filter(args)
    require = args.require + changes
    targets = SanityTargets.create(args.include, args.exclude, require)

    if not targets.include:
        raise AllTargetsSkipped()

    tests = list(sanity_get_tests())

    if args.test:
        disabled = []
        tests = [target for target in tests if target.name in args.test]
    else:
        disabled = [target.name for target in tests if not target.enabled and not args.allow_disabled]
        tests = [target for target in tests if target.enabled or args.allow_disabled]

    if args.skip_test:
        tests = [target for target in tests if target.name not in args.skip_test]

    targets_use_pypi = any(isinstance(test, SanityMultipleVersion) and test.needs_pypi for test in tests) and not args.list_tests
    host_state = prepare_profiles(args, targets_use_pypi=targets_use_pypi)  # sanity

    get_content_config(args)  # make sure content config has been parsed prior to delegation

    if args.delegate:
        raise Delegate(host_state=host_state, require=changes, exclude=args.exclude)

    configure_pypi_proxy(args, host_state.controller_profile)  # sanity

    if disabled:
        display.warning('Skipping tests disabled by default without --allow-disabled: %s' % ', '.join(sorted(disabled)))

    target_profiles = {profile.config.python.version: profile for profile in host_state.targets(PosixProfile)}  # type: t.Dict[str, PosixProfile]

    total = 0
    failed = []

    result: t.Optional[TestResult]

    for test in tests:
        if args.list_tests:
            print(test.name)  # display goes to stderr, this should be on stdout
            continue

        for version in SUPPORTED_PYTHON_VERSIONS:
            options = ''

            if isinstance(test, SanityMultipleVersion):
                if version not in target_versions and version not in args.host_settings.skipped_python_versions:
                    continue  # version was not requested, skip it silently
            else:
                if version != args.controller_python.version:
                    continue  # only multi-version sanity tests use target versions, the rest use the controller version

            if test.supported_python_versions and version not in test.supported_python_versions:
                result = SanitySkipped(test.name, version)
                result.reason = f'Skipping sanity test "{test.name}" on Python {version} because it is unsupported.' \
                                f' Supported Python versions: {", ".join(test.supported_python_versions)}'
            else:
                if isinstance(test, SanityCodeSmellTest):
                    settings = test.load_processor(args)
                elif isinstance(test, SanityMultipleVersion):
                    settings = test.load_processor(args, version)
                elif isinstance(test, SanitySingleVersion):
                    settings = test.load_processor(args)
                elif isinstance(test, SanityVersionNeutral):
                    settings = test.load_processor(args)
                else:
                    raise Exception('Unsupported test type: %s' % type(test))

                all_targets = list(targets.targets)

                if test.all_targets:
                    usable_targets = list(targets.targets)
                elif test.no_targets:
                    usable_targets = []
                else:
                    usable_targets = list(targets.include)

                all_targets = SanityTargets.filter_and_inject_targets(test, all_targets)
                usable_targets = SanityTargets.filter_and_inject_targets(test, usable_targets)

                usable_targets = sorted(test.filter_targets_by_version(args, list(usable_targets), version))
                usable_targets = settings.filter_skipped_targets(usable_targets)
                sanity_targets = SanityTargets(tuple(all_targets), tuple(usable_targets))

                test_needed = bool(usable_targets or test.no_targets or args.prime_venvs)
                result = None

                if test_needed and version in args.host_settings.skipped_python_versions:
                    # Deferred checking of Python availability. Done here since it is now known to be required for running the test.
                    # Earlier checking could cause a spurious warning to be generated for a collection which does not support the Python version.
                    # If the user specified a Python version, an error will be generated before reaching this point when the Python interpreter is not found.
                    result = SanitySkipped(test.name, version)
                    result.reason = f'Skipping sanity test "{test.name}" on Python {version} because it could not be found.'

                if not result:
                    if isinstance(test, SanityMultipleVersion):
                        display.info(f'Running sanity test "{test.name}" on Python {version}')
                    else:
                        display.info(f'Running sanity test "{test.name}"')

                if test_needed and not result:
                    if isinstance(test, SanityMultipleVersion):
                        # multi-version sanity tests handle their own requirements (if any) and use the target python
                        test_profile = target_profiles[version]
                        result = test.test(args, sanity_targets, test_profile.python)
                        options = ' --python %s' % version
                    elif isinstance(test, SanitySingleVersion):
                        # single version sanity tests use the controller python
                        test_profile = host_state.controller_profile
                        virtualenv_python = create_sanity_virtualenv(args, test_profile.python, test.name)

                        if virtualenv_python:
                            virtualenv_yaml = check_sanity_virtualenv_yaml(virtualenv_python)

                            if test.require_libyaml and not virtualenv_yaml:
                                result = SanitySkipped(test.name)
                                result.reason = f'Skipping sanity test "{test.name}" on Python {version} due to missing libyaml support in PyYAML.'
                            else:
                                if virtualenv_yaml is False:
                                    display.warning(f'Sanity test "{test.name}" on Python {version} may be slow due to missing libyaml support in PyYAML.')

                                if args.prime_venvs:
                                    result = SanitySkipped(test.name)
                                else:
                                    result = test.test(args, sanity_targets, virtualenv_python)
                        else:
                            result = SanitySkipped(test.name, version)
                            result.reason = f'Skipping sanity test "{test.name}" on Python {version} due to missing virtual environment support.'
                    elif isinstance(test, SanityVersionNeutral):
                        if args.prime_venvs:
                            result = SanitySkipped(test.name)
                        else:
                            # version neutral sanity tests handle their own requirements (if any)
                            result = test.test(args, sanity_targets)
                    else:
                        raise Exception('Unsupported test type: %s' % type(test))
                elif result:
                    pass
                else:
                    result = SanitySkipped(test.name, version)

            result.write(args)

            total += 1

            if isinstance(result, SanityFailure):
                failed.append(result.test + options)

    controller = args.controller

    if created_venvs and isinstance(controller, DockerConfig) and controller.name == 'default' and not args.prime_venvs:
        names = ', '.join(created_venvs)
        display.warning(f'There following sanity test virtual environments are out-of-date in the "default" container: {names}')

    if failed:
        message = 'The %d sanity test(s) listed below (out of %d) failed. See error output above for details.\n%s' % (
            len(failed), total, '\n'.join(failed))

        if args.failure_ok:
            display.error(message)
        else:
            raise ApplicationError(message)


@cache
def collect_code_smell_tests():  # type: () -> t.Tuple[SanityTest, ...]
    """Return a tuple of available code smell sanity tests."""
    paths = glob.glob(os.path.join(SANITY_ROOT, 'code-smell', '*.py'))

    if data_context().content.is_ansible:
        # include Ansible specific code-smell tests which are not configured to be skipped
        ansible_code_smell_root = os.path.join(data_context().content.root, 'test', 'sanity', 'code-smell')
        skip_tests = read_lines_without_comments(os.path.join(ansible_code_smell_root, 'skip.txt'), remove_blank_lines=True, optional=True)
        paths.extend(path for path in glob.glob(os.path.join(ansible_code_smell_root, '*.py')) if os.path.basename(path) not in skip_tests)

    tests = tuple(SanityCodeSmellTest(p) for p in paths)

    return tests


class SanityIgnoreParser:
    """Parser for the consolidated sanity test ignore file."""
    NO_CODE = '_'

    def __init__(self, args):  # type: (SanityConfig) -> None
        if data_context().content.collection:
            ansible_version = '%s.%s' % tuple(get_ansible_version().split('.')[:2])

            ansible_label = 'Ansible %s' % ansible_version
            file_name = 'ignore-%s.txt' % ansible_version
        else:
            ansible_label = 'Ansible'
            file_name = 'ignore.txt'

        self.args = args
        self.relative_path = os.path.join(data_context().content.sanity_path, file_name)
        self.path = os.path.join(data_context().content.root, self.relative_path)
        self.ignores = collections.defaultdict(lambda: collections.defaultdict(dict))  # type: t.Dict[str, t.Dict[str, t.Dict[str, int]]]
        self.skips = collections.defaultdict(lambda: collections.defaultdict(int))  # type: t.Dict[str, t.Dict[str, int]]
        self.parse_errors = []  # type: t.List[t.Tuple[int, int, str]]
        self.file_not_found_errors = []  # type: t.List[t.Tuple[int, str]]

        lines = read_lines_without_comments(self.path, optional=True)
        targets = SanityTargets.get_targets()
        paths = set(target.path for target in targets)
        tests_by_name = {}  # type: t.Dict[str, SanityTest]
        versioned_test_names = set()  # type: t.Set[str]
        unversioned_test_names = {}  # type: t.Dict[str, str]
        directories = paths_to_dirs(list(paths))
        paths_by_test = {}  # type: t.Dict[str, t.Set[str]]

        display.info('Read %d sanity test ignore line(s) for %s from: %s' % (len(lines), ansible_label, self.relative_path), verbosity=1)

        for test in sanity_get_tests():
            test_targets = SanityTargets.filter_and_inject_targets(test, targets)

            if isinstance(test, SanityMultipleVersion):
                versioned_test_names.add(test.name)

                for python_version in test.supported_python_versions:
                    test_name = '%s-%s' % (test.name, python_version)

                    paths_by_test[test_name] = set(target.path for target in test.filter_targets_by_version(args, test_targets, python_version))
                    tests_by_name[test_name] = test
            else:
                unversioned_test_names.update(dict(('%s-%s' % (test.name, python_version), test.name) for python_version in SUPPORTED_PYTHON_VERSIONS))

                paths_by_test[test.name] = set(target.path for target in test.filter_targets_by_version(args, test_targets, ''))
                tests_by_name[test.name] = test

        for line_no, line in enumerate(lines, start=1):
            if not line:
                self.parse_errors.append((line_no, 1, "Line cannot be empty or contain only a comment"))
                continue

            parts = line.split(' ')
            path = parts[0]
            codes = parts[1:]

            if not path:
                self.parse_errors.append((line_no, 1, "Line cannot start with a space"))
                continue

            if path.endswith(os.path.sep):
                if path not in directories:
                    self.file_not_found_errors.append((line_no, path))
                    continue
            else:
                if path not in paths:
                    self.file_not_found_errors.append((line_no, path))
                    continue

            if not codes:
                self.parse_errors.append((line_no, len(path), "Error code required after path"))
                continue

            code = codes[0]

            if not code:
                self.parse_errors.append((line_no, len(path) + 1, "Error code after path cannot be empty"))
                continue

            if len(codes) > 1:
                self.parse_errors.append((line_no, len(path) + len(code) + 2, "Error code cannot contain spaces"))
                continue

            parts = code.split('!')
            code = parts[0]
            commands = parts[1:]

            parts = code.split(':')
            test_name = parts[0]
            error_codes = parts[1:]

            test = tests_by_name.get(test_name)

            if not test:
                unversioned_name = unversioned_test_names.get(test_name)

                if unversioned_name:
                    self.parse_errors.append((line_no, len(path) + len(unversioned_name) + 2, "Sanity test '%s' cannot use a Python version like '%s'" % (
                        unversioned_name, test_name)))
                elif test_name in versioned_test_names:
                    self.parse_errors.append((line_no, len(path) + len(test_name) + 1, "Sanity test '%s' requires a Python version like '%s-%s'" % (
                        test_name, test_name, args.controller_python.version)))
                else:
                    self.parse_errors.append((line_no, len(path) + 2, "Sanity test '%s' does not exist" % test_name))

                continue

            if path.endswith(os.path.sep) and not test.include_directories:
                self.parse_errors.append((line_no, 1, "Sanity test '%s' does not support directory paths" % test_name))
                continue

            if path not in paths_by_test[test_name] and not test.no_targets:
                self.parse_errors.append((line_no, 1, "Sanity test '%s' does not test path '%s'" % (test_name, path)))
                continue

            if commands and error_codes:
                self.parse_errors.append((line_no, len(path) + len(test_name) + 2, "Error code cannot contain both '!' and ':' characters"))
                continue

            if commands:
                command = commands[0]

                if len(commands) > 1:
                    self.parse_errors.append((line_no, len(path) + len(test_name) + len(command) + 3, "Error code cannot contain multiple '!' characters"))
                    continue

                if command == 'skip':
                    if not test.can_skip:
                        self.parse_errors.append((line_no, len(path) + len(test_name) + 2, "Sanity test '%s' cannot be skipped" % test_name))
                        continue

                    existing_line_no = self.skips.get(test_name, {}).get(path)

                    if existing_line_no:
                        self.parse_errors.append((line_no, 1, "Duplicate '%s' skip for path '%s' first found on line %d" % (test_name, path, existing_line_no)))
                        continue

                    self.skips[test_name][path] = line_no
                    continue

                self.parse_errors.append((line_no, len(path) + len(test_name) + 2, "Command '!%s' not recognized" % command))
                continue

            if not test.can_ignore:
                self.parse_errors.append((line_no, len(path) + 1, "Sanity test '%s' cannot be ignored" % test_name))
                continue

            if test.error_code:
                if not error_codes:
                    self.parse_errors.append((line_no, len(path) + len(test_name) + 1, "Sanity test '%s' requires an error code" % test_name))
                    continue

                error_code = error_codes[0]

                if len(error_codes) > 1:
                    self.parse_errors.append((line_no, len(path) + len(test_name) + len(error_code) + 3, "Error code cannot contain multiple ':' characters"))
                    continue

                if error_code in test.optional_error_codes:
                    self.parse_errors.append((line_no, len(path) + len(test_name) + 3, "Optional error code '%s' cannot be ignored" % (
                        error_code)))
                    continue
            else:
                if error_codes:
                    self.parse_errors.append((line_no, len(path) + len(test_name) + 2, "Sanity test '%s' does not support error codes" % test_name))
                    continue

                error_code = self.NO_CODE

            existing = self.ignores.get(test_name, {}).get(path, {}).get(error_code)

            if existing:
                if test.error_code:
                    self.parse_errors.append((line_no, 1, "Duplicate '%s' ignore for error code '%s' for path '%s' first found on line %d" % (
                        test_name, error_code, path, existing)))
                else:
                    self.parse_errors.append((line_no, 1, "Duplicate '%s' ignore for path '%s' first found on line %d" % (
                        test_name, path, existing)))

                continue

            self.ignores[test_name][path][error_code] = line_no

    @staticmethod
    def load(args):  # type: (SanityConfig) -> SanityIgnoreParser
        """Return the current SanityIgnore instance, initializing it if needed."""
        try:
            return SanityIgnoreParser.instance  # type: ignore[attr-defined]
        except AttributeError:
            pass

        instance = SanityIgnoreParser(args)

        SanityIgnoreParser.instance = instance  # type: ignore[attr-defined]

        return instance


class SanityIgnoreProcessor:
    """Processor for sanity test ignores for a single run of one sanity test."""
    def __init__(self,
                 args,  # type: SanityConfig
                 test,  # type: SanityTest
                 python_version,  # type: t.Optional[str]
                 ):  # type: (...) -> None
        name = test.name
        code = test.error_code

        if python_version:
            full_name = '%s-%s' % (name, python_version)
        else:
            full_name = name

        self.args = args
        self.test = test
        self.code = code
        self.parser = SanityIgnoreParser.load(args)
        self.ignore_entries = self.parser.ignores.get(full_name, {})
        self.skip_entries = self.parser.skips.get(full_name, {})
        self.used_line_numbers = set()  # type: t.Set[int]

    def filter_skipped_targets(self, targets):  # type: (t.List[TestTarget]) -> t.List[TestTarget]
        """Return the given targets, with any skipped paths filtered out."""
        return sorted(target for target in targets if target.path not in self.skip_entries)

    def process_errors(self, errors, paths):  # type: (t.List[SanityMessage], t.List[str]) -> t.List[SanityMessage]
        """Return the given errors filtered for ignores and with any settings related errors included."""
        errors = self.filter_messages(errors)
        errors.extend(self.get_errors(paths))

        errors = sorted(set(errors))

        return errors

    def filter_messages(self, messages):  # type: (t.List[SanityMessage]) -> t.List[SanityMessage]
        """Return a filtered list of the given messages using the entries that have been loaded."""
        filtered = []

        for message in messages:
            if message.code in self.test.optional_error_codes and not self.args.enable_optional_errors:
                continue

            path_entry = self.ignore_entries.get(message.path)

            if path_entry:
                code = message.code if self.code else SanityIgnoreParser.NO_CODE
                line_no = path_entry.get(code)

                if line_no:
                    self.used_line_numbers.add(line_no)
                    continue

            filtered.append(message)

        return filtered

    def get_errors(self, paths):  # type: (t.List[str]) -> t.List[SanityMessage]
        """Return error messages related to issues with the file."""
        messages = []  # type: t.List[SanityMessage]

        # unused errors

        unused = []  # type: t.List[t.Tuple[int, str, str]]

        if self.test.no_targets or self.test.all_targets:
            # tests which do not accept a target list, or which use all targets, always return all possible errors, so all ignores can be checked
            targets = SanityTargets.get_targets()
            test_targets = SanityTargets.filter_and_inject_targets(self.test, targets)
            paths = [target.path for target in test_targets]

        for path in paths:
            path_entry = self.ignore_entries.get(path)

            if not path_entry:
                continue

            unused.extend((line_no, path, code) for code, line_no in path_entry.items() if line_no not in self.used_line_numbers)

        messages.extend(SanityMessage(
            code=self.code,
            message="Ignoring '%s' on '%s' is unnecessary" % (code, path) if self.code else "Ignoring '%s' is unnecessary" % path,
            path=self.parser.relative_path,
            line=line,
            column=1,
            confidence=calculate_best_confidence(((self.parser.path, line), (path, 0)), self.args.metadata) if self.args.metadata.changes else None,
        ) for line, path, code in unused)

        return messages


class SanitySuccess(TestSuccess):
    """Sanity test success."""
    def __init__(self, test, python_version=None):  # type: (str, t.Optional[str]) -> None
        super().__init__(COMMAND, test, python_version)


class SanitySkipped(TestSkipped):
    """Sanity test skipped."""
    def __init__(self, test, python_version=None):  # type: (str, t.Optional[str]) -> None
        super().__init__(COMMAND, test, python_version)


class SanityFailure(TestFailure):
    """Sanity test failure."""
    def __init__(
            self,
            test,  # type: str
            python_version=None,  # type: t.Optional[str]
            messages=None,  # type: t.Optional[t.Sequence[SanityMessage]]
            summary=None,  # type: t.Optional[str]
    ):  # type: (...) -> None
        super().__init__(COMMAND, test, python_version, messages, summary)


class SanityMessage(TestMessage):
    """Single sanity test message for one file."""


class SanityTargets:
    """Sanity test target information."""
    def __init__(self, targets, include):  # type: (t.Tuple[TestTarget, ...], t.Tuple[TestTarget, ...]) -> None
        self.targets = targets
        self.include = include

    @staticmethod
    def create(include, exclude, require):  # type: (t.List[str], t.List[str], t.List[str]) -> SanityTargets
        """Create a SanityTargets instance from the given include, exclude and require lists."""
        _targets = SanityTargets.get_targets()
        _include = walk_internal_targets(_targets, include, exclude, require)
        return SanityTargets(_targets, _include)

    @staticmethod
    def filter_and_inject_targets(test, targets):  # type: (SanityTest, t.Iterable[TestTarget]) -> t.List[TestTarget]
        """Filter and inject targets based on test requirements and the given target list."""
        test_targets = list(targets)

        if not test.include_symlinks:
            # remove all symlinks unless supported by the test
            test_targets = [target for target in test_targets if not target.symlink]

        if not test.include_directories or not test.include_symlinks:
            # exclude symlinked directories unless supported by the test
            test_targets = [target for target in test_targets if not target.path.endswith(os.path.sep)]

        if test.include_directories:
            # include directories containing any of the included files
            test_targets += tuple(TestTarget(path, None, None, '') for path in paths_to_dirs([target.path for target in test_targets]))

            if not test.include_symlinks:
                # remove all directory symlinks unless supported by the test
                test_targets = [target for target in test_targets if not target.symlink]

        return test_targets

    @staticmethod
    def get_targets():  # type: () -> t.Tuple[TestTarget, ...]
        """Return a tuple of sanity test targets. Uses a cached version when available."""
        try:
            return SanityTargets.get_targets.targets  # type: ignore[attr-defined]
        except AttributeError:
            targets = tuple(sorted(walk_sanity_targets()))

        SanityTargets.get_targets.targets = targets  # type: ignore[attr-defined]

        return targets


class SanityTest(metaclass=abc.ABCMeta):
    """Sanity test base class."""
    ansible_only = False

    def __init__(self, name=None):  # type: (t.Optional[str]) -> None
        if not name:
            name = self.__class__.__name__
            name = re.sub(r'Test$', '', name)  # drop Test suffix
            name = re.sub(r'(.)([A-Z][a-z]+)', r'\1-\2', name).lower()  # use dashes instead of capitalization

        self.name = name
        self.enabled = True

        # Optional error codes represent errors which spontaneously occur without changes to the content under test, such as those based on the current date.
        # Because these errors can be unpredictable they behave differently than normal error codes:
        #  * They are not reported by default. The `--enable-optional-errors` option must be used to display these errors.
        #  * They cannot be ignored. This is done to maintain the integrity of the ignore system.
        self.optional_error_codes = set()  # type: t.Set[str]

    @property
    def error_code(self):  # type: () -> t.Optional[str]
        """Error code for ansible-test matching the format used by the underlying test program, or None if the program does not use error codes."""
        return None

    @property
    def can_ignore(self):  # type: () -> bool
        """True if the test supports ignore entries."""
        return True

    @property
    def can_skip(self):  # type: () -> bool
        """True if the test supports skip entries."""
        return not self.all_targets and not self.no_targets

    @property
    def all_targets(self):  # type: () -> bool
        """True if test targets will not be filtered using includes, excludes, requires or changes. Mutually exclusive with no_targets."""
        return False

    @property
    def no_targets(self):  # type: () -> bool
        """True if the test does not use test targets. Mutually exclusive with all_targets."""
        return False

    @property
    def include_directories(self):  # type: () -> bool
        """True if the test targets should include directories."""
        return False

    @property
    def include_symlinks(self):  # type: () -> bool
        """True if the test targets should include symlinks."""
        return False

    @property
    def py2_compat(self):  # type: () -> bool
        """True if the test only applies to code that runs on Python 2.x."""
        return False

    @property
    def supported_python_versions(self):  # type: () -> t.Optional[t.Tuple[str, ...]]
        """A tuple of supported Python versions or None if the test does not depend on specific Python versions."""
        return CONTROLLER_PYTHON_VERSIONS

    def filter_targets(self, targets):  # type: (t.List[TestTarget]) -> t.List[TestTarget]  # pylint: disable=unused-argument
        """Return the given list of test targets, filtered to include only those relevant for the test."""
        if self.no_targets:
            return []

        raise NotImplementedError('Sanity test "%s" must implement "filter_targets" or set "no_targets" to True.' % self.name)

    def filter_targets_by_version(self, args, targets, python_version):  # type: (SanityConfig, t.List[TestTarget], str) -> t.List[TestTarget]
        """Return the given list of test targets, filtered to include only those relevant for the test, taking into account the Python version."""
        del python_version  # python_version is not used here, but derived classes may make use of it

        targets = self.filter_targets(targets)

        if self.py2_compat:
            # This sanity test is a Python 2.x compatibility test.
            content_config = get_content_config(args)

            if content_config.py2_support:
                # This collection supports Python 2.x.
                # Filter targets to include only those that require support for remote-only Python versions.
                targets = self.filter_remote_targets(targets)
            else:
                # This collection does not support Python 2.x.
                # There are no targets to test.
                targets = []

        return targets

    @staticmethod
    def filter_remote_targets(targets):  # type: (t.List[TestTarget]) -> t.List[TestTarget]
        """Return a filtered list of the given targets, including only those that require support for remote-only Python versions."""
        targets = [target for target in targets if (
            is_subdir(target.path, data_context().content.module_path) or
            is_subdir(target.path, data_context().content.module_utils_path) or
            is_subdir(target.path, data_context().content.unit_module_path) or
            is_subdir(target.path, data_context().content.unit_module_utils_path) or
            # include modules/module_utils within integration test library directories
            re.search('^%s/.*/library/' % re.escape(data_context().content.integration_targets_path), target.path) or
            # special handling for content in ansible-core
            (data_context().content.is_ansible and (
                # utility code that runs in target environments and requires support for remote-only Python versions
                is_subdir(target.path, 'test/lib/ansible_test/_util/target/') or
                # integration test support modules/module_utils continue to require support for remote-only Python versions
                re.search('^test/support/integration/.*/(modules|module_utils)/', target.path) or
                # collection loader requires support for remote-only Python versions
                re.search('^lib/ansible/utils/collection_loader/', target.path)
            ))
        )]

        return targets


class SanitySingleVersion(SanityTest, metaclass=abc.ABCMeta):
    """Base class for sanity test plugins which should run on a single python version."""
    @property
    def require_libyaml(self):  # type: () -> bool
        """True if the test requires PyYAML to have libyaml support."""
        return False

    @abc.abstractmethod
    def test(self, args, targets, python):  # type: (SanityConfig, SanityTargets, PythonConfig) -> TestResult
        """Run the sanity test and return the result."""

    def load_processor(self, args):  # type: (SanityConfig) -> SanityIgnoreProcessor
        """Load the ignore processor for this sanity test."""
        return SanityIgnoreProcessor(args, self, None)


class SanityCodeSmellTest(SanitySingleVersion):
    """Sanity test script."""
    def __init__(self, path):
        name = os.path.splitext(os.path.basename(path))[0]
        config_path = os.path.splitext(path)[0] + '.json'

        super().__init__(name=name)

        self.path = path
        self.config_path = config_path if os.path.exists(config_path) else None
        self.config = None

        if self.config_path:
            self.config = read_json_file(self.config_path)

        if self.config:
            self.enabled = not self.config.get('disabled')

            self.output = self.config.get('output')  # type: t.Optional[str]
            self.extensions = self.config.get('extensions')  # type: t.List[str]
            self.prefixes = self.config.get('prefixes')  # type: t.List[str]
            self.files = self.config.get('files')  # type: t.List[str]
            self.text = self.config.get('text')  # type: t.Optional[bool]
            self.ignore_self = self.config.get('ignore_self')  # type: bool
            self.minimum_python_version = self.config.get('minimum_python_version')  # type: t.Optional[str]

            self.__all_targets = self.config.get('all_targets')  # type: bool
            self.__no_targets = self.config.get('no_targets')  # type: bool
            self.__include_directories = self.config.get('include_directories')  # type: bool
            self.__include_symlinks = self.config.get('include_symlinks')  # type: bool
            self.__py2_compat = self.config.get('py2_compat', False)  # type: bool
        else:
            self.output = None
            self.extensions = []
            self.prefixes = []
            self.files = []
            self.text = None  # type: t.Optional[bool]
            self.ignore_self = False
            self.minimum_python_version = None  # type: t.Optional[str]

            self.__all_targets = False
            self.__no_targets = True
            self.__include_directories = False
            self.__include_symlinks = False
            self.__py2_compat = False

        if self.no_targets:
            mutually_exclusive = (
                'extensions',
                'prefixes',
                'files',
                'text',
                'ignore_self',
                'all_targets',
                'include_directories',
                'include_symlinks',
            )

            problems = sorted(name for name in mutually_exclusive if getattr(self, name))

            if problems:
                raise ApplicationError('Sanity test "%s" option "no_targets" is mutually exclusive with options: %s' % (self.name, ', '.join(problems)))

    @property
    def all_targets(self):  # type: () -> bool
        """True if test targets will not be filtered using includes, excludes, requires or changes. Mutually exclusive with no_targets."""
        return self.__all_targets

    @property
    def no_targets(self):  # type: () -> bool
        """True if the test does not use test targets. Mutually exclusive with all_targets."""
        return self.__no_targets

    @property
    def include_directories(self):  # type: () -> bool
        """True if the test targets should include directories."""
        return self.__include_directories

    @property
    def include_symlinks(self):  # type: () -> bool
        """True if the test targets should include symlinks."""
        return self.__include_symlinks

    @property
    def py2_compat(self):  # type: () -> bool
        """True if the test only applies to code that runs on Python 2.x."""
        return self.__py2_compat

    @property
    def supported_python_versions(self):  # type: () -> t.Optional[t.Tuple[str, ...]]
        """A tuple of supported Python versions or None if the test does not depend on specific Python versions."""
        versions = super().supported_python_versions

        if self.minimum_python_version:
            versions = tuple(version for version in versions if str_to_version(version) >= str_to_version(self.minimum_python_version))

        return versions

    def filter_targets(self, targets):  # type: (t.List[TestTarget]) -> t.List[TestTarget]
        """Return the given list of test targets, filtered to include only those relevant for the test."""
        if self.no_targets:
            return []

        if self.text is not None:
            if self.text:
                targets = [target for target in targets if not is_binary_file(target.path)]
            else:
                targets = [target for target in targets if is_binary_file(target.path)]

        if self.extensions:
            targets = [target for target in targets if os.path.splitext(target.path)[1] in self.extensions
                       or (is_subdir(target.path, 'bin') and '.py' in self.extensions)]

        if self.prefixes:
            targets = [target for target in targets if any(target.path.startswith(pre) for pre in self.prefixes)]

        if self.files:
            targets = [target for target in targets if os.path.basename(target.path) in self.files]

        if self.ignore_self and data_context().content.is_ansible:
            relative_self_path = os.path.relpath(self.path, data_context().content.root)
            targets = [target for target in targets if target.path != relative_self_path]

        return targets

    def test(self, args, targets, python):  # type: (SanityConfig, SanityTargets, PythonConfig) -> TestResult
        """Run the sanity test and return the result."""
        cmd = [python.path, self.path]

        env = ansible_environment(args, color=False)
        env.update(PYTHONUTF8='1')  # force all code-smell sanity tests to run with Python UTF-8 Mode enabled

        pattern = None
        data = None

        settings = self.load_processor(args)

        paths = [target.path for target in targets.include]

        if self.config:
            if self.output == 'path-line-column-message':
                pattern = '^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$'
            elif self.output == 'path-message':
                pattern = '^(?P<path>[^:]*): (?P<message>.*)$'
            else:
                raise ApplicationError('Unsupported output type: %s' % self.output)

        if not self.no_targets:
            data = '\n'.join(paths)

            if data:
                display.info(data, verbosity=4)

        try:
            stdout, stderr = intercept_python(args, python, cmd, data=data, env=env, capture=True)
            status = 0
        except SubprocessError as ex:
            stdout = ex.stdout
            stderr = ex.stderr
            status = ex.status

        if args.explain:
            return SanitySuccess(self.name)

        if stdout and not stderr:
            if pattern:
                matches = parse_to_list_of_dict(pattern, stdout)

                messages = [SanityMessage(
                    message=m['message'],
                    path=m['path'],
                    line=int(m.get('line', 0)),
                    column=int(m.get('column', 0)),
                ) for m in matches]

                messages = settings.process_errors(messages, paths)

                if not messages:
                    return SanitySuccess(self.name)

                return SanityFailure(self.name, messages=messages)

        if stderr or status:
            summary = u'%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
            return SanityFailure(self.name, summary=summary)

        messages = settings.process_errors([], paths)

        if messages:
            return SanityFailure(self.name, messages=messages)

        return SanitySuccess(self.name)

    def load_processor(self, args):  # type: (SanityConfig) -> SanityIgnoreProcessor
        """Load the ignore processor for this sanity test."""
        return SanityIgnoreProcessor(args, self, None)


class SanityVersionNeutral(SanityTest, metaclass=abc.ABCMeta):
    """Base class for sanity test plugins which are idependent of the python version being used."""
    @abc.abstractmethod
    def test(self, args, targets):  # type: (SanityConfig, SanityTargets) -> TestResult
        """Run the sanity test and return the result."""

    def load_processor(self, args):  # type: (SanityConfig) -> SanityIgnoreProcessor
        """Load the ignore processor for this sanity test."""
        return SanityIgnoreProcessor(args, self, None)

    @property
    def supported_python_versions(self):  # type: () -> t.Optional[t.Tuple[str, ...]]
        """A tuple of supported Python versions or None if the test does not depend on specific Python versions."""
        return None


class SanityMultipleVersion(SanityTest, metaclass=abc.ABCMeta):
    """Base class for sanity test plugins which should run on multiple python versions."""
    @abc.abstractmethod
    def test(self, args, targets, python):  # type: (SanityConfig, SanityTargets, PythonConfig) -> TestResult
        """Run the sanity test and return the result."""

    def load_processor(self, args, python_version):  # type: (SanityConfig, str) -> SanityIgnoreProcessor
        """Load the ignore processor for this sanity test."""
        return SanityIgnoreProcessor(args, self, python_version)

    @property
    def needs_pypi(self):  # type: () -> bool
        """True if the test requires PyPI, otherwise False."""
        return False

    @property
    def supported_python_versions(self):  # type: () -> t.Optional[t.Tuple[str, ...]]
        """A tuple of supported Python versions or None if the test does not depend on specific Python versions."""
        return SUPPORTED_PYTHON_VERSIONS

    def filter_targets_by_version(self, args, targets, python_version):  # type: (SanityConfig, t.List[TestTarget], str) -> t.List[TestTarget]
        """Return the given list of test targets, filtered to include only those relevant for the test, taking into account the Python version."""
        if not python_version:
            raise Exception('python_version is required to filter multi-version tests')

        targets = super().filter_targets_by_version(args, targets, python_version)

        if python_version in REMOTE_ONLY_PYTHON_VERSIONS:
            content_config = get_content_config(args)

            if python_version not in content_config.modules.python_versions:
                # when a remote-only python version is not supported there are no paths to test
                return []

            # when a remote-only python version is supported, tests must be applied only to targets that support remote-only Python versions
            targets = self.filter_remote_targets(targets)

        return targets


@cache
def sanity_get_tests():  # type: () -> t.Tuple[SanityTest, ...]
    """Return a tuple of the available sanity tests."""
    import_plugins('commands/sanity')
    sanity_plugins = {}  # type: t.Dict[str, t.Type[SanityTest]]
    load_plugins(SanityTest, sanity_plugins)
    sanity_plugins.pop('sanity')  # SanityCodeSmellTest
    sanity_tests = tuple(plugin() for plugin in sanity_plugins.values() if data_context().content.is_ansible or not plugin.ansible_only)
    sanity_tests = tuple(sorted(sanity_tests + collect_code_smell_tests(), key=lambda k: k.name))
    return sanity_tests


def create_sanity_virtualenv(
        args,  # type: SanityConfig
        python,  # type: PythonConfig
        name,  # type: str
        coverage=False,  # type: bool
        minimize=False,  # type: bool
):  # type: (...) -> t.Optional[VirtualPythonConfig]
    """Return an existing sanity virtual environment matching the requested parameters or create a new one."""
    commands = collect_requirements(  # create_sanity_virtualenv()
        python=python,
        controller=True,
        virtualenv=False,
        command=None,
        ansible=False,
        cryptography=False,
        coverage=coverage,
        minimize=minimize,
        sanity=name,
    )

    if commands:
        label = f'sanity.{name}'
    else:
        label = 'sanity'  # use a single virtualenv name for tests which have no requirements

    # The path to the virtual environment must be kept short to avoid the 127 character shebang length limit on Linux.
    # If the limit is exceeded, generated entry point scripts from pip installed packages will fail with syntax errors.
    virtualenv_install = json.dumps([command.serialize() for command in commands], indent=4)
    virtualenv_hash = hashlib.sha256(to_bytes(virtualenv_install)).hexdigest()[:8]
    virtualenv_cache = os.path.join(os.path.expanduser('~/.ansible/test/venv'))
    virtualenv_path = os.path.join(virtualenv_cache, label, f'{python.version}', virtualenv_hash)
    virtualenv_marker = os.path.join(virtualenv_path, 'marker.txt')

    meta_install = os.path.join(virtualenv_path, 'meta.install.json')
    meta_yaml = os.path.join(virtualenv_path, 'meta.yaml.json')

    virtualenv_python = VirtualPythonConfig(
        version=python.version,
        path=os.path.join(virtualenv_path, 'bin', 'python'),
    )

    if not os.path.exists(virtualenv_marker):
        # a virtualenv without a marker is assumed to have been partially created
        remove_tree(virtualenv_path)

        if not create_virtual_environment(args, python, virtualenv_path):
            return None

        run_pip(args, virtualenv_python, commands, None)  # create_sanity_virtualenv()

        write_text_file(meta_install, virtualenv_install)

        # false positive: pylint: disable=no-member
        if any(isinstance(command, PipInstall) and command.has_package('pyyaml') for command in commands):
            virtualenv_yaml = yamlcheck(virtualenv_python)
        else:
            virtualenv_yaml = None

        write_json_file(meta_yaml, virtualenv_yaml)

        created_venvs.append(f'{label}-{python.version}')

    # touch the marker to keep track of when the virtualenv was last used
    pathlib.Path(virtualenv_marker).touch()

    return virtualenv_python


def check_sanity_virtualenv_yaml(python):  # type: (VirtualPythonConfig) -> t.Optional[bool]
    """Return True if PyYAML has libyaml support for the given sanity virtual environment, False if it does not and None if it was not found."""
    virtualenv_path = os.path.dirname(os.path.dirname(python.path))
    meta_yaml = os.path.join(virtualenv_path, 'meta.yaml.json')
    virtualenv_yaml = read_json_file(meta_yaml)

    return virtualenv_yaml