test_jid.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for salt.utils.jid
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals
  7. import datetime
  8. import os
  9. # Import Salt libs
  10. import salt.utils.jid
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import patch
  13. class JidTestCase(TestCase):
  14. def test_jid_to_time(self):
  15. test_jid = 20131219110700123489
  16. expected_jid = '2013, Dec 19 11:07:00.123489'
  17. self.assertEqual(salt.utils.jid.jid_to_time(test_jid), expected_jid)
  18. # Test incorrect lengths
  19. incorrect_jid_length = 2012
  20. self.assertEqual(salt.utils.jid.jid_to_time(incorrect_jid_length), '')
  21. def test_is_jid(self):
  22. self.assertTrue(salt.utils.jid.is_jid('20131219110700123489')) # Valid JID
  23. self.assertFalse(salt.utils.jid.is_jid(20131219110700123489)) # int
  24. self.assertFalse(salt.utils.jid.is_jid('2013121911070012348911111')) # Wrong length
  25. def test_gen_jid(self):
  26. now = datetime.datetime(2002, 12, 25, 12, 0, 0, 0)
  27. with patch('salt.utils.jid._utc_now', return_value=now):
  28. ret = salt.utils.jid.gen_jid({})
  29. self.assertEqual(ret, '20021225120000000000')
  30. salt.utils.jid.LAST_JID_DATETIME = None
  31. ret = salt.utils.jid.gen_jid({'unique_jid': True})
  32. self.assertEqual(ret, '20021225120000000000_{0}'.format(os.getpid()))
  33. ret = salt.utils.jid.gen_jid({'unique_jid': True})
  34. self.assertEqual(ret, '20021225120000000001_{0}'.format(os.getpid()))
  35. def test_gen_jid_utc(self):
  36. utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
  37. with patch('salt.utils.jid._utc_now', return_value=utcnow):
  38. ret = salt.utils.jid.gen_jid({'utc_jid': True})
  39. self.assertEqual(ret, '20021225120700000000')
  40. def test_gen_jid_utc_unique(self):
  41. utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
  42. with patch('salt.utils.jid._utc_now', return_value=utcnow):
  43. salt.utils.jid.LAST_JID_DATETIME = None
  44. ret = salt.utils.jid.gen_jid({'utc_jid': True, 'unique_jid': True})
  45. self.assertEqual(ret, '20021225120700000000_{0}'.format(os.getpid()))
  46. ret = salt.utils.jid.gen_jid({'utc_jid': True, 'unique_jid': True})
  47. self.assertEqual(ret, '20021225120700000001_{0}'.format(os.getpid()))