test_jid.py 1.5 KB

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