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


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.HostGroup.hostGroup_create import HostGroupCreate
from WorkApi.API.HostGroup.hostGroup_massadd import HostGroupMassadd
from WorkApi.API.HostGroup.hostGroup_massremove import HostGroupMassremove

from WorkData.Zabbix.hstgrp import DataHstGrp
from WorkData.Zabbix.hosts_groups import DataHostsGroups

import allure

# allure.label("")


@allure.feature("测试模块：hostgroup.massremove")
class TestHostGroupMassremove(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_hostids = [10266, 10272, 10296]

    @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("调用接口：hostgroup.create")
    def hostgroup_create(self, name=None, groupid=None, flags=None, internal=None):
        api = HostGroupCreate(_host=self.host)
        api.name = name
        api.groupid = groupid
        api.flags = flags
        api.internal = internal
        api.get_response()
        return api.response

    @allure.step("调用接口：hostgroup.massadd")
    def hostgroup_massadd(self, groups=None, hosts=None, templates=None):
        api = HostGroupMassadd(_host=self.host)
        api.groups = groups
        api.hosts = hosts
        api.templates = templates
        api.get_response()
        return api.response

    @allure.step("调用接口：hostgroup.massremove")
    def hostgroup_massremove(self, groups=None, hosts=None, templates=None):
        api = HostGroupMassremove(_host=self.host)
        api.groups = groups
        api.hosts = hosts
        api.templates = templates
        api.get_response()
        return api.response

    @allure.step("创建测试数据")
    def case_create(self, name):
        response = self.hostgroup_create(name=name)
        self.check_code(response=response, code=0)
        groupid = UtilsResponse().get_result(response=response)["groupids"][0]
        return groupid

    @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("查询表：hstgrp")
    def select_hstgrp(self, groupid=None, name=None, internal=None, flags=None):
        session = self.db_session()
        sql = DataHstGrp().select_all_from_allKeys(session=session, groupid=groupid, name=name, internal=internal, flags=flags)
        return sql

    @allure.step("查询表：hosts_groups")
    def select_hosts_groups(self, groupid=None, hostid=None):
        session = self.db_session()
        sql = DataHostsGroups().select_all_from_allKeys(session=session, groupid=groupid, hostid=hostid)
        return sql

    @allure.step("校验查询结果")
    def check_select(self, groupids, hostids):
        for x, y in enumerate(groupids):
            for q, w in enumerate(hostids):
                sql = self.select_hosts_groups(groupid=y, hostid=w)
                assert sql != []

    @allure.step("校验查询结果")
    def check_select_delete(self, groupids, hostids):
        for x, y in enumerate(groupids):
            for q, w in enumerate(hostids):
                sql = self.select_hosts_groups(groupid=y, hostid=w)
                assert sql == []

    @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.title("hostgroup.massremove：一个主机组 批量删除 一个主机")
    @allure.story("批量删除主机组：一个主机组 批量删除 一个主机")
    @allure.severity("blocker")
    def test_case_01(self):
        name = self.base_name + "01"
        groupid = self.case_create(name=name)
        self.hostgroup_massadd(groups=[groupid], hosts=[self.base_hostids[0]])

        response = self.hostgroup_massremove(groups=[groupid], hosts=[self.base_hostids[0]])
        self.check_code(response=response, code=0)
        self.check_select_delete(hostids=[self.base_hostids[0]], groupids=[groupid])

    @allure.title("hostgroup.massremove：多个主机组 批量删除 一个主机")
    @allure.story("批量删除主机组：多个主机组 批量删除 一个主机")
    def test_case_02(self):
        name_1 = self.base_name + "021"
        name_2 = self.base_name + "022"
        groupid_1 = self.case_create(name=name_1)
        groupid_2 = self.case_create(name=name_2)
        self.hostgroup_massadd(groups=[groupid_1, groupid_2], hosts=[self.base_hostids[0]])

        response = self.hostgroup_massremove(groups=[groupid_1, groupid_2], hosts=[self.base_hostids[0]])
        self.check_code(response=response, code=0)
        self.check_select_delete(hostids=[self.base_hostids[0]], groupids=[groupid_1, groupid_2])

    @allure.title("hostgroup.massremove：多个主机组 批量删除 多个主机")
    @allure.story("批量删除主机组：多个主机组 批量删除 多个主机")
    def test_case_03(self):
        name_1 = self.base_name + "031"
        name_2 = self.base_name + "032"
        name_3 = self.base_name + "033"
        groupid_1 = self.case_create(name=name_1)
        groupid_2 = self.case_create(name=name_2)
        groupid_3 = self.case_create(name=name_3)
        self.hostgroup_massadd(groups=[groupid_1, groupid_2, groupid_3], hosts=[self.base_hostids[0], self.base_hostids[1], self.base_hostids[2]])

        response = self.hostgroup_massremove(groups=[groupid_1, groupid_2, groupid_3], hosts=[self.base_hostids[0], self.base_hostids[1], self.base_hostids[2]])
        self.check_code(response=response, code=0)
        self.check_select_delete(hostids=[self.base_hostids[0], self.base_hostids[1], self.base_hostids[2]], groupids=[groupid_1, groupid_2, groupid_3])

    @allure.title("hostgroup.massremove：多个主机组 批量删除 部分多个主机")
    @allure.story("批量删除主机组：多个主机组 批量删除 部分多个主机")
    def test_case_04(self):
        name_1 = self.base_name + "041"
        name_2 = self.base_name + "042"
        name_3 = self.base_name + "043"
        groupid_1 = self.case_create(name=name_1)
        groupid_2 = self.case_create(name=name_2)
        groupid_3 = self.case_create(name=name_3)
        self.hostgroup_massadd(groups=[groupid_1, groupid_2, groupid_3], hosts=[self.base_hostids[0], self.base_hostids[1], self.base_hostids[2]])

        response = self.hostgroup_massremove(groups=[groupid_1, groupid_3], hosts=[self.base_hostids[0], self.base_hostids[1]])
        self.check_code(response=response, code=0)
        self.check_select_delete(hostids=[self.base_hostids[0], self.base_hostids[1]], groupids=[groupid_1, groupid_3])

        self.check_select(hostids=[self.base_hostids[2]], groupids=[groupid_1, groupid_2, groupid_3])
        self.check_select(hostids=[self.base_hostids[0], self.base_hostids[1]], groupids=[groupid_2])


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(case=case, case_info=case_info)
    r.run_main()
    # a = TestHostGroupMassremove()
    # a.setup_class()
    # a.test_case_04()
