# -*- 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,
    MagicMock,
    patch,
    mock_open)

from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
from salt.states import apache
import salt.utils

apache.__opts__ = {}
apache.__salt__ = {}


@skipIf(NO_MOCK, NO_MOCK_REASON)
class ApacheTestCase(TestCase):
    '''
    Test cases for salt.states.apache
    '''
    # 'configfile' function tests: 1

    @patch('os.path.exists', MagicMock(return_value=True))
    def test_configfile(self):
        '''
        Test to allows for inputting a yaml dictionary into a file
        for apache configuration files.
        '''
        name = 'yaml'
        config = 'VirtualHost: this: "*:80"'

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

        mock = MagicMock(side_effect=[config, '', ''])
        with patch.object(salt.utils, 'fopen', mock_open(read_data=config)):
            with patch.dict(apache.__salt__,
                            {'apache.config': mock}):
                ret.update({'comment': 'Configuration is up to date.'})
                self.assertDictEqual(apache.configfile(name, config), ret)

                ret.update({'comment': 'Configuration will update.',
                            'changes': {'new': '',
                                        'old': 'VirtualHost: this: "*:80"'},
                            'result': None})
                with patch.dict(apache.__opts__, {'test': True}):
                    self.assertDictEqual(apache.configfile(name, config), ret)

                ret.update({'comment': 'Successfully created configuration.',
                            'result': True})
                with patch.dict(apache.__opts__, {'test': False}):
                    self.assertDictEqual(apache.configfile(name, config), ret)


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

@@ -911,20 +911,48 @@
 name = '
-yaml
+/etc/distro/specific/apache.conf
 '%0A      
@@ -989,16 +989,65 @@
  %22*:80%22'
+%0A        new_config = 'LiteralHost: that: %22*:79%22'
 %0A%0A      
@@ -1165,63 +1165,8 @@
 '%7D%0A%0A
-        mock = MagicMock(side_effect=%5Bconfig, '', ''%5D)%0A
     
@@ -1238,16 +1238,73 @@
 nfig)):%0A
+            mock_config = MagicMock(return_value=config)%0A
         
@@ -1343,36 +1343,8 @@
 t__,
-%0A                           
  %7B'a
@@ -1362,16 +1362,23 @@
 g': mock
+_config
 %7D):%0A    
@@ -1513,32 +1513,248 @@
  config), ret)%0A%0A
+        with patch.object(salt.utils, 'fopen', mock_open(read_data=config)):%0A            mock_config = MagicMock(return_value=new_config)%0A            with patch.dict(apache.__salt__, %7B'apache.config': mock_config%7D):%0A
                 
@@ -1853,18 +1853,26 @@
 %7B'new': 
-''
+new_config
 ,%0A      
@@ -1916,35 +1916,14 @@
 d': 
-'VirtualHost: this: %22*:80%22'
+config
 %7D,%0A 
@@ -2089,32 +2089,36 @@
 onfigfile(name, 
+new_
 config), ret)%0A%0A 
@@ -2108,32 +2108,248 @@
 _config), ret)%0A%0A
+        with patch.object(salt.utils, 'fopen', mock_open(read_data=config)):%0A            mock_config = MagicMock(return_value=new_config)%0A            with patch.dict(apache.__salt__, %7B'apache.config': mock_config%7D):%0A
                 

fix failing apache state unit test