123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- # -*- coding: utf-8 -*-
- """
- Test cases for xml state
- """
- # Import Python libs
- from __future__ import absolute_import, print_function, unicode_literals
- # Import Salt Libs
- import salt.states.xml as xml
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, patch
- from tests.support.unit import TestCase
- class XMLTestCase(TestCase, LoaderModuleMockMixin):
- """
- Test cases for salt.states.xml
- """
- def setup_loader_modules(self):
- return {xml: {}}
- def test_value_already_present(self):
- """
- Test for existing value_present
- """
- name = "testfile.xml"
- xpath = ".//list[@id='1']"
- value = "test value"
- state_return = {
- "name": name,
- "changes": {},
- "result": True,
- "comment": "{0} is already present".format(value),
- }
- with patch.dict(xml.__salt__, {"xml.get_value": MagicMock(return_value=value)}):
- self.assertDictEqual(xml.value_present(name, xpath, value), state_return)
- def test_value_update(self):
- """
- Test for updating value_present
- """
- name = "testfile.xml"
- xpath = ".//list[@id='1']"
- value = "test value"
- old_value = "not test value"
- state_return = {
- "name": name,
- "changes": {name: {"new": value, "old": old_value}},
- "result": True,
- "comment": "{0} updated".format(name),
- }
- with patch.dict(
- xml.__salt__, {"xml.get_value": MagicMock(return_value=old_value)}
- ):
- with patch.dict(
- xml.__salt__, {"xml.set_value": MagicMock(return_value=True)}
- ):
- self.assertDictEqual(
- xml.value_present(name, xpath, value), state_return
- )
- def test_value_update_test(self):
- """
- Test for value_present test=True
- """
- name = "testfile.xml"
- xpath = ".//list[@id='1']"
- value = "test value"
- old_value = "not test value"
- state_return = {
- "name": name,
- "changes": {name: {"old": old_value, "new": value}},
- "result": None,
- "comment": "{0} will be updated".format(name),
- }
- with patch.dict(
- xml.__salt__, {"xml.get_value": MagicMock(return_value=old_value)}
- ):
- self.assertDictEqual(
- xml.value_present(name, xpath, value, test=True), state_return
- )
- def test_value_update_invalid_xpath(self):
- """
- Test for value_present invalid xpath
- """
- name = "testfile.xml"
- xpath = ".//list[@id='1']"
- value = "test value"
- state_return = {
- "name": name,
- "changes": {},
- "result": False,
- "comment": "xpath query {0} not found in {1}".format(xpath, name),
- }
- with patch.dict(xml.__salt__, {"xml.get_value": MagicMock(return_value=False)}):
- self.assertDictEqual(
- xml.value_present(name, xpath, value, test=True), state_return
- )
|