From 1d9a8ebbbb7c12870e9dbce49adc1fb23a6f9395 Mon Sep 17 00:00:00 2001 From: Aaron Berdy Date: Fri, 20 Oct 2023 13:53:28 -0700 Subject: [PATCH] test: pytest xfail python version mismatch (#751) --- test/integ_tests/test_create_quantum_job.py | 168 ++++++++++---------- 1 file changed, 85 insertions(+), 83 deletions(-) diff --git a/test/integ_tests/test_create_quantum_job.py b/test/integ_tests/test_create_quantum_job.py index 7cab63c94..640cb1688 100644 --- a/test/integ_tests/test_create_quantum_job.py +++ b/test/integ_tests/test_create_quantum_job.py @@ -14,9 +14,9 @@ import json import os.path import re +import sys import tempfile from pathlib import Path -from warnings import warn import job_test_script import pytest @@ -24,7 +24,15 @@ from braket.aws.aws_quantum_job import AwsQuantumJob from braket.devices import Devices -from braket.jobs import get_input_data_dir, hybrid_job, save_job_result +from braket.jobs import Framework, get_input_data_dir, hybrid_job, retrieve_image, save_job_result + + +@pytest.fixture +def decorator_python_version(aws_session): + image_uri = retrieve_image(Framework.BASE, aws_session.region) + tag = aws_session.get_full_image_tag(image_uri) + major_version, minor_version = re.search(r"-py(\d)(\d+)-", tag).groups() + return major_version, minor_version def test_failed_quantum_job(aws_session, capsys): @@ -190,6 +198,11 @@ def test_completed_quantum_job(aws_session, capsys): assert data in log_data +@pytest.mark.xfail( + (sys.version_info.major, sys.version_info.minor) != decorator_python_version, + raises=RuntimeError, + reason="Python version mismatch", +) def test_decorator_job(): class MyClass: attribute = "value" @@ -197,46 +210,38 @@ class MyClass: def __str__(self): return f"MyClass({self.attribute})" - try: - - @hybrid_job( - device=Devices.Amazon.SV1, - include_modules="job_test_script", - dependencies=str(Path("test", "integ_tests", "requirements.txt")), - input_data=str(Path("test", "integ_tests", "requirements")), - ) - def decorator_job(a, b: int, c=0, d: float = 1.0, **extras): - save_job_result(job_test_script.job_helper()) - with open(Path(get_input_data_dir()) / "requirements.txt", "r") as f: - assert f.readlines() == ["pytest\n"] - with open(Path("test", "integ_tests", "requirements.txt"), "r") as f: - assert f.readlines() == ["pytest\n"] - assert dir(pytest) - assert a.attribute == "value" - assert b == 2 - assert c == 0 - assert d == 5 - assert extras["extra_arg"] == "extra_value" - - hp_file = os.environ["AMZN_BRAKET_HP_FILE"] - with open(hp_file, "r") as f: - hyperparameters = json.load(f) - assert hyperparameters == { - "a": "MyClass{value}", - "b": "2", - "c": "0", - "d": "5", - "extra_arg": "extra_value", - } - - with open("test/output_file.txt", "w") as f: - f.write("hello") - - except RuntimeError as e: - if str(e).startswith("Python version must match between local environment and container."): - warn("skipping test due to python version mismatch") - return - raise e + @hybrid_job( + device=Devices.Amazon.SV1, + include_modules="job_test_script", + dependencies=str(Path("test", "integ_tests", "requirements.txt")), + input_data=str(Path("test", "integ_tests", "requirements")), + ) + def decorator_job(a, b: int, c=0, d: float = 1.0, **extras): + save_job_result(job_test_script.job_helper()) + with open(Path(get_input_data_dir()) / "requirements.txt", "r") as f: + assert f.readlines() == ["pytest\n"] + with open(Path("test", "integ_tests", "requirements.txt"), "r") as f: + assert f.readlines() == ["pytest\n"] + assert dir(pytest) + assert a.attribute == "value" + assert b == 2 + assert c == 0 + assert d == 5 + assert extras["extra_arg"] == "extra_value" + + hp_file = os.environ["AMZN_BRAKET_HP_FILE"] + with open(hp_file, "r") as f: + hyperparameters = json.load(f) + assert hyperparameters == { + "a": "MyClass{value}", + "b": "2", + "c": "0", + "d": "5", + "extra_arg": "extra_value", + } + + with open("test/output_file.txt", "w") as f: + f.write("hello") job = decorator_job(MyClass(), 2, d=5, extra_arg="extra_value") assert job.result()["status"] == "SUCCESS" @@ -257,50 +262,47 @@ def decorator_job(a, b: int, c=0, d: float = 1.0, **extras): os.chdir(current_dir) +@pytest.mark.xfail( + (sys.version_info.major, sys.version_info.minor) != decorator_python_version, + raises=RuntimeError, + reason="Python version mismatch", +) def test_decorator_job_submodule(): - try: - - @hybrid_job( - device=Devices.Amazon.SV1, - include_modules=[ + @hybrid_job( + device=Devices.Amazon.SV1, + include_modules=[ + "job_test_module", + ], + dependencies=Path( + "test", "integ_tests", "job_test_module", "job_test_submodule", "requirements.txt" + ), + input_data={ + "my_input": str(Path("test", "integ_tests", "requirements.txt")), + "my_dir": str(Path("test", "integ_tests", "job_test_module")), + }, + ) + def decorator_job_submodule(): + save_job_result(submodule_helper()) + with open(Path(get_input_data_dir("my_input")) / "requirements.txt", "r") as f: + assert f.readlines() == ["pytest\n"] + with open(Path("test", "integ_tests", "requirements.txt"), "r") as f: + assert f.readlines() == ["pytest\n"] + with open( + Path(get_input_data_dir("my_dir")) / "job_test_submodule" / "requirements.txt", "r" + ) as f: + assert f.readlines() == ["pytest\n"] + with open( + Path( + "test", + "integ_tests", "job_test_module", - ], - dependencies=Path( - "test", "integ_tests", "job_test_module", "job_test_submodule", "requirements.txt" + "job_test_submodule", + "requirements.txt", ), - input_data={ - "my_input": str(Path("test", "integ_tests", "requirements.txt")), - "my_dir": str(Path("test", "integ_tests", "job_test_module")), - }, - ) - def decorator_job_submodule(): - save_job_result(submodule_helper()) - with open(Path(get_input_data_dir("my_input")) / "requirements.txt", "r") as f: - assert f.readlines() == ["pytest\n"] - with open(Path("test", "integ_tests", "requirements.txt"), "r") as f: - assert f.readlines() == ["pytest\n"] - with open( - Path(get_input_data_dir("my_dir")) / "job_test_submodule" / "requirements.txt", "r" - ) as f: - assert f.readlines() == ["pytest\n"] - with open( - Path( - "test", - "integ_tests", - "job_test_module", - "job_test_submodule", - "requirements.txt", - ), - "r", - ) as f: - assert f.readlines() == ["pytest\n"] - assert dir(pytest) - - except RuntimeError as e: - if str(e).startswith("Python version must match between local environment and container."): - warn("skipping test due to python version mismatch") - return - raise e + "r", + ) as f: + assert f.readlines() == ["pytest\n"] + assert dir(pytest) job = decorator_job_submodule() assert job.result()["status"] == "SUCCESS"