Patch to fix test_aggregates_basic_ops and test_aggregates tests

Use method list_hypervisor to get info about running vms on hypervisor
instead of deprecated in Nova method list_servers_on_hypervisors

Related-prod: PRODX-42808
Change-Id: Ib94a11748ad319bae85d1d1ef29ff31927020510
diff --git a/tempest/serial_tests/api/admin/test_aggregates.py b/tempest/serial_tests/api/admin/test_aggregates.py
index 2ca91aa..134fe57 100644
--- a/tempest/serial_tests/api/admin/test_aggregates.py
+++ b/tempest/serial_tests/api/admin/test_aggregates.py
@@ -214,6 +214,9 @@
     @decorators.idempotent_id('96be03c7-570d-409c-90f8-e4db3c646996')
     def test_aggregate_add_host_create_server_with_az(self):
         """Test adding a host to the given aggregate and creating a server"""
+        if CONF.production:
+            raise self.skipException("Not allowed to run this test "
+                                     "on production environment")
         self.useFixture(fixtures.LockFixture('availability_zone'))
         az_name = data_utils.rand_name(self.az_name_prefix)
         aggregate = self._create_test_aggregate(availability_zone=az_name)
@@ -226,12 +229,23 @@
             if agg['availability_zone']:
                 hosts_in_zone.extend(agg['hosts'])
         hosts = [v for v in self.hosts_available if v not in hosts_in_zone]
-        if not hosts:
+        hosts_available = []
+        list_hypervisors = (
+            self.os_admin.hypervisor_client.list_hypervisors(
+                {"with_servers": True}))["hypervisors"]
+        for host in hosts:
+            hypervisor_vms = next((
+                hyper["running_vms"] for hyper in list_hypervisors if
+                hyper['service']['host'] == host), None)
+            if hypervisor_vms == 0:
+                hosts_available.append(host)
+        if not hosts_available:
             raise self.skipException("All hosts are already in other "
-                                     "availability zones, so can't add "
+                                     "availability zones or have running "
+                                     "instances, so can't add "
                                      "host to aggregate. \nAggregates list: "
                                      "%s" % aggregates)
-        host = hosts[0]
+        host = hosts_available[0]
 
         self.client.add_host(aggregate['id'], host=host)
         self.addCleanup(self.client.remove_host, aggregate['id'], host=host)
diff --git a/tempest/serial_tests/scenario/test_aggregates_basic_ops.py b/tempest/serial_tests/scenario/test_aggregates_basic_ops.py
index ba31d84..eb69db3 100644
--- a/tempest/serial_tests/scenario/test_aggregates_basic_ops.py
+++ b/tempest/serial_tests/scenario/test_aggregates_basic_ops.py
@@ -15,10 +15,13 @@
 
 from tempest.common import tempest_fixtures as fixtures
 from tempest.common import utils
+from tempest import config
 from tempest.lib.common.utils import data_utils
 from tempest.lib import decorators
 from tempest.scenario import manager
 
+CONF = config.CONF
+
 
 @decorators.serial
 class TestAggregatesBasicOps(manager.ScenarioTest):
@@ -58,9 +61,16 @@
             binary='nova-compute')['services']
         self.assertNotEmpty(svc_list)
         hosts_available = []
+        list_hypervisors = (
+            self.os_admin.hypervisor_client.list_hypervisors(
+                {"with_servers": True}))["hypervisors"]
         for host in svc_list:
             if (host['state'] == 'up' and host['status'] == 'enabled'):
-                hosts_available.append(host['host'])
+                hypervisor_vms = next((
+                    hyper["running_vms"] for hyper in list_hypervisors if
+                    hyper['service']['host'] == host["host"]), None)
+                if hypervisor_vms == 0:
+                    hosts_available.append(host["host"])
         aggregates = self.aggregates_client.list_aggregates()['aggregates']
         hosts_in_zone = []
         for agg in aggregates:
@@ -69,7 +79,8 @@
         hosts = [v for v in hosts_available if v not in hosts_in_zone]
         if not hosts:
             raise self.skipException("All hosts are already in other "
-                                     "availability zones, so can't add "
+                                     "availability zones or have running "
+                                     "instances, so can't add "
                                      "host to aggregate. \nAggregates list: "
                                      "%s" % aggregates)
         return hosts[0]
@@ -117,6 +128,9 @@
     @decorators.attr(type='slow')
     @utils.services('compute')
     def test_aggregate_basic_ops(self):
+        if CONF.production:
+            raise self.skipException("Not allowed to run this test "
+                                     "on production environment")
         self.useFixture(fixtures.LockFixture('availability_zone'))
         az = 'foo_zone'
         aggregate_name = data_utils.rand_name('aggregate-scenario')