【大爆炸】__init__.py 申请资源,传递局部环境变量

#coding:utf-8
"""
    申请本package 需要的资源,如果申请失败的话,停止后续测试,并发出告警?
    申请成功的话,把资源写入环境变量中

    申请网关:
        package_name + "id/name"
        eg:
            test_nose_gw = "xxx"
        os.environ["test_nose_gw"]="xxx"

    用例中引用:
        os.environ.get("test_openapi_block_gw") 失败默认返回None


    本用例:
        test_openapi_gw_gw1 : for test_gw-015-*

        用例中引用:
        os.environ.get("test_openapi_gw_gw1")






"""
import os
import sys
import time
sys.path.append("../")

import lib.base_case_lib_openapi
from aliyunsdkcore.profile import region_provider



gw_new_id=None
common = lib.base_case_lib_openapi.common_lib("apply")
region_provider.modify_point("sgw",
                            "cn-hangzhou",
                                   "sgw.cn-hangzhou.aliyuncs.com")


def setUp():
    global gw_new_id
    print "This is a package setup function"
    #申请网关
    result = common.do_create_gateway(GatewayClass="Standard",
                          PostPaid=True,
                          Description="daily apply for openapi_block",
                          Type="File",
                          ReleaseAfterExpiration=True,
                          StorageBundleId="sb-0007vb109u7z4l4yltmi",
                          VSwitchId="vsw-bp1o0d7jj3brrp4of1t5f",
                          Name="test_openapi_gw_gw1",
                          Location="Cloud",
                          publicNetworkBandwidth=None,)
    if not result["Success"]:
        print (" create gw  fail !")
        assert False, "create gw  fail"
    gw_new_id = result["GatewayId"]


    #部署网关
    print("step::deploy  gw")
    print("gwid is %s " % gw_new_id)
    result = common.do_DeployGatewayRequest(GatewayId=gw_new_id, GatewayClass="Standard",VSwitchId="vsw-bp1o0d7jj3brrp4of1t5f")
    if not result["Success"]:
        print(" deploy gw   fail !")
        assert False
    print("api response:%s " % result["Response"])

    deployid = result["TaskId"]

    result = common.wait_until_task_completed_new(deployid, gw_new_id, "task.state.completed",
                                                pagesize=10,
                                                pagenumber=1, interval=10, times=60)

    if not result:
        print(" assure  deploying   fail !")
        assert False
    print("assure  deploying    success")
    
    # #购买缓存盘
    #
    # print("step::buy cache ")
    # result = common.do_OperateGatewayRequest(
    #     OperateAction="add_cache",
    #     Params="{\"cacheGBSize\":%s,\"cacheCategory\":\"%s\"}" % (50, "cloud_ssd"),
    #     GatewayId=gw_new_id, )
    # if not result["Success"]:
    #     print(" add caching  fail !")
    #     assert False
    # add_taskid = result["TaskId"]
    #
    # print("step:: assure add caching   ")
    # result = common.wait_until_task_completed(add_taskid, gw_new_id, "task.state.completed",
    #                                         pagesize=10,
    #                                         pagenumber=1, interval=10, times=30)
    # if not result:
    #     print(" assure  add caching  fail !")
    #     assert False
    # print("assure  add caching  success")


    #升级网关
    result = common.do_UpgradeGatewayRequest(GatewayId=gw_new_id)
    if not result["Success"]:
        print(" update gw   fail !")
        assert False
    print("api response:%s " % result["Response"])

    result = common.wait_until_gw_running("sb-0007vb109u7z4l4yltmi", gw_new_id, "Running")
    if not result:
        print(" waiting gw  fail !")
        assert False
    print("waiting gw success !")

    #设置环境变量
    os.environ["test_openapi_gw_gw1"]=gw_new_id



def tearDown():
    print "This is a package teardown function"
    print os.environ.get("test_openapi_gw_gw1")
    time.sleep(60)
    result = common.do_DeleteGatewayRequest(GatewayId=gw_new_id)
    if not result["Success"]:
        print(" delete gw   fail !")
        assert False
    print "delete case pass !"

# 在初始化的时候申请资源,如果资源申请失败就不跑本package 的相关用例 资源用局部环境变量传递给当前进程和子进程

  

posted @ 2021-03-05 17:00  峡谷恶霸  阅读(43)  评论(0编辑  收藏  举报