# -*- coding: utf-8 -*-
# 测试用例
# 作者: 陈磊
# 时间: 2019-11-11


from __future__ import division

from WorkCase import CaseBase
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsDataBase import UtilsDataBase
from WorkUtils.UtilsResponse import UtilsResponse
from WorkApi.API.Host.host_create import HostCreate

from WorkData.Zabbix.interface import DataInterface

import allure

# allure.label("")


@allure.feature("测试模块：host.create")
class TestHostCreate(object):
    log = UtilsLog()
    env = CaseBase().environment
    host = env["host"]
    db_url = env["db_url"]
    db_port = env["db_port"]
    db_user = env["db_user"]
    db_pw = env["db_pw"]
    db_base = env["db_base"]
    base_name = "SS测试主机接口"
    base_ip = "SS测试主机接口IP"
    base_host = "SS测试主机"
    base_description = "SS测试主机描述"

    @classmethod
    def setup_class(cls):
        cls.log.debug("开始执行测试套件.......")
        # session = UtilsDataBase().conn_mysql(db_url=cls.db_url, db_port=cls.db_port, db_base=cls.db_base, db_user=cls.db_user, db_pw=cls.db_pw)
        # DataHstGrp().delete_like_name(session=session, name=cls.base_name)

    @classmethod
    def teardown_class(cls):
        cls.log.debug("结束执行测试套件.......")

    def setup_method(self):
        self.log.debug("测试用例执行开始...")

    def teardown_method(self):
        self.log.debug("测试用例执行结束...")

    @allure.step("调用接口：host.create")
    def host_create(self, host=None, description=None, groupids=None, interfaces=None, tags=None, templateids=None, macros=None, inventory=None, status=None,
                    tls_accept=None, tls_psk_identity=None, tls_psk=None):
        api = HostCreate(_host=self.host)
        api.host = host
        api.description = description
        api.groupids = groupids
        api.interfaces = interfaces
        api.tags = tags
        api.templateids = templateids
        api.macros = macros
        api.inventory = inventory
        api.status = status
        api.tls_accept = tls_accept
        api.tls_psk_identity = tls_psk_identity
        api.tls_psk = tls_psk
        api.get_response()
        return api.response

    @allure.step("连接数据库：zabbix")
    def db_session(self):
        session = UtilsDataBase().conn_mysql(db_url=self.db_url, db_port=self.db_port, db_base=self.db_base, db_user=self.db_user, db_pw=self.db_pw)
        return session

    @allure.step("校验查询结果")
    def check_select(self, groupid, name, internal, flags):
        sql = self.select_hstgrp(groupid=groupid)
        for x, y in enumerate(sql):
            assert y.groupid == groupid
            assert y.name == name
            assert y.flags == flags
            assert y.internal == internal

    @allure.step("断言返回结果：校验排序")
    def check_sortfield(self, response, possible):
        result = UtilsResponse().get_result(response=response)
        UtilsResponse().check_sort(_list=result, key="groupid", possible=possible)

    @allure.step("断言返回结果：校验返回数据的数量")
    def check_num(self, response, num):
        result = UtilsResponse().get_result(response=response)
        assert len(result) == num

    @allure.step("断言返回结果")
    def check_code(self, response, code):
        _code = UtilsResponse().get_code(response=response)
        assert _code == code

    @allure.step("断言返回错误结果")
    def check_status(self, response, status):
        _status = UtilsResponse().get_status(response=response)
        assert _status == status

    @allure.title("host.create：IP地址相应信息")
    @allure.story("创建主机接口：IP地址相应信息")
    @allure.severity("blocker")
    def test_case_01(self):
        host = self.base_name + "01"
        description = self.base_description + "01"
        interfaces = {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.3.1",
                "dns": "",
                "port": "10050",
                "bulk": 1
        }
        response = self.host_create(host=host, description=description, groupids=[87], interfaces=[interfaces], status=0,
                                    tls_accept=1, tls_psk_identity="22", tls_psk="22")
        self.check_code(response=response, code=0)
        # groupid = UtilsResponse().get_result(response=response)["groupids"][0]
        # self.check_select(groupid=groupid, name=name, flags=flags, internal=internal)

    @allure.title("host.create：重复创建")
    @allure.story("创建主机接口：重复创建")
    def test_case_02(self):
        name = self.base_name + "02"
        flags = 0
        internal = 0
        response = self.host_create(name=name)
        self.check_code(response=response, code=0)
        groupid = UtilsResponse().get_result(response=response)["groupids"][0]
        self.check_select(groupid=groupid, name=name, flags=flags, internal=internal)

        response = self.host_create(name=name, flags=flags, internal=internal)
        self.check_code(response=response, code=100)

    @allure.title("host.create：无参数调用")
    @allure.story("创建主机接口：无参数调用")
    def test_case_03(self):
        response = self.host_create()
        self.check_status(response=response, status=400)

    @allure.title("host.create：name=空字符串")
    @allure.story("创建主机接口：name=空字符串")
    def test_case_04(self):
        response = self.host_create(name="")
        self.check_status(response=response, status=400)


if __name__ == "__main__":
    from WorkUtils.UtilsPyTest import UtilsPyTest
    from WorkUtils.UtilsCmd import UtilsCmd

    import os

    # 执行自动化测试用例
    # case_info = os.path.split(__file__)
    # case = UtilsCmd().pytest_cmd()
    # r = UtilsPyTest(version=case["version"])
    # r.run_main(case_info=case_info)

    a = TestHostCreate()
    # a.setup_class()
    a.test_case_01()
    # a.test_case_02()
    # a.test_case_03()
    # a.test_case_04()
