def test_present_overwrite(self): self.setGrains({'a': 'aval', 'foo': 'bar'}) # Overwrite an existing grain ret = grains.present( name='foo', value='newbar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': 'newbar'}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'newbar'}) self.assertGrainFileContent("a: aval\n" + "foo: newbar\n" ) self.setGrains({'a': 'aval', 'foo': 'bar'}) # Clear a grain (set to None) ret = grains.present( name='foo', value=None) self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': None}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': None}) self.assertGrainFileContent("a: aval\n" + "foo: null\n" ) self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) # Overwrite an existing nested grain ret = grains.present( name='foo:is:nested', value='newbar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': 'newbar'}}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'newbar'}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested: newbar\n" ) self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) # Clear a nested grain (set to None) ret = grains.present( name='foo:is:nested', value=None) self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': None}}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': None}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested: null\n" )
def test_present_overwrite(self): grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": "bar"} # Overwrite an existing grain ret = grains.present(name="foo", value="newbar") self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": "newbar"}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "newbar"}) # Overwrite a grain to a list ret = grains.present(name="foo", value=["l1", "l2"]) self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": ["l1", "l2"]}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["l1", "l2"]}) grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": "bar"} # Fails setting a grain to a dict ret = grains.present(name="foo", value={"k1": "v1"}) self.assertEqual(ret["result"], False) self.assertEqual(ret["comment"], "Grain value cannot be dict") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"}) grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": "bar"} # Clear a grain (set to None) ret = grains.present(name="foo", value=None) self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": None}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": None})
def test_present_fails_to_set_dict_or_list(self): self.setGrains({'a': 'aval', 'foo': 'bar'}) # Fails to overwrite a grain to a list ret = grains.present( name='foo', value=['l1', 'l2']) self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'The key \'foo\' exists and the ' + 'given value is a dict or a list. ' + 'Use \'force=True\' to overwrite.') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.setGrains({'a': 'aval', 'foo': 'bar'}) # Fails setting a grain to a dict ret = grains.present( name='foo', value={'k1': 'v1'}) self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'The key \'foo\' exists and the given ' + 'value is a dict or a list. Use ' + '\'force=True\' to overwrite.') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.setGrains({'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=',') self.assertEqual(ret['result'], False) self.assertEqual(ret['changes'], {}) self.assertEqual(ret['comment'], 'The key \'foo:is:nested\' exists and the ' + 'given value is a dict or a list. ' + 'Use \'force=True\' to overwrite.') self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) # Fails setting a nested grain to a dict ret = grains.present( name='foo:is:nested', value={'k1': 'v1'}) self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'The key \'foo:is:nested\' exists and the ' + 'given value is a dict or a list. ' + 'Use \'force=True\' to overwrite.') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'bar'}}})
def test_present_add(self): # Set a non existing grain grains.__grains__ = grainsmod.__grains__ = {"a": "aval"} ret = grains.present(name="foo", value="bar") self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": "bar"}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"})
def test_present_add(self): # Set a non existing grain self.setGrains({'a': 'aval'}) ret = grains.present( name='foo', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': 'bar'}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.assertGrainFileContent("a: aval\n" + "foo: bar\n" ) # Set a non existing nested grain self.setGrains({'a': 'aval'}) ret = grains.present( name='foo:is:nested', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': 'bar'}}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested: bar\n" ) # Set a non existing nested dict grain self.setGrains({'a': 'aval'}) ret = grains.present( name='foo:is:nested', value={'bar': 'is a dict'}) self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': {'bar': 'is a dict'}}}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': {'bar': 'is a dict'}}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " bar: is a dict\n" )
def test_present_already_set(self): grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": "bar"} # Grain already set ret = grains.present(name="foo", value="bar") self.assertEqual(ret["result"], True) self.assertEqual(ret["comment"], "Grain is already set") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"})
def test_present_unknown_failure(self): grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": "bar"} # Unknown reason failure grainsmod.__salt__["grains.setval"] = MagicMock() ret = grains.present(name="foo", value="baz") self.assertEqual(ret["result"], False) self.assertEqual(ret["comment"], "Failed to set grain foo") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"})
def test_present_unknown_failure(self, mocked_setval): mocked_setval.return_value = 'Failed to set grain foo' self.setGrains({'a': 'aval', 'foo': 'bar'}) # Unknown reason failure ret = grains.present(name='foo', value='baz') self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'Failed to set grain foo') self.assertEqual(ret['changes'], {}) self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.assertGrainFileContent("a: aval\n" + "foo: bar\n")
def test_present_fail_merge_dict(self): self.setGrains({'a': 'aval', 'foo': {'k1': 'v1'}}) # Fails setting a grain to a dict ret = grains.present(name='foo', value={'k2': 'v2'}) self.assertEqual(ret['result'], False) self.assertEqual( ret['comment'], 'The key \'foo\' exists but ' + 'is a dict or a list. ' + 'Use \'force=True\' to overwrite.') self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': {'k1': 'v1'}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n")
def test_present_fails_to_convert_value_to_key(self): self.setGrains({'a': 'aval', 'foo': 'bar'}) # Fails converting a value to a nested grain key ret = grains.present(name='foo:is:nested', value={'k1': 'v1'}) self.assertEqual(ret['result'], False) self.assertEqual( ret['comment'], 'The key \'foo\' value is \'bar\', ' + 'which is different from the provided ' + 'key \'is\'. Use \'force=True\' to overwrite.') self.assertEqual(ret['changes'], {})
def test_present_add(self): # Set a non existing grain grains.__grains__ = grainsmod.__grains__ = {'a': 'aval'} ret = grains.present( name='foo', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': 'bar'}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'})
def test_present_fails_to_convert_value_to_key(self): self.setGrains({'a': 'aval', 'foo': 'bar'}) # Fails converting a value to a nested grain key ret = grains.present( name='foo:is:nested', value={'k1': 'v1'}) self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'The key \'foo\' value is \'bar\', ' + 'which is different from the provided ' + 'key \'is\'. Use \'force=True\' to overwrite.') self.assertEqual(ret['changes'], {})
def test_present_fails_to_convert_value_to_key(self): with self.setGrains({"a": "aval", "foo": "bar"}): # Fails converting a value to a nested grain key ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) self.assertEqual(ret["result"], False) self.assertEqual( ret["comment"], "The key 'foo' value is 'bar', " + "which is different from the provided " + "key 'is'. Use 'force=True' to overwrite.", ) self.assertEqual(ret["changes"], {})
def test_present_already_set(self): grains.__grains__ = grainsmod.__grains__ = {'a': 'aval', 'foo': 'bar'} # Grain already set ret = grains.present( name='foo', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'})
def test_present_unknown_failure(self): grains.__grains__ = grainsmod.__grains__ = {'a': 'aval', 'foo': 'bar'} # Unknown reason failure grainsmod.__salt__['grains.setval'] = MagicMock() ret = grains.present( name='foo', value='baz') self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'Failed to set grain foo') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'})
def test_present_already_set(self): with self.setGrains({'a': 'aval', 'foo': 'bar'}): # Grain already set ret = grains.present(name='foo', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': 'bar'}) with self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}): # Nested grain already set ret = grains.present(name='foo:is:nested', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'is': { 'nested': 'bar' } } }) with self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}): # Nested dict grain already set ret = grains.present(name='foo:is', value={'nested': 'bar'}) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'is': { 'nested': 'bar' } } })
def test_present_already_set(self): with self.setGrains({"a": "aval", "foo": "bar"}): # Grain already set ret = grains.present(name="foo", value="bar") self.assertEqual(ret["result"], True) self.assertEqual(ret["comment"], "Grain is already set") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"}) with self.setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Nested grain already set ret = grains.present(name="foo:is:nested", value="bar") self.assertEqual(ret["result"], True) self.assertEqual(ret["comment"], "Grain is already set") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "bar" } } }) with self.setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Nested dict grain already set ret = grains.present(name="foo:is", value={"nested": "bar"}) self.assertEqual(ret["result"], True) self.assertEqual(ret["comment"], "Grain is already set") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "bar" } } })
def test_present_convert_value_to_key(self): self.setGrains({'a': 'aval', 'foo': 'is'}) # Converts a value to a nested grain key ret = grains.present( name='foo:is:nested', value={'k1': 'v1'}) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo:is:nested to {\'k1\': \'v1\'}') self.assertEqual(ret['changes'], {'foo': {'is': {'nested': {'k1': 'v1'}}}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': {'k1': 'v1'}}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " k1: v1\n" ) self.setGrains({'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'}) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo:is:nested to {\'k1\': \'v1\'}') self.assertEqual(ret['changes'], {'foo': ['one', {'is': {'nested': {'k1': 'v1'}}}, 'correct']}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': ['one', {'is': {'nested': {'k1': 'v1'}}}, 'correct']}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- one\n" + "- is:\n" + " nested:\n" + " k1: v1\n" + "- correct\n" )
def test_present_fail_overwrite(self): with self.setGrains({"a": "aval", "foo": {"is": {"nested": "val"}}}): # Overwrite an existing grain ret = grains.present(name="foo:is", value="newbar") self.assertEqual(ret["result"], False) self.assertEqual(ret["changes"], {}) self.assertEqual( ret["comment"], "The key 'foo:is' exists but is a dict or a list. Use 'force=True' to" " overwrite.", ) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "val" } } }) with self.setGrains({"a": "aval", "foo": {"is": {"nested": "val"}}}): # Clear a grain (set to None) ret = grains.present(name="foo:is", value=None) self.assertEqual(ret["result"], False) self.assertEqual(ret["changes"], {}) self.assertEqual( ret["comment"], "The key 'foo:is' exists but is a dict or a list. Use 'force=True' to" " overwrite.", ) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "val" } } })
def test_present_unknown_failure(self): with patch("salt.modules.grains.setval") as mocked_setval: mocked_setval.return_value = "Failed to set grain foo" with self.setGrains({"a": "aval", "foo": "bar"}): # Unknown reason failure ret = grains.present(name="foo", value="baz") self.assertEqual(ret["result"], False) self.assertEqual(ret["comment"], "Failed to set grain foo") self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": "bar" }) self.assertGrainFileContent("a: aval\n" + "foo: bar\n")
def test_present_add(self): # Set a non existing grain with self.setGrains({'a': 'aval'}): ret = grains.present(name='foo', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': 'bar'}) self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.assertGrainFileContent("a: aval\nfoo: bar\n") # Set a non existing nested grain with self.setGrains({'a': 'aval'}): ret = grains.present(name='foo:is:nested', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': 'bar'}}}) self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) self.assertGrainFileContent("a: aval\n" "foo:\n" " is:\n" " nested: bar\n" ) # Set a non existing nested dict grain with self.setGrains({'a': 'aval'}): ret = grains.present( name='foo:is:nested', value={'bar': 'is a dict'}) self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': {'bar': 'is a dict'}}}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': {'bar': 'is a dict'}}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " bar: is a dict\n" )
def test_present_overwrite_test(self): with patch.dict(grains.__opts__, {'test': True}): with self.setGrains({'a': 'aval', 'foo': 'bar'}): # Overwrite an existing grain ret = grains.present(name='foo', value='newbar') self.assertEqual(ret['result'], None) self.assertEqual(ret['changes'], {'changed': { 'foo': 'newbar' }}) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': 'bar' }) self.assertGrainFileContent("a: aval\n" + "foo: bar\n")
def test_present_already_set(self): self.setGrains({'a': 'aval', 'foo': 'bar'}) # Grain already set ret = grains.present( name='foo', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) # Nested grain already set ret = grains.present( name='foo:is:nested', value='bar') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'bar'}}}) # Nested dict grain already set ret = grains.present( name='foo:is', value={'nested': 'bar'}) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Grain is already set') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'bar'}}})
def test_present_fail_overwrite(self): with self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'val'}}}): # Overwrite an existing grain ret = grains.present(name='foo:is', value='newbar') self.assertEqual(ret['result'], False) self.assertEqual(ret['changes'], {}) self.assertEqual( ret['comment'], 'The key \'foo:is\' exists but is a dict or a list. Use \'force=True\' to overwrite.' ) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'is': { 'nested': 'val' } } }) with self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'val'}}}): # Clear a grain (set to None) ret = grains.present(name='foo:is', value=None) self.assertEqual(ret['result'], False) self.assertEqual(ret['changes'], {}) self.assertEqual( ret['comment'], 'The key \'foo:is\' exists but is a dict or a list. Use \'force=True\' to overwrite.' ) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'is': { 'nested': 'val' } } })
def test_present_fail_overwrite(self): self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'val'}}}) # Overwrite an existing grain ret = grains.present( name='foo:is', value='newbar') self.assertEqual(ret['result'], False) self.assertEqual(ret['changes'], {}) self.assertEqual(ret['comment'], 'The key \'foo:is\' exists but is a dict or a list. Use \'force=True\' to overwrite.') self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'val'}}}) self.setGrains({'a': 'aval', 'foo': {'is': {'nested': 'val'}}}) # Clear a grain (set to None) ret = grains.present( name='foo:is', value=None) self.assertEqual(ret['result'], False) self.assertEqual(ret['changes'], {}) self.assertEqual(ret['comment'], 'The key \'foo:is\' exists but is a dict or a list. Use \'force=True\' to overwrite.') self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': 'val'}}})
def test_present_overwrite_test(self): with patch.dict(grains.__opts__, {"test": True}): with self.setGrains({"a": "aval", "foo": "bar"}): # Overwrite an existing grain ret = grains.present(name="foo", value="newbar") self.assertEqual(ret["result"], None) self.assertEqual(ret["changes"], {"changed": { "foo": "newbar" }}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": "bar" }) self.assertGrainFileContent("a: aval\n" + "foo: bar\n")
def test_present(self): ''' Test to ensure that a grain is set ''' ret = {'comment': 'Grain value cannot be dict', 'changes': {}, 'name': self.name, 'result': False} ret1 = {'changes': {}, 'name': self.name, 'result': True, 'comment': 'Grain is already set'} ret2 = {'changes': {'new': self.name}, 'name': self.name, 'result': None, 'comment': 'Grain cheese is set to be added'} ret3 = {'changes': {}, 'name': self.name, 'result': False, 'comment': 'Failed to set grain cheese'} ret4 = {'changes': {self.name: self.value}, 'name': self.name, 'result': True, 'comment': 'Set grain cheese to edam'} self.assertDictEqual(grains.present(self.name, {}), ret) with patch.dict(grains.__grains__, {self.name: self.value}): self.assertDictEqual(grains.present(self.name, self.value), ret1) with patch.dict(grains.__opts__, {'test': True}): self.assertDictEqual(grains.present(self.name, self.value), ret2) with patch.dict(grains.__opts__, {'test': False}): mock = MagicMock(side_effect=[{self.name: 'eves'}, {self.name: self.value}]) with patch.dict(grains.__salt__, {'grains.setval': mock}): self.assertDictEqual(grains.present(self.name, self.value), ret3) self.assertDictEqual(grains.present(self.name, self.value), ret4)
def test_present_overwrite_test(self): grains.__opts__['test'] = True self.setGrains({'a': 'aval', 'foo': 'bar'}) # Overwrite an existing grain ret = grains.present( name='foo', value='newbar') self.assertEqual(ret['result'], None) self.assertEqual(ret['changes'], {'changed': {'foo': 'newbar'}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.assertGrainFileContent("a: aval\n" + "foo: bar\n" )
def test_present_add_key_to_existing(self): with self.setGrains({"a": "aval", "foo": {"k1": "v1"}}): # Fails setting a grain to a dict ret = grains.present(name="foo:k2", value="v2") self.assertEqual(ret["result"], True) self.assertEqual(ret["comment"], "Set grain foo:k2 to v2") self.assertEqual(ret["changes"], {"foo": {"k2": "v2", "k1": "v1"}}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "k1": "v1", "k2": "v2" } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n" + " k2: v2\n")
def test_present_unknown_failure(self, mocked_setval): mocked_setval.return_value = 'Failed to set grain foo' self.setGrains({'a': 'aval', 'foo': 'bar'}) # Unknown reason failure ret = grains.present( name='foo', value='baz') self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'Failed to set grain foo') self.assertEqual(ret['changes'], {}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': 'bar'}) self.assertGrainFileContent("a: aval\n" + "foo: bar\n" )
def test_present_add_key_to_existing(self): with self.setGrains({'a': 'aval', 'foo': {'k1': 'v1'}}): # Fails setting a grain to a dict ret = grains.present(name='foo:k2', value='v2') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo:k2 to v2') self.assertEqual(ret['changes'], {'foo': {'k2': 'v2', 'k1': 'v1'}}) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'k1': 'v1', 'k2': 'v2' } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n" + " k2: v2\n")
def test_present_fail_merge_dict(self): with self.setGrains({"a": "aval", "foo": {"k1": "v1"}}): # Fails setting a grain to a dict ret = grains.present(name="foo", value={"k2": "v2"}) self.assertEqual(ret["result"], False) self.assertEqual( ret["comment"], "The key 'foo' exists but " + "is a dict or a list. " + "Use 'force=True' to overwrite.", ) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "k1": "v1" } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n")
def test_present_fail_merge_dict(self): self.setGrains({'a': 'aval', 'foo': {'k1': 'v1'}}) # Fails setting a grain to a dict ret = grains.present( name='foo', value={'k2': 'v2'}) self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'The key \'foo\' exists but ' + 'is a dict or a list. ' + 'Use \'force=True\' to overwrite.') self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'k1': 'v1'}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n" )
def test_present_add_key_to_existing(self): self.setGrains({'a': 'aval', 'foo': {'k1': 'v1'}}) # Fails setting a grain to a dict ret = grains.present( name='foo:k2', value='v2') self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo:k2 to v2') self.assertEqual(ret['changes'], {'foo': {'k2': 'v2', 'k1': 'v1'}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'k1': 'v1', 'k2': 'v2'}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n" + " k2: v2\n" )
def test_present_add(self): # Set a non existing grain with self.setGrains({"a": "aval"}): ret = grains.present(name="foo", value="bar") self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": "bar"}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"}) self.assertGrainFileContent("a: aval\nfoo: bar\n") # Set a non existing nested grain with self.setGrains({"a": "aval"}): ret = grains.present(name="foo:is:nested", value="bar") self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": { "is": { "nested": "bar" } }}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "bar" } } }) self.assertGrainFileContent("a: aval\n" "foo:\n" " is:\n" " nested: bar\n") # Set a non existing nested dict grain with self.setGrains({"a": "aval"}): ret = grains.present(name="foo:is:nested", value={"bar": "is a dict"}) self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": { "is": { "nested": { "bar": "is a dict" } } }}) self.assertEqual( grains.__grains__, { "a": "aval", "foo": { "is": { "nested": { "bar": "is a dict" } } } }, ) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " bar: is a dict\n")
def test_present_fails_to_set_dict_or_list(self): with self.setGrains({"a": "aval", "foo": "bar"}): # Fails to overwrite a grain to a list ret = grains.present(name="foo", value=["l1", "l2"]) self.assertEqual(ret["result"], False) self.assertEqual( ret["comment"], "The key 'foo' exists and the " + "given value is a dict or a list. " + "Use 'force=True' to overwrite.", ) self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"}) with self.setGrains({"a": "aval", "foo": "bar"}): # Fails setting a grain to a dict ret = grains.present(name="foo", value={"k1": "v1"}) self.assertEqual(ret["result"], False) self.assertEqual( ret["comment"], "The key 'foo' exists and the given " + "value is a dict or a list. Use " + "'force=True' to overwrite.", ) self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"}) with self.setGrains({"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=",") self.assertEqual(ret["result"], False) self.assertEqual(ret["changes"], {}) self.assertEqual( ret["comment"], "The key 'foo:is:nested' exists and the " + "given value is a dict or a list. " + "Use 'force=True' to overwrite.", ) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "bar" } } }) with self.setGrains({"a": "aval", "foo": {"is": {"nested": "bar"}}}): # Fails setting a nested grain to a dict ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) self.assertEqual(ret["result"], False) self.assertEqual( ret["comment"], "The key 'foo:is:nested' exists and the " + "given value is a dict or a list. " + "Use 'force=True' to overwrite.", ) self.assertEqual(ret["changes"], {}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "is": { "nested": "bar" } } })
def test_present_force_to_set_dict_or_list(self): self.setGrains({'a': 'aval', 'foo': 'bar'}) # Force to overwrite a grain to a list ret = grains.present( name='foo', value=['l1', 'l2'], force=True) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo to [\'l1\', \'l2\']') self.assertEqual(ret['changes'], {'foo': ['l1', 'l2']}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': ['l1', 'l2']}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- l1\n" + "- l2\n" ) self.setGrains({'a': 'aval', 'foo': 'bar'}) # Force setting a grain to a dict ret = grains.present( name='foo', value={'k1': 'v1'}, force=True) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo to {\'k1\': \'v1\'}') self.assertEqual(ret['changes'], {'foo': {'k1': 'v1'}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'k1': 'v1'}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n" ) self.setGrains({'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) self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': {'is': {'nested': ['l1', 'l2']}}}) self.assertEqual(ret['comment'], 'Set grain foo:is:nested to [\'l1\', \'l2\']') self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': ['l1', 'l2']}}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " - l1\n" + " - l2\n" ) self.setGrains({'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) self.assertEqual(ret['result'], True) self.assertEqual(ret['comment'], 'Set grain foo:is:nested to {\'k1\': \'v1\'}') self.assertEqual(ret['changes'], {'foo': {'is': {'nested': {'k1': 'v1'}}, 'and': 'other'}}) self.assertEqual( grains.__grains__, {'a': 'aval', 'foo': {'is': {'nested': {'k1': 'v1'}}, 'and': 'other'}}) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " and: other\n" + " is:\n" + " nested:\n" + " k1: v1\n" )
def test_present_force_to_set_dict_or_list(self): with self.setGrains({"a": "aval", "foo": "bar"}): # Force to overwrite a grain to a list ret = grains.present(name="foo", value=["l1", "l2"], force=True) self.assertEqual(ret["result"], True) self.assertEqual( ret["comment"], "Set grain foo to ['l1', 'l2']" if six.PY3 else "Set grain foo to [u'l1', u'l2']", ) self.assertEqual(ret["changes"], {"foo": ["l1", "l2"]}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": ["l1", "l2"] }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- l1\n" + "- l2\n") with self.setGrains({"a": "aval", "foo": "bar"}): # Force setting a grain to a dict ret = grains.present(name="foo", value={"k1": "v1"}, force=True) self.assertEqual(ret["result"], True) self.assertEqual( ret["comment"], "Set grain foo to {'k1': 'v1'}" if six.PY3 else "Set grain foo to {u'k1': u'v1'}", ) self.assertEqual(ret["changes"], {"foo": {"k1": "v1"}}) self.assertEqual(grains.__grains__, { "a": "aval", "foo": { "k1": "v1" } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n") with self.setGrains({"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) self.assertEqual(ret["result"], True) self.assertEqual(ret["changes"], {"foo": { "is": { "nested": ["l1", "l2"] } }}) self.assertEqual( ret["comment"], "Set grain foo:is:nested to ['l1', 'l2']" if six.PY3 else "Set grain foo:is:nested to [u'l1', u'l2']", ) self.assertEqual( grains.__grains__, { "a": "aval", "foo": { "is": { "nested": ["l1", "l2"] } } }, ) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " - l1\n" + " - l2\n") with self.setGrains({ "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) self.assertEqual(ret["result"], True) self.assertEqual( ret["comment"], "Set grain foo:is:nested to {'k1': 'v1'}" if six.PY3 else "Set grain foo:is:nested to {u'k1': u'v1'}", ) self.assertEqual( ret["changes"], {"foo": { "is": { "nested": { "k1": "v1" } }, "and": "other" }}, ) self.assertEqual( grains.__grains__, { "a": "aval", "foo": { "is": { "nested": { "k1": "v1" } }, "and": "other" } }, ) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " and: other\n" + " is:\n" + " nested:\n" + " k1: v1\n")
def test_present_convert_value_to_key(self): with self.setGrains({"a": "aval", "foo": "is"}): # Converts a value to a nested grain key ret = grains.present(name="foo:is:nested", value={"k1": "v1"}) self.assertEqual(ret["result"], True) self.assertEqual( ret["comment"], "Set grain foo:is:nested to {'k1': 'v1'}" if six.PY3 else "Set grain foo:is:nested to {u'k1': u'v1'}", ) self.assertEqual(ret["changes"], {"foo": { "is": { "nested": { "k1": "v1" } } }}) self.assertEqual( grains.__grains__, { "a": "aval", "foo": { "is": { "nested": { "k1": "v1" } } } }, ) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " k1: v1\n") with self.setGrains({"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"}) self.assertEqual(ret["result"], True) self.assertEqual( ret["comment"], "Set grain foo:is:nested to {'k1': 'v1'}" if six.PY3 else "Set grain foo:is:nested to {u'k1': u'v1'}", ) self.assertEqual( ret["changes"], {"foo": ["one", { "is": { "nested": { "k1": "v1" } } }, "correct"]}, ) self.assertEqual( grains.__grains__, { "a": "aval", "foo": ["one", { "is": { "nested": { "k1": "v1" } } }, "correct"], }, ) self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- one\n" + "- is:\n" + " nested:\n" + " k1: v1\n" + "- correct\n")
def test_present(self): ''' Test to ensure that a grain is set ''' ret = { 'comment': 'Grain value cannot be dict', 'changes': {}, 'name': self.name, 'result': False } ret1 = { 'changes': {}, 'name': self.name, 'result': True, 'comment': 'Grain is already set' } ret2 = { 'changes': { 'new': self.name }, 'name': self.name, 'result': None, 'comment': 'Grain cheese is set to be added' } ret3 = { 'changes': {}, 'name': self.name, 'result': False, 'comment': 'Failed to set grain cheese' } ret4 = { 'changes': { self.name: self.value }, 'name': self.name, 'result': True, 'comment': 'Set grain cheese to edam' } self.assertDictEqual(grains.present(self.name, {}), ret) with patch.dict(grains.__grains__, {self.name: self.value}): self.assertDictEqual(grains.present(self.name, self.value), ret1) with patch.dict(grains.__opts__, {'test': True}): self.assertDictEqual(grains.present(self.name, self.value), ret2) with patch.dict(grains.__opts__, {'test': False}): mock = MagicMock(side_effect=[{ self.name: 'eves' }, { self.name: self.value }]) with patch.dict(grains.__salt__, {'grains.setval': mock}): self.assertDictEqual(grains.present(self.name, self.value), ret3) self.assertDictEqual(grains.present(self.name, self.value), ret4)
def test_present_force_to_set_dict_or_list(self): with self.setGrains({'a': 'aval', 'foo': 'bar'}): # Force to overwrite a grain to a list ret = grains.present(name='foo', value=['l1', 'l2'], force=True) self.assertEqual(ret['result'], True) self.assertEqual( ret['comment'], "Set grain foo to ['l1', 'l2']" if six.PY3 else "Set grain foo to [u'l1', u'l2']") self.assertEqual(ret['changes'], {'foo': ['l1', 'l2']}) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': ['l1', 'l2'] }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- l1\n" + "- l2\n") with self.setGrains({'a': 'aval', 'foo': 'bar'}): # Force setting a grain to a dict ret = grains.present(name='foo', value={'k1': 'v1'}, force=True) self.assertEqual(ret['result'], True) self.assertEqual( ret['comment'], "Set grain foo to {'k1': 'v1'}" if six.PY3 else "Set grain foo to {u'k1': u'v1'}") self.assertEqual(ret['changes'], {'foo': {'k1': 'v1'}}) self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'k1': 'v1' } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " k1: v1\n") with self.setGrains({'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) self.assertEqual(ret['result'], True) self.assertEqual(ret['changes'], {'foo': { 'is': { 'nested': ['l1', 'l2'] } }}) self.assertEqual( ret['comment'], "Set grain foo:is:nested to ['l1', 'l2']" if six.PY3 else "Set grain foo:is:nested to [u'l1', u'l2']") self.assertEqual(grains.__grains__, { 'a': 'aval', 'foo': { 'is': { 'nested': ['l1', 'l2'] } } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " is:\n" + " nested:\n" + " - l1\n" + " - l2\n") with self.setGrains({ '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) self.assertEqual(ret['result'], True) self.assertEqual( ret['comment'], "Set grain foo:is:nested to {'k1': 'v1'}" if six.PY3 else "Set grain foo:is:nested to {u'k1': u'v1'}") self.assertEqual( ret['changes'], {'foo': { 'is': { 'nested': { 'k1': 'v1' } }, 'and': 'other' }}) self.assertEqual( grains.__grains__, { 'a': 'aval', 'foo': { 'is': { 'nested': { 'k1': 'v1' } }, 'and': 'other' } }) self.assertGrainFileContent("a: aval\n" + "foo:\n" + " and: other\n" + " is:\n" + " nested:\n" + " k1: v1\n")