1
0

test_mac_timezone.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Integration tests for mac_timezone
  4. If using parallels, make sure Time sync is turned off. Otherwise, parallels will
  5. keep changing your date/time settings while the tests are running. To turn off
  6. Time sync do the following:
  7. - Go to actions -> configure
  8. - Select options at the top and 'More Options' on the left
  9. - Set time to 'Do not sync'
  10. '''
  11. # Import Python libs
  12. from __future__ import absolute_import, unicode_literals, print_function
  13. import datetime
  14. import pytest
  15. # Import Salt Testing libs
  16. from tests.support.case import ModuleCase
  17. from tests.support.unit import skipIf
  18. from tests.support.helpers import flaky
  19. # Import Salt libs
  20. import salt.utils.path
  21. import salt.utils.platform
  22. # Import 3rd Party libs
  23. from salt.ext import six
  24. @pytest.mark.skip_if_not_root
  25. @flaky
  26. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  27. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  28. class MacTimezoneModuleTest(ModuleCase):
  29. '''
  30. Validate the mac_timezone module
  31. '''
  32. USE_NETWORK_TIME = False
  33. TIME_SERVER = 'time.apple.com'
  34. TIME_ZONE = ''
  35. CURRENT_DATE = ''
  36. CURRENT_TIME = ''
  37. def setUp(self):
  38. '''
  39. Get current settings
  40. '''
  41. self.USE_NETWORK_TIME = self.run_function('timezone.get_using_network_time')
  42. self.TIME_SERVER = self.run_function('timezone.get_time_server')
  43. self.TIME_ZONE = self.run_function('timezone.get_zone')
  44. self.CURRENT_DATE = self.run_function('timezone.get_date')
  45. self.CURRENT_TIME = self.run_function('timezone.get_time')
  46. self.run_function('timezone.set_using_network_time', [False])
  47. self.run_function('timezone.set_zone', ['America/Denver'])
  48. def tearDown(self):
  49. '''
  50. Reset to original settings
  51. '''
  52. self.run_function('timezone.set_time_server', [self.TIME_SERVER])
  53. self.run_function('timezone.set_using_network_time',
  54. [self.USE_NETWORK_TIME])
  55. self.run_function('timezone.set_zone', [self.TIME_ZONE])
  56. if not self.USE_NETWORK_TIME:
  57. self.run_function('timezone.set_date', [self.CURRENT_DATE])
  58. self.run_function('timezone.set_time', [self.CURRENT_TIME])
  59. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  60. @pytest.mark.destructive_test
  61. def test_get_set_date(self):
  62. '''
  63. Test timezone.get_date
  64. Test timezone.set_date
  65. '''
  66. # Correct Functionality
  67. self.assertTrue(self.run_function('timezone.set_date', ['2/20/2011']))
  68. self.assertEqual(self.run_function('timezone.get_date'), '2/20/2011')
  69. # Test bad date format
  70. self.assertEqual(
  71. self.run_function('timezone.set_date', ['13/12/2014']),
  72. 'ERROR executing \'timezone.set_date\': '
  73. 'Invalid Date/Time Format: 13/12/2014'
  74. )
  75. def test_get_time(self):
  76. '''
  77. Test timezone.get_time
  78. '''
  79. text_time = self.run_function('timezone.get_time')
  80. self.assertNotEqual(text_time, 'Invalid Timestamp')
  81. obj_date = datetime.datetime.strptime(text_time, '%H:%M:%S')
  82. self.assertIsInstance(obj_date, datetime.date)
  83. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  84. @pytest.mark.destructive_test
  85. def test_set_time(self):
  86. '''
  87. Test timezone.set_time
  88. '''
  89. # Correct Functionality
  90. self.assertTrue(self.run_function('timezone.set_time', ['3:14']))
  91. # Test bad time format
  92. self.assertEqual(
  93. self.run_function('timezone.set_time', ['3:71']),
  94. 'ERROR executing \'timezone.set_time\': '
  95. 'Invalid Date/Time Format: 3:71')
  96. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  97. @pytest.mark.destructive_test
  98. def test_get_set_zone(self):
  99. '''
  100. Test timezone.get_zone
  101. Test timezone.set_zone
  102. '''
  103. # Correct Functionality
  104. self.assertTrue(
  105. self.run_function('timezone.set_zone', ['Pacific/Wake']))
  106. self.assertEqual(
  107. self.run_function('timezone.get_zone'), 'Pacific/Wake')
  108. # Test bad time zone
  109. self.assertEqual(
  110. self.run_function('timezone.set_zone', ['spongebob']),
  111. 'ERROR executing \'timezone.set_zone\': '
  112. 'Invalid Timezone: spongebob')
  113. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  114. @pytest.mark.destructive_test
  115. def test_get_offset(self):
  116. '''
  117. Test timezone.get_offset
  118. '''
  119. self.assertTrue(
  120. self.run_function('timezone.set_zone', ['Pacific/Wake']))
  121. self.assertIsInstance(self.run_function('timezone.get_offset'),
  122. six.string_types)
  123. self.assertEqual(self.run_function('timezone.get_offset'), '+1200')
  124. self.assertTrue(
  125. self.run_function('timezone.set_zone', ['America/Los_Angeles']))
  126. self.assertIsInstance(self.run_function('timezone.get_offset'),
  127. six.string_types)
  128. self.assertEqual(self.run_function('timezone.get_offset'), '-0700')
  129. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  130. @pytest.mark.destructive_test
  131. def test_get_set_zonecode(self):
  132. '''
  133. Test timezone.get_zonecode
  134. Test timezone.set_zonecode
  135. '''
  136. self.assertTrue(
  137. self.run_function('timezone.set_zone', ['America/Los_Angeles']))
  138. self.assertIsInstance(self.run_function('timezone.get_zonecode'),
  139. six.string_types)
  140. self.assertEqual(self.run_function('timezone.get_zonecode'), 'PDT')
  141. self.assertTrue(
  142. self.run_function('timezone.set_zone', ['Pacific/Wake']))
  143. self.assertIsInstance(self.run_function('timezone.get_zonecode'),
  144. six.string_types)
  145. self.assertEqual(self.run_function('timezone.get_zonecode'), 'WAKT')
  146. def test_list_zones(self):
  147. '''
  148. Test timezone.list_zones
  149. '''
  150. zones = self.run_function('timezone.list_zones')
  151. self.assertIsInstance(self.run_function('timezone.list_zones'), list)
  152. self.assertIn(
  153. 'America/Denver',
  154. self.run_function('timezone.list_zones'))
  155. self.assertIn(
  156. 'America/Los_Angeles',
  157. self.run_function('timezone.list_zones'))
  158. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  159. @pytest.mark.destructive_test
  160. def test_zone_compare(self):
  161. '''
  162. Test timezone.zone_compare
  163. '''
  164. self.assertTrue(
  165. self.run_function('timezone.set_zone', ['America/Denver']))
  166. self.assertTrue(
  167. self.run_function('timezone.zone_compare', ['America/Denver']))
  168. self.assertFalse(
  169. self.run_function('timezone.zone_compare', ['Pacific/Wake']))
  170. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  171. @pytest.mark.destructive_test
  172. def test_get_set_using_network_time(self):
  173. '''
  174. Test timezone.get_using_network_time
  175. Test timezone.set_using_network_time
  176. '''
  177. self.assertTrue(
  178. self.run_function('timezone.set_using_network_time', [True]))
  179. self.assertTrue(self.run_function('timezone.get_using_network_time'))
  180. self.assertTrue(
  181. self.run_function('timezone.set_using_network_time', [False]))
  182. self.assertFalse(self.run_function('timezone.get_using_network_time'))
  183. @skipIf(True, 'Skip until we can figure out why modifying the system clock causes ZMQ errors')
  184. @pytest.mark.destructive_test
  185. def test_get_set_time_server(self):
  186. '''
  187. Test timezone.get_time_server
  188. Test timezone.set_time_server
  189. '''
  190. self.assertTrue(
  191. self.run_function('timezone.set_time_server', ['spongebob.com']))
  192. self.assertEqual(
  193. self.run_function('timezone.get_time_server'), 'spongebob.com')