Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions azure-quantum/azure/quantum/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ class ConnectionConstants:
# pylint: disable=unnecessary-lambda-assignment
GET_QUANTUM_PRODUCTION_ENDPOINT = \
lambda location: f"https://{location}.quantum.azure.com/"
GET_QUANTUM_PRODUCTION_ENDPOINT_v2 = \
lambda location: f"https://{location}-v2.quantum.azure.com/"
GET_QUANTUM_CANARY_ENDPOINT = \
lambda location: f"https://{location or 'eastus2euap'}.quantum.azure.com/"
GET_QUANTUM_DOGFOOD_ENDPOINT = \
lambda location: f"https://{location}.quantum-test.azure.com/"
GET_QUANTUM_DOGFOOD_ENDPOINT_v2 = \
lambda location: f"https://{location}-v2.quantum-test.azure.com/"

ARM_PRODUCTION_ENDPOINT = "https://management.azure.com/"
ARM_DOGFOOD_ENDPOINT = "https://api-dogfood.resources.windows-int.net/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WorkspaceConnectionParams:
ResourceGroupName=(?P<resource_group>[^\s;]+);
WorkspaceName=(?P<workspace_name>[^\s;]+);
ApiKey=(?P<api_key>[^\s;]+);
QuantumEndpoint=(?P<quantum_endpoint>https://(?P<location>[^\s\.]+).quantum(?:-test)?.azure.com/);
QuantumEndpoint=(?P<quantum_endpoint>https://(?P<location>[^\s\.]+?)(?:-v2)?.quantum(?:-test)?.azure.com/);
""",
re.VERBOSE | re.IGNORECASE)

Expand Down
72 changes: 72 additions & 0 deletions azure-quantum/tests/unit/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
quantum_endpoint=ConnectionConstants.GET_QUANTUM_PRODUCTION_ENDPOINT(LOCATION)
)

SIMPLE_CONNECTION_STRING_V2 = ConnectionConstants.VALID_CONNECTION_STRING(
subscription_id=SUBSCRIPTION_ID,
resource_group=RESOURCE_GROUP,
workspace_name=WORKSPACE,
api_key=API_KEY,
quantum_endpoint=ConnectionConstants.GET_QUANTUM_PRODUCTION_ENDPOINT_v2(LOCATION)
)


class TestWorkspace(QuantumTestBase):
def test_create_workspace_instance_valid(self):
Expand Down Expand Up @@ -204,6 +212,70 @@ def test_workspace_from_connection_string(self):
self.assertEqual(workspace.subscription_id, SUBSCRIPTION_ID)
self.assertEqual(workspace.resource_group, RESOURCE_GROUP)
self.assertEqual(workspace.name, WORKSPACE)

def test_workspace_from_connection_string_v2(self):
"""Test that v2 QuantumEndpoint format is correctly parsed."""
with mock.patch.dict(
os.environ,
clear=True
):
workspace = Workspace.from_connection_string(SIMPLE_CONNECTION_STRING_V2)
self.assertEqual(workspace.location, LOCATION)
self.assertEqual(workspace.subscription_id, SUBSCRIPTION_ID)
self.assertEqual(workspace.resource_group, RESOURCE_GROUP)
self.assertEqual(workspace.name, WORKSPACE)
self.assertIsInstance(workspace.credential, AzureKeyCredential)
self.assertEqual(workspace.credential.key, API_KEY)
# pylint: disable=protected-access
self.assertIsInstance(
workspace._client._config.authentication_policy,
AzureKeyCredentialPolicy)
auth_policy = workspace._client._config.authentication_policy
self.assertEqual(auth_policy._name, ConnectionConstants.QUANTUM_API_KEY_HEADER)
self.assertEqual(id(auth_policy._credential),
id(workspace.credential))

def test_workspace_from_connection_string_v2_dogfood(self):
"""Test v2 QuantumEndpoint with dogfood environment."""
canary_location = "eastus2euap"
dogfood_connection_string_v2 = ConnectionConstants.VALID_CONNECTION_STRING(
subscription_id=SUBSCRIPTION_ID,
resource_group=RESOURCE_GROUP,
workspace_name=WORKSPACE,
api_key=API_KEY,
quantum_endpoint=ConnectionConstants.GET_QUANTUM_DOGFOOD_ENDPOINT_v2(canary_location)
)

with mock.patch.dict(os.environ, clear=True):
workspace = Workspace.from_connection_string(dogfood_connection_string_v2)
self.assertEqual(workspace.location, canary_location)
self.assertEqual(workspace.subscription_id, SUBSCRIPTION_ID)
self.assertEqual(workspace.resource_group, RESOURCE_GROUP)
self.assertEqual(workspace.name, WORKSPACE)
self.assertIsInstance(workspace.credential, AzureKeyCredential)
self.assertEqual(workspace.credential.key, API_KEY)

def test_env_connection_string_v2(self):
"""Test v2 QuantumEndpoint from environment variable."""
with mock.patch.dict(os.environ):
self.clear_env_vars(os.environ)
os.environ[EnvironmentVariables.CONNECTION_STRING] = SIMPLE_CONNECTION_STRING_V2

workspace = Workspace()
self.assertEqual(workspace.location, LOCATION)
self.assertEqual(workspace.subscription_id, SUBSCRIPTION_ID)
self.assertEqual(workspace.name, WORKSPACE)
self.assertEqual(workspace.resource_group, RESOURCE_GROUP)
self.assertIsInstance(workspace.credential, AzureKeyCredential)
self.assertEqual(workspace.credential.key, API_KEY)
# pylint: disable=protected-access
self.assertIsInstance(
workspace._client._config.authentication_policy,
AzureKeyCredentialPolicy)
auth_policy = workspace._client._config.authentication_policy
self.assertEqual(auth_policy._name, ConnectionConstants.QUANTUM_API_KEY_HEADER)
self.assertEqual(id(auth_policy._credential),
id(workspace.credential))

def test_create_workspace_instance_invalid(self):
def assert_value_error(exception):
Expand Down