From f01483ff926b172b01bfff5674c3fe7ed0cf49e0 Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Wed, 10 Jan 2024 14:30:35 -0500 Subject: [PATCH] Fix salt-cloud get_cloud_config_value for list objects --- changelog/65789.fixed.md | 1 + salt/config/__init__.py | 4 ++- tests/pytests/unit/cloud/test_cloud.py | 49 +++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 changelog/65789.fixed.md diff --git a/changelog/65789.fixed.md b/changelog/65789.fixed.md new file mode 100644 index 00000000000..cda794d8bd6 --- /dev/null +++ b/changelog/65789.fixed.md @@ -0,0 +1 @@ +Fix salt-cloud get_cloud_config_value for list objects diff --git a/salt/config/__init__.py b/salt/config/__init__.py index 14aa7061a92..4d3a3e826e8 100644 --- a/salt/config/__init__.py +++ b/salt/config/__init__.py @@ -3248,7 +3248,9 @@ def get_cloud_config_value(name, vm_, opts, default=None, search_global=True): # Let's get the value from the profile, if present if "profile" in vm_ and vm_["profile"] is not None: if name in opts["profiles"][vm_["profile"]]: - if isinstance(value, dict): + if isinstance(value, dict) and isinstance( + opts["profiles"][vm_["profile"]][name], dict + ): value.update(opts["profiles"][vm_["profile"]][name].copy()) else: value = deepcopy(opts["profiles"][vm_["profile"]][name]) diff --git a/tests/pytests/unit/cloud/test_cloud.py b/tests/pytests/unit/cloud/test_cloud.py index 5d16c2d2cd8..b227a7977f0 100644 --- a/tests/pytests/unit/cloud/test_cloud.py +++ b/tests/pytests/unit/cloud/test_cloud.py @@ -1,6 +1,7 @@ import pytest from salt.cloud import Cloud +from salt.config import get_cloud_config_value from salt.exceptions import SaltCloudSystemExit from tests.support.mock import MagicMock, patch @@ -194,7 +195,6 @@ def test_vm_config_merger_nooverridevalue(): @pytest.mark.skip_on_fips_enabled_platform def test_cloud_run_profile_create_returns_boolean(master_config): - master_config["profiles"] = {"test_profile": {"provider": "test_provider:saltify"}} master_config["providers"] = { "test_provider": { @@ -213,3 +213,50 @@ def test_cloud_run_profile_create_returns_boolean(master_config): with pytest.raises(SaltCloudSystemExit): ret = cloud.run_profile("test_profile", ["test_vm"]) assert ret == {"test_vm": False} + + +@pytest.mark.parametrize( + "value", + [ + [{"key1": "value1"}, {"key1": "value1", "key2": "value2"}], + ["a", "b"], + [1, 2, 4], + {"key1": "value1", "key2": 123}, + "some text", + 1234, + ], +) +def test_get_cloud_config_value(value): + value_name = "test_value_name" + opts = { + "providers": { + "my-cool-cloud-provider": { + "cool-cloud": { + "driver": "cool-cloud", + "profiles": { + "my-cool-cloud-profile": { + "provider": "my-cool-cloud-provider:cool-cloud", + value_name: value, + "profile": "my-cool-cloud-profile", + } + }, + } + } + }, + "profiles": { + "my-cool-cloud-profile": { + "provider": "my-cool-cloud-provider:cool-cloud", + value_name: value, + "profile": "my-cool-cloud-profile", + } + }, + "profile": "my-cool-cloud-profile", + } + vm_ = { + value_name: value, + "profile": "my-cool-cloud-profile", + "driver": "cool-cloud", + } + + result = get_cloud_config_value(value_name, vm_, opts) + assert result == value