xref: /openbmc/openbmc/poky/meta/lib/oeqa/core/tests/test_loader.py (revision 79641f25e882b55af6a647cfc0bf6bcc025661a5)
1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 Intel Corporation
4#
5# SPDX-License-Identifier: MIT
6#
7
8import os
9import unittest
10
11from common import setup_sys_path, TestBase
12setup_sys_path()
13
14from oeqa.core.exception import OEQADependency
15from oeqa.core.utils.test import getSuiteModules, getSuiteCasesIDs
16
17class TestLoader(TestBase):
18    @unittest.skip("invalid directory is missing oetag.py")
19    def test_fail_duplicated_module(self):
20        cases_path = self.cases_path
21        invalid_path = os.path.join(cases_path, 'loader', 'invalid')
22        self.cases_path = [self.cases_path, invalid_path]
23        expect = 'Duplicated oetag module found in'
24        msg = 'Expected ImportError exception for having duplicated module'
25        try:
26            # Must throw ImportEror because duplicated module
27            tc = self._testLoader()
28            self.fail(msg)
29        except ImportError as e:
30            result = True if expect in str(e) else False
31            self.assertTrue(result, msg=msg)
32        finally:
33            self.cases_path = cases_path
34
35    def test_filter_modules(self):
36        expected_modules = {'oetag'}
37        tc = self._testLoader(modules=expected_modules)
38        modules = getSuiteModules(tc.suites)
39        msg = 'Expected just %s modules' % ', '.join(expected_modules)
40        self.assertEqual(modules, expected_modules, msg=msg)
41
42    def test_filter_cases(self):
43        modules = ['oetag', 'data']
44        expected_cases = {'data.DataTest.testDataOk',
45                          'oetag.TagTest.testTagGood'}
46        tc = self._testLoader(modules=modules, tests=expected_cases)
47        cases = set(getSuiteCasesIDs(tc.suites))
48        msg = 'Expected just %s cases' % ', '.join(expected_cases)
49        self.assertEqual(cases, expected_cases, msg=msg)
50
51    def test_import_from_paths(self):
52        cases_path = self.cases_path
53        cases2_path = os.path.join(cases_path, 'loader', 'valid')
54        expected_modules = {'another'}
55        self.cases_path = [self.cases_path, cases2_path]
56        tc = self._testLoader(modules=expected_modules)
57        modules = getSuiteModules(tc.suites)
58        self.cases_path = cases_path
59        msg = 'Expected modules from two different paths'
60        self.assertEqual(modules, expected_modules, msg=msg)
61
62if __name__ == '__main__':
63    unittest.main()
64