使用Python的unittest来进行阿里云sdk的简易测试


# 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()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值