# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import

# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.mock import (
    NO_MOCK,
    NO_MOCK_REASON,
    patch)

from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
from salt.states import ini_manage

ini_manage.__salt__ = {}
ini_manage.__opts__ = {}


@skipIf(NO_MOCK, NO_MOCK_REASON)
class IniManageTestCase(TestCase):
    '''
    Test cases for salt.states.ini_manage
    '''
    # 'options_present' function tests: 1

    def test_options_present(self):
        '''
        Test to verify options present in file.
        '''
        name = 'salt'

        ret = {'name': name,
               'result': None,
               'comment': '',
               'changes': {}}

        with patch.dict(ini_manage.__opts__, {'test': True}):
            comt = (('ini file {0} shall be validated for presence of '
                     'given options under their respective '
                     'sections').format(name))
            ret.update({'comment': comt})
            self.assertDictEqual(ini_manage.options_present(name), ret)

        with patch.dict(ini_manage.__opts__, {'test': False}):
            comt = ('No anomaly detected')
            ret.update({'comment': comt, 'result': True})
            self.assertDictEqual(ini_manage.options_present(name), ret)

    # 'options_absent' function tests: 1

    def test_options_absent(self):
        '''
        Test to verify options absent in file.
        '''
        name = 'salt'

        ret = {'name': name,
               'result': None,
               'comment': '',
               'changes': {}}

        with patch.dict(ini_manage.__opts__, {'test': True}):
            comt = (('ini file {0} shall be validated for absence of '
                     'given options under their respective '
                     'sections').format(name))
            ret.update({'comment': comt})
            self.assertDictEqual(ini_manage.options_absent(name), ret)

        with patch.dict(ini_manage.__opts__, {'test': False}):
            comt = ('No anomaly detected')
            ret.update({'comment': comt, 'result': True})
            self.assertDictEqual(ini_manage.options_absent(name), ret)

    # 'sections_present' function tests: 1

    def test_sections_present(self):
        '''
        Test to verify sections present in file.
        '''
        name = 'salt'

        ret = {'name': name,
               'result': None,
               'comment': '',
               'changes': {}}

        with patch.dict(ini_manage.__opts__, {'test': True}):
            comt = (('ini file {0} shall be validated for '
                     'presence of given sections').format(name))
            ret.update({'comment': comt})
            self.assertDictEqual(ini_manage.sections_present(name), ret)

        with patch.dict(ini_manage.__opts__, {'test': False}):
            comt = ('No anomaly detected')
            ret.update({'comment': comt, 'result': True})
            self.assertDictEqual(ini_manage.sections_present(name), ret)

    # 'sections_absent' function tests: 1

    def test_sections_absent(self):
        '''
        Test to verify sections absent in file.
        '''
        name = 'salt'

        ret = {'name': name,
               'result': None,
               'comment': '',
               'changes': {}}

        with patch.dict(ini_manage.__opts__, {'test': True}):
            comt = (('ini file {0} shall be validated for absence of '
                     'given sections').format(name))
            ret.update({'comment': comt})
            self.assertDictEqual(ini_manage.sections_absent(name), ret)

        with patch.dict(ini_manage.__opts__, {'test': False}):
            comt = ('No anomaly detected')
            ret.update({'comment': comt, 'result': True})
            self.assertDictEqual(ini_manage.sections_absent(name), ret)


if __name__ == '__main__':
    from integration import run_tests
    run_tests(IniManageTestCase, needs_daemon=False)

@@ -249,16 +249,31 @@
 mport (%0A
+    MagicMock,%0A
     NO_M
@@ -277,16 +277,16 @@
 O_MOCK,%0A
-
     NO_M
@@ -1266,32 +1266,263 @@
 nt(name), ret)%0A%0A
+        changes = %7B'first': 'who is on',%0A                   'second': 'what is on',%0A                   'third': %22I don't know%22%7D%0A        with patch.dict(ini_manage.__salt__, %7B'ini.set_option': MagicMock(return_value=changes)%7D):%0A    
         with pat
@@ -1572,32 +1572,36 @@
 %7D):%0A            
+ 
+   
 comt = ('No anom
@@ -1593,38 +1593,42 @@
 omt = ('
-No anomaly det
+Changes take eff
 ect
-ed
 ')%0A
+    
         
@@ -1666,35 +1666,59 @@
 , 'result': True
-%7D)%0A
+, 'changes': changes%7D)%0A    
             self
@@ -3250,32 +3250,263 @@
 nt(name), ret)%0A%0A
+        changes = %7B'first': 'who is on',%0A                   'second': 'what is on',%0A                   'third': %22I don't know%22%7D%0A        with patch.dict(ini_manage.__salt__, %7B'ini.set_option': MagicMock(return_value=changes)%7D):%0A    
         with pat
@@ -3556,32 +3556,36 @@
 %7D):%0A            
+ 
+   
 comt = ('No anom
@@ -3577,38 +3577,42 @@
 omt = ('
-No anomaly det
+Changes take eff
 ect
-ed
 ')%0A
+    
         
@@ -3650,35 +3650,59 @@
 , 'result': True
-%7D)%0A
+, 'changes': changes%7D)%0A    
             self

update unit tests for ini state