# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
'''

# Import Python Libs
from __future__ import absolute_import

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

from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
from salt.modules import timezone

timezone.__salt__ = {}


@skipIf(NO_MOCK, NO_MOCK_REASON)
class TimezoneTestCase(TestCase):
    '''
    Timezone test case
    '''
    TEST_TZ = 'UTC'

    @patch('salt.utils.which', MagicMock(return_value=False))
    def test_get_zone_centos(self):
        '''
        Test CentOS is recognized
        :return: 
        '''
        timezone.__grains__ = {'os': 'centos'}
        with patch('salt.modules.timezone._get_zone_etc_localtime', MagicMock(return_value=self.TEST_TZ)):
            assert timezone.get_zone() == self.TEST_TZ

    @patch('salt.utils.which', MagicMock(return_value=False))
    def test_get_zone_os_family_rh_suse(self):
        '''
        Test RedHat and Suse are recognized
        :return: 
        '''
        for osfamily in ['RedHat', 'Suse']:
            timezone.__grains__ = {'os': 'unknown', 'os_family': [osfamily]}
            with patch('salt.modules.timezone._get_zone_sysconfig', MagicMock(return_value=self.TEST_TZ)):
                assert timezone.get_zone() == self.TEST_TZ

    @patch('salt.utils.which', MagicMock(return_value=False))
    def test_get_zone_os_family_debian_gentoo(self):
        '''
        Test Debian and Gentoo are recognized
        :return: 
        '''
        for osfamily in ['Debian', 'Gentoo']:
            timezone.__grains__ = {'os': 'unknown', 'os_family': [osfamily]}
            with patch('salt.modules.timezone._get_zone_etc_timezone', MagicMock(return_value=self.TEST_TZ)):
                assert timezone.get_zone() == self.TEST_TZ

    @patch('salt.utils.which', MagicMock(return_value=False))
    def test_get_zone_os_family_allbsd_nilinuxrt(self):
        '''
        Test *BSD and NILinuxRT are recognized
        :return: 
        '''
        for osfamily in ['FreeBSD', 'OpenBSD', 'NetBSD', 'NILinuxRT']:
            timezone.__grains__ = {'os': 'unknown', 'os_family': osfamily}
            with patch('salt.modules.timezone._get_zone_etc_localtime', MagicMock(return_value=self.TEST_TZ)):
                assert timezone.get_zone() == self.TEST_TZ

    @patch('salt.utils.which', MagicMock(return_value=False))
    def test_get_zone_os_family_slowlaris(self):
        '''
        Test Slowlaris is recognized
        :return: 
        '''
        timezone.__grains__ = {'os': 'unknown', 'os_family': ['Solaris']}
        with patch('salt.modules.timezone._get_zone_solaris', MagicMock(return_value=self.TEST_TZ)):
            assert timezone.get_zone() == self.TEST_TZ

    @patch('salt.utils.which', MagicMock(return_value=False))
    def test_get_zone_os_family_aix(self):
        '''
        Test IBM AIX is recognized
        :return: 
        '''
        timezone.__grains__ = {'os': 'unknown', 'os_family': ['AIX']}
        with patch('salt.modules.timezone._get_zone_aix', MagicMock(return_value=self.TEST_TZ)):
            assert timezone.get_zone() == self.TEST_TZ

@@ -436,32 +436,8 @@
 ne%0A%0A
-timezone.__salt__ = %7B%7D%0A%0A
 %0A@sk
@@ -560,16 +560,320 @@
  'UTC'%0A%0A
+    def setUp(self):%0A        '''%0A        Setup test%0A        :return:%0A        '''%0A        timezone.__salt__ = %7B%7D%0A        timezone.__grains__ = %7B'os': 'unknown'%7D%0A%0A    def tearDown(self):%0A        '''%0A        Teardown test%0A        :return:%0A        '''%0A        timezone.__salt__ = timezone.__grains__ = None%0A%0A
     @pat
@@ -1065,25 +1065,24 @@
 grains__
- = %7B
+%5B
 'os'
-:
+%5D =
  'centos
@@ -1082,17 +1082,16 @@
 'centos'
-%7D
 %0A       
@@ -1516,37 +1516,17 @@
 grains__
- = %7B'os': 'unknown', 
+%5B
 'os_fami
@@ -1520,33 +1520,35 @@
 ns__%5B'os_family'
-:
+%5D =
  %5Bosfamily%5D%7D%0A   
@@ -1534,33 +1534,32 @@
 y'%5D = %5Bosfamily%5D
-%7D
 %0A            wit
@@ -1990,37 +1990,17 @@
 grains__
- = %7B'os': 'unknown', 
+%5B
 'os_fami
@@ -2002,17 +2002,19 @@
 _family'
-:
+%5D =
  %5Bosfami
@@ -2016,17 +2016,16 @@
 sfamily%5D
-%7D
 %0A       
@@ -2496,37 +2496,17 @@
 grains__
- = %7B'os': 'unknown', 
+%5B
 'os_fami
@@ -2508,17 +2508,19 @@
 _family'
-:
+%5D =
  osfamil
@@ -2520,17 +2520,16 @@
 osfamily
-%7D
 %0A       
@@ -2909,37 +2909,17 @@
 grains__
- = %7B'os': 'unknown', 
+%5B
 'os_fami
@@ -2921,17 +2921,19 @@
 _family'
-:
+%5D =
  %5B'Solar
@@ -2936,17 +2936,16 @@
 olaris'%5D
-%7D
 %0A       
@@ -3307,29 +3307,9 @@
 ns__
- = %7B'os': 'unknown', 
+%5B
 'os_
@@ -3319,17 +3319,19 @@
 ily'
-:
+%5D =
  %5B'AIX'%5D
 %7D%0A  
@@ -3326,17 +3326,16 @@
  %5B'AIX'%5D
-%7D
 %0A       

Refactor with setup/teardown