Skip to content

Commit

Permalink
Testing: Create and delete test index
Browse files Browse the repository at this point in the history
Create a test index before running pytest, then delete it after. Uses
'PINECONE_API_KEY' Action secret for access to the Pinecone API.
  • Loading branch information
daverigby committed Feb 6, 2024
1 parent dbaddd5 commit e48f4db
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/actions/create-index/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: 'Create Index'
description: 'Creates an index to be used in other tests'

inputs:
region:
description: 'The region of the index'
required: false
default: 'us-west-2'
cloud:
description: 'The cloud of the index'
required: false
default: 'aws'
name_prefix:
description: 'The prefix of the index name'
required: false
default: 'index-name'
dimension:
description: 'The dimension of the index'
required: false
default: '3'
metric:
description: 'The metric of the index'
required: false
default: 'cosine'
PINECONE_API_KEY:
description: 'The Pinecone API key'
required: true

outputs:
index_name:
description: 'The name of the index, including randomized suffix'
value: ${{ steps.create-index.outputs.index_name }}

runs:
using: 'composite'
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Create index
id: create-index
shell: bash
run: python3 scripts/create_index.py
env:
PINECONE_API_KEY: ${{ inputs.PINECONE_API_KEY }}
NAME_PREFIX: ${{ inputs.name_prefix }}
REGION: ${{ inputs.region }}
CLOUD: ${{ inputs.cloud }}
DIMENSION: ${{ inputs.dimension }}
METRIC: ${{ inputs.metric }}
26 changes: 26 additions & 0 deletions .github/actions/delete-index/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: 'Delete Index'
description: 'Deletes an index to be used in other tests'

inputs:
index_name:
description: 'The name of the index to delete'
required: true
PINECONE_API_KEY:
description: 'The Pinecone API key'
required: true


runs:
using: 'composite'
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Delete index
shell: bash
run: python3 scripts/delete.py
env:
PINECONE_API_KEY: ${{ inputs.PINECONE_API_KEY }}
INDEX_NAME: ${{ inputs.index_name }}
18 changes: 18 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Create test index
id: create-index
uses: ./.github/actions/create-index
timeout-minutes: 2
with:
name_prefix: test-${{ github.run_number }}
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
- name: Test with pytest
run: |
pytest
env:
PINECONE_API_KEY: '${{ secrets.PINECONE_API_KEY }}'
INDEX_NAME: ${{ steps.create-index.outputs.index_name }}
- name: Delete test index
if: always()
uses: ./.github/actions/delete-index
timeout-minutes: 2
with:
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
index_name: ${{ steps.create-index.outputs.index_name }}

58 changes: 58 additions & 0 deletions tests/scripts/create_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Create a Pinecone index for use in some later GitHub action. Takes input from
environment variables, then writes the output to the file named in
GITHUB_OUTPUT.
"""

import os
import random
import string
from pinecone import Pinecone


def read_env_var(name):
value = os.environ.get(name)
if value is None:
raise 'Environment variable {} is not set'.format(name)
return value


def random_string(length):
return ''.join(random.choice(string.ascii_lowercase) for _ in range(length))


def write_gh_output(name, value):
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
print(f'{name}={value}', file=fh)


def main():
pc = Pinecone(api_key=read_env_var('PINECONE_API_KEY'))
index_name = read_env_var('NAME_PREFIX') + random_string(20)
environment = os.environ.get('ENVIRONMENT')
if environment:
spec = {
'pod': {
'environment': environment,
'pod_type': 'p1.x1'
}
}
else:
spec = {
'serverless': {
'cloud': read_env_var('CLOUD'),
'region': read_env_var('REGION'),
}
}

pc.create_index(
name=index_name,
metric=read_env_var('METRIC'),
dimension=int(read_env_var('DIMENSION')),
spec=spec
)
write_gh_output('index_name', index_name)


if __name__ == '__main__':
main()
24 changes: 24 additions & 0 deletions tests/scripts/delete_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Delete the Pinecone index named by the env var 'INDEX_NAME'
"""

import os
from pinecone import Pinecone


def read_env_var(name):
value = os.environ.get(name)
if value is None:
raise 'Environment variable {} is not set'.format(name)
return value


def main():
pc = Pinecone(api_key=read_env_var('PINECONE_API_KEY'))
to_delete = read_env_var('INDEX_NAME')
pc.delete_index(name=to_delete)
print('Index deleted: ' + to_delete)


if __name__ == '__main__':
main()

0 comments on commit e48f4db

Please sign in to comment.