Skip to content

Commit

Permalink
Refactor: integration tests (#1543)
Browse files Browse the repository at this point in the history
* integration tests with pytest

* integration tests with pytest

* change action name

* added lint and black

* remove test_docker from other tests

* add build containers

* split ServerlessClient import

* fix integration tests action

* fix docker tests

* black formatted

* fix docker tests

* fix tests

* experimental tests

* fix test experimental

* remove unused imports

* fix experimental tests

* black

* fix test docker

* fix file add

* fix tests

* fix file open

* format

* add logs

* add logs

* modify experimental file download

* fix format

* new simple test

* skip tests

* format

* change filename

* upload a function before file

* modify tests

* fix lint

* modified tests

* remove skip

* fix tests

* fix lint

* modify tests and create a new folder

* fix scripts

* fix tests

* include docker tests

* merge tests

* restore file_download file

* ignore downloaded file in tests

* delete docker build action
  • Loading branch information
korgan00 authored Dec 10, 2024
1 parent baf9026 commit edac72a
Show file tree
Hide file tree
Showing 29 changed files with 596 additions and 104 deletions.
73 changes: 0 additions & 73 deletions .github/workflows/docker-build.yaml

This file was deleted.

49 changes: 49 additions & 0 deletions .github/workflows/integration-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Integration tests

on:
pull_request:

permissions:
contents: read

jobs:
verify-integration:
name: lint, test

runs-on: ubuntu-latest
strategy:
fail-fast: false # keep running if one leg fails
matrix:
python-version:
- '3.11'

defaults:
run:
working-directory: ./tests

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 #4.1.7
- name: Build the containers
run: docker compose -f ../docker-compose-dev.yaml build

- uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Set up tox environment
run: |
pver=${{ matrix.python-version }}
tox_env="-epy${pver/./}"
echo tox_env
echo TOX_ENV=$tox_env >> $GITHUB_ENV
- name: Install tox
run: |
pip install tox==4.16.0
- name: Run styles check
run: tox -elint

- name: Test using tox environment
run: |
tox ${{ env.TOX_ENV }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
tests/**/downloaded_*.tar

# pyenv
.python-version
Expand Down
10 changes: 5 additions & 5 deletions tests/basic/02_arguments_and_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from qiskit import QuantumCircuit

from qiskit_serverless import ServerlessClient, QiskitFunction
import os

circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()
circuit.draw()

from qiskit_serverless import ServerlessClient, QiskitFunction
import os

serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
Expand All @@ -32,9 +32,9 @@
print(job)

try:
print(job.result())
print(job.result())
except:
print(job.error_message())
print(job.error_message())

print(job.status())
print(job.logs())
4 changes: 2 additions & 2 deletions tests/basic/04_distributed_workloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
print(job)

try:
print(job.result())
print(job.result())
except:
print(job.error_message())
print(job.error_message())

print(job.status())
print(job.logs())
4 changes: 3 additions & 1 deletion tests/basic/05_retrieving_past_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from qiskit_serverless import QiskitFunction

function = QiskitFunction(
title="pattern-to-fetch-results", entrypoint="pattern.py", working_dir="./source_files/"
title="pattern-to-fetch-results",
entrypoint="pattern.py",
working_dir="./source_files/",
)
serverless.upload(function)

Expand Down
18 changes: 9 additions & 9 deletions tests/basic/06_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from qiskit_serverless import QiskitFunction, ServerlessClient

serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
)

help = """
Expand All @@ -16,18 +16,18 @@
"""

function_with_custom_image = QiskitFunction(
title="custom-image-function",
image="test_function:latest",
provider=os.environ.get("PROVIDER_ID", "mockprovider"),
description=help
title="custom-image-function",
image="test_function:latest",
provider=os.environ.get("PROVIDER_ID", "mockprovider"),
description=help,
)
serverless.upload(function_with_custom_image)

my_functions = serverless.list()
for function in my_functions:
print("Name: " + function.title)
print(function.description)
print()
print("Name: " + function.title)
print(function.description)
print()

my_function = serverless.get("custom-image-function")
job = my_function.run(message="Argument for the custum function")
Expand Down
2 changes: 2 additions & 0 deletions tests/basic/function/runner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler as Sampler


def custom_function(arguments):
# all print statement will be available in job logs
print("Running function...")
Expand All @@ -20,6 +21,7 @@ def custom_function(arguments):
print("Completed running pattern.")
return quasi_dists


class Runner:
def run(self, arguments: dict) -> dict:
return custom_function(arguments)
6 changes: 3 additions & 3 deletions tests/basic/source_files/pattern_with_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import pendulum

dt_toronto = pendulum.datetime(2012, 1, 1, tz='America/Toronto')
dt_vancouver = pendulum.datetime(2012, 1, 1, tz='America/Vancouver')
dt_toronto = pendulum.datetime(2012, 1, 1, tz="America/Toronto")
dt_vancouver = pendulum.datetime(2012, 1, 1, tz="America/Vancouver")

diff = dt_vancouver.diff(dt_toronto).in_hours()
diff = dt_vancouver.diff(dt_toronto).in_hours()

print(diff)
save_result({"hours": diff})
42 changes: 42 additions & 0 deletions tests/docker/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# pylint: disable=import-error, invalid-name
""" Fixtures for tests """
import os

from pytest import fixture
from testcontainers.compose import DockerCompose
from qiskit_serverless import ServerlessClient, QiskitFunction

resources_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "source_files"
)


