|
1 | 1 | import logging |
2 | | -from collections import OrderedDict |
3 | 2 | from pathlib import Path |
4 | 3 |
|
5 | 4 | import pytest |
6 | 5 |
|
7 | 6 | import salt.loader |
8 | 7 | import salt.pillar |
9 | 8 | import salt.utils.cache |
| 9 | +from salt.utils.odict import OrderedDict |
10 | 10 | from tests.support.mock import MagicMock |
11 | 11 |
|
12 | 12 |
|
@@ -208,3 +208,106 @@ def test_remote_pillar_timeout(temp_salt_minion, tmp_path): |
208 | 208 | msg = r"^Pillar timed out after \d{1,4} seconds$" |
209 | 209 | with pytest.raises(salt.exceptions.SaltClientError): |
210 | 210 | pillar.compile_pillar() |
| 211 | + |
| 212 | + |
| 213 | +def test_issue_33437_pillar_merge_empty_pillar_should_preserve_data( |
| 214 | + temp_salt_minion, tmp_path |
| 215 | +): |
| 216 | + """ |
| 217 | + Test that merging pillars when one is empty should preserve the non-empty data. |
| 218 | +
|
| 219 | + Issue #33437: When merging pillar data, if one pillar file is empty (e.g., due to |
| 220 | + conditional rendering that produces no output), None values were overwriting |
| 221 | + existing non-empty dictionaries. This test verifies that the fix preserves |
| 222 | + existing data when merging with empty/None values. |
| 223 | + """ |
| 224 | + # Create pillar roots |
| 225 | + pillar_base = tmp_path / "base" |
| 226 | + pillar_base.mkdir(parents=True) |
| 227 | + |
| 228 | + # First pillar file with data |
| 229 | + common_pillar = pillar_base / "common.sls" |
| 230 | + common_pillar.write_text( |
| 231 | + """ |
| 232 | +program: |
| 233 | + modules: |
| 234 | + 00-definitions.conf: null |
| 235 | + 01-cleanup.conf: null |
| 236 | +""" |
| 237 | + ) |
| 238 | + |
| 239 | + # Second pillar file that may be empty (conditional rendering) |
| 240 | + modules_pillar = pillar_base / "modules.sls" |
| 241 | + modules_pillar.write_text( |
| 242 | + """ |
| 243 | +program: |
| 244 | + modules: |
| 245 | + {% if 'test' in grains.get('roles', []) %} |
| 246 | + 10-dostuff.conf: null |
| 247 | + {% endif %} |
| 248 | +""" |
| 249 | + ) |
| 250 | + |
| 251 | + # Create top.sls |
| 252 | + top_sls = pillar_base / "top.sls" |
| 253 | + top_sls.write_text( |
| 254 | + """ |
| 255 | +base: |
| 256 | + '*': |
| 257 | + - common |
| 258 | + - modules |
| 259 | +""" |
| 260 | + ) |
| 261 | + |
| 262 | + opts = temp_salt_minion.config.copy() |
| 263 | + opts["pillar_roots"] = {"base": [str(pillar_base)]} |
| 264 | + |
| 265 | + # Test case 1: Minion with 'test' role - modules.sls has content |
| 266 | + grains_with_role = salt.loader.grains(opts) |
| 267 | + grains_with_role["roles"] = ["test"] |
| 268 | + |
| 269 | + pillar_with_role = salt.pillar.Pillar( |
| 270 | + opts, grains_with_role, temp_salt_minion.id, "base" |
| 271 | + ) |
| 272 | + pillar_data_with_role = pillar_with_role.compile_pillar() |
| 273 | + |
| 274 | + # Should have all three modules |
| 275 | + assert "program" in pillar_data_with_role |
| 276 | + assert "modules" in pillar_data_with_role["program"] |
| 277 | + modules_with_role = pillar_data_with_role["program"]["modules"] |
| 278 | + assert "00-definitions.conf" in modules_with_role |
| 279 | + assert "01-cleanup.conf" in modules_with_role |
| 280 | + assert "10-dostuff.conf" in modules_with_role |
| 281 | + |
| 282 | + # Test case 2: Minion without 'test' role - modules.sls produces None for modules |
| 283 | + # THIS IS WHERE THE BUG OCCURRED - None was overwriting existing data |
| 284 | + grains_without_role = salt.loader.grains(opts) |
| 285 | + grains_without_role["roles"] = ["saltmaster"] |
| 286 | + |
| 287 | + pillar_without_role = salt.pillar.Pillar( |
| 288 | + opts, grains_without_role, temp_salt_minion.id, "base" |
| 289 | + ) |
| 290 | + pillar_data_without_role = pillar_without_role.compile_pillar() |
| 291 | + |
| 292 | + # Expected: Should have the two modules from common.sls (None should not overwrite) |
| 293 | + assert "program" in pillar_data_without_role, ( |
| 294 | + "Bug: When modules.sls produces None, the entire pillar becomes empty. " |
| 295 | + "Expected: program key should exist with data from common.sls" |
| 296 | + ) |
| 297 | + assert "modules" in pillar_data_without_role["program"], ( |
| 298 | + "Bug: modules key is missing. Expected: Should have modules from common.sls" |
| 299 | + ) |
| 300 | + modules_without_role = pillar_data_without_role["program"]["modules"] |
| 301 | + assert modules_without_role is not None, ( |
| 302 | + "Bug: modules is None. Expected: Should be a dict with data from common.sls" |
| 303 | + ) |
| 304 | + assert "00-definitions.conf" in modules_without_role, ( |
| 305 | + "Bug: Data from common.sls is lost when modules.sls produces None. " |
| 306 | + "Expected: 00-definitions.conf should be present" |
| 307 | + ) |
| 308 | + assert "01-cleanup.conf" in modules_without_role, ( |
| 309 | + "Bug: Data from common.sls is lost when modules.sls produces None. " |
| 310 | + "Expected: 01-cleanup.conf should be present" |
| 311 | + ) |
| 312 | + # Should NOT have the conditional module |
| 313 | + assert "10-dostuff.conf" not in modules_without_role |
0 commit comments