Skip to content

Commit

Permalink
Merge pull request #56143 from waynew/51854-minion-pillar-cache-excep…
Browse files Browse the repository at this point in the history
…tion

Use encoding when caching pillar data
  • Loading branch information
dwoz authored Mar 10, 2020
2 parents 9f27caa + 58cc948 commit 8a8e9c9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
5 changes: 3 additions & 2 deletions salt/minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from salt._compat import ipaddress
from salt.utils.network import parse_host_port
from salt.ext.six.moves import range
from salt.template import SLS_ENCODING
from salt.utils.zeromq import zmq, ZMQDefaultLoop, install_zmq, ZMQ_VERSION_INFO
import salt.transport.client
import salt.defaults.exitcodes
Expand Down Expand Up @@ -865,11 +866,11 @@ def __init__(self, opts, context=None):
penv = 'base'
cache_top = {penv: {self.opts['id']: ['cache']}}
with salt.utils.files.fopen(ptop, 'wb') as fp_:
salt.utils.yaml.safe_dump(cache_top, fp_)
salt.utils.yaml.safe_dump(cache_top, fp_, encoding=SLS_ENCODING)
os.chmod(ptop, 0o600)
cache_sls = os.path.join(pdir, 'cache.sls')
with salt.utils.files.fopen(cache_sls, 'wb') as fp_:
salt.utils.yaml.safe_dump(self.opts['pillar'], fp_)
salt.utils.yaml.safe_dump(self.opts['pillar'], fp_, encoding=SLS_ENCODING)
os.chmod(cache_sls, 0o600)


Expand Down
53 changes: 53 additions & 0 deletions tests/integration/minion/test_minion_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import os

import salt.loader
import salt.minion
import salt.utils.yaml
from salt.utils.files import fopen
from tests.support.case import ModuleCase
from tests.support.helpers import with_tempdir
from tests.support.mock import patch


class BasePillarTest(ModuleCase):
@with_tempdir()
def test_minion_cache_should_cache_files(self, tempdir):
pillar = {"this": {"is": {"some": "pillar data"}}}
opts = {
"file_client": "remote",
"minion_pillar_cache": "true",
"master_type": "local",
"discovery": False,
"master": "local",
"__role": "",
"id": "test",
"saltenv": "base",
"pillar_cache": True,
"pillar_cache_backend": "disk",
"pillar_cache_ttl": 3600,
"cachedir": tempdir,
"state_top": "top.sls",
"pillar_roots": {"base": tempdir},
"extension_modules": tempdir,
"file_ignore_regex": [],
"file_ignore_glob": [],
"pillar": pillar,
}
with patch("salt.loader.grains", return_value={}), patch(
"salt.minion.SMinion.gen_modules"
), patch("salt.minion.SMinion.eval_master"), patch(
"salt.minion.install_zmq"
), patch(
"salt.minion.ZMQDefaultLoop.current"
):
minion = salt.minion.SMinion(opts)
self.assertTrue("pillar" in os.listdir(tempdir))
pillar_cache = os.path.join(tempdir, "pillar")
self.assertTrue("top.sls" in os.listdir(pillar_cache))
self.assertTrue("cache.sls" in os.listdir(pillar_cache))
with fopen(os.path.join(pillar_cache, "cache.sls"), "rb") as f:
cached_data = salt.utils.yaml.safe_load(f)
assert cached_data == pillar
3 changes: 2 additions & 1 deletion tests/unit/test_module_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ def test_module_name_source_match(self):
'integration.logging.handlers.test_logstash_mod',
'integration.master.test_event_return',
'integration.minion.test_blackout',
'integration.minion.test_pillar',
'integration.minion.test_executor',
'integration.minion.test_minion_cache',
'integration.minion.test_pillar',
'integration.minion.test_timeout',
'integration.modules.test_decorators',
'integration.modules.test_pkg',
Expand Down

0 comments on commit 8a8e9c9

Please sign in to comment.