2014-03-12 16:46:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-11-21 19:05:13 +00:00
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
2014-03-11 22:35:01 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Salt Testing libs
|
2014-11-26 18:28:15 +00:00
|
|
|
from salttesting.unit import TestCase
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Salt libs
|
2014-11-26 18:28:15 +00:00
|
|
|
import salt.utils
|
2014-03-11 22:35:01 +00:00
|
|
|
from salt.modules import ini_manage as ini
|
|
|
|
|
|
|
|
|
|
|
|
class IniManageTestCase(TestCase):
|
|
|
|
|
|
|
|
TEST_FILE_CONTENT = '''\
|
|
|
|
# Comment on the first line
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
# First main option
|
|
|
|
option1=main1
|
|
|
|
|
|
|
|
# Second main option
|
|
|
|
option2=main2
|
|
|
|
|
|
|
|
|
2014-03-11 22:35:01 +00:00
|
|
|
[main]
|
|
|
|
# Another comment
|
|
|
|
test1=value 1
|
2014-03-13 11:46:17 +00:00
|
|
|
|
2014-03-11 22:35:01 +00:00
|
|
|
test2=value 2
|
|
|
|
|
|
|
|
[SectionB]
|
|
|
|
test1=value 1B
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
# Blank line should be above
|
2014-03-11 22:35:01 +00:00
|
|
|
test3 = value 3B
|
|
|
|
|
|
|
|
[SectionC]
|
|
|
|
# The following option is empty
|
|
|
|
empty_option=
|
|
|
|
'''
|
|
|
|
|
|
|
|
maxDiff = None
|
|
|
|
|
|
|
|
def setUp(self):
|
2015-06-05 23:09:23 +00:00
|
|
|
self.tfile = tempfile.NamedTemporaryFile(delete=False, mode='w+')
|
2014-03-11 22:35:01 +00:00
|
|
|
self.tfile.write(self.TEST_FILE_CONTENT)
|
|
|
|
self.tfile.close()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.remove(self.tfile.name)
|
|
|
|
|
|
|
|
def test_get_option(self):
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'main', 'test1'),
|
|
|
|
'value 1')
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'main', 'test2'),
|
|
|
|
'value 2')
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'SectionB', 'test1'),
|
|
|
|
'value 1B')
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'SectionB', 'test3'),
|
|
|
|
'value 3B')
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'SectionC', 'empty_option'),
|
|
|
|
'')
|
|
|
|
|
|
|
|
def test_get_section(self):
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_section(self.tfile.name, 'SectionB'),
|
|
|
|
{'test1': 'value 1B', 'test3': 'value 3B'})
|
|
|
|
|
|
|
|
def test_remove_option(self):
|
|
|
|
self.assertEqual(
|
|
|
|
ini.remove_option(self.tfile.name, 'SectionB', 'test1'),
|
|
|
|
'value 1B')
|
|
|
|
self.assertIsNone(ini.get_option(self.tfile.name, 'SectionB', 'test1'))
|
|
|
|
|
|
|
|
def test_remove_section(self):
|
|
|
|
self.assertEqual(
|
|
|
|
ini.remove_section(self.tfile.name, 'SectionB'),
|
|
|
|
{'test1': 'value 1B', 'test3': 'value 3B'})
|
|
|
|
self.assertEqual(ini.get_section(self.tfile.name, 'SectionB'), {})
|
|
|
|
|
|
|
|
def test_set_option(self):
|
|
|
|
result = ini.set_option(self.tfile.name, {
|
|
|
|
'SectionB': {
|
|
|
|
'test3': 'new value 3B',
|
|
|
|
'test_set_option': 'test_set_value'
|
|
|
|
},
|
|
|
|
'SectionD': {
|
|
|
|
'test_set_option2': 'test_set_value1'
|
|
|
|
}
|
|
|
|
})
|
2015-08-21 07:06:11 +00:00
|
|
|
self.assertEqual(result, {
|
2014-03-11 22:41:53 +00:00
|
|
|
'SectionB': {'test3': {'after': 'new value 3B',
|
|
|
|
'before': 'value 3B'},
|
2014-03-11 22:35:01 +00:00
|
|
|
'test_set_option': {'after': 'test_set_value',
|
|
|
|
'before': None}
|
|
|
|
},
|
2015-08-21 09:17:13 +00:00
|
|
|
'SectionD': {'after': {'test_set_option2': 'test_set_value1',
|
|
|
|
'before': None}
|
2014-03-11 22:35:01 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
# Check existing option updated
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'SectionB', 'test3'),
|
|
|
|
'new value 3B')
|
|
|
|
# Check new section and option added
|
|
|
|
self.assertEqual(
|
|
|
|
ini.get_option(self.tfile.name, 'SectionD', 'test_set_option2'),
|
|
|
|
'test_set_value1')
|
|
|
|
|
|
|
|
def test_empty_value_preserved_after_edit(self):
|
|
|
|
ini.set_option(self.tfile.name, {
|
|
|
|
'SectionB': {'test3': 'new value 3B'},
|
|
|
|
})
|
2015-06-05 23:09:23 +00:00
|
|
|
with salt.utils.fopen(self.tfile.name, 'r') as fp:
|
2014-03-11 22:35:01 +00:00
|
|
|
file_content = fp.read()
|
2015-08-20 14:35:36 +00:00
|
|
|
self.assertIn('\nempty_option = \n', file_content,
|
2014-03-11 22:41:53 +00:00
|
|
|
'empty_option was not preserved')
|
2014-03-11 22:35:01 +00:00
|
|
|
|
|
|
|
def test_empty_lines_preserved_after_edit(self):
|
|
|
|
ini.set_option(self.tfile.name, {
|
|
|
|
'SectionB': {'test3': 'new value 3B'},
|
|
|
|
})
|
2015-06-05 23:09:23 +00:00
|
|
|
with salt.utils.fopen(self.tfile.name, 'r') as fp:
|
2014-03-11 22:35:01 +00:00
|
|
|
file_content = fp.read()
|
2014-03-13 11:46:17 +00:00
|
|
|
self.assertEqual('''\
|
|
|
|
# Comment on the first line
|
|
|
|
|
|
|
|
# First main option
|
2015-08-20 14:35:36 +00:00
|
|
|
option1 = main1
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
# Second main option
|
2015-08-20 14:35:36 +00:00
|
|
|
option2 = main2
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
[main]
|
|
|
|
# Another comment
|
2015-08-20 14:35:36 +00:00
|
|
|
test1 = value 1
|
2014-03-13 11:46:17 +00:00
|
|
|
|
2015-08-20 14:35:36 +00:00
|
|
|
test2 = value 2
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
[SectionB]
|
2015-08-20 14:35:36 +00:00
|
|
|
test1 = value 1B
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
# Blank line should be above
|
2015-08-20 14:35:36 +00:00
|
|
|
test3 = new value 3B
|
2014-03-13 11:46:17 +00:00
|
|
|
|
|
|
|
[SectionC]
|
|
|
|
# The following option is empty
|
2015-08-20 14:35:36 +00:00
|
|
|
empty_option =
|
2014-03-13 11:46:17 +00:00
|
|
|
''', file_content)
|
|
|
|
|
|
|
|
def test_empty_lines_preserved_after_multiple_edits(self):
|
|
|
|
ini.set_option(self.tfile.name, {
|
|
|
|
'SectionB': {'test3': 'this value will be edited two times'},
|
|
|
|
})
|
2014-03-13 12:32:52 +00:00
|
|
|
self.test_empty_lines_preserved_after_edit()
|
2014-03-31 01:57:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(IniManageTestCase, needs_daemon=False)
|