From b72a937b726820d1029f79f84c56b86b3dec5a80 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sat, 19 Oct 2024 17:41:24 -0400 Subject: [PATCH 1/7] Put date into index name --- scripts/create.py | 40 +++++++++++++++++++++++++++- tests/integration/helpers/helpers.py | 19 ++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/scripts/create.py b/scripts/create.py index 93b2c5c5..b9c6ffb3 100644 --- a/scripts/create.py +++ b/scripts/create.py @@ -1,6 +1,8 @@ import os +import re import random import string +from datetime import datetime from pinecone import Pinecone @@ -20,9 +22,45 @@ def write_gh_output(name, value): print(f"{name}={value}", file=fh) +def generate_index_name(test_name: str) -> str: + github_actor = os.getenv("GITHUB_ACTOR", None) + user = os.getenv("USER", None) + index_owner = github_actor or user + + current_date = datetime.now() + formatted_date = current_date.strftime("%Y%m%d-%f") + + github_job = os.getenv("GITHUB_JOB", None) + + if test_name.startswith("test_"): + test_name = test_name[5:] + + # Remove trailing underscore, if any + if test_name.endswith("_"): + test_name = test_name[:-1] + + name_parts = [index_owner, formatted_date, github_job, test_name] + index_name = "-".join([x for x in name_parts if x is not None]) + + # Remove invalid characters + replace_with_hyphen = re.compile(r"[\[\(_,\s]") + index_name = re.sub(replace_with_hyphen, "-", index_name) + replace_with_empty = re.compile(r"[\]\)\.]") + index_name = re.sub(replace_with_empty, "", index_name) + + max_length = 45 + index_name = index_name[:max_length] + + # Trim final character if it is not alphanumeric + if index_name.endswith("_") or index_name.endswith("-"): + index_name = index_name[:-1] + + return index_name.lower() + + def main(): pc = Pinecone(api_key=read_env_var("PINECONE_API_KEY")) - index_name = read_env_var("NAME_PREFIX") + random_string(20) + index_name = generate_index_name(read_env_var("NAME_PREFIX") + random_string(20)) pc.create_index( name=index_name, metric=read_env_var("METRIC"), diff --git a/tests/integration/helpers/helpers.py b/tests/integration/helpers/helpers.py index 7f8ab09d..e29b0b9a 100644 --- a/tests/integration/helpers/helpers.py +++ b/tests/integration/helpers/helpers.py @@ -4,6 +4,7 @@ import random import string from typing import Any +from datetime import datetime def random_string(length): @@ -11,19 +12,23 @@ def random_string(length): def generate_index_name(test_name: str) -> str: - buildNumber = os.getenv("GITHUB_BUILD_NUMBER", None) + github_actor = os.getenv("GITHUB_ACTOR", None) + user = os.getenv("USER", None) + index_owner = github_actor or user + + current_date = datetime.now() + formatted_date = current_date.strftime("%Y%m%d-%f") + + github_job = os.getenv("GITHUB_JOB", None) if test_name.startswith("test_"): test_name = test_name[5:] - # Trim name length to save space for other info in name - test_name = test_name[:20] - # Remove trailing underscore, if any if test_name.endswith("_"): test_name = test_name[:-1] - name_parts = [buildNumber, test_name, random_string(45)] + name_parts = [index_owner, formatted_date, github_job, test_name] index_name = "-".join([x for x in name_parts if x is not None]) # Remove invalid characters @@ -36,8 +41,8 @@ def generate_index_name(test_name: str) -> str: index_name = index_name[:max_length] # Trim final character if it is not alphanumeric - if test_name.endswith("_") or test_name.endswith("-"): - test_name = test_name[:-1] + if index_name.endswith("_") or index_name.endswith("-"): + index_name = index_name[:-1] return index_name.lower() From 264d3d44a67325b56ff17a2c694b35a1085b55a8 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sat, 19 Oct 2024 18:04:44 -0400 Subject: [PATCH 2/7] Adjust generated collection names --- tests/integration/control/pod/conftest.py | 4 ++-- .../control/pod/test_collections_errors.py | 16 ++++++---------- tests/integration/data/conftest.py | 4 ++-- tests/integration/helpers/helpers.py | 16 ++++++++++------ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/integration/control/pod/conftest.py b/tests/integration/control/pod/conftest.py index 4748f28e..17ef6f2f 100644 --- a/tests/integration/control/pod/conftest.py +++ b/tests/integration/control/pod/conftest.py @@ -3,7 +3,7 @@ import string import time from pinecone import Pinecone, PodSpec -from ...helpers import generate_index_name, get_environment_var +from ...helpers import generate_index_name, generate_collection_name, get_environment_var @pytest.fixture() @@ -99,7 +99,7 @@ def reusable_collection(): index = pc.Index(index_name) index.upsert(vectors=vectors) - collection_name = "reused-coll-" + random_string() + collection_name = generate_collection_name("reused-coll-" + random_string()) pc.create_collection(name=collection_name, source=index_name) time_waited = 0 diff --git a/tests/integration/control/pod/test_collections_errors.py b/tests/integration/control/pod/test_collections_errors.py index 2e2d7965..fa57c2e2 100644 --- a/tests/integration/control/pod/test_collections_errors.py +++ b/tests/integration/control/pod/test_collections_errors.py @@ -1,11 +1,7 @@ -import string import random import pytest from pinecone import PodSpec - - -def random_string(): - return "".join(random.choice(string.ascii_lowercase) for i in range(10)) +from ...helpers import generate_collection_name, generate_index_name, random_string class TestCollectionErrorCases: @@ -13,7 +9,7 @@ def test_create_index_with_nonexistent_source_collection( self, client, dimension, metric, environment ): with pytest.raises(Exception) as e: - index_name = "from-nonexistent-coll-" + random_string() + index_name = generate_index_name("from-nonexistent-coll-" + random_string(10)) client.create_index( name=index_name, dimension=dimension, @@ -43,7 +39,7 @@ def test_create_index_in_mismatched_environment( target_env = random.choice([x for x in envs if x != environment]) with pytest.raises(Exception) as e: - index_name = "from-coll-" + random_string() + index_name = generate_index_name("from-coll-" + random_string(10)) client.create_index( name=index_name, dimension=dimension, @@ -59,7 +55,7 @@ def test_create_index_with_mismatched_dimension( ): with pytest.raises(Exception) as e: client.create_index( - name="from-coll-" + random_string(), + name=generate_index_name("from-coll-" + random_string(10)), dimension=dimension + 1, metric=metric, spec=PodSpec(environment=environment, source_collection=reusable_collection), @@ -91,13 +87,13 @@ def test_create_index_with_mismatched_dimension( # assert 'Source collection is not ready' in str(e.value) def test_create_collection_from_not_ready_index(self, client, notready_index): - name = "coll3-" + random_string() + name = generate_collection_name("coll3") with pytest.raises(Exception) as e: client.create_collection(name, notready_index) assert "Source index is not ready" in str(e.value) def test_create_collection_with_invalid_index(self, client): - name = "coll4-" + random_string() + name = generate_collection_name("coll4") with pytest.raises(Exception) as e: client.create_collection(name, "invalid_index") assert "Resource invalid_index not found" in str(e.value) diff --git a/tests/integration/data/conftest.py b/tests/integration/data/conftest.py index ef0ab62f..05559678 100644 --- a/tests/integration/data/conftest.py +++ b/tests/integration/data/conftest.py @@ -2,7 +2,7 @@ import os import time import json -from ..helpers import get_environment_var, random_string +from ..helpers import get_environment_var, random_string, generate_index_name from .seed import setup_data, setup_list_data, setup_weird_ids_data # Test matrix needs to consider the following dimensions: @@ -57,7 +57,7 @@ def spec(): @pytest.fixture(scope="session") def index_name(): - return "dataplane-" + random_string(20) + return generate_index_name("dataplane") @pytest.fixture(scope="session") diff --git a/tests/integration/helpers/helpers.py b/tests/integration/helpers/helpers.py index e29b0b9a..96fc09e5 100644 --- a/tests/integration/helpers/helpers.py +++ b/tests/integration/helpers/helpers.py @@ -11,7 +11,11 @@ def random_string(length): return "".join(random.choice(string.ascii_lowercase) for i in range(length)) -def generate_index_name(test_name: str) -> str: +def generate_collection_name(label): + return generate_index_name(label) + + +def generate_index_name(label: str) -> str: github_actor = os.getenv("GITHUB_ACTOR", None) user = os.getenv("USER", None) index_owner = github_actor or user @@ -21,14 +25,14 @@ def generate_index_name(test_name: str) -> str: github_job = os.getenv("GITHUB_JOB", None) - if test_name.startswith("test_"): - test_name = test_name[5:] + if label.startswith("test_"): + label = label[5:] # Remove trailing underscore, if any - if test_name.endswith("_"): - test_name = test_name[:-1] + if label.endswith("_"): + label = label[:-1] - name_parts = [index_owner, formatted_date, github_job, test_name] + name_parts = [index_owner, formatted_date, github_job, label] index_name = "-".join([x for x in name_parts if x is not None]) # Remove invalid characters From 26d91e58314fd82dbcea48a9fd73d4348c16da52 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sat, 19 Oct 2024 18:10:00 -0400 Subject: [PATCH 3/7] Use name helpers in more places --- tests/integration/control/pod/test_collections.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/integration/control/pod/test_collections.py b/tests/integration/control/pod/test_collections.py index fd369fef..aad022d4 100644 --- a/tests/integration/control/pod/test_collections.py +++ b/tests/integration/control/pod/test_collections.py @@ -1,12 +1,8 @@ -import string import random import pytest import time from pinecone import PodSpec - - -def random_string(): - return "".join(random.choice(string.ascii_lowercase) for i in range(10)) +from ...helpers import generate_index_name, generate_collection_name class TestCollectionsHappyPath: @@ -18,7 +14,7 @@ def test_index_to_collection_to_index_happy_path( vectors = [(str(i), random_vector()) for i in range(num_vectors)] index.upsert(vectors=vectors) - collection_name = "coll1-" + random_string() + collection_name = generate_collection_name("coll1") client.create_collection(name=collection_name, source=ready_index) desc = client.describe_collection(collection_name) assert desc["name"] == collection_name @@ -51,7 +47,7 @@ def test_index_to_collection_to_index_happy_path( assert desc["size"] > 0 # Create index from collection - index_name = "index-from-collection-" + collection_name + index_name = generate_index_name("index-from-collection-" + collection_name) print(f"Creating index {index_name} from collection {collection_name}...") client.create_index( name=index_name, @@ -91,7 +87,7 @@ def test_create_index_with_different_metric_from_orig_index( metrics = ["cosine", "euclidean", "dotproduct"] target_metric = random.choice([x for x in metrics if x != metric]) - index_name = "from-coll-" + random_string() + index_name = generate_index_name("from-" + reusable_collection) client.create_index( name=index_name, dimension=dimension, From 4c6686ffdac14991bb419a604a72d7db3f76f7a7 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sat, 19 Oct 2024 18:13:01 -0400 Subject: [PATCH 4/7] Expose collection name helper in helpers package --- tests/integration/helpers/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/helpers/__init__.py b/tests/integration/helpers/__init__.py index c7ddd18a..d5ffc930 100644 --- a/tests/integration/helpers/__init__.py +++ b/tests/integration/helpers/__init__.py @@ -3,6 +3,7 @@ get_environment_var, random_string, generate_index_name, + generate_collection_name, poll_stats_for_namespace, poll_fetch_for_ids_in_namespace, ) From 327c21158c2aea631b4dac024faa5d79eb097d58 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sat, 19 Oct 2024 18:19:44 -0400 Subject: [PATCH 5/7] Use helpers in more places --- tests/integration/control/pod/conftest.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/integration/control/pod/conftest.py b/tests/integration/control/pod/conftest.py index 17ef6f2f..6c00c9ba 100644 --- a/tests/integration/control/pod/conftest.py +++ b/tests/integration/control/pod/conftest.py @@ -1,6 +1,5 @@ import pytest import random -import string import time from pinecone import Pinecone, PodSpec from ...helpers import generate_index_name, generate_collection_name, get_environment_var @@ -69,17 +68,13 @@ def index_exists(index_name, client): return index_name in client.list_indexes().names() -def random_string(): - return "".join(random.choice(string.ascii_lowercase) for i in range(10)) - - @pytest.fixture(scope="session") def reusable_collection(): pc = Pinecone( api_key=get_environment_var("PINECONE_API_KEY"), additional_headers={"sdk-test-suite": "pinecone-python-client"}, ) - index_name = "temp-index-" + random_string() + index_name = generate_index_name("temp-index") dimension = int(get_environment_var("DIMENSION")) print(f"Creating index {index_name} to prepare a collection...") pc.create_index( @@ -99,7 +94,7 @@ def reusable_collection(): index = pc.Index(index_name) index.upsert(vectors=vectors) - collection_name = generate_collection_name("reused-coll-" + random_string()) + collection_name = generate_collection_name("reused-coll") pc.create_collection(name=collection_name, source=index_name) time_waited = 0 From 9e8cf855879298380f98316e0dde2707a994c0dd Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sat, 19 Oct 2024 23:56:56 -0400 Subject: [PATCH 6/7] Remove duplicate run of lint job --- .github/workflows/lint.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 36f6d62e..f79fd228 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -1,5 +1,5 @@ name: "Lint" -on: [push, pull_request] +on: [pull_request] jobs: lint: From bd4ff63853ef12166e1e1bbb199ddbfcb76888fa Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sun, 20 Oct 2024 00:13:49 -0400 Subject: [PATCH 7/7] Slightly adjust timestamp --- scripts/create.py | 3 +-- tests/integration/helpers/helpers.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts/create.py b/scripts/create.py index b9c6ffb3..05a12c36 100644 --- a/scripts/create.py +++ b/scripts/create.py @@ -27,8 +27,7 @@ def generate_index_name(test_name: str) -> str: user = os.getenv("USER", None) index_owner = github_actor or user - current_date = datetime.now() - formatted_date = current_date.strftime("%Y%m%d-%f") + formatted_date = datetime.now().strftime("%Y%m%d-%H%M%S%f")[:-3] github_job = os.getenv("GITHUB_JOB", None) diff --git a/tests/integration/helpers/helpers.py b/tests/integration/helpers/helpers.py index 96fc09e5..9adb01ac 100644 --- a/tests/integration/helpers/helpers.py +++ b/tests/integration/helpers/helpers.py @@ -20,8 +20,7 @@ def generate_index_name(label: str) -> str: user = os.getenv("USER", None) index_owner = github_actor or user - current_date = datetime.now() - formatted_date = current_date.strftime("%Y%m%d-%f") + formatted_date = datetime.now().strftime("%Y%m%d-%H%M%S%f")[:-3] github_job = os.getenv("GITHUB_JOB", None)