Skip to content

Commit

Permalink
test: add test case for sync cmd apic integration create apim and `…
Browse files Browse the repository at this point in the history
…apic integration create aws` (Azure#86)

* test: add test case for apim sync

* refactor: refactor for apim preparer

* refactor: refactoring case and utils, optimize checkers

* chore: remove print and add explaination

* refactor: rename file

* fix: try to fix error determing the version

* revert: Remove specific azure-cli and azure-core installations

* test: add aws sync testcase (Azure#87)

* test: add test case for aws sync command

* fix: remove key value

* fix: remove pip install

* chore: renaming constants

* refactor: update the utils and test case

* refactor: updated
  • Loading branch information
frankqianms authored Dec 27, 2024
1 parent 58016fd commit 94f6719
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@

TEST_REGION = "eastus"
# to set USERASSIGNED_IDENTITY, refer to https://learn.microsoft.com/en-us/azure/api-center/import-api-management-apis?tabs=portal#option-2-import-apis-directly-from-your-api-management-instance
USERASSIGNED_IDENTITY = os.getenv('USERASSIGNED_IDENTITY')
USERASSIGNED_IDENTITY = os.getenv('USERASSIGNED_IDENTITY')
# aws credentials KeyVault references
AWS_ACCESS_KEY_LINK = os.getenv('AWS_ACCESS_KEY_LINK')
AWS_SECRET_ACCESS_KEY_LINK = os.getenv('AWS_SECRET_ACCESS_KEY_LINK')
AWS_REGION = "us-west-2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
interactions:
- request:
body: '{"properties": {"apiSourceType": "AzureApiManagement", "azureApiManagementSource":
{"msiResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test",
"resourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiManagement/service/clitest000004"},
"importSpecification": "always"}}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic integration create apim
Connection:
- keep-alive
Content-Length:
- '467'
Content-Type:
- application/json
ParameterSetName:
- -g -n --azure-apim -i --msi-resource-id
User-Agent:
- AZURECLI/2.67.0 azsdk-python-core/1.28.0 Python/3.11.9 (Windows-10-10.0.22631-SP0)
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003?api-version=2024-06-01-preview
response:
body:
string: '{"type":"Microsoft.ApiCenter/services/workspaces/apiSources","properties":{"apiSourceType":"AzureApiManagement","apimSource":{"resourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiManagement/service/clitest000004","msiResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test"},"azureApiManagementSource":{"resourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiManagement/service/clitest000004","msiResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test"},"sourceLifecycleStage":"design","targetEnvironmentId":"/workspaces/default/environments/azure-api-management","targetLifecycleStage":"design","importSpecification":"always","linkState":{"state":"initializing","lastUpdatedOn":"2024-12-24T07:30:53.3997958Z"}},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003","name":"cli000003","systemData":{"createdAt":"2024-12-24T07:30:53.3997938Z","lastModifiedAt":"2024-12-24T07:30:53.3997919Z"}}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview, 2024-06-01-preview
cache-control:
- no-cache
content-length:
- '1408'
content-type:
- application/json; charset=utf-8
date:
- Tue, 24 Dec 2024 07:30:53 GMT
etag:
- 0a00e783-0000-3300-0000-676a632d0000
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000; includeSubDomains
vary:
- Accept-Encoding
x-cache:
- CONFIG_NOCACHE
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-subscription-global-writes:
- '11999'
x-ms-ratelimit-remaining-subscription-writes:
- '799'
x-msedge-ref:
- 'Ref A: 8A292E25303F4B6F8FE86A99C399FA6B Ref B: MAA201060513045 Ref C: 2024-12-24T07:30:50Z'
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic integration show
Connection:
- keep-alive
ParameterSetName:
- -g -n -i
User-Agent:
- AZURECLI/2.67.0 azsdk-python-core/1.28.0 Python/3.11.9 (Windows-10-10.0.22631-SP0)
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003?api-version=2024-06-01-preview
response:
body:
string: '{"type":"Microsoft.ApiCenter/services/workspaces/apiSources","properties":{"apiSourceType":"AzureApiManagement","apimSource":{"resourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiManagement/service/clitest000004","msiResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test"},"azureApiManagementSource":{"resourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiManagement/service/clitest000004","msiResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test"},"sourceLifecycleStage":"design","targetEnvironmentId":"/workspaces/default/environments/azure-api-management","targetLifecycleStage":"design","importSpecification":"always","linkState":{"state":"initializing","lastUpdatedOn":"2024-12-24T07:30:53.3997958Z"}},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003","name":"cli000003","systemData":{"createdAt":"2024-12-24T07:30:53.3997938Z","lastModifiedAt":"2024-12-24T07:30:53.3997919Z"}}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview, 2024-06-01-preview
cache-control:
- no-cache
content-length:
- '1408'
content-type:
- application/json; charset=utf-8
date:
- Tue, 24 Dec 2024 07:30:55 GMT
etag:
- 0a00e783-0000-3300-0000-676a632d0000
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000; includeSubDomains
vary:
- Accept-Encoding
x-cache:
- CONFIG_NOCACHE
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-subscription-global-reads:
- '16499'
x-msedge-ref:
- 'Ref A: EC6649C72E924E18B01BEBABEE4C3426 Ref B: MAA201060516027 Ref C: 2024-12-24T07:30:54Z'
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
interactions:
- request:
body: '{"properties": {"amazonApiGatewaySource": {"accessKey": "https://kv-canary-franktest.vault.azure.net/secrets/AccessKey",
"regionName": "us-west-2", "secretAccessKey": "https://kv-canary-franktest.vault.azure.net/secrets/SecretAccessKey"},
"apiSourceType": "AmazonApiGateway", "importSpecification": "always"}}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic integration create aws
Connection:
- keep-alive
Content-Length:
- '309'
Content-Type:
- application/json
ParameterSetName:
- -g -n -i --aws-access-key-reference --aws-region --aws-secret-access-key-reference
User-Agent:
- AZURECLI/2.67.0 azsdk-python-core/1.28.0 Python/3.11.9 (Windows-10-10.0.22631-SP0)
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003?api-version=2024-06-01-preview
response:
body:
string: '{"type":"Microsoft.ApiCenter/services/workspaces/apiSources","properties":{"apiSourceType":"AmazonApiGateway","amazonApiGatewaySource":{"accessKey":"https://kv-canary-franktest.vault.azure.net/secrets/AccessKey","secretAccessKey":"https://kv-canary-franktest.vault.azure.net/secrets/SecretAccessKey","regionName":"us-west-2","msiResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test"},"sourceLifecycleStage":"design","targetEnvironmentId":"/workspaces/default/environments/amazon-api-gateway","targetLifecycleStage":"design","importSpecification":"always","linkState":{"state":"initializing","lastUpdatedOn":"2024-12-27T05:53:55.9076442Z"}},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003","name":"cli000003","systemData":{"createdAt":"2024-12-27T05:53:55.907644Z","lastModifiedAt":"2024-12-27T05:53:55.9076428Z"}}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview, 2024-06-01-preview
cache-control:
- no-cache
content-length:
- '1080'
content-type:
- application/json; charset=utf-8
date:
- Fri, 27 Dec 2024 05:53:55 GMT
etag:
- 0f00b5e3-0000-3300-0000-676e40f30000
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000; includeSubDomains
vary:
- Accept-Encoding
x-cache:
- CONFIG_NOCACHE
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-subscription-global-writes:
- '11999'
x-ms-ratelimit-remaining-subscription-writes:
- '799'
x-msedge-ref:
- 'Ref A: D235AE0BE4F94FA2A7C867F38533C9C6 Ref B: MAA201060516009 Ref C: 2024-12-27T05:53:53Z'
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic integration show
Connection:
- keep-alive
ParameterSetName:
- -g -n -i
User-Agent:
- AZURECLI/2.67.0 azsdk-python-core/1.28.0 Python/3.11.9 (Windows-10-10.0.22631-SP0)
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003?api-version=2024-06-01-preview
response:
body:
string: '{"type":"Microsoft.ApiCenter/services/workspaces/apiSources","properties":{"apiSourceType":"AmazonApiGateway","amazonApiGatewaySource":{"accessKey":"https://kv-canary-franktest.vault.azure.net/secrets/AccessKey","secretAccessKey":"https://kv-canary-franktest.vault.azure.net/secrets/SecretAccessKey","regionName":"us-west-2","msiResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-apic-extension-cli-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/apic-extension-cli-test"},"sourceLifecycleStage":"design","targetEnvironmentId":"/workspaces/default/environments/amazon-api-gateway","targetLifecycleStage":"design","importSpecification":"always","linkState":{"state":"syncing","lastUpdatedOn":"2024-12-27T05:53:57.0047644Z"}},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002/workspaces/default/apiSources/cli000003","name":"cli000003","systemData":{"createdAt":"2024-12-27T05:53:55.907644Z","lastModifiedAt":"2024-12-27T05:53:57.0047642Z"}}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview, 2024-06-01-preview
cache-control:
- no-cache
content-length:
- '1075'
content-type:
- application/json; charset=utf-8
date:
- Fri, 27 Dec 2024 05:53:57 GMT
etag:
- 0f00b6e3-0000-3300-0000-676e40f50000
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000; includeSubDomains
vary:
- Accept-Encoding
x-cache:
- CONFIG_NOCACHE
x-content-type-options:
- nosniff
x-ms-ratelimit-remaining-subscription-global-reads:
- '16499'
x-msedge-ref:
- 'Ref A: C4F90FBD93284E49AC7A80530307B1ED Ref B: MAA201060513053 Ref C: 2024-12-27T05:53:56Z'
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import jmespath
import collections
from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer
from azure.cli.testsdk.checkers import JMESPathCheck
from azure.cli.testsdk.exceptions import JMESPathCheckAssertionError
from .utils import ApicServicePreparer, ApimServicePreparer
from .constants import TEST_REGION, AWS_ACCESS_KEY_LINK, AWS_SECRET_ACCESS_KEY_LINK, AWS_REGION, USERASSIGNED_IDENTITY

