Support testing against system installed version (#57764)

* Drop six usage and Py2 support

Signed-off-by: Benjamin Drung <benjamin.drung@cloud.ionos.com>

* Fix import grouping in tests/unit/modules/test_baredoc.py

Signed-off-by: Benjamin Drung <benjamin.drung@cloud.ionos.com>

* Support testing against system installed version

The Debian package of salt comes with an autopkgtest, which runs the
unit tests against the install salt package:

```
cp -r conf scripts tests "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"
LC_ALL=C.UTF-8 NO_INTERNET=1 python3 ./tests/runtests.py -v --no-report --unit
```

Some test cases fail, because the salt source code is not in `CODE_DIR`.
To support testing against system installed version, just import `salt`
to determine the location of the module and set `SALT_CODE_DIR` to it.

`test_check_virtualname` and `test_module_name_source_match` can now run
against the system installed version and do not need to be skipped any
more.

Signed-off-by: Benjamin Drung <benjamin.drung@cloud.ionos.com>

* Running pre-commit bits manually.

Co-authored-by: Gareth J. Greenaway <gareth@wiked.org>
Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com>
This commit is contained in:
Benjamin Drung 2020-10-06 00:36:49 +02:00 committed by GitHub
parent 341f907b86
commit 16b17038dc
5 changed files with 16 additions and 49 deletions

View file

@ -10,17 +10,18 @@
Tests related paths
"""
import logging
import os
import re
import sys
import tempfile
import salt
import salt.utils.path
log = logging.getLogger(__name__)
SALT_CODE_DIR = os.path.dirname(os.path.normpath(os.path.abspath(salt.__file__)))
TESTS_DIR = os.path.dirname(
os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
)

View file

@ -206,6 +206,7 @@ RUNTIME_VARS = RuntimeVars(
RUNNING_TESTS_USER=this_user(),
RUNTIME_CONFIGS={},
CODE_DIR=paths.CODE_DIR,
SALT_CODE_DIR=paths.SALT_CODE_DIR,
BASE_FILES=paths.BASE_FILES,
PROD_FILES=paths.PROD_FILES,
TESTS_DIR=paths.TESTS_DIR,

View file

@ -3,19 +3,14 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
# Import Python libs
import fnmatch
import os
# Import Salt libs
import salt.utils.path
import salt.utils.stringutils
# Import Salt Testing libs
from tests.support.paths import list_test_mods
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase, skipIf
from tests.support.unit import TestCase
EXCLUDED_DIRS = [
os.path.join("tests", "integration", "cloud", "helpers"),
@ -102,10 +97,6 @@ class BadTestModuleNamesTestCase(TestCase):
error_msg += "If it is a tests module, then please rename as suggested."
self.assertEqual([], bad_names, error_msg)
@skipIf(
not os.path.isdir(os.path.join(RUNTIME_VARS.CODE_DIR, "salt")),
"Failed to find salt directory in '{}'.".format(RUNTIME_VARS.CODE_DIR),
)
def test_module_name_source_match(self):
"""
Check all the test mods and check if they correspond to actual files in
@ -242,11 +233,11 @@ class BadTestModuleNamesTestCase(TestCase):
# The path from the root of the repo
relpath = salt.utils.path.join(
"salt", stem.replace(".", os.sep), ".".join((flower[5:], "py"))
stem.replace(".", os.sep), ".".join((flower[5:], "py"))
)
# The full path to the file we expect to find
abspath = salt.utils.path.join(RUNTIME_VARS.CODE_DIR, relpath)
abspath = salt.utils.path.join(RUNTIME_VARS.SALT_CODE_DIR, relpath)
if not os.path.isfile(abspath):
# Maybe this is in a dunder init?

View file

@ -1,32 +1,19 @@
# -*- coding: utf-8 -*-
"""
tests.unit.test_virtualname
~~~~~~~~~~~~~~~~~~~~
"""
# Import Python libs
from __future__ import absolute_import
import importlib.util
import logging
import os
# Import Salt libs
import salt.ext.six as six
# Import Salt Testing libs
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase, skipIf
try:
import importlib.util
except ImportError:
import imp
from tests.support.unit import TestCase
log = logging.getLogger(__name__)
class FakeEntry(object):
class FakeEntry:
def __init__(self, name, path, is_file=True):
self.name = name
self.path = path
@ -46,18 +33,9 @@ class VirtualNameTestCase(TestCase):
@staticmethod
def _import_module(testpath):
if six.PY3:
spec = importlib.util.spec_from_file_location("tmpmodule", testpath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
fp, pathname, description = imp.find_module("tmpmodule", testpath)
try:
module = imp.load_module("tmpmodule", fp, pathname, description)
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
spec = importlib.util.spec_from_file_location("tmpmodule", testpath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def _check_modules(self, path):
@ -78,29 +56,25 @@ class VirtualNameTestCase(TestCase):
if hasattr(module, "__virtualname__"):
if module.__virtualname__ not in name:
ret.append(
'Virtual name "{0}" is not in the module filename "{1}": {2}'.format(
'Virtual name "{}" is not in the module filename "{}": {}'.format(
module.__virtualname__, name, path
)
)
return ret
@skipIf(
not os.path.isdir(os.path.join(RUNTIME_VARS.CODE_DIR, "salt")),
"Failed to find salt directory in '{}'.".format(RUNTIME_VARS.CODE_DIR),
)
def test_check_virtualname(self):
"""
Test that the virtualname is in __name__ of the module
"""
errors = []
for entry in os.listdir(os.path.join(RUNTIME_VARS.CODE_DIR, "salt/")):
for entry in os.listdir(RUNTIME_VARS.SALT_CODE_DIR):
name, path = os.path.splitext(os.path.basename(entry))[0], entry
if name.startswith(".") or name.startswith("_") or not os.path.isdir(path):
continue
if name in ("cli", "defaults", "spm", "daemons", "ext", "templates"):
continue
if name == "cloud":
entry = os.path.join(RUNTIME_VARS.CODE_DIR, "salt", "cloud", "clouds")
entry = os.path.join(RUNTIME_VARS.SALT_CODE_DIR, "cloud", "clouds")
errors.extend(self._check_modules(entry))
for error in errors:
log.critical(error)

View file

@ -43,7 +43,7 @@ class SSHThinTestCase(TestCase):
self.ext_conf = {
"test": {
"py-version": [2, 7],
"path": os.path.join(RUNTIME_VARS.CODE_DIR, "salt"),
"path": RUNTIME_VARS.SALT_CODE_DIR,
"dependencies": {"jinja2": self.jinja_fp},
}
}