1#!/usr/bin/env python3
2#
3# Copyright 2021, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Tests the correctness of config files for LTP tests launcher."""
18
19import pprint
20import re
21import unittest
22
23
24class LTPConfigTest(unittest.TestCase):
25    """Tests the correctness of LTP tests configuration."""
26
27    @staticmethod
28    def stable_test_entry_name(stable_key, no_suffix=False):
29        if no_suffix:
30            for s in ["_32bit", "_64bit"]:
31                if stable_key.endswith(s):
32                    stable_key = stable_key.removesuffix(s)
33                    break
34        return stable_key
35
36    def get_parsed_tests(self, no_suffix=False):
37        from configs import stable_tests
38        from configs import disabled_tests
39
40        return {
41            "STABLE_TESTS": set(self.stable_test_entry_name(t, no_suffix) for t in stable_tests.STABLE_TESTS),
42            "DISABLED_TESTS": disabled_tests.DISABLED_TESTS,
43            "DISABLED_TESTS_HWASAN": disabled_tests.DISABLED_TESTS_HWASAN,
44        }
45
46    def test_configs_correct_format(self):
47        parsed_tests = self.get_parsed_tests()
48
49        for container in ["STABLE_TESTS", "DISABLED_TESTS", "DISABLED_TESTS_HWASAN"]:
50            with self.subTest(container=container):
51                test_syntax = re.compile(r"\A[\w|-]+\.[\w|-]+_(32|64)bit\Z")
52                for t in parsed_tests[container]:
53                    self.assertIsNotNone(test_syntax.match(t), '"{}" should be in the form "<class>.<method>_{{32,64}}bit"'.format(t))
54
55    def test_configs_collide(self):
56        parsed_tests = self.get_parsed_tests()
57        success = True
58
59        """
60        DISABLED_TESTS_HWASAN is currently ignored for matching with
61        STABLE_TESTS. This because tests in DISABLED_TESTS_HWASAN can be
62        executed on non-HWASAN devices.
63        """
64
65        multiple_occurrences = {
66            "DISABLED_TESTS": {},
67            "DISABLED_TESTS_HWASAN": {},
68        }
69
70        for container_name in ["STABLE_TESTS", "DISABLED_TESTS_HWASAN"]:
71            for st in parsed_tests[container_name]:
72                """
73                gen_ltp_config.py filters the stable tests by testing the
74                existence of the disabled test substring into the stable test
75                substring. Because of this, this test has to do the same, and
76                list::count() is not an option.
77                """
78                for dt in parsed_tests["DISABLED_TESTS"]:
79                    if dt in st:
80                        multiple_occurrences["DISABLED_TESTS"][st] = dt
81                        success = False
82        self.assertTrue(success, 'Test(s) in {} also in {}: \n{}'.format(
83            container_name,
84            "DISABLED_TESTS",
85            pprint.pformat(multiple_occurrences)))
86
87
88if __name__ == '__main__':
89    unittest.main(verbosity=3)
90