# override the JMESPathCheck class to support checking multiple possible values as a list
class JMESPathCheckAny(JMESPathCheck):
def __init__(self, query, expected_results, case_sensitive=True):
super().__init__(query, expected_results, case_sensitive)
if not isinstance(expected_results, list):
raise ValueError("expected_results should be a list of possible values")

def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_value = jmespath.search(self._query, json_value, jmespath.Options(collections.OrderedDict))
if self._case_sensitive:
if actual_value not in [result for result in self._expected_result]:
raise JMESPathCheckAssertionError(self._query, self._expected_result, actual_value, execution_result.output)
else:
if actual_value.lower() not in [result.lower() for result in self._expected_result]:
raise JMESPathCheckAssertionError(self._query, self._expected_result, actual_value, execution_result.output)

class IntegrationCommandTests(ScenarioTest):
# override the check method to support checking multiple possible values
def check(self, query, expected_results, case_sensitive=True):
query = self._apply_kwargs(query)
expected_results = self._apply_kwargs(expected_results)

if isinstance(expected_results, list):
return JMESPathCheckAny(query, expected_results, case_sensitive)
else:
return JMESPathCheck(query, expected_results, case_sensitive)

# TODO: change the location to TEST_REGION when the APIC resource provider is available in all regions
@ResourceGroupPreparer(name_prefix="clirg", location="centraluseuap", random_name_length=32)
@ApicServicePreparer()
@ApimServicePreparer()
def test_integration_create_apim(self):
if self.is_live:
# prepare test data
self.kwargs.update({
'integration_name': self.create_random_name(prefix='cli', length=8)
})

