def create_organization(self, organization_name, regionCode): """Create DevOps organization""" # validate the organization name organization_manager = OrganizationManager(creds=self._creds) print("1") validation = organization_manager.validate_organization_name( organization_name) if not validation.valid: return validation print("2") # validate region code: valid_region = False for region in self.list_regions().value: if region.name == regionCode: valid_region = True print("3") if not valid_region: validation.message = "not a valid region code - run 'az functionapp devops-build organization' regions to find a valid regionCode" # pylint: disable=line-too-long validation.valid = False return validation print("4") new_organization = organization_manager.create_organization( regionCode, organization_name) print("5") new_organization.valid = True return new_organization
def create_organization(self, organization_name, regionCode): """Create DevOps organization""" # validate the organization name organization_manager = OrganizationManager(creds=self._creds) validation = organization_manager.validate_organization_name(organization_name) if not validation.valid: return validation # validate region code: valid_region = False for region in self.list_regions().value: if region.name == regionCode: valid_region = True if not valid_region: validation.message = "not a valid region code - run 'az functionapp devops-build organization' regions to find a valid regionCode" # pylint: disable=line-too-long validation.valid = False return validation new_organization = organization_manager.create_organization(regionCode, organization_name) new_organization.valid = True return new_organization
def list_regions(self): """List DevOps regions""" organization_manager = OrganizationManager(creds=self._creds) regions = organization_manager.list_regions() return regions
def test_invalid_organization_without_credential(self): no_cred_organization_manager = OrganizationManager(creds=None) with self.assertRaises(HttpOperationError): no_cred_organization_manager.list_organizations()
def setUp(self): self.organization_manager = OrganizationManager( creds=get_credentials())
class TestOrganizationManager(unittest.TestCase): @classmethod def setUpClass(cls): logging.disable(logging.CRITICAL) @classmethod def tearDownClass(cls): logging.disable(logging.NOTSET) def setUp(self): self.organization_manager = OrganizationManager( creds=get_credentials()) def tearDown(self): self.organization_manager.close_connection() def test_valid_organization_name(self): valid_name = "organization-name-" + id_generator(size=3).lower() validation = self.organization_manager.validate_organization_name( valid_name) self.assertTrue(validation.valid) def test_invalid_no_organization_name(self): invalid_name = None validation = self.organization_manager.validate_organization_name( invalid_name) self.assertFalse(validation.valid) def test_invalid_empty_organization_name(self): invalid_name = '' validation = self.organization_manager.validate_organization_name( invalid_name) self.assertFalse(validation.valid) def test_invalid_organization_name_characters(self): invalid_name = 'invalid-organization-name#' validation = self.organization_manager.validate_organization_name( invalid_name) self.assertFalse(validation.valid) def test_invalid_collided_organization_name(self): organizations = self.organization_manager.list_organizations() if organizations.count > 0: existing_name = organizations.value[0].accountName validation = self.organization_manager.validate_organization_name( existing_name) self.assertFalse(validation.valid) def test_list_organizations(self): organizations = self.organization_manager.list_organizations() self.assertIsNotNone(organizations) self.assertIsNotNone(organizations.value) self.assertGreaterEqual(organizations.count, 0) def test_invalid_organization_without_credential(self): no_cred_organization_manager = OrganizationManager(creds=None) with self.assertRaises(HttpOperationError): no_cred_organization_manager.list_organizations() @unittest.skipIf( not CREATE_DEVOPS_OBJECTS, "Set CREATE_DEVOPS_OBJECTS to True if you want to create resources for unit testing" ) def test_create_organization(self): existing_organization_names = [ org.accountName for org in self.organization_manager.list_organizations().value ] # If the organization exists, we will skip this test if ORGANIZATION_NAME in existing_organization_names: raise unittest.SkipTest( "Organization already exists. No need to create a new organization." ) result = self.organization_manager.create_organization( 'CUS', ORGANIZATION_NAME) self.assertIsNotNone(result.id) self.assertEqual(result.name, ORGANIZATION_NAME) def test_invalid_create_duplicated_organization(self): existing_organization_names = [ org.accountName for org in self.organization_manager.list_organizations().value ] # If there is no existing organization, we will skip this test if existing_organization_names.count == 0: raise unittest.SkipTest( "There is no existing organization. Cannot create a duplicate." ) organization_name = existing_organization_names[0] with self.assertRaises(HttpOperationError): self.organization_manager.create_organization( 'CUS', organization_name) def test_list_regions(self): regions = self.organization_manager.list_regions() self.assertIsNotNone(regions) self.assertIsNotNone(regions.value) self.assertGreaterEqual(regions.count, 0)