From 8feadd417ec380587cc852ff29896a2242f2d485 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Wed, 13 Dec 2023 05:33:06 +0000 Subject: [PATCH] No `CamelCase` please. Signed-off-by: Pedro Algarvio --- tests/pytests/unit/states/test_grains.py | 248 +++++++++++------------ 1 file changed, 119 insertions(+), 129 deletions(-) diff --git a/tests/pytests/unit/states/test_grains.py b/tests/pytests/unit/states/test_grains.py index eacdacb82f62..8015eaf9340a 100644 --- a/tests/pytests/unit/states/test_grains.py +++ b/tests/pytests/unit/states/test_grains.py @@ -36,28 +36,18 @@ def configure_loader_modules(minion_opts): return {grains: loader_globals, grainsmod: loader_globals} -def assertGrainFileContent(grains_string): - if os.path.isdir(grains.__opts__["conf_file"]): - grains_file = os.path.join(grains.__opts__["conf_file"], "grains") - else: - grains_file = os.path.join( - os.path.dirname(grains.__opts__["conf_file"]), "grains" - ) +def assert_grain_file_content(grains_string): + grains_file = os.path.join(grains.__opts__["conf_dir"], "grains") with salt.utils.files.fopen(grains_file, "r") as grf: grains_data = salt.utils.stringutils.to_unicode(grf.read()) assert grains_string == grains_data @contextlib.contextmanager -def setGrains(grains_data): +def set_grains(grains_data): with patch.dict(grains.__grains__, grains_data): with patch.dict(grainsmod.__grains__, grains_data): - if os.path.isdir(grains.__opts__["conf_file"]): - grains_file = os.path.join(grains.__opts__["conf_file"], "grains") - else: - grains_file = os.path.join( - os.path.dirname(grains.__opts__["conf_file"]), "grains" - ) + grains_file = os.path.join(grains.__opts__["conf_dir"], "grains") with salt.utils.files.fopen(grains_file, "w+") as grf: salt.utils.yaml.safe_dump(grains_data, grf, default_flow_style=False) yield @@ -67,7 +57,7 @@ def setGrains(grains_data): def test_exists_missing(): - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.exists(name="foo") assert ret["result"] is False assert ret["comment"] == "Grain does not exist" @@ -75,7 +65,7 @@ def test_exists_missing(): def test_exists_found(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Grain already set ret = grains.exists(name="foo") assert ret["result"] is True @@ -86,7 +76,7 @@ def test_exists_found(): def test_make_hashable(): - with setGrains({"cmplx_lst_grain": [{"a": "aval"}, {"foo": "bar"}]}): + with set_grains({"cmplx_lst_grain": [{"a": "aval"}, {"foo": "bar"}]}): hashable_list = {"cmplx_lst_grain": [{"a": "aval"}, {"foo": "bar"}]} assert grains.make_hashable(grains.__grains__).issubset( grains.make_hashable(hashable_list) @@ -97,23 +87,23 @@ def test_make_hashable(): def test_present_add(): # Set a non existing grain - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.present(name="foo", value="bar") assert ret["result"] is True assert ret["changes"] == {"foo": "bar"} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - assertGrainFileContent("a: aval\nfoo: bar\n") + assert_grain_file_content("a: aval\nfoo: bar\n") # Set a non existing nested grain - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.present(name="foo:is:nested", value="bar") assert ret["result"] is True assert ret["changes"] == {"foo": {"is": {"nested": "bar"}}} assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": "bar"}}} - assertGrainFileContent("a: aval\nfoo:\n is:\n nested: bar\n") + assert_grain_file_content("a: aval\nfoo:\n is:\n nested: bar\n") # Set a non existing nested dict grain - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.present(name="foo:is:nested", value={"bar": "is a dict"}) assert ret["result"] is True assert ret["changes"] == {"foo": {"is": {"nested": {"bar": "is a dict"}}}} @@ -121,24 +111,24 @@ def test_present_add(): "a": "aval", "foo": {"is": {"nested": {"bar": "is a dict"}}}, } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n is:\n nested:\n bar: is a dict\n" ) def test_present_add_key_to_existing(): - with setGrains({"a": "aval", "foo": {"k1": "v1"}}): + with set_grains({"a": "aval", "foo": {"k1": "v1"}}): # Fails setting a grain to a dict ret = grains.present(name="foo:k2", value="v2") assert ret["result"] is True assert ret["comment"] == "Set grain foo:k2 to v2" assert ret["changes"] == {"foo": {"k2": "v2", "k1": "v1"}} assert grains.__grains__ == {"a": "aval", "foo": {"k1": "v1", "k2": "v2"}} - assertGrainFileContent("a: aval\nfoo:\n k1: v1\n k2: v2\n") + assert_grain_file_content("a: aval\nfoo:\n k1: v1\n k2: v2\n") def test_present_already_set(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Grain already set ret = grains.present(name="foo", value="bar") assert ret["result"] is True @@ -146,7 +136,7 @@ def test_present_already_set(): assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Nested grain already set ret = grains.present(name="foo:is:nested", value="bar") assert ret["result"] is True @@ -154,7 +144,7 @@ def test_present_already_set(): assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": "bar"}}} - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Nested dict grain already set ret = grains.present(name="foo:is", value={"nested": "bar"}) assert ret["result"] is True @@ -164,41 +154,41 @@ def test_present_already_set(): def test_present_overwrite(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Overwrite an existing grain ret = grains.present(name="foo", value="newbar") assert ret["result"] is True assert ret["changes"] == {"foo": "newbar"} assert grains.__grains__ == {"a": "aval", "foo": "newbar"} - assertGrainFileContent("a: aval\nfoo: newbar\n") + assert_grain_file_content("a: aval\nfoo: newbar\n") - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Clear a grain (set to None) ret = grains.present(name="foo", value=None) assert ret["result"] is True assert ret["changes"] == {"foo": None} assert grains.__grains__ == {"a": "aval", "foo": None} - assertGrainFileContent("a: aval\nfoo: null\n") + assert_grain_file_content("a: aval\nfoo: null\n") - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Overwrite an existing nested grain ret = grains.present(name="foo:is:nested", value="newbar") assert ret["result"] is True assert ret["changes"] == {"foo": {"is": {"nested": "newbar"}}} assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": "newbar"}}} - assertGrainFileContent("a: aval\nfoo:\n is:\n nested: newbar\n") + assert_grain_file_content("a: aval\nfoo:\n is:\n nested: newbar\n") - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Clear a nested grain (set to None) ret = grains.present(name="foo:is:nested", value=None) assert ret["result"] is True assert ret["changes"] == {"foo": {"is": {"nested": None}}} assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": None}}} - assertGrainFileContent("a: aval\nfoo:\n is:\n nested: null\n") + assert_grain_file_content("a: aval\nfoo:\n is:\n nested: null\n") def test_present_fail_overwrite(): - with setGrains({"a": "aval", "foo": {"is": {"nested": "val"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "val"}}}): # Overwrite an existing grain ret = grains.present(name="foo:is", value="newbar") assert ret["result"] is False @@ -209,7 +199,7 @@ def test_present_fail_overwrite(): ) assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": "val"}}} - with setGrains({"a": "aval", "foo": {"is": {"nested": "val"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "val"}}}): # Clear a grain (set to None) ret = grains.present(name="foo:is", value=None) assert ret["result"] is False @@ -222,7 +212,7 @@ def test_present_fail_overwrite(): def test_present_fails_to_set_dict_or_list(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Fails to overwrite a grain to a list ret = grains.present(name="foo", value=["l1", "l2"]) assert ret["result"] is False @@ -233,7 +223,7 @@ def test_present_fails_to_set_dict_or_list(): assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Fails setting a grain to a dict ret = grains.present(name="foo", value={"k1": "v1"}) assert ret["result"] is False @@ -244,7 +234,7 @@ def test_present_fails_to_set_dict_or_list(): assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Fails to overwrite a nested grain to a list ret = grains.present(name="foo,is,nested", value=["l1", "l2"], delimiter=",") assert ret["result"] is False @@ -255,7 +245,7 @@ def test_present_fails_to_set_dict_or_list(): ) assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": "bar"}}} - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Fails setting a nested grain to a dict ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) assert ret["result"] is False @@ -268,7 +258,7 @@ def test_present_fails_to_set_dict_or_list(): def test_present_fail_merge_dict(): - with setGrains({"a": "aval", "foo": {"k1": "v1"}}): + with set_grains({"a": "aval", "foo": {"k1": "v1"}}): # Fails setting a grain to a dict ret = grains.present(name="foo", value={"k2": "v2"}) assert ret["result"] is False @@ -277,29 +267,29 @@ def test_present_fail_merge_dict(): == "The key 'foo' exists but is a dict or a list. Use 'force=True' to overwrite." ) assert grains.__grains__ == {"a": "aval", "foo": {"k1": "v1"}} - assertGrainFileContent("a: aval\nfoo:\n k1: v1\n") + assert_grain_file_content("a: aval\nfoo:\n k1: v1\n") def test_present_force_to_set_dict_or_list(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Force to overwrite a grain to a list ret = grains.present(name="foo", value=["l1", "l2"], force=True) assert ret["result"] is True assert ret["comment"] == "Set grain foo to ['l1', 'l2']" assert ret["changes"] == {"foo": ["l1", "l2"]} assert grains.__grains__ == {"a": "aval", "foo": ["l1", "l2"]} - assertGrainFileContent("a: aval\nfoo:\n- l1\n- l2\n") + assert_grain_file_content("a: aval\nfoo:\n- l1\n- l2\n") - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Force setting a grain to a dict ret = grains.present(name="foo", value={"k1": "v1"}, force=True) assert ret["result"] is True assert ret["comment"] == "Set grain foo to {'k1': 'v1'}" assert ret["changes"] == {"foo": {"k1": "v1"}} assert grains.__grains__ == {"a": "aval", "foo": {"k1": "v1"}} - assertGrainFileContent("a: aval\nfoo:\n k1: v1\n") + assert_grain_file_content("a: aval\nfoo:\n k1: v1\n") - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Force to overwrite a nested grain to a list ret = grains.present( name="foo,is,nested", value=["l1", "l2"], delimiter=",", force=True @@ -311,11 +301,11 @@ def test_present_force_to_set_dict_or_list(): "a": "aval", "foo": {"is": {"nested": ["l1", "l2"]}}, } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n is:\n nested:\n - l1\n - l2\n" ) - with setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}, "and": "other"}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": "bar"}, "and": "other"}}): # Force setting a nested grain to a dict ret = grains.present(name="foo:is:nested", value={"k1": "v1"}, force=True) assert ret["result"] is True @@ -327,13 +317,13 @@ def test_present_force_to_set_dict_or_list(): "a": "aval", "foo": {"is": {"nested": {"k1": "v1"}}, "and": "other"}, } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n and: other\n is:\n nested:\n k1: v1\n" ) def test_present_fails_to_convert_value_to_key(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Fails converting a value to a nested grain key ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) assert ret["result"] is False @@ -346,17 +336,17 @@ def test_present_fails_to_convert_value_to_key(): def test_present_overwrite_test(): with patch.dict(grains.__opts__, {"test": True}): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Overwrite an existing grain ret = grains.present(name="foo", value="newbar") assert ret["result"] is None assert ret["changes"] == {"changed": {"foo": "newbar"}} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - assertGrainFileContent("a: aval\nfoo: bar\n") + assert_grain_file_content("a: aval\nfoo: bar\n") def test_present_convert_value_to_key(): - with setGrains({"a": "aval", "foo": "is"}): + with set_grains({"a": "aval", "foo": "is"}): # Converts a value to a nested grain key ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) assert ret["result"] is True @@ -366,9 +356,9 @@ def test_present_convert_value_to_key(): "a": "aval", "foo": {"is": {"nested": {"k1": "v1"}}}, } - assertGrainFileContent("a: aval\nfoo:\n is:\n nested:\n k1: v1\n") + assert_grain_file_content("a: aval\nfoo:\n is:\n nested:\n k1: v1\n") - with setGrains({"a": "aval", "foo": ["one", "is", "correct"]}): + with set_grains({"a": "aval", "foo": ["one", "is", "correct"]}): # Converts a list element to a nested grain key ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) assert ret["result"] is True @@ -380,7 +370,7 @@ def test_present_convert_value_to_key(): "a": "aval", "foo": ["one", {"is": {"nested": {"k1": "v1"}}}, "correct"], } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n- one\n- is:\n nested:\n k1: v1\n- correct\n" ) @@ -388,14 +378,14 @@ def test_present_convert_value_to_key(): def test_present_unknown_failure(): with patch("salt.modules.grains.setval") as mocked_setval: mocked_setval.return_value = "Failed to set grain foo" - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Unknown reason failure ret = grains.present(name="foo", value="baz") assert ret["result"] is False assert ret["comment"] == "Failed to set grain foo" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - assertGrainFileContent("a: aval\nfoo: bar\n") + assert_grain_file_content("a: aval\nfoo: bar\n") # 'absent' function tests: 6 @@ -403,45 +393,45 @@ def test_present_unknown_failure(): def test_absent_already(): # Unset a non existent grain - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.absent(name="foo") assert ret["result"] is True assert ret["comment"] == "Grain foo does not exist" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval"} - assertGrainFileContent("a: aval\n") + assert_grain_file_content("a: aval\n") # Unset a non existent nested grain - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.absent(name="foo:is:nested") assert ret["result"] is True assert ret["comment"] == "Grain foo:is:nested does not exist" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval"} - assertGrainFileContent("a: aval\n") + assert_grain_file_content("a: aval\n") def test_absent_unset(): # Unset a grain - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): ret = grains.absent(name="foo") assert ret["result"] is True assert ret["comment"] == "Value for grain foo was set to None" assert ret["changes"] == {"grain": "foo", "value": None} assert grains.__grains__ == {"a": "aval", "foo": None} - assertGrainFileContent("a: aval\nfoo: null\n") + assert_grain_file_content("a: aval\nfoo: null\n") # Unset grain when its value is False - with setGrains({"a": "aval", "foo": False}): + with set_grains({"a": "aval", "foo": False}): ret = grains.absent(name="foo") assert ret["result"] is True assert ret["comment"] == "Value for grain foo was set to None" assert ret["changes"] == {"grain": "foo", "value": None} assert grains.__grains__ == {"a": "aval", "foo": None} - assertGrainFileContent("a: aval\nfoo: null\n") + assert_grain_file_content("a: aval\nfoo: null\n") # Unset a nested grain - with setGrains( + with set_grains( {"a": "aval", "foo": ["order", {"is": {"nested": "bar"}}, "correct"]} ): ret = grains.absent(name="foo,is,nested", delimiter=",") @@ -452,12 +442,12 @@ def test_absent_unset(): "a": "aval", "foo": ["order", {"is": {"nested": None}}, "correct"], } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n- order\n- is:\n nested: null\n- correct\n" ) # Unset a nested value don't change anything - with setGrains({"a": "aval", "foo": ["order", {"is": "nested"}, "correct"]}): + with set_grains({"a": "aval", "foo": ["order", {"is": "nested"}, "correct"]}): ret = grains.absent(name="foo:is:nested") assert ret["result"] is True assert ret["comment"] == "Grain foo:is:nested does not exist" @@ -466,23 +456,23 @@ def test_absent_unset(): "a": "aval", "foo": ["order", {"is": "nested"}, "correct"], } - assertGrainFileContent("a: aval\nfoo:\n- order\n- is: nested\n- correct\n") + assert_grain_file_content("a: aval\nfoo:\n- order\n- is: nested\n- correct\n") def test_absent_unset_test(): with patch.dict(grains.__opts__, {"test": True}): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): # Overwrite an existing grain ret = grains.absent(name="foo") assert ret["result"] is None assert ret["changes"] == {"grain": "foo", "value": None} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - assertGrainFileContent("a: aval\nfoo: bar\n") + assert_grain_file_content("a: aval\nfoo: bar\n") def test_absent_fails_nested_complex_grain(): # Unset a nested complex grain - with setGrains( + with set_grains( {"a": "aval", "foo": ["order", {"is": {"nested": "bar"}}, "correct"]} ): ret = grains.absent(name="foo:is") @@ -496,14 +486,14 @@ def test_absent_fails_nested_complex_grain(): "a": "aval", "foo": ["order", {"is": {"nested": "bar"}}, "correct"], } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n- order\n- is:\n nested: bar\n- correct\n" ) def test_absent_force_nested_complex_grain(): # Unset a nested complex grain - with setGrains( + with set_grains( {"a": "aval", "foo": ["order", {"is": {"nested": "bar"}}, "correct"]} ): ret = grains.absent(name="foo:is", force=True) @@ -514,30 +504,30 @@ def test_absent_force_nested_complex_grain(): "a": "aval", "foo": ["order", {"is": None}, "correct"], } - assertGrainFileContent("a: aval\nfoo:\n- order\n- is: null\n- correct\n") + assert_grain_file_content("a: aval\nfoo:\n- order\n- is: null\n- correct\n") def test_absent_delete(): # Delete a grain - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): ret = grains.absent(name="foo", destructive=True) assert ret["result"] is True assert ret["comment"] == "Grain foo was deleted" assert ret["changes"] == {"deleted": "foo"} assert grains.__grains__ == {"a": "aval"} - assertGrainFileContent("a: aval\n") + assert_grain_file_content("a: aval\n") # Delete a previously unset grain - with setGrains({"a": "aval", "foo": None}): + with set_grains({"a": "aval", "foo": None}): ret = grains.absent(name="foo", destructive=True) assert ret["result"] is True assert ret["comment"] == "Grain foo was deleted" assert ret["changes"] == {"deleted": "foo"} assert grains.__grains__ == {"a": "aval"} - assertGrainFileContent("a: aval\n") + assert_grain_file_content("a: aval\n") # Delete a nested grain - with setGrains( + with set_grains( { "a": "aval", "foo": [ @@ -555,7 +545,7 @@ def test_absent_delete(): "a": "aval", "foo": ["order", {"is": {"other": "value"}}, "correct"], } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n- order\n- is:\n other: value\n- correct\n" ) @@ -565,40 +555,40 @@ def test_absent_delete(): def test_append(): # Append to an existing list - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): ret = grains.append(name="foo", value="baz") assert ret["result"] is True assert ret["comment"] == "Value baz was added to grain foo" assert ret["changes"] == {"added": "baz"} assert grains.__grains__ == {"a": "aval", "foo": ["bar", "baz"]} - assertGrainFileContent("a: aval\nfoo:\n- bar\n- baz\n") + assert_grain_file_content("a: aval\nfoo:\n- bar\n- baz\n") def test_append_nested(): # Append to an existing nested list - with setGrains({"a": "aval", "foo": {"list": ["bar"]}}): + with set_grains({"a": "aval", "foo": {"list": ["bar"]}}): ret = grains.append(name="foo,list", value="baz", delimiter=",") assert ret["result"] is True assert ret["comment"] == "Value baz was added to grain foo:list" assert ret["changes"] == {"added": "baz"} assert grains.__grains__ == {"a": "aval", "foo": {"list": ["bar", "baz"]}} - assertGrainFileContent("a: aval\nfoo:\n list:\n - bar\n - baz\n") + assert_grain_file_content("a: aval\nfoo:\n list:\n - bar\n - baz\n") def test_append_already(): # Append to an existing list - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): ret = grains.append(name="foo", value="bar") assert ret["result"] is True assert ret["comment"] == "Value bar is already in the list " + "for grain foo" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": ["bar"]} - assertGrainFileContent("a: aval\nfoo:\n- bar\n") + assert_grain_file_content("a: aval\nfoo:\n- bar\n") def test_append_fails_not_a_list(): # Fail to append to an existing grain, not a list - with setGrains({"a": "aval", "foo": {"bar": "val"}}): + with set_grains({"a": "aval", "foo": {"bar": "val"}}): ret = grains.append(name="foo", value="baz") assert ret["result"] is False assert ret["comment"] == "Grain foo is not a valid list" @@ -608,18 +598,18 @@ def test_append_fails_not_a_list(): def test_append_convert_to_list(): # Append to an existing grain, converting to a list - with setGrains({"a": "aval", "foo": {"bar": "val"}}): - assertGrainFileContent("a: aval\nfoo:\n bar: val\n") + with set_grains({"a": "aval", "foo": {"bar": "val"}}): + assert_grain_file_content("a: aval\nfoo:\n bar: val\n") ret = grains.append(name="foo", value="baz", convert=True) assert ret["result"] is True assert ret["comment"] == "Value baz was added to grain foo" assert ret["changes"] == {"added": "baz"} assert grains.__grains__ == {"a": "aval", "foo": [{"bar": "val"}, "baz"]} - assertGrainFileContent("a: aval\nfoo:\n- bar: val\n- baz\n") + assert_grain_file_content("a: aval\nfoo:\n- bar: val\n- baz\n") # Append to an existing grain, converting to a list a multi-value dict - with setGrains({"a": "aval", "foo": {"bar": "val", "other": "value"}}): - assertGrainFileContent("a: aval\nfoo:\n bar: val\n other: value\n") + with set_grains({"a": "aval", "foo": {"bar": "val", "other": "value"}}): + assert_grain_file_content("a: aval\nfoo:\n bar: val\n other: value\n") ret = grains.append(name="foo", value="baz", convert=True) assert ret["result"] is True assert ret["comment"] == "Value baz was added to grain foo" @@ -628,12 +618,12 @@ def test_append_convert_to_list(): "a": "aval", "foo": [{"bar": "val", "other": "value"}, "baz"], } - assertGrainFileContent("a: aval\nfoo:\n- bar: val\n other: value\n- baz\n") + assert_grain_file_content("a: aval\nfoo:\n- bar: val\n other: value\n- baz\n") def test_append_fails_inexistent(): # Append to a non existing grain - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.append(name="foo", value="bar") assert ret["result"] is False assert ret["comment"] == "Grain foo does not exist" @@ -643,30 +633,30 @@ def test_append_fails_inexistent(): def test_append_convert_to_list_empty(): # Append to an existing list - with setGrains({"foo": None}): + with set_grains({"foo": None}): ret = grains.append(name="foo", value="baz", convert=True) assert ret["result"] is True assert ret["comment"] == "Value baz was added to grain foo" assert ret["changes"] == {"added": "baz"} assert grains.__grains__ == {"foo": ["baz"]} - assertGrainFileContent("foo:\n- baz\n") + assert_grain_file_content("foo:\n- baz\n") # 'list_present' function tests: 7 def test_list_present(): - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): ret = grains.list_present(name="foo", value="baz") assert ret["result"] is True assert ret["comment"] == "Append value baz to grain foo" assert ret["changes"] == {"new": {"foo": ["bar", "baz"]}} assert grains.__grains__ == {"a": "aval", "foo": ["bar", "baz"]} - assertGrainFileContent("a: aval\nfoo:\n- bar\n- baz\n") + assert_grain_file_content("a: aval\nfoo:\n- bar\n- baz\n") def test_list_present_nested(): - with setGrains({"a": "aval", "foo": {"is": {"nested": ["bar"]}}}): + with set_grains({"a": "aval", "foo": {"is": {"nested": ["bar"]}}}): ret = grains.list_present(name="foo,is,nested", value="baz", delimiter=",") assert ret["result"] is True assert ret["comment"] == "Append value baz to grain foo:is:nested" @@ -675,63 +665,63 @@ def test_list_present_nested(): "a": "aval", "foo": {"is": {"nested": ["bar", "baz"]}}, } - assertGrainFileContent( + assert_grain_file_content( "a: aval\nfoo:\n is:\n nested:\n - bar\n - baz\n" ) def test_list_present_inexistent(): - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.list_present(name="foo", value="baz") assert ret["result"] is True assert ret["comment"] == "Append value baz to grain foo" assert ret["changes"] == {"new": {"foo": ["baz"]}} assert grains.__grains__ == {"a": "aval", "foo": ["baz"]} - assertGrainFileContent("a: aval\nfoo:\n- baz\n") + assert_grain_file_content("a: aval\nfoo:\n- baz\n") def test_list_present_inexistent_nested(): - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.list_present(name="foo:is:nested", value="baz") assert ret["result"] is True assert ret["comment"] == "Append value baz to grain foo:is:nested" assert ret["changes"] == {"new": {"foo": {"is": {"nested": ["baz"]}}}} assert grains.__grains__ == {"a": "aval", "foo": {"is": {"nested": ["baz"]}}} - assertGrainFileContent("a: aval\nfoo:\n is:\n nested:\n - baz\n") + assert_grain_file_content("a: aval\nfoo:\n is:\n nested:\n - baz\n") def test_list_present_not_a_list(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): ret = grains.list_present(name="foo", value="baz") assert ret["result"] is False assert ret["comment"] == "Grain foo is not a valid list" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - assertGrainFileContent("a: aval\nfoo: bar\n") + assert_grain_file_content("a: aval\nfoo: bar\n") def test_list_present_nested_already(): - with setGrains({"a": "aval", "b": {"foo": ["bar"]}}): + with set_grains({"a": "aval", "b": {"foo": ["bar"]}}): ret = grains.list_present(name="b:foo", value="bar") assert ret["result"] is True assert ret["comment"] == "Value bar is already in grain b:foo" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "b": {"foo": ["bar"]}} - assertGrainFileContent("a: aval\nb:\n foo:\n - bar\n") + assert_grain_file_content("a: aval\nb:\n foo:\n - bar\n") def test_list_present_already(): - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): ret = grains.list_present(name="foo", value="bar") assert ret["result"] is True assert ret["comment"] == "Value bar is already in grain foo" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": ["bar"]} - assertGrainFileContent("a: aval\nfoo:\n- bar\n") + assert_grain_file_content("a: aval\nfoo:\n- bar\n") def test_list_present_unknown_failure(): - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): # Unknown reason failure with patch.dict(grainsmod.__salt__, {"grains.append": MagicMock()}): @@ -740,67 +730,67 @@ def test_list_present_unknown_failure(): assert ret["comment"] == "Failed append value baz to grain foo" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": ["bar"]} - assertGrainFileContent("a: aval\nfoo:\n- bar\n") + assert_grain_file_content("a: aval\nfoo:\n- bar\n") # 'list_absent' function tests: 6 def test_list_absent(): - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): ret = grains.list_absent(name="foo", value="bar") assert ret["result"] is True assert ret["comment"] == "Value bar was deleted from grain foo" assert ret["changes"] == {"deleted": ["bar"]} assert grains.__grains__ == {"a": "aval", "foo": []} - assertGrainFileContent("a: aval\nfoo: []\n") + assert_grain_file_content("a: aval\nfoo: []\n") def test_list_absent_nested(): - with setGrains({"a": "aval", "foo": {"list": ["bar"]}}): + with set_grains({"a": "aval", "foo": {"list": ["bar"]}}): ret = grains.list_absent(name="foo:list", value="bar") assert ret["result"] is True assert ret["comment"] == "Value bar was deleted from grain foo:list" assert ret["changes"] == {"deleted": ["bar"]} assert grains.__grains__ == {"a": "aval", "foo": {"list": []}} - assertGrainFileContent("a: aval\nfoo:\n list: []\n") + assert_grain_file_content("a: aval\nfoo:\n list: []\n") def test_list_absent_inexistent(): - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.list_absent(name="foo", value="baz") assert ret["result"] is True assert ret["comment"] == "Grain foo does not exist" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval"} - assertGrainFileContent("a: aval\n") + assert_grain_file_content("a: aval\n") def test_list_absent_inexistent_nested(): - with setGrains({"a": "aval"}): + with set_grains({"a": "aval"}): ret = grains.list_absent(name="foo:list", value="baz") assert ret["result"] is True assert ret["comment"] == "Grain foo:list does not exist" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval"} - assertGrainFileContent("a: aval\n") + assert_grain_file_content("a: aval\n") def test_list_absent_not_a_list(): - with setGrains({"a": "aval", "foo": "bar"}): + with set_grains({"a": "aval", "foo": "bar"}): ret = grains.list_absent(name="foo", value="bar") assert ret["result"] is False assert ret["comment"] == "Grain foo is not a valid list" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": "bar"} - assertGrainFileContent("a: aval\nfoo: bar\n") + assert_grain_file_content("a: aval\nfoo: bar\n") def test_list_absent_already(): - with setGrains({"a": "aval", "foo": ["bar"]}): + with set_grains({"a": "aval", "foo": ["bar"]}): ret = grains.list_absent(name="foo", value="baz") assert ret["result"] is True assert ret["comment"] == "Value baz is absent from grain foo" assert ret["changes"] == {} assert grains.__grains__ == {"a": "aval", "foo": ["bar"]} - assertGrainFileContent("a: aval\nfoo:\n- bar\n") + assert_grain_file_content("a: aval\nfoo:\n- bar\n")