@fixture(scope="module")
def serverless_client():
"""Fixture for testing files."""
compose = DockerCompose(
resources_path,
compose_file_name="../../../docker-compose-dev.yaml",
pull=True,
)
compose.start()

connection_url = "http://localhost:8000"
compose.wait_for(f"{connection_url}/backoffice")

serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", connection_url),
)

# Initialize serverless folder for current user
function = QiskitFunction(
title="hello-world",
entrypoint="hello_world.py",
working_dir=resources_path,
)
serverless.upload(function)

yield serverless

compose.stop()
9 changes: 9 additions & 0 deletions tests/docker/source_files/circuit_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from qiskit import QuantumCircuit


def create_hello_world_circuit() -> QuantumCircuit:
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()
return circuit
10 changes: 10 additions & 0 deletions tests/docker/source_files/consume_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import tarfile
from qiskit_serverless import save_result

with tarfile.open("/data/my_file.tar", "r:gz") as tar:
with tar.extractfile("./my_file.txt") as f:
text = f.read()

print(text)
save_result({"Message": str(text)})
Binary file added tests/docker/source_files/data.tar
Binary file not shown.
1 change: 1 addition & 0 deletions tests/docker/source_files/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello World!")
22 changes: 22 additions & 0 deletions tests/docker/source_files/pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler as Sampler

from qiskit_serverless import save_result

# all print statement will be available in job logs
print("Running pattern...")

# creating circuit
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()

# running Sampler primitive
sampler = Sampler()
quasi_dists = sampler.run([(circuit)]).result()[0].data.meas.get_counts()

# saves results of program execution,
# which will be accessible by calling `.result()`
save_result(quasi_dists)
print("Completed running pattern.")
20 changes: 20 additions & 0 deletions tests/docker/source_files/pattern_with_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# source_files/program_3.py

from qiskit_serverless import get_arguments, save_result

from qiskit.primitives import StatevectorSampler as Sampler

# get all arguments passed to this program
arguments = get_arguments()

# get specific argument that we are interested in
circuit = arguments.get("circuit")

sampler = Sampler()

quasi_dists = sampler.run([(circuit)]).result()[0].data.meas.get_counts()

print(f"Quasi distribution: {quasi_dists}")

# saving results of a program
save_result({"quasi_dists": quasi_dists})
13 changes: 13 additions & 0 deletions tests/docker/source_files/pattern_with_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# source_files/program_4.py

from qiskit_serverless import save_result

import pendulum

dt_toronto = pendulum.datetime(2012, 1, 1, tz="America/Toronto")
dt_vancouver = pendulum.datetime(2012, 1, 1, tz="America/Vancouver")

diff = dt_vancouver.diff(dt_toronto).in_hours()

print(diff)
save_result({"hours": diff})
Loading

0 comments on commit edac72a

Please sign in to comment.