test_mac_timezone.py 8.0 KB

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