Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI test user's PR to main (dont merge) #535

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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/configure-aws-credentials@v4.0.2
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"
166 changes: 166 additions & 0 deletions .github/workflows/test-CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
name: Test Library CI

on:
# Trigger the workflow on push to the specific branch
push:
branches:
- dev
- main
- CAD-22795-restore-changes # Temp

# 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.
branches:
- dev
- main
- CAD-22795-restore-changes # Temp

# 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

# Check out the branch from the pull request, whether it's from a fork or not
- name: Checkout the pull request's branch
run: |
set -ex
# debug
echo "==== before"
git status
if [[ "${{ github.event_name }}" == "pull_request_target" ]]; then
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
echo "PR from a fork detected. Checking out the fork's branch."
git remote add fork https://github.com/${{ github.event.pull_request.head.repo.full_name }}.git
git fetch fork ${{ github.event.pull_request.head.ref }}
git checkout -B ci-testing-branch FETCH_HEAD
else
echo "PR from the same repository detected. Checking out the branch."
git fetch origin ${{ github.event.pull_request.head.ref }}
git checkout ${{ github.event.pull_request.head.ref }}
fi
elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
# we believe that `actions/checkout` will checkout to the branch that you chose in the `dispatch` tab
# so we do nothing
echo "we're in the right branch. no need to checkout (dispatch)"
elif [[ "${{ github.event_name }}" == "push" ]]; then
# we believe that `actions/checkout` will checkout to the branch that we push into (i.e. either `dev` or `main`)
echo "we're in the right branch. no need to checkout (push)"
else
echo "we're not in push/pull/dispatch. error."
exit 1
fi
# debug
echo "==== after"
git status

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

# 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_target" ]]; then
# Use the target branch of the pull request
echo "were in pull_request_target"
target_branch="${{ github.event.pull_request.base.ref }}" # Probably should be updated to produce the target branch and not the branch
elif [[ "${{ github.event_name }}" == "push" ]]; then
echo "were in push"
target_branch="$(gh pr view --json baseRefName,headRefName --jq .baseRefName)"
else
# Use the branch of the push event
# todo: verify that dispatch works
echo "On dsipatch - WIP"
target_branch="${{ github.ref_name }}"
echo $target_branch
echo $target_branch
echo $target_branch
echo $target_branch
echo $target_branch
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
env:
GH_TOKEN: ${{ github.token }}

# 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-ipynb
uses: tj-actions/changed-files@v44
with:
files: |
**/*.ipynb

- name: Print changed notebook files
run: |
echo "Changed notebook files: ${{ steps.changed-files-ipynb.outputs.all_changed_files }}"

- name: Set changed notebook into environment variables
run: |
set -ex
if [ "${{ github.event_name }}" == 'pull_request_target' ]; 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

- 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: Correct directory after manual checkout # Debug step to confirm path

run: |
git status
pwd
ls -la .github/actions/ # Debug step to confirm path

# Run notebook tests if any changed notebooks are detected
- name: Run notebook tests
if: steps.changed-files-ipynb.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 }}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
with:
python-version: "3.11"

- name: Debug
run: git status

- name: Install dependencies
run: |
set -e
Expand Down
2 changes: 2 additions & 0 deletions requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pytest

testbook
torch
torchvision
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"\n",
"\n",
"@qfunc\n",
"def prepare_minus(target: Output[QBit]):\n",
"def prepare_minus(target: Output[QBit])\n",
" allocate(out=target, num_qubits=1)\n",
" X(target)\n",
" H(target)"
Expand Down
2 changes: 0 additions & 2 deletions tutorials/qml_with_classiq_guide/qml_with_classiq_guide.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@
"source": [
"- Algorithms and application tutorials using the VQE primitive:\n",
" - [Molecule Eigensolver](https://docs.classiq.io/latest/explore/applications/chemistry/molecule_eigensolver/molecule_eigensolver/)\n",
" - [H2 Molecule Example](https://docs.classiq.io/latest/explore/community/QClass_2024/Submissions/HW3/bogachan_arslan-HW3_QClass2024/#part-1)\n",
" - [Ground State Solver](https://docs.classiq.io/latest/user-guide/applications/ground-state-solving/#references)\n",
"- Further reading from the reference manual:\n",
" - [Execution Primitives](https://docs.classiq.io/latest/user-guide/executor/primitives/#vqe)\n"
Expand Down Expand Up @@ -1235,7 +1234,6 @@
"- Algorithms and application tutorials using the PyTorch intgration:\n",
" - [Quantum Autoencoder](https://docs.classiq.io/latest/explore/algorithms/qml/quantum_autoencoder/quantum_autoencoder/)\n",
" - [QGAN](https://docs.classiq.io/latest/explore/algorithms/qml/qgan/qgan_bars_and_strips/)\n",
" - [QNN for the XOR Problem](https://docs.classiq.io/latest/explore/community/QClass_2024/Submissions/HW4/Claudia_Zendejas-Morales_HW4_QClass2024/)\n",
"- Further reading from the reference manual:\n",
" - [QNNs with Classiq](https://docs.classiq.io/latest/user-guide/applications/qml/qnn/)\n",
" - [QLayer](https://docs.classiq.io/latest/user-guide/applications/qml/qnn/qlayer/)"
Expand Down
Loading