test_jid.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, skipIf
  12. from tests.support.mock import (
  13. patch,
  14. NO_MOCK,
  15. NO_MOCK_REASON
  16. )
  17. class JidTestCase(TestCase):
  18. def test_jid_to_time(self):
  19. test_jid = 20131219110700123489
  20. expected_jid = '2013, Dec 19 11:07:00.123489'
  21. self.assertEqual(salt.utils.jid.jid_to_time(test_jid), expected_jid)
  22. # Test incorrect lengths
  23. incorrect_jid_length = 2012
  24. self.assertEqual(salt.utils.jid.jid_to_time(incorrect_jid_length), '')
  25. def test_is_jid(self):
  26. self.assertTrue(salt.utils.jid.is_jid('20131219110700123489')) # Valid JID
  27. self.assertFalse(salt.utils.jid.is_jid(20131219110700123489)) # int
  28. self.assertFalse(salt.utils.jid.is_jid('2013121911070012348911111')) # Wrong length
  29. @skipIf(NO_MOCK, NO_MOCK_REASON)
  30. def test_gen_jid(self):
  31. now = datetime.datetime(2002, 12, 25, 12, 00, 00, 00)
  32. with patch('datetime.datetime'):
  33. datetime.datetime.now.return_value = now
  34. ret = salt.utils.jid.gen_jid({})
  35. self.assertEqual(ret, '20021225120000000000')
  36. salt.utils.jid.LAST_JID_DATETIME = None
  37. ret = salt.utils.jid.gen_jid({'unique_jid': True})
  38. self.assertEqual(ret, '20021225120000000000_{0}'.format(os.getpid()))
  39. ret = salt.utils.jid.gen_jid({'unique_jid': True})
  40. self.assertEqual(ret, '20021225120000000001_{0}'.format(os.getpid()))