def tearDownClass(cls):
     try:
         for hostid in cls.disabledHosts:
             hosts = Host.list(cls.apiclient, id=hostid)
             assert (
                 validateList(hosts)[0] == PASS
             ), "hosts\
                     list validation failed"
             if hosts[0].resourcestate.lower() == DISABLED.lower():
                 cmd = updateHost.updateHostCmd()
                 cmd.id = hostid
                 cmd.resourcestate = ENABLED
                 cmd.allocationstate = ENABLE
                 cls.apiclient.updateHost(cmd)
         cleanup_resources(cls.apiclient, cls._cleanup)
     except Exception as e:
         raise Exception("Warning: Exception during cleanup : %s" % e)
    def test_01_disable_enable_host(self):
        """disable enable host
            1. Disable host and verify following things:
                For admin user:
                    1. Should be able to  stop exsiting vms but can not start.
                    2. Should not be able to  deploy new vm,
                       and create snapshot on the same host
                For Non-admin user:
                    1. Should not be able to stop exsiting vms but
                       cant not start
                    2. Should not be create to deploy new vm,
                    snapshot on the same host
            2. Enable the above disabled host and verify that:
                -All users should be create to deploy new vm,
                snapshot on the same host
            3. Try to reconnect the host :
                -Host should get reconnected successfully
        """
        # Step 1
        vm_root = VirtualMachine.create(
            self.apiclient,
            self.testdata["small"],
            templateid=self.template.id,
            accountid=self.admin_account.name,
            domainid=self.admin_account.domainid,
            serviceofferingid=self.service_offering.id,
            zoneid=self.zone.id,
        )
        hostid = vm_root.hostid

        vm_user = VirtualMachine.create(
            self.userapiclient,
            self.testdata["small"],
            templateid=self.template.id,
            accountid=self.account.name,
            domainid=self.account.domainid,
            serviceofferingid=self.service_offering.id,
            zoneid=self.zone.id,
        )

        cmd = updateHost.updateHostCmd()
        cmd.id = hostid
        cmd.resourcestate = DISABLED
        cmd.allocationstate = DISABLE
        self.apiclient.updateHost(cmd)
        self.disabledHosts.append(hostid)

        hostList = Host.list(self.apiclient, id=hostid)

        self.assertEqual(hostList[0].resourcestate, DISABLED, "Check if the host is in disabled state")
        # Verify the exsisting vms should be running
        self.assertEqual(vm_user.state.lower(), "running", "Verify that the user vm is running")

        self.assertEqual(vm_root.state.lower(), "running", "Verify that the root vm is running")

        vm_root.stop(self.apiclient)
        vm_user.stop(self.apiclient)
        root_state = self.dbclient.execute("select state from vm_instance where name='%s'" % vm_root.name)[0][0]
        user_state = self.dbclient.execute("select state from vm_instance where name='%s'" % vm_user.name)[0][0]

        self.assertEqual(root_state.lower(), "stopped", "verify that vm should stop")

        self.assertEqual(user_state.lower(), "stopped", "verify that vm should stop")

        with self.assertRaises(Exception):
            VirtualMachine.create(
                self.apiclient,
                self.testdata["small"],
                templateid=self.template.id,
                accountid=self.admin_account.name,
                domainid=self.admin_account.domainid,
                serviceofferingid=self.service_offering.id,
                zoneid=self.zone.id,
                hostid=hostid,
            )

        root_volume = list_volumes(self.apiclient, virtualmachineid=vm_root.id, type="ROOT", listall=True)

        self.assertEqual(
            validateList(root_volume)[0], PASS, "list root volume response is empty for volume id %s" % vm_root.id
        )

        if self.snapshotSupported:
            with self.assertRaises(Exception):
                Snapshot.create(self.apiclient, root_volume[0].id)

        # non-admin user should fail to create vm, snap, temp etc
        with self.assertRaises(Exception):
            VirtualMachine.create(
                self.apiclient,
                self.testdata["small"],
                templateid=self.template.id,
                accountid=self.account.name,
                domainid=self.account.domainid,
                serviceofferingid=self.service_offering.id,
                zoneid=self.zone.id,
                hostid=hostid,
            )
        root_volume = list_volumes(self.apiclient, virtualmachineid=vm_user.id, type="ROOT", listall=True)
        self.assertEqual(
            validateList(root_volume)[0], PASS, "list root volume response is empty for volume id %s" % vm_user.id
        )
        if self.snapshotSupported:
            with self.assertRaises(Exception):
                Snapshot.create(self.userapiclient, root_volume[0].id)

        # Step 2
        cmd.resourcestate = ENABLED
        cmd.allocationstate = ENABLE
        self.apiclient.updateHost(cmd)

        hostList = Host.list(self.apiclient, id=hostid)

        self.assertEqual(hostList[0].resourcestate, ENABLED, "Check if the host is in enabled state")
        # After enabling the zone all users should be able to add new VM,
        # volume, templatee and iso

        root_vm_new = VirtualMachine.create(
            self.apiclient,
            self.testdata["small"],
            templateid=self.template.id,
            accountid=self.admin_account.name,
            domainid=self.admin_account.domainid,
            serviceofferingid=self.service_offering.id,
            zoneid=self.zone.id,
            hostid=hostid,
        )

        self.assertEqual(
            root_vm_new.state.lower(), "running", "Verify that admin should create new VM in running state"
        )

        root_volume = list_volumes(self.apiclient, virtualmachineid=vm_root.id, type="ROOT", listall=True)

        self.assertEqual(
            validateList(root_volume)[0], PASS, "list root volume response is empty for volume id %s" % vm_root.id
        )

        if self.snapshotSupported:
            Snapshot.create(self.apiclient, root_volume[0].id)

        # Non root user
        user_vm_new = VirtualMachine.create(
            self.userapiclient,
            self.testdata["small"],
            templateid=self.template.id,
            accountid=self.account.name,
            domainid=self.account.domainid,
            serviceofferingid=self.service_offering.id,
            zoneid=self.zone.id,
        )

        self.assertEqual(user_vm_new.state.lower(), "running", "Verify that admin should create new VM")

        root_volume = list_volumes(self.apiclient, virtualmachineid=vm_user.id, type="ROOT", listall=True)

        self.assertEqual(
            validateList(root_volume)[0], PASS, "list root volume response is empty for volume id %s" % vm_user.id
        )
        if self.snapshotSupported:
            Snapshot.create(self.userapiclient, root_volume[0].id)

        # Step 4
        # reconnect the host
        cmd = reconnectHost.reconnectHostCmd()
        cmd.id = hostid
        self.apiclient.reconnectHost(cmd)
        # Host takes some time to come back to Up state so included sleep
        time.sleep(90)
        hostList = Host.list(self.apiclient, id=hostid)

        self.assertEqual(hostList[0].state.lower(), "up", "Check if the host get reconnected successfully")

        return