From 0599a2b88109dede078f83560314b633402d3ceb Mon Sep 17 00:00:00 2001 From: Frank Villaro-Dixon Date: Fri, 16 Oct 2020 16:44:18 +0200 Subject: [PATCH] setup: confort: reverse timestamp on namespace name Each namespace created by kubetest contains the timestamp. This commit reverses the timestamp (1234 becomes 4321). This is useful when debugging tests. Indeed, when using tab-completion and kubectl, it is easier to (visually) distinguish namespaces by their leading digits than by digits near the end. By using a reversed timestamp, the leading digits will be more spread out. Signed-off-by: Frank Villaro-Dixon --- kubetest/utils.py | 10 +++++----- tests/test_utils.py | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/kubetest/utils.py b/kubetest/utils.py index ecbcbf9..db85145 100644 --- a/kubetest/utils.py +++ b/kubetest/utils.py @@ -17,8 +17,8 @@ def new_namespace(test_name: str) -> str: Kubernetes namespace names follow a DNS-1123 label that consists of lower case alphanumeric characters or '-' and must start with an alphanumeric. - The test name and current timestamp are formatted to comply to this spec and - appended to the 'kubetest' prefix. + The test name and current reversed timestamp are formatted to comply to this + spec and appended to the 'kubetest' prefix. Args: test_name: The name of the test case for the namespace. @@ -27,7 +27,7 @@ def new_namespace(test_name: str) -> str: The namespace name. """ prefix = "kubetest" - timestamp = str(int(time.time())) + reversed_timestamp = str(int(time.time()))[::-1] test_name = test_name.replace("_", "-").lower() test_name = test_name.replace("[", "-") test_name = test_name.replace("]", "-") @@ -36,12 +36,12 @@ def new_namespace(test_name: str) -> str: # characters. Check the length of all components (+2 for the dashes # joining the components). If the total length exceeds 63, truncate # the test name. - name_len = len(prefix) + len(timestamp) + len(test_name) + 2 + name_len = len(prefix) + len(reversed_timestamp) + len(test_name) + 2 if name_len > 63: test_name = test_name[: -(name_len - 63)] - return "-".join((prefix, test_name, timestamp)) + return "-".join((prefix, test_name, reversed_timestamp)) def selector_string(selectors: Mapping[str, str]) -> str: diff --git a/tests/test_utils.py b/tests/test_utils.py index a778a25..0f058a0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,18 +8,18 @@ @pytest.mark.parametrize( "name,expected", [ - ("", "kubetest--1536849367"), - ("TestName", "kubetest-testname-1536849367"), - ("TESTNAME", "kubetest-testname-1536849367"), - ("Test-Name", "kubetest-test-name-1536849367"), - ("Test1_FOO-BAR_2", "kubetest-test1-foo-bar-2-1536849367"), - ("123456", "kubetest-123456-1536849367"), - ("___", "kubetest-----1536849367"), + ("", "kubetest--7639486351"), + ("TestName", "kubetest-testname-7639486351"), + ("TESTNAME", "kubetest-testname-7639486351"), + ("Test-Name", "kubetest-test-name-7639486351"), + ("Test1_FOO-BAR_2", "kubetest-test1-foo-bar-2-7639486351"), + ("123456", "kubetest-123456-7639486351"), + ("___", "kubetest-----7639486351"), ( "test-" * 14, - "kubetest-test-test-test-test-test-test-test-test-tes-1536849367", + "kubetest-test-test-test-test-test-test-test-test-tes-7639486351", ), - ("test[a]-foo", "kubetest-test-a--foo-1536849367"), + ("test[a]-foo", "kubetest-test-a--foo-7639486351"), ], ) def test_new_namespace(name, expected):