Move grain_opts to subdict to prevent overwriting core grains

This commit is contained in:
Erik Johnson 2024-08-07 13:30:04 -05:00 committed by Daniel Wozniak
parent ae1d3f3eb3
commit 6c3365f67c
2 changed files with 21 additions and 1 deletions

View file

@ -11,5 +11,5 @@ def opts():
if __opts__.get("grain_opts", False) or (
isinstance(__pillar__, dict) and __pillar__.get("grain_opts", False)
):
return __opts__
return {"opts": __opts__}
return {}

View file

@ -0,0 +1,20 @@
"""
tests.pytests.unit.grains.test_opts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import salt.grains.opts as opts
from tests.support.mock import patch
def test_grain_opts_does_not_overwrite_core_grains(tmp_path):
"""
Tests that enabling grain_opts doesn't overwrite the core grains
See: https://github.com/saltstack/salt/issues/66784
"""
dunder_opts = {"grain_opts": True}
with patch.object(opts, "__opts__", dunder_opts, create=True):
with patch.object(opts, "__pillar__", {}, create=True):
assert opts.opts() == {"opts": dunder_opts}