"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/tests/unit/pillar/test_file_tree.py" (18 Nov 2020, 6879 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_file_tree.py": 3002.1_vs_3002.2.

    1 """
    2 test for pillar file_tree.py
    3 """
    4 
    5 
    6 import os
    7 import shutil
    8 import tempfile
    9 
   10 import salt.pillar.file_tree as file_tree
   11 import salt.utils.files
   12 import salt.utils.stringutils
   13 from tests.support.helpers import TstSuiteLoggingHandler
   14 from tests.support.mixins import LoaderModuleMockMixin
   15 from tests.support.mock import MagicMock, patch
   16 from tests.support.runtests import RUNTIME_VARS
   17 from tests.support.unit import TestCase
   18 
   19 MINION_ID = "test-host"
   20 NODEGROUP_PATH = os.path.join("nodegroups", "test-group", "files")
   21 HOST_PATH = os.path.join("hosts", MINION_ID, "files")
   22 
   23 BASE_PILLAR_CONTENT = {"files": {"hostfile": b"base", "groupfile": b"base"}}
   24 DEV_PILLAR_CONTENT = {
   25     "files": {
   26         "hostfile": b"base",
   27         "groupfile": b"dev2",
   28         "hostfile1": b"dev1",
   29         "groupfile1": b"dev1",
   30         "hostfile2": b"dev2",
   31     }
   32 }
   33 PARENT_PILLAR_CONTENT = {
   34     "files": {"hostfile": b"base", "groupfile": b"base", "hostfile2": b"dev2"}
   35 }
   36 
   37 FILE_DATA = {
   38     os.path.join("base", HOST_PATH, "hostfile"): "base",
   39     os.path.join("dev1", HOST_PATH, "hostfile1"): "dev1",
   40     os.path.join("dev2", HOST_PATH, "hostfile2"): "dev2",
   41     os.path.join("base", NODEGROUP_PATH, "groupfile"): "base",
   42     os.path.join("dev1", NODEGROUP_PATH, "groupfile1"): "dev1",
   43     os.path.join("dev2", NODEGROUP_PATH, "groupfile"): "dev2",  # test merging
   44 }
   45 
   46 _CHECK_MINIONS_RETURN = {"minions": [MINION_ID], "missing": []}
   47 
   48 
   49 class FileTreePillarTestCase(TestCase, LoaderModuleMockMixin):
   50     "test file_tree pillar"
   51     maxDiff = None
   52 
   53     def setup_loader_modules(self):
   54         self.tmpdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
   55         self.addCleanup(shutil.rmtree, self.tmpdir)
   56         cachedir = os.path.join(self.tmpdir, "cachedir")
   57         os.makedirs(os.path.join(cachedir, "file_tree"))
   58         self.pillar_path = self._create_pillar_files()
   59         return {
   60             file_tree: {
   61                 "__opts__": {
   62                     "cachedir": cachedir,
   63                     "pillar_roots": {
   64                         "base": [os.path.join(self.pillar_path, "base")],
   65                         "dev": [
   66                             os.path.join(self.pillar_path, "base"),
   67                             os.path.join(self.pillar_path, "dev1"),
   68                             os.path.join(self.pillar_path, "dev2"),
   69                         ],
   70                         "parent": [
   71                             os.path.join(self.pillar_path, "base", "sub1"),
   72                             os.path.join(self.pillar_path, "dev2", "sub"),
   73                             os.path.join(self.pillar_path, "base", "sub2"),
   74                         ],
   75                     },
   76                     "pillarenv": "base",
   77                     "nodegroups": {"test-group": [MINION_ID]},
   78                     "optimization_order": [0, 1, 2],
   79                     "file_buffer_size": 262144,
   80                     "file_roots": {"base": "", "dev": "", "parent": ""},
   81                     "extension_modules": "",
   82                     "renderer": "yaml_jinja",
   83                     "renderer_blacklist": [],
   84                     "renderer_whitelist": [],
   85                 }
   86             }
   87         }
   88 
   89     def _create_pillar_files(self):
   90         "create files in tempdir"
   91         pillar_path = os.path.join(self.tmpdir, "file_tree")
   92         for filename in FILE_DATA:
   93             filepath = os.path.join(pillar_path, filename)
   94             os.makedirs(os.path.dirname(filepath))
   95             with salt.utils.files.fopen(filepath, "w") as data_file:
   96                 data_file.write(salt.utils.stringutils.to_str(FILE_DATA[filename]))
   97         return pillar_path
   98 
   99     def test_absolute_path(self):
  100         "check file tree is imported correctly with an absolute path"
  101         absolute_path = os.path.join(self.pillar_path, "base")
  102         with patch(
  103             "salt.utils.minions.CkMinions.check_minions",
  104             MagicMock(return_value=_CHECK_MINIONS_RETURN),
  105         ):
  106             mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  107             self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  108 
  109             with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
  110                 mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  111                 self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  112 
  113     def test_relative_path(self):
  114         "check file tree is imported correctly with a relative path"
  115         with patch(
  116             "salt.utils.minions.CkMinions.check_minions",
  117             MagicMock(return_value=_CHECK_MINIONS_RETURN),
  118         ):
  119             mypillar = file_tree.ext_pillar(MINION_ID, None, ".")
  120             self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  121 
  122             with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
  123                 mypillar = file_tree.ext_pillar(MINION_ID, None, ".")
  124                 self.assertEqual(DEV_PILLAR_CONTENT, mypillar)
  125 
  126     def test_parent_path(self):
  127         "check if file tree is merged correctly with a .. path"
  128         with patch(
  129             "salt.utils.minions.CkMinions.check_minions",
  130             MagicMock(return_value=_CHECK_MINIONS_RETURN),
  131         ):
  132             with patch.dict(file_tree.__opts__, {"pillarenv": "parent"}):
  133                 mypillar = file_tree.ext_pillar(MINION_ID, None, "..")
  134                 self.assertEqual(PARENT_PILLAR_CONTENT, mypillar)
  135 
  136     def test_no_pillarenv(self):
  137         "confirm that file_tree yells when pillarenv is missing for a relative path"
  138         with patch(
  139             "salt.utils.minions.CkMinions.check_minions",
  140             MagicMock(return_value=_CHECK_MINIONS_RETURN),
  141         ):
  142             with patch.dict(file_tree.__opts__, {"pillarenv": None}):
  143                 with TstSuiteLoggingHandler() as handler:
  144                     mypillar = file_tree.ext_pillar(MINION_ID, None, ".")
  145                     self.assertEqual({}, mypillar)
  146 
  147                     for message in handler.messages:
  148                         if (
  149                             message.startswith("ERROR:")
  150                             and "pillarenv is not set" in message
  151                         ):
  152                             break
  153                     else:
  154                         raise AssertionError("Did not find error message")
  155 
  156     def test_file_tree_bytes(self):
  157         """
  158         test file_tree pillar returns bytes
  159         """
  160         absolute_path = os.path.join(self.pillar_path, "base")
  161         with patch(
  162             "salt.utils.minions.CkMinions.check_minions",
  163             MagicMock(return_value=_CHECK_MINIONS_RETURN),
  164         ):
  165             mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  166             self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  167 
  168             with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
  169                 mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  170                 self.assertEqual(mypillar["files"]["groupfile"], b"base")