test_jid.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.mock import patch
  12. from tests.support.unit import TestCase
  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(
  25. salt.utils.jid.is_jid("2013121911070012348911111")
  26. ) # Wrong length
  27. def test_gen_jid(self):
  28. now = datetime.datetime(2002, 12, 25, 12, 0, 0, 0)
  29. with patch("salt.utils.jid._utc_now", return_value=now):
  30. ret = salt.utils.jid.gen_jid({})
  31. self.assertEqual(ret, "20021225120000000000")
  32. salt.utils.jid.LAST_JID_DATETIME = None
  33. ret = salt.utils.jid.gen_jid({"unique_jid": True})
  34. self.assertEqual(ret, "20021225120000000000_{0}".format(os.getpid()))
  35. ret = salt.utils.jid.gen_jid({"unique_jid": True})
  36. self.assertEqual(ret, "20021225120000000001_{0}".format(os.getpid()))
  37. def test_gen_jid_utc(self):
  38. utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
  39. with patch("salt.utils.jid._utc_now", return_value=utcnow):
  40. ret = salt.utils.jid.gen_jid({"utc_jid": True})
  41. self.assertEqual(ret, "20021225120700000000")
  42. def test_gen_jid_utc_unique(self):
  43. utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
  44. with patch("salt.utils.jid._utc_now", return_value=utcnow):
  45. salt.utils.jid.LAST_JID_DATETIME = None
  46. ret = salt.utils.jid.gen_jid({"utc_jid": True, "unique_jid": True})
  47. self.assertEqual(ret, "20021225120700000000_{0}".format(os.getpid()))
  48. ret = salt.utils.jid.gen_jid({"utc_jid": True, "unique_jid": True})
  49. self.assertEqual(ret, "20021225120700000001_{0}".format(os.getpid()))