From 66b520ac023d08761c8621ffa3340ad733988d43 Mon Sep 17 00:00:00 2001
From: Benjamin Drung <benjamin.drung@cloud.ionos.com>
Date: Tue, 23 Jun 2020 17:08:03 +0200
Subject: 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.

Forwarded: https://github.com/saltstack/salt/pull/57764
Signed-off-by: Benjamin Drung <benjamin.drung@cloud.ionos.com>
---
 tests/support/paths.py             |  2 ++
 tests/support/runtests.py          |  1 +
 tests/unit/modules/test_baredoc.py |  8 ++------
 tests/unit/test_module_names.py    | 10 +++-------
 tests/unit/test_virtualname.py     | 10 +++-------
 tests/unit/utils/test_thin.py      |  2 +-
 tests/unit/utils/test_versions.py  |  4 ++--
 7 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/tests/support/paths.py b/tests/support/paths.py
index 1e1e081dc4..e07a9e0614 100644
--- a/tests/support/paths.py
+++ b/tests/support/paths.py
@@ -19,10 +19,12 @@ 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__)))
 )
diff --git a/tests/support/runtests.py b/tests/support/runtests.py
index 799ddb20ae..2426774944 100644
--- a/tests/support/runtests.py
+++ b/tests/support/runtests.py
@@ -210,6 +210,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,
diff --git a/tests/unit/modules/test_baredoc.py b/tests/unit/modules/test_baredoc.py
index 08078c042a..32dd246b7d 100644
--- a/tests/unit/modules/test_baredoc.py
+++ b/tests/unit/modules/test_baredoc.py
@@ -3,8 +3,6 @@
 # Import Python libs
 from __future__ import absolute_import
 
-import os
-
 # Import module
 import salt.modules.baredoc as baredoc
 from tests.support.mixins import LoaderModuleMockMixin
@@ -22,10 +20,8 @@ class BaredocTest(TestCase, LoaderModuleMockMixin):
     def setup_loader_modules(self):
         return {
             baredoc: {
-                "__opts__": {
-                    "extension_modules": os.path.join(RUNTIME_VARS.CODE_DIR, "salt"),
-                },
-                "__grains__": {"saltpath": os.path.join(RUNTIME_VARS.CODE_DIR, "salt")},
+                "__opts__": {"extension_modules": RUNTIME_VARS.SALT_CODE_DIR},
+                "__grains__": {"saltpath": RUNTIME_VARS.SALT_CODE_DIR},
             }
         }
 
diff --git a/tests/unit/test_module_names.py b/tests/unit/test_module_names.py
index 18fe913137..2390c60a8a 100644
--- a/tests/unit/test_module_names.py
+++ b/tests/unit/test_module_names.py
@@ -17,7 +17,7 @@ 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"),
@@ -105,10 +105,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
@@ -244,11 +240,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?
diff --git a/tests/unit/test_virtualname.py b/tests/unit/test_virtualname.py
index 201796e518..a121a23276 100644
--- a/tests/unit/test_virtualname.py
+++ b/tests/unit/test_virtualname.py
@@ -15,7 +15,7 @@ import salt.ext.six as six
 
 # Import Salt Testing libs
 from tests.support.runtests import RUNTIME_VARS
-from tests.support.unit import TestCase, skipIf
+from tests.support.unit import TestCase
 
 try:
     import importlib.util
@@ -84,23 +84,19 @@ class VirtualNameTestCase(TestCase):
                     )
         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)
diff --git a/tests/unit/utils/test_thin.py b/tests/unit/utils/test_thin.py
index 5315904e26..01b7b5941a 100644
--- a/tests/unit/utils/test_thin.py
+++ b/tests/unit/utils/test_thin.py
@@ -44,7 +44,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},
             }
         }
diff --git a/tests/unit/utils/test_versions.py b/tests/unit/utils/test_versions.py
index 579e6fcd61..f4c830d90d 100644
--- a/tests/unit/utils/test_versions.py
+++ b/tests/unit/utils/test_versions.py
@@ -23,7 +23,7 @@ from salt.ext import six
 from salt.utils.versions import LooseVersion, StrictVersion
 from tests.support.helpers import slowTest
 from tests.support.mock import patch
-from tests.support.paths import CODE_DIR
+from tests.support.paths import SALT_CODE_DIR
 from tests.support.unit import TestCase, skipIf
 
 if six.PY2:
@@ -108,7 +108,7 @@ class VersionTestCase(TestCase):
         query = "salt.utils.versions.warn_until("
         names = salt.version.SaltStackVersion.NAMES
 
-        cmd = "grep -lr {} -A 1 {}".format(query, os.path.join(CODE_DIR, "salt"))
+        cmd = ["grep", "-lr", query, "-A", "1", SALT_CODE_DIR]
 
         grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
 
-- 
2.25.1

