Skip to content

Commit

Permalink
restoring attampt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadav Ben Ami authored and Nadav Ben Ami committed Oct 8, 2024
1 parent f073698 commit 04bc441
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 14 deletions.
55 changes: 55 additions & 0 deletions .github/actions/run-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: "Test Notebooks Action"
description: "Runs tests on a list of notebooks based on the specified environment."

inputs:
# diff files - set to python inside pytest
should_test_all_files:
description: "'true' / 'false'."
required: true
list_of_ipynb_changed:
description: "The list of changed files."
required: true
# aws environment
is_dev:
description: "The secret for environment authentication."
required: true
m2m_secret_arn:
description: "The secret for environment authentication."
required: true
aws_role:
description: "The AWS role to assume for configuring credentials during the execution."
required: true
# environment
classiq_ide:
description: "The secret for environment authentication."
required: true
classiq_host:
description: "The secret for environment authentication."
required: true

runs:
using: "composite"
steps:
- uses: aws-actions/[email protected]
with:
role-to-assume: ${{ inputs.aws_role }}
aws-region: us-east-1
mask-aws-account-id: true

- name: Set authentication
run: .github/scripts/get_m2m_token.sh
shell: bash
env:
IS_DEV: "${{ inputs.is_dev }}"
M2M_SECRET_ARN: "${{ inputs.m2m_secret_arn }}"

- name: Run Notebooks
run: python -m pytest --log-cli-level=INFO tests
env:
JUPYTER_PLATFORM_DIRS: "1"
SHOULD_TEST_ALL_FILES: "${{ inputs.should_test_all_files }}"
LIST_OF_IPYNB_CHANGED: "${{ inputs.list_of_ipynb_changed }}"
CLASSIQ_IDE: "${{ inputs.classiq_ide }}"
CLASSIQ_HOST: "${{ inputs.classiq_host }}"

shell: bash
10 changes: 10 additions & 0 deletions .github/scripts/get_m2m_token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

export SECRET_ARN=$M2M_SECRET_ARN
if [ "$IS_DEV" = "true" ]; then
aws codeartifact login --tool pip --domain classiq-cadmium --repository Pypi-Classiq-Non-Prod
fi

aws secretsmanager get-secret-value --secret-id "$SECRET_ARN" | \
jq '{"classiqTokenAccount": .SecretString | fromjson | .access_token }' > "${HOME}/.classiq-credentials"
46 changes: 37 additions & 9 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
name: "Test notebooks"
name: "Test notebooks old"

on: [pull_request]
on:
pull_request:
push:
branches:
- dev

jobs:
tests:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: "Install dependencies"
run: |
python -m pip install -U -r requirements.txt
python -m pip install -U -r requirements_tests.txt

- name: Get changed files - all
id: changed-files-all
Expand All @@ -26,26 +29,51 @@ jobs:
files: |
**.ipynb
- uses: aws-actions/[email protected]
with:
role-to-assume: ${{ secrets.AWS_ROLE }}
aws-region: us-east-1
mask-aws-account-id: true

- name: Set environment variables
run: |
set -ex
echo "CLASSIQ_TEXT_ONLY=true" >> $GITHUB_ENV
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "SHOULD_TEST_ALL_FILES=false" >> $GITHUB_ENV
echo "HAS_ANY_FILE_CHANGED=${{ steps.changed-files-all.outputs.any_changed }}" >> $GITHUB_ENV
echo "LIST_OF_FILE_CHANGED=${{ steps.changed-files-all.outputs.all_changed_files }}" >> $GITHUB_ENV
echo "HAS_ANY_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.any_changed }}" >> $GITHUB_ENV
echo "LIST_OF_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.all_changed_files }}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == 'workflow_dispatch' ]; then
elif [[ "${{ github.event_name }}" == 'workflow_dispatch' || "${{ github.head_ref || github.ref_name }}" == "dev" ]]; then
echo "SHOULD_TEST_ALL_FILES=true" >> $GITHUB_ENV
echo "HAS_ANY_FILE_CHANGED=None" >> $GITHUB_ENV
echo "LIST_OF_FILE_CHANGED=None" >> $GITHUB_ENV
echo "HAS_ANY_IPYNB_CHANGED=None" >> $GITHUB_ENV
echo "LIST_OF_IPYNB_CHANGED=None" >> $GITHUB_ENV
fi
if [ "${{ github.head_ref || github.ref_name }}" == "dev" ]; then
echo "CLASSIQ_IDE=https://nightly.platform.classiq.io" >> $GITHUB_ENV
echo "CLASSIQ_HOST=https://staging.api.classiq.io" >> $GITHUB_ENV
echo "IS_DEV=true" >> $GITHUB_ENV
fi
- name: Set authentication
run: echo "${{ secrets.CLASSIQ_CREDENTIALS_B64 }}" | base64 --decode > "${HOME}/.classiq-credentials"
run: .github/scripts/get_m2m_token.sh
env:
PROD_M2M_SECRET_ARN: "${{ secrets.PROD_M2M_SECRET_ARN }}"
NIGHTLY_M2M_SECRET_ARN: "${{ secrets.NIGHTLY_M2M_SECRET_ARN }}"

