diff --git a/.github/actions/create-index/action.yml b/.github/actions/create-index/action.yml new file mode 100644 index 0000000..f017118 --- /dev/null +++ b/.github/actions/create-index/action.yml @@ -0,0 +1,47 @@ +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: Create index + id: create-index + shell: bash + run: python3 tests/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 }} \ No newline at end of file diff --git a/.github/actions/delete-index/action.yml b/.github/actions/delete-index/action.yml new file mode 100644 index 0000000..1cc230f --- /dev/null +++ b/.github/actions/delete-index/action.yml @@ -0,0 +1,21 @@ +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: Delete index + shell: bash + run: python3 tests/scripts/delete_index.py + env: + PINECONE_API_KEY: ${{ inputs.PINECONE_API_KEY }} + INDEX_NAME: ${{ inputs.index_name }} diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index fd04aae..92589b1 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -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 }} + diff --git a/tests/scripts/create_index.py b/tests/scripts/create_index.py new file mode 100644 index 0000000..419bfe2 --- /dev/null +++ b/tests/scripts/create_index.py @@ -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() diff --git a/tests/scripts/delete_index.py b/tests/scripts/delete_index.py new file mode 100644 index 0000000..ee57ece --- /dev/null +++ b/tests/scripts/delete_index.py @@ -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() diff --git a/tests/test_dummy.py b/tests/test_dummy.py new file mode 100644 index 0000000..62e45ca --- /dev/null +++ b/tests/test_dummy.py @@ -0,0 +1,2 @@ +def test_dummy(): + pass \ No newline at end of file