def test_get_zone(self): ''' Test to get current timezone (i.e. America/Denver) ''' mock = MagicMock(side_effect=['Time zone: A', 'A']) with patch.object(salt.utils, 'which', return_value=True): with patch.dict(timezone.__salt__, {'cmd.run': mock}): self.assertEqual(timezone.get_zone(), 'A') self.assertRaises(CommandExecutionError, timezone.get_zone) with patch.object(salt.utils, 'which', return_value=False): file_data = '\n'.join(['#', 'A']) with patch('salt.utils.fopen', mock_open(read_data=file_data), create=True) as mfile: mfile.return_value.__iter__.return_value = file_data.splitlines( ) with patch.dict(timezone.__grains__, {'os_family': 'Debian'}): self.assertEqual(timezone.get_zone(), '#\nA') with patch.dict(timezone.__grains__, {'os_family': 'Gentoo'}): self.assertEqual(timezone.get_zone(), '') with patch.dict(timezone.__grains__, {'os_family': 'FreeBSD'}): with patch.object(os, 'readlink', return_value='/usr/share/zoneinfo/'): self.assertEqual(timezone.get_zone(), '') with patch.dict(timezone.__grains__, {'os_family': 'Solaris'}): with patch.dict(timezone.__salt__, {'cmd.run': MagicMock(return_value='A=B')}): self.assertEqual(timezone.get_zone(), 'B')
def test_get_zone(self): ''' Test to get current timezone (i.e. America/Denver) ''' zone = 'MST' with patch.object(salt.utils, 'which', return_value=True): mock_cmd = MagicMock(return_value={'stderr': 'error', 'retcode': 1}) with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}): self.assertRaises(CommandExecutionError, timezone.get_zone) mock_cmd = MagicMock(return_value={'stdout': 'Timezone: {0}'.format(zone), 'retcode': 0}) with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}): self.assertEqual(timezone.get_zone(), zone) mock_cmd = MagicMock(return_value={'stdout': 'ZoneCTL: {0}'.format(zone), 'retcode': 0}) with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}): self.assertRaises(CommandExecutionError, timezone.get_zone) with patch.object(salt.utils, 'which', return_value=False): file_data = '\n'.join(['#', 'A']) with patch('salt.utils.fopen', mock_open(read_data=file_data), create=True) as mfile: mfile.return_value.__iter__.return_value = file_data.splitlines() with patch.dict(timezone.__grains__, {'os_family': 'Debian', 'os': 'Debian'}): self.assertEqual(timezone.get_zone(), '#\nA') with patch.dict(timezone.__grains__, {'os_family': 'Gentoo', 'os': 'Gentoo'}): self.assertEqual(timezone.get_zone(), '') with patch.dict(timezone.__grains__, {'os_family': 'FreeBSD', 'os': 'FreeBSD'}): zone = 'America/Denver' linkpath = '/usr/share/zoneinfo/' + zone with patch.object(os, 'readlink', return_value=linkpath): self.assertEqual(timezone.get_zone(), zone) with patch.dict(timezone.__grains__, {'os_family': 'Solaris', 'os': 'Solaris'}): fl_data = 'TZ=Foo\n' with patch('salt.utils.fopen', mock_open(read_data=fl_data)) as mfile: mfile.return_value.__iter__.return_value = [fl_data] self.assertEqual(timezone.get_zone(), 'Foo')
def test_get_zone_os_family_aix(self): ''' Test IBM AIX is recognized :return: ''' with patch.dict(timezone.__grains__, {'os_family': ['AIX']}): with patch('salt.modules.timezone._get_zone_aix', MagicMock(return_value=self.TEST_TZ)): assert timezone.get_zone() == self.TEST_TZ
def test_get_zone_os_family_slowlaris(self): ''' Test Slowlaris is recognized :return: ''' with patch.dict(timezone.__grains__, {'os_family': ['Solaris']}): with patch('salt.modules.timezone._get_zone_solaris', MagicMock(return_value=self.TEST_TZ)): assert timezone.get_zone() == self.TEST_TZ
def test_get_zone_centos(self): ''' Test CentOS is recognized :return: ''' with patch.dict(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
def test_get_zone_os_family_allbsd_nilinuxrt(self): ''' Test *BSD and NILinuxRT are recognized :return: ''' for osfamily in ['FreeBSD', 'OpenBSD', 'NetBSD', 'NILinuxRT']: with patch.dict(timezone.__grains__, {'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
def test_get_zone_os_family_debian_gentoo(self): ''' Test Debian and Gentoo are recognized :return: ''' for osfamily in ['Debian', 'Gentoo']: with patch.dict(timezone.__grains__, {'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
def test_get_zone_os_family_rh_suse(self): ''' Test RedHat and Suse are recognized :return: ''' for osfamily in ['RedHat', 'Suse']: with patch.dict(timezone.__grains__, {'os_family': [osfamily]}): with patch('salt.modules.timezone._get_zone_sysconfig', MagicMock(return_value=self.TEST_TZ)): assert timezone.get_zone() == self.TEST_TZ
def test_get_zone_os_family_aix(self): """ Test IBM AIX is recognized :return: """ with patch.dict(timezone.__grains__, {"os_family": ["AIX"]}): with patch( "salt.modules.timezone._get_zone_aix", MagicMock(return_value=self.TEST_TZ), ): assert timezone.get_zone() == self.TEST_TZ
def test_get_zone_os_family_slowlaris(self): """ Test Slowlaris is recognized :return: """ with patch.dict(timezone.__grains__, {"os_family": ["Solaris"]}): with patch( "salt.modules.timezone._get_zone_solaris", MagicMock(return_value=self.TEST_TZ), ): assert timezone.get_zone() == self.TEST_TZ
def test_get_zone_centos(self): """ Test CentOS is recognized :return: """ with patch.dict(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
def test_get_zone_os_family_allbsd_nilinuxrt(self): """ Test *BSD and NILinuxRT are recognized :return: """ for osfamily in ["FreeBSD", "OpenBSD", "NetBSD", "NILinuxRT"]: with patch.dict(timezone.__grains__, {"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
def test_get_zone_os_family_debian_gentoo(self): """ Test Debian and Gentoo are recognized :return: """ for osfamily in ["Debian", "Gentoo"]: with patch.dict(timezone.__grains__, {"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
def test_get_zone_os_family_rh_suse(self): """ Test RedHat and Suse are recognized :return: """ for osfamily in ["RedHat", "Suse"]: with patch.dict(timezone.__grains__, {"os_family": [osfamily]}): with patch( "salt.modules.timezone._get_zone_sysconfig", MagicMock(return_value=self.TEST_TZ), ): assert timezone.get_zone() == self.TEST_TZ
def test_get_zone(self): ''' Test to get current timezone (i.e. America/Denver) ''' zone = 'MST' with patch.object(salt.utils, 'which', return_value=True): mock_cmd = MagicMock(return_value={ 'stderr': 'error', 'retcode': 1 }) with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}): self.assertRaises(CommandExecutionError, timezone.get_zone) mock_cmd = MagicMock(return_value={ 'stdout': 'Timezone: {0}'.format(zone), 'retcode': 0 }) with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}): self.assertEqual(timezone.get_zone(), zone) mock_cmd = MagicMock(return_value={ 'stdout': 'ZoneCTL: {0}'.format(zone), 'retcode': 0 }) with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}): self.assertRaises(CommandExecutionError, timezone.get_zone) with patch.object(salt.utils, 'which', return_value=False): file_data = '\n'.join(['#', 'A']) with patch('salt.utils.fopen', mock_open(read_data=file_data), create=True) as mfile: mfile.return_value.__iter__.return_value = file_data.splitlines( ) with patch.dict(timezone.__grains__, { 'os_family': 'Debian', 'os': 'Debian' }): self.assertEqual(timezone.get_zone(), '#\nA') with patch.dict(timezone.__grains__, { 'os_family': 'Gentoo', 'os': 'Gentoo' }): self.assertEqual(timezone.get_zone(), '') with patch.dict(timezone.__grains__, { 'os_family': 'FreeBSD', 'os': 'FreeBSD' }): zone = 'America/Denver' linkpath = '/usr/share/zoneinfo/' + zone with patch.object(os, 'readlink', return_value=linkpath): self.assertEqual(timezone.get_zone(), zone) with patch.dict(timezone.__grains__, { 'os_family': 'Solaris', 'os': 'Solaris' }): fl_data = 'TZ=Foo\n' with patch('salt.utils.fopen', mock_open(read_data=fl_data)) as mfile: mfile.return_value.__iter__.return_value = [fl_data] self.assertEqual(timezone.get_zone(), 'Foo')