Skip to content

Commit

Permalink
test: add tests for optional params
Browse files Browse the repository at this point in the history
  • Loading branch information
blackchoey committed May 15, 2024
1 parent dcd00c3 commit 51ea542
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
interactions:
- request:
body: '{"identity": {"type": "SystemAssigned"}, "location": "westeurope", "tags":
{"test": "value"}}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic service create
Connection:
- keep-alive
Content-Length:
- '93'
Content-Type:
- application/json
ParameterSetName:
- -g --name --location --tags --identity
User-Agent:
- AZURECLI/2.58.0 azsdk-python-core/1.28.0 Python/3.10.11 (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/cli000002?api-version=2024-03-01
response:
body:
string: '{"type":"Microsoft.ApiCenter/services","location":"westeurope","identity":{"type":"SystemAssigned","principalId":"c9869f92-bc42-4001-84cf-0d11469f2508","tenantId":"72f988bf-86f1-41af-91ab-2d7cd011db47"},"sku":{"name":"Free"},"properties":{},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/cli000002","name":"cli000002","tags":{"test":"value"},"systemData":{"createdAt":"2024-05-15T07:47:48.5289093Z","lastModifiedAt":"2024-05-15T07:47:48.528896Z"}}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview
cache-control:
- no-cache
content-length:
- '524'
content-type:
- application/json; charset=utf-8
date:
- Wed, 15 May 2024 07:47:49 GMT
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-writes:
- '1199'
x-msedge-ref:
- 'Ref A: 7F87441C541D4B62AA0D2BCE2176371C Ref B: MAA201060516023 Ref C: 2024-05-15T07:47:44Z'
x-powered-by:
- ASP.NET
status:
code: 201
message: Created
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic service list
Connection:
- keep-alive
ParameterSetName:
- -g
User-Agent:
- AZURECLI/2.58.0 azsdk-python-core/1.28.0 Python/3.10.11 (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?api-version=2024-03-01
response:
body:
string: '{"value":[{"type":"Microsoft.ApiCenter/services","location":"eastus","sku":{"name":"Free"},"properties":{},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002","name":"clitest000002","tags":{},"systemData":{"createdAt":"2024-05-15T07:47:45.6763919Z","lastModifiedAt":"2024-05-15T07:47:45.6763836Z"}}]}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview
cache-control:
- no-cache
content-length:
- '387'
content-type:
- application/json; charset=utf-8
date:
- Wed, 15 May 2024 07:47:48 GMT
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-msedge-ref:
- 'Ref A: 89D283DB5381427AA576BD4F443D0C71 Ref B: MAA201060516037 Ref C: 2024-05-15T07:47:47Z'
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,60 @@
interactions:
- request:
body: '{"identity": {"type": "SystemAssigned"}, "tags": {"test": "value"}}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
CommandName:
- apic service update
Connection:
- keep-alive
Content-Length:
- '67'
Content-Type:
- application/json
ParameterSetName:
- -g -s --tags --identity
User-Agent:
- AZURECLI/2.58.0 azsdk-python-core/1.28.0 Python/3.10.11 (Windows-10-10.0.22631-SP0)
method: PATCH
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002?api-version=2024-03-01
response:
body:
string: '{"type":"Microsoft.ApiCenter/services","location":"eastus","identity":{"type":"SystemAssigned","principalId":"c2ef3539-0d52-4f64-a8d7-c6888542e17c","tenantId":"72f988bf-86f1-41af-91ab-2d7cd011db47"},"sku":{"name":"Free"},"properties":{},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clirg000001/providers/Microsoft.ApiCenter/services/clitest000002","name":"clitest000002","tags":{"test":"value"},"systemData":{"lastModifiedAt":"2024-05-15T07:47:59.7722946Z"}}'
headers:
api-supported-versions:
- 2023-07-01-preview, 2024-03-01, 2024-03-15-preview
cache-control:
- no-cache
content-length:
- '486'
content-type:
- application/json; charset=utf-8
date:
- Wed, 15 May 2024 07:48:00 GMT
etag:
- 2d0033f1-0000-0100-0000-664468af0000
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-writes:
- '1199'
x-msedge-ref:
- 'Ref A: 4B27B6B145694297BDB83FE8474E6B17 Ref B: MAA201060514023 Ref C: 2024-05-15T07:47:54Z'
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# --------------------------------------------------------------------------------------------

from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer
from .utils import ApicServicePreparer, ApicEnvironmentPreparer
from .utils import ApicServicePreparer, ApicEnvironmentPreparer, ApicMetadataPreparer

class EnvironmentCommandsTests(ScenarioTest):

Expand All @@ -21,6 +21,29 @@ def test_environment_create(self):
self.check('customProperties', '{{}}')
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
@ApicMetadataPreparer()
def test_environment_create_with_all_optional_params(self, metadata_name):
self.kwargs.update({
'name': self.create_random_name(prefix='cli', length=24),
'custom_properties': '{{"{}":true}}'.format(metadata_name),
'onboarding': "{developerPortalUri:['https://developer.contoso.com'],instructions:'instructions markdown'}",
'server': "{type:'Azure API Management',managementPortalUri:['example.com']}"
})
self.cmd('az apic environment create -g {rg} -s {s} --environment-id {name} --title "test environment" --type testing --custom-properties \'{custom_properties}\' --description "environment description" --onboarding "{onboarding}" --server "{server}"', checks=[
self.check('customProperties.{}'.format(metadata_name), True),
self.check('description', 'environment description'),
self.check('kind', 'testing'),
self.check('name', '{name}'),
self.check('onboarding.developerPortalUri[0]', 'https://developer.contoso.com'),
self.check('onboarding.instructions', 'instructions markdown'),
self.check('server.managementPortalUri[0]', 'example.com'),
self.check('server.type', 'Azure API Management'),
self.check('title', 'test environment'),
self.check('type', 'Microsoft.ApiCenter/services/workspaces/environments')
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
@ApicEnvironmentPreparer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def test_metadata_list(self, metadata_name1, metadata_name2):
self.check('@[0].name', metadata_name1),
self.check('@[1].name', metadata_name2)
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
@ApicMetadataPreparer(parameter_name='metadata_name1')
@ApicMetadataPreparer(parameter_name='metadata_name2')
def test_metadata_list(self, metadata_name1):
self.kwargs.update({
'metadata_name': metadata_name1
})
self.cmd('az apic metadata list -g {rg} -s {s} --filter "name eq \'{metadata_name}\'"', checks=[
self.check('length(@)', 1),
self.check('@[0].name', metadata_name1),
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def test_create_service(self, resource_group):
self.check('name', '{name}'),
self.check('resourceGroup', '{rg}')
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
def test_create_service_with_all_optional_params(self, resource_group):
self.kwargs.update({
'name': self.create_random_name(prefix='cli', length=24),
'rg': resource_group
})
self.cmd('az apic service create -g {rg} --name {name} --location westeurope --tags \'{{test:value}}\' --identity \'{{type:SystemAssigned}}\'', checks=[
self.check('name', '{name}'),
self.check('resourceGroup', '{rg}'),
self.check('identity.type', 'SystemAssigned'),
self.check('location', 'westeurope'),
self.check('tags.test', 'value')
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
Expand All @@ -29,13 +43,40 @@ def test_show_service(self):
self.check('resourceGroup', '{rg}')
])

@unittest.skip('The Control Plane API has bug')
@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer(parameter_name='service_name1')
@ApicServicePreparer(parameter_name='service_name2')
def test_list_service(self, service_name1, service_name2):
self.cmd('az apic service list', checks=[
self.check('length(@)', 2),
self.check('@[0].name', service_name1),
self.check('@[1].name', service_name2)
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
def test_list_service_in_rg(self, service_name):
self.cmd('az apic service list -g {rg}', checks=[
self.check('length(@)', 1),
self.check('@[0].name', service_name)
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
def test_update_service(self):
self.cmd('az apic service update -g {rg} -s {s} --tags "{{test:value}}"', checks=[
self.check('tags.test', 'value')
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
def test_update_service_with_all_optional_params(self):
self.cmd('az apic service update -g {rg} -s {s} --tags "{{test:value}}" --identity "{{type:SystemAssigned}}"', checks=[
self.check('tags.test', 'value'),
self.check('identity.type', 'SystemAssigned')
])

@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer()
def test_delete_service(self):
Expand All @@ -59,4 +100,28 @@ def test_import_from_apim(self, identity_id):
self.cmd('az role assignment create --role "API Management Service Reader Role" --assignee-object-id {identity_id} --assignee-principal-type ServicePrincipal --scope {apim_id}')
self.cmd('az apic service import-from-apim -g {rg} --service-name {s} --source-resource-ids {apim_id}/apis/*')

# TODO: check result
# TODO: check result

@unittest.skip('The Control Plane API has bug')
@ResourceGroupPreparer(name_prefix="clirg", location='eastus', random_name_length=32)
@ApicServicePreparer(enable_system_assigned_identity=True)
def test_import_from_apim_for_one_api(self, identity_id):
self.kwargs.update({
'apim_name': self.create_random_name(prefix='cli', length=24),
'identity_id': identity_id
})
apim_service = self.cmd('az apim create -g {rg} --name {apim_name} --publisher-name test --publisher-email test@example.com --sku-name Consumption').get_output_in_json()
# Add echo api
self.cmd('az apim api create -g {rg} --service-name {apim_name} --api-id echo --display-name "Echo API" --path "/echo"')
self.cmd('az apim api operation create -g {rg} --service-name {apim_name} --api-id echo --url-template "/echo" --method "GET" --display-name "GetOperation"')
# Add foo api
self.cmd('az apim api create -g {rg} --service-name {apim_name} --api-id foo --display-name "Foo API" --path "/foo"')
self.cmd('az apim api operation create -g {rg} --service-name {apim_name} --api-id foo --url-template "/foo" --method "GET" --display-name "GetOperation"')
apim_id = apim_service['id']
self.kwargs.update({
'apim_id': apim_id
})
self.cmd('az role assignment create --role "API Management Service Reader Role" --assignee-object-id {identity_id} --assignee-principal-type ServicePrincipal --scope {apim_id}')
self.cmd('az apic service import-from-apim -g {rg} --service-name {s} --source-resource-ids {apim_id}/apis/echo')

# TODO: check result
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, name_prefix='clitest', length=24,
parameter_name='metadata_name', resource_group_parameter_name='resource_group',
apic_service_parameter_name='service_name',
schema='{"type":"boolean", "title":"Public Facing"}',
assignments='[{entity:api,required:true,deprecated:false}]',
assignments='[{entity:api,required:true,deprecated:false},{entity:environment,required:true,deprecated:false},{entity:deployment,required:true,deprecated:false}]',
key='m'):
super(ApicMetadataPreparer, self).__init__(name_prefix, length)
self.cli_ctx = get_dummy_cli()
Expand Down

0 comments on commit 51ea542

Please sign in to comment.