if self.kwargs['use_system_assigned_identity'] or not self.is_live:
self.cmd('az apic integration create apim -g {rg} -n {s} --azure-apim {apim_name} -i {integration_name}')
else:
self.cmd('az apic integration create apim -g {rg} -n {s} --azure-apim {apim_name} -i {integration_name} --msi-resource-id "{usi_id}"')

# verify command results
self.cmd('az apic integration show -g {rg} -n {s} -i {integration_name}', checks=[
self.check('apiSourceType', 'AzureApiManagement'),
self.check('name', '{integration_name}'),
self.check('linkState.state', list(['initializing', 'syncing']))
])

@ResourceGroupPreparer(name_prefix="clirg", location="centraluseuap", random_name_length=32)
@ApicServicePreparer(user_assigned_identity=USERASSIGNED_IDENTITY)
def test_integration_create_aws(self):
if self.is_live:
# prepare test data
self.kwargs.update({
'integration_name': self.create_random_name(prefix='cli', length=8),
'usi_id': USERASSIGNED_IDENTITY,
'access_key_link': AWS_ACCESS_KEY_LINK,
'secret_access_key_link': AWS_SECRET_ACCESS_KEY_LINK,
'aws_region': AWS_REGION
})

self.cmd('az apic integration create aws -g {rg} -n {s} -i {integration_name} --aws-access-key-reference {access_key_link} --aws-region {aws_region} --aws-secret-access-key-reference {secret_access_key_link}')

# verify command results
self.cmd('az apic integration show -g {rg} -n {s} -i {integration_name}', checks=[
self.check('apiSourceType', 'AmazonApiGateway'),
self.check('name', '{integration_name}'),
self.check('linkState.state', list(['initializing', 'syncing']))
])
Loading

0 comments on commit 94f6719

Please sign in to comment.