- name: Install dependencies
run: |
set -e
# Pre is needed for Dev pre releases
python -m pip install --extra-index-url https://pypi.org/simple --pre -U -r requirements.txt
python -m pip install --extra-index-url https://pypi.org/simple -U -r requirements_tests.txt
- name: "Run tests"
run: python -m pytest tests
run: python -m pytest --log-cli-level=INFO tests
env:
JUPYTER_PLATFORM_DIRS: "1"
108 changes: 108 additions & 0 deletions .github/workflows/test-CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Test Library CI

on:
# Trigger the workflow on push to the specific branch
push:
branches:
- dev
- main
-
# Trigger the workflow on pull requests targeting the specific branch
pull_request_target: # Note: `pull_request_target` ensures that the tests run in the context of the `main` branch, not in the user's fork. This has important security implications and should not be changed to `pull_request`
# types: [opened, synchronize] # Triggered when PR is open or updated with new commit
branches:
- dev
- main

# Add a manual trigger option for running the workflow
workflow_dispatch:

jobs:
test:
permissions:
id-token: write
contents: read

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install dependencies
run: |
set -e
# Pre is needed for Dev pre releases
python -m pip install --extra-index-url https://pypi.org/simple --pre -U -r requirements.txt
python -m pip install --extra-index-url https://pypi.org/simple -U -r requirements_tests.txt
# A bunch of if-else. Might move to an action
# Decide environment based on the target branch (for both push and PR events)
- name: Set environment based on target branch
run: |
set -ex
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
# Use the target branch of the pull request
target_branch="${{ github.event.pull_request.base.ref }}"
else
# Use the branch of the push event
# todo: verify that dispatch works
target_branch="${{ github.ref_name }}"
fi
if [[ "$target_branch" == "main" ]]; then
echo "Running on prod environment."
echo "M2M_SECRET_ARN=${{ secrets.PROD_M2M_SECRET_ARN }}" >> $GITHUB_ENV
echo "CLASSIQ_IDE=https://platform.classiq.io" >> $GITHUB_ENV
echo "CLASSIQ_HOST=https://api.classiq.io" >> $GITHUB_ENV
echo "IS_DEV=false" >> $GITHUB_ENV
else
echo "Running on dev environment."
echo "M2M_SECRET_ARN=${{ secrets.NIGHTLY_M2M_SECRET_ARN }}" >> $GITHUB_ENV
echo "CLASSIQ_IDE=https://nightly.platform.classiq.io" >> $GITHUB_ENV
echo "CLASSIQ_HOST=https://staging.api.classiq.io" >> $GITHUB_ENV
echo "IS_DEV=true" >> $GITHUB_ENV
fi
shell: bash

# The following 2 steps can also be grouped into one action
# Step to detect changed .ipynb files
- name: Get changed notebook files
id: changed-files
uses: tj-actions/changed-files@v44
with:
files: |
**/*.ipynb
- name: Set changed notebook into environment variables
run: |
set -ex
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "SHOULD_TEST_ALL_FILES=false" >> $GITHUB_ENV
elif [[ "${{ github.event_name }}" == 'workflow_dispatch' || "${{ github.head_ref || github.ref_name }}" == "dev" ]]; then
echo "SHOULD_TEST_ALL_FILES=true" >> $GITHUB_ENV
fi
# Run notebook tests if any changed notebooks are detected
- name: Run notebook tests
if: steps.changed-files.outputs.any_changed == 'true'
uses: ./.github/actions/run-tests # Calls your composite action
with:
# diff files - set to python inside pytest
should_test_all_files: ${{ env.SHOULD_TEST_ALL_FILES }}
list_of_ipynb_changed: ${{ steps.changed-files-ipynb.outputs.all_changed_files }}
# aws environment
m2m_secret_arn: ${{ env.M2M_SECRET_ARN }}
aws_role: ${{ secrets.AWS_ROLE }}
is_dev: ${{ env.IS_DEV }}
# environment
classiq_ide: ${{ env.CLASSIQ_IDE }}
classiq_host: ${{ env.CLASSIQ_HOST }}
1 change: 1 addition & 0 deletions requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest

testbook
torch
5 changes: 4 additions & 1 deletion tests/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os
import logging

from testbook import testbook # type: ignore[import]
from utils_for_tests import iterate_notebooks

TIMEOUT: int = 60 * 3 # 3 minutes
TIMEOUT: int = 60 * 10 # 10 minutes
LOGGER = logging.getLogger(__name__)


def test_notebooks() -> None:
current_dir = os.getcwd()
for notebook_path in iterate_notebooks():
LOGGER.info(f"Exeucting notebook {notebook_path}")
os.chdir(os.path.dirname(notebook_path))

with testbook(os.path.basename(notebook_path), execute=True, timeout=TIMEOUT):
Expand Down
8 changes: 4 additions & 4 deletions tests/utils_for_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def iterate_notebooks() -> Iterable[str]:
if os.environ.get("SHOULD_TEST_ALL_FILES", "") == "true":
notebooks_to_test = _get_all_notebooks()
else:
if os.environ.get("HAS_ANY_IPYNB_CHANGED", "") == "true":
notebooks_to_test = os.environ.get("LIST_OF_IPYNB_CHANGED", "").split()
else:
notebooks_to_test = []
# if os.environ.get("HAS_ANY_IPYNB_CHANGED", "") == "true":
notebooks_to_test = os.environ.get("LIST_OF_IPYNB_CHANGED", "").split()
# else:
# notebooks_to_test = []

return notebooks_to_test

Expand Down

0 comments on commit 04bc441

Please sign in to comment.