Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Porting #51126 to master #54585

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions salt/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ def _gather_buffer_space():
# Tell the client to display the jid when a job is published
'show_jid': bool,

# Generate jids based on UTC time instead of local time
'utc_jid': bool,

# Ensure that a generated jid is always unique. If this is set, the jid
# format is different due to an underscore and process id being appended
# to the jid. WARNING: A change to the jid format may break external
Expand Down
7 changes: 5 additions & 2 deletions salt/utils/jid.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ def gen_jid(opts=None):
opts = {}
global LAST_JID_DATETIME # pylint: disable=global-statement

if opts.get('utc_jid', False):
jid_dt = datetime.datetime.utcnow()
else:
jid_dt = datetime.datetime.now()
if not opts.get('unique_jid', False):
return '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
jid_dt = datetime.datetime.now()
return '{0:%Y%m%d%H%M%S%f}'.format(jid_dt)
if LAST_JID_DATETIME and LAST_JID_DATETIME >= jid_dt:
jid_dt = LAST_JID_DATETIME + datetime.timedelta(microseconds=1)
LAST_JID_DATETIME = jid_dt
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/utils/test_jid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,38 @@ def test_is_jid(self):

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid(self):
now = datetime.datetime(2002, 12, 25, 12, 00, 00, 00)
now = datetime.datetime(2002, 12, 25, 12, 0, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.now.return_value = now
ret = salt.utils.jid.gen_jid({})
self.assertEqual(ret, '20021225120000000000')

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid_unique(self):
now = datetime.datetime(2002, 12, 25, 12, 0, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.now.return_value = now
salt.utils.jid.LAST_JID_DATETIME = None
ret = salt.utils.jid.gen_jid({'unique_jid': True})
self.assertEqual(ret, '20021225120000000000_{0}'.format(os.getpid()))
ret = salt.utils.jid.gen_jid({'unique_jid': True})
self.assertEqual(ret, '20021225120000000001_{0}'.format(os.getpid()))

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid_utc(self):
utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.utcnow.return_value = utcnow
ret = salt.utils.jid.gen_jid({'utc_jid': True})
self.assertEqual(ret, '20021225120700000000')

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid_utc_unique(self):
utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.utcnow.return_value = utcnow
salt.utils.jid.LAST_JID_DATETIME = None
ret = salt.utils.jid.gen_jid({'utc_jid': True, 'unique_jid': True})
self.assertEqual(ret, '20021225120700000000_{0}'.format(os.getpid()))
ret = salt.utils.jid.gen_jid({'utc_jid': True, 'unique_jid': True})
self.assertEqual(ret, '20021225120700000001_{0}'.format(os.getpid()))