From d822a0f37e9528877105ba882ec380f253f8e719 Mon Sep 17 00:00:00 2001
From: licanwei
Date: Wed, 29 Aug 2018 13:18:55 +0800
Subject: [PATCH] Remove hosts if can't find hosts in host agrregate
Change-Id: Ie2dc20d8068f6d945a487238a0b6e5bc2f72fa3b
Closes-Bug: #1789366
---
watcher/decision_engine/scope/compute.py | 9 +++++++++
watcher/tests/decision_engine/scope/fake_scopes.py | 6 ++++++
watcher/tests/decision_engine/scope/test_compute.py | 13 +++++++++++++
3 files changed, 28 insertions(+)
diff --git a/watcher/decision_engine/scope/compute.py b/watcher/decision_engine/scope/compute.py
index 903642833..8348b1512 100644
--- a/watcher/decision_engine/scope/compute.py
+++ b/watcher/decision_engine/scope/compute.py
@@ -167,6 +167,7 @@ class ComputeScope(base.BaseScope):
instance_metadata = []
projects_to_exclude = []
compute_scope = []
+ found_nothing_flag = False
model_hosts = list(cluster_model.get_all_compute_nodes().keys())
if not self.scope:
@@ -182,9 +183,13 @@ class ComputeScope(base.BaseScope):
if 'host_aggregates' in rule:
self._collect_aggregates(rule['host_aggregates'],
allowed_nodes)
+ if not allowed_nodes:
+ found_nothing_flag = True
elif 'availability_zones' in rule:
self._collect_zones(rule['availability_zones'],
allowed_nodes)
+ if not allowed_nodes:
+ found_nothing_flag = True
elif 'exclude' in rule:
self.exclude_resources(
rule['exclude'], instances=instances_to_exclude,
@@ -195,6 +200,10 @@ class ComputeScope(base.BaseScope):
instances_to_exclude = set(instances_to_exclude)
if allowed_nodes:
nodes_to_remove = set(model_hosts) - set(allowed_nodes)
+ # This branch means user set host_aggregates and/or availability_zones
+ # but can't find any nodes, so we should remove all nodes.
+ elif found_nothing_flag:
+ nodes_to_remove = set(model_hosts)
nodes_to_remove.update(nodes_to_exclude)
self.remove_nodes_from_model(nodes_to_remove, cluster_model)
diff --git a/watcher/tests/decision_engine/scope/fake_scopes.py b/watcher/tests/decision_engine/scope/fake_scopes.py
index 7aeefd926..0d1b6b03f 100644
--- a/watcher/tests/decision_engine/scope/fake_scopes.py
+++ b/watcher/tests/decision_engine/scope/fake_scopes.py
@@ -47,3 +47,9 @@ fake_scope_2 = [{'storage': [{'availability_zones': [{'name': 'zone_0'}]},
]}]
}
]
+
+fake_scope_3 = [{'compute': [{'host_aggregates': [{'id': '1'}]},
+ {'exclude': []
+ }]
+ }
+ ]
diff --git a/watcher/tests/decision_engine/scope/test_compute.py b/watcher/tests/decision_engine/scope/test_compute.py
index 03979e6ba..89b654355 100644
--- a/watcher/tests/decision_engine/scope/test_compute.py
+++ b/watcher/tests/decision_engine/scope/test_compute.py
@@ -281,3 +281,16 @@ class TestComputeScope(base.TestCase):
model.get_instance_by_uuid('INSTANCE_0').watcher_exclude)
self.assertTrue(
model.get_instance_by_uuid('INSTANCE_1').watcher_exclude)
+
+ @mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_detail')
+ @mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_list')
+ def test_get_scoped_model_with_hostaggregate_null(
+ self, mock_list, mock_detail):
+ cluster = self.fake_cluster.generate_scenario_1()
+ audit_scope = fake_scopes.fake_scope_3
+ mock_list.return_value = [mock.Mock(id=i,
+ name="HA_{0}".format(i))
+ for i in range(2)]
+ model = compute.ComputeScope(audit_scope, mock.Mock(),
+ osc=mock.Mock()).get_scoped_model(cluster)
+ self.assertEqual(0, len(model.edges()))