Skip to content
Open
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
6 changes: 3 additions & 3 deletions sagemaker-core/src/sagemaker/core/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ def unique_name_from_base_uuid4(base, max_length=63):
Returns:
str: Input parameter with appended timestamp.
"""
random.seed(int(uuid.uuid4())) # using uuid to randomize
unique = str(uuid.uuid4())
trimmed_base = base[: max_length - len(unique) - 1]
return "{}-{}".format(trimmed_base, unique)


def unique_name_from_base(base, max_length=63):
"""Placeholder Docstring"""
random.seed(int(uuid.uuid4())) # using uuid to randomize, otherwise system timestamp is used.
unique = "%04x" % random.randrange(16**4) # 4-digit hex
# Draw from the OS entropy source instead of reseeding the global ``random`` module, which
# would reset the random state of the caller's own code (e.g. ``random.seed(42)``).
unique = "%04x" % random.SystemRandom().randrange(16**4) # 4-digit hex
ts = str(int(time.time()))
available_length = max_length - 2 - len(ts) - len(unique)
trimmed = base[:available_length]
Expand Down
21 changes: 21 additions & 0 deletions sagemaker-core/tests/unit/test_common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import absolute_import

import pytest
import random
import time
import tempfile
import os
Expand Down Expand Up @@ -128,6 +129,16 @@ def test_unique_name_from_base_uuid4_max_length(self):
result = unique_name_from_base_uuid4(base, max_length=50)
assert len(result) <= 50

def test_unique_name_from_base_uuid4_preserves_global_random_state(self):
"""Generating a name must not reseed the global ``random`` module."""
random.seed(1234)
expected = random.random()

random.seed(1234)
unique_name_from_base_uuid4("my-resource")

assert random.random() == expected


class TestUniqueNameFromBase:
"""Test unique_name_from_base function."""
Expand All @@ -153,6 +164,16 @@ def test_unique_name_from_base_max_length(self):
result = unique_name_from_base(base, max_length=45)
assert len(result) <= 45

def test_unique_name_from_base_preserves_global_random_state(self):
"""Generating a name must not reseed the global ``random`` module."""
random.seed(1234)
expected = random.random()

random.seed(1234)
unique_name_from_base("my-job")

assert random.random() == expected


class TestBaseNameFromImage:
"""Test base_name_from_image function."""
Expand Down
Loading