# Author:Marvin Mao
# Date:2019/11/19 10:39
# test_custom_domain.py
# -*- coding= utf-8 -*-
# /usr/bin/env python
import logging
import unittest
import fc2
from FC_SDK_TEST.test_config import *
fc_common = TestCommon()
logging.basicConfig(level=logging.INFO)
class FcTestCustomDomain(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(FcTestCustomDomain, self).__init__(*args, **kwargs)
self.endpoint = fc_common.test_endpoint
self.accessKeyID = fc_common.test_accessKeyID
self.accessKeySecret = fc_common.test_accessKeySecret
self.client = fc2.Client(endpoint=self.endpoint,
accessKeyID=self.accessKeyID,
accessKeySecret=self.accessKeySecret)
def setUp(self):
# Check if the custom domain name exists in the list
custom_domains = self.client.list_custom_domains().data["customDomains"]
custom_domains_list = []
for i in range(len(custom_domains)):
custom_domains_list.append(custom_domains[i]["domainName"])
if fc_common.test_custom_domain in custom_domains_list:
# If it exists, delete the test domain
self.client.delete_custom_domain(fc_common.test_custom_domain)
else:
logging.info("The domain name tested does not exist in the current account")
# Create http function
self.client.create_service(fc_common.test_custom_service)
logging.info("Create a service to test custom domain names")
self.client.create_function(serviceName=fc_common.test_custom_service,
functionName=fc_common.test_function_name,
runtime=fc_common.test_function_run_time,
handler=fc_common.test_function_handler,
codeZipFile=fc_common.test_custom_http_file)
logging.info("Create a function to test custom domain names")
self.client.create_trigger(serviceName=fc_common.test_custom_service,
functionName=fc_common.test_function_name,
triggerName=fc_common.test_custom_http_trigger,
triggerType=fc_common.test_custom_http_trigger_type,
triggerConfig=fc_common.test_custom_http_trigger_config,
sourceArn=fc_common.test_custom_http_trigger_source_arn,
invocationRole=fc_common.test_custom_http_trigger_invocation_role)
def test_add_domain(self):
# add domain with http
logging.info('add tested custom domain name:{0} with http protocol'.format(fc_common.test_custom_domain))
domain = self.client.create_custom_domain(fc_common.test_custom_domain)
domain_information = domain.data
self.assertEqual(domain_information["domainName"], fc_common.test_custom_domain)
self.assertEqual(domain_information["protocol"], fc_common.test_custom_domain_http_protocol)
self.assertIsNone(domain_information["certConfig"])
self.assertTrue('createdTime' in domain_information)
self.assertTrue('lastModifiedTime' in domain_information)
self.assertTrue('accountId' in domain_information)
self.assertTrue('apiVersion' in domain_information)
def test_add_domain_https_type(self):
# add domain with https
logging.info('add tested custom domain name:{0}'.format(fc_common.test_custom_domain))
domain = self.client.create_custom_domain(fc_common.test_custom_domain,
protocol=fc_common.test_custom_domain_https_protocol,
certConfig=fc_common.test_custom_domain_cert)
domain_information = domain.data
self.assertEqual(domain_information["domainName"], fc_common.test_custom_domain)
self.assertEqual(domain_information["protocol"], fc_common.test_custom_domain_https_protocol)
self.assertEqual(domain_information["certConfig"]["certName"], fc_common.test_custom_domain_cert_name)
self.assertTrue('createdTime' in domain_information)
self.assertTrue('lastModifiedTime' in domain_information)
self.assertTrue('accountId' in domain_information)
self.assertTrue('apiVersion' in domain_information)
def test_add_domain_with_route(self):
# add domain with http
logging.info('add tested custom domain name:{0} with http protocol'.format(fc_common.test_custom_domain))
domain = self.client.create_custom_domain(fc_common.test_custom_domain, routeConfig=fc_common.test_add_route)
domain_information = domain.data
self.assertEqual(domain_information["domainName"], fc_common.test_custom_domain)
self.assertEqual(domain_information["protocol"], fc_common.test_custom_domain_http_protocol)
self.assertIsNone(domain_information["certConfig"])
self.assertIsNotNone(domain_information["routeConfig"])
self.assertTrue('createdTime' in domain_information)
self.assertTrue('lastModifiedTime' in domain_information)
self.assertTrue('accountId' in domain_information)
self.assertTrue('apiVersion' in domain_information)
def test_list_custom_domain(self):
# add domain with http
logging.info('add tested custom domain name:{0} with http protocol'.format(fc_common.test_custom_domain))
self.client.create_custom_domain(fc_common.test_custom_domain)
domains = self.client.list_custom_domains().data
domain_total = len(domains["customDomains"])
domains_name_list = []
for i in range(domain_total):
domains_name_list.append(domains["customDomains"][i]["domainName"])
self.assertTrue(fc_common.test_custom_domain in domains_name_list)
def test_update_domain(self):
# add domain with http
logging.info('add tested custom domain name:{0} with http protocol'.format(fc_common.test_custom_domain))
self.client.create_custom_domain(fc_common.test_custom_domain)
# update protocol and route
domain_information = self.client.update_custom_domain(fc_common.test_custom_domain,
protocol=fc_common.test_custom_domain_https_protocol,
certConfig=fc_common.test_custom_domain_cert,
routeConfig=fc_common.test_update_route).data
self.assertEqual(domain_information["domainName"], fc_common.test_custom_domain)
self.assertEqual(domain_information["protocol"], fc_common.test_custom_domain_https_protocol)
self.assertEqual(domain_information["certConfig"]["certName"], 'test-fc')
self.assertEqual(domain_information["routeConfig"], fc_common.test_update_route)
self.assertTrue('createdTime' in domain_information)
self.assertTrue('lastModifiedTime' in domain_information)
self.assertTrue('accountId' in domain_information)
self.assertTrue('apiVersion' in domain_information)
def test_delete_domain(self):
# add domain with http
logging.info('add tested custom domain name:{0} with http protocol'.format(fc_common.test_custom_domain))
self.client.create_custom_domain(fc_common.test_custom_domain)
# delete domain
self.client.delete_custom_domain(fc_common.test_custom_domain)
# check domain list
domains = self.client.list_custom_domains().data
domain_total = len(domains["customDomains"])
domains_name_list = []
for i in range(domain_total):
domains_name_list.append(domains["customDomains"][i]["domainName"])
self.assertNotIn(fc_common.test_custom_domain, domains_name_list)
def tearDown(self):
logging.info("======Start to delete the created pre-condition!======")
# delete http trigger
logging.info('delete function http trigger: {0}'.format(fc_common.test_custom_http_trigger))
self.client.delete_trigger(serviceName=fc_common.test_custom_service,
functionName=fc_common.test_function_name,
triggerName=fc_common.test_custom_http_trigger)
# delete function
logging.info('delete function: {0}'.format(fc_common.test_function_name))
self.client.delete_function(serviceName=fc_common.test_custom_service,
functionName=fc_common.test_function_name)
# delete service
logging.info('delete service: {0}'.format(fc_common.test_service_name))
self.client.delete_service(serviceName=fc_common.test_custom_service)
# delete domain
# check domain list
domains = self.client.list_custom_domains().data
domain_total = len(domains["customDomains"])
domains_name_list = []
for i in range(domain_total):
domains_name_list.append(domains["customDomains"][i]["domainName"])
if fc_common.test_custom_domain in domains_name_list:
logging.info('delete domain: {0}'.format(fc_common.test_custom_domain))
self.client.delete_custom_domain(fc_common.test_custom_domain)
else:
logging.info('There is no the domain:{0} '.format(fc_common.test_custom_domain))
if __name__ == '__main__':
unittest.main()
使用Python的unittest来进行阿里云sdk的简易测试
最新推荐文章于 2021-03-02 16:56:45 发布