Skip to content

Commit

Permalink
test: update test case to setup live test pipeline (#76)
Browse files Browse the repository at this point in the history
* test: update test case to setup live test pipeline (#75)

* test: update test case

* update

* .

* .

* .

* .

* .

* .

* .

* .

* .

* .

* .

* test: update test case

* refactor: enable both identity

* fix: bad if else

* fix: fix bad parameter
  • Loading branch information
frankqianms authored Oct 28, 2024
1 parent ae37879 commit c79e74d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

TEST_REGION = "eastus"
import os

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')
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer
from .utils import ApicServicePreparer
from .constants import TEST_REGION
from .constants import TEST_REGION, USERASSIGNED_IDENTITY

# if USERASSIGNED_IDENTITY is set, enable_system_assigned_identity is False, otherwise use system assigned identity
enable_system_assigned_identity = False if USERASSIGNED_IDENTITY else True

class ServiceCommandsTests(ScenarioTest):

Expand Down Expand Up @@ -111,7 +114,7 @@ def test_delete_service(self):
self.cmd('az apic show -g {rg} -n {s}', expect_failure=True)

@ResourceGroupPreparer(name_prefix="clirg", location=TEST_REGION, random_name_length=32)
@ApicServicePreparer(enable_system_assigned_identity=True)
@ApicServicePreparer(enable_system_assigned_identity=enable_system_assigned_identity)
def test_import_from_apim(self):
self.kwargs.update({
'apim_name': self.create_random_name(prefix='cli', length=24)
Expand All @@ -127,7 +130,7 @@ def test_import_from_apim(self):


@ResourceGroupPreparer(name_prefix="clirg", location=TEST_REGION, random_name_length=32)
@ApicServicePreparer(enable_system_assigned_identity=True)
@ApicServicePreparer(enable_system_assigned_identity=enable_system_assigned_identity)
def test_import_from_apim_for_one_api(self):
self.kwargs.update({
'apim_name': self.create_random_name(prefix='cli', length=24)
Expand All @@ -146,7 +149,7 @@ def test_import_from_apim_for_one_api(self):
])

@ResourceGroupPreparer(name_prefix="clirg", location=TEST_REGION, random_name_length=32)
@ApicServicePreparer(enable_system_assigned_identity=True)
@ApicServicePreparer(enable_system_assigned_identity=enable_system_assigned_identity)
def test_import_from_apim_for_multiple_apis(self):
self.kwargs.update({
'apim_name': self.create_random_name(prefix='cli', length=24)
Expand Down Expand Up @@ -195,7 +198,7 @@ def test_examples_delete_service(self):
self.cmd('az apic show -g {rg} -n {s}', expect_failure=True)

@ResourceGroupPreparer(name_prefix="clirg", location=TEST_REGION, random_name_length=32)
@ApicServicePreparer(enable_system_assigned_identity=True)
@ApicServicePreparer(enable_system_assigned_identity=enable_system_assigned_identity)
def test_examples_import_all_apis_from_apim(self):
self.kwargs.update({
'apim_name': self.create_random_name(prefix='cli', length=24)
Expand All @@ -204,7 +207,7 @@ def test_examples_import_all_apis_from_apim(self):
self.cmd('az apic import-from-apim -g {rg} --service-name {s} --apim-name {apim_name} --apim-apis *')

@ResourceGroupPreparer(name_prefix="clirg", location=TEST_REGION, random_name_length=32)
@ApicServicePreparer(enable_system_assigned_identity=True)
@ApicServicePreparer(enable_system_assigned_identity=enable_system_assigned_identity)
def test_examples_import_selected_apis_from_apim(self):
self.kwargs.update({
'apim_name': self.create_random_name(prefix='cli', length=24)
Expand Down Expand Up @@ -240,7 +243,7 @@ def _prepare_apim(self):
apic_service = self.cmd('az apic show -g {rg} -n {s}').get_output_in_json()
self.kwargs.update({
'identity_id': apic_service['identity']['principalId']
})
}) if enable_system_assigned_identity else None
# Create APIM service
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
Expand All @@ -251,7 +254,13 @@ def _prepare_apim(self):
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
'apim_id': apim_id,
'usi_id': USERASSIGNED_IDENTITY
})
# Grant system assigned identity of API Center access to APIM
self.cmd('az role assignment create --role "API Management Service Reader Role" --assignee-object-id {identity_id} --assignee-principal-type ServicePrincipal --scope {apim_id}')

if enable_system_assigned_identity:
# Grant system assigned identity of API Center access to APIM
self.cmd('az role assignment create --role "API Management Service Reader Role" --assignee-object-id {identity_id} --assignee-principal-type ServicePrincipal --scope {apim_id}')
else:
# add user-assigned identity to api center service:
self.cmd('az apic update --name {s} -g {rg} --identity {{type:UserAssigned,user-assigned-identities:{usi_id}}}')

0 comments on commit c79e74d

Please sign in to comment.