From cbcaf04ed33e65c1ae0299cda5c0204d0eef9712 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Sun, 3 Feb 2019 16:59:30 +0100 Subject: [PATCH] Only check main interface in dynamic groups For Network and Network scope dynamic groups, only the interface with the same name as the host (the main interface) is taken into account. If only another interface is part of the network or network scope, the host is not part of the group. JIRA INFRA-784 #action In Progress --- app/models.py | 4 +-- tests/functional/test_models.py | 62 ++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/app/models.py b/app/models.py index bdbd11f..0214a12 100644 --- a/app/models.py +++ b/app/models.py @@ -1044,7 +1044,7 @@ class AnsibleGroup(CreatedMixin, db.Model): Host.query.join(Host.interfaces) .join(Interface.network) .join(Network.scope) - .filter(NetworkScope.name == self.name) + .filter(NetworkScope.name == self.name, Interface.name == Host.name) .order_by(Host.name) .all() ) @@ -1052,7 +1052,7 @@ class AnsibleGroup(CreatedMixin, db.Model): return ( Host.query.join(Host.interfaces) .join(Interface.network) - .filter(Network.vlan_name == self.name) + .filter(Network.vlan_name == self.name, Interface.name == Host.name) .order_by(Host.name) .all() ) diff --git a/tests/functional/test_models.py b/tests/functional/test_models.py index 8493080..4c25661 100644 --- a/tests/functional/test_models.py +++ b/tests/functional/test_models.py @@ -271,24 +271,25 @@ def test_ansible_dynamic_network_group( ): network1 = network_factory(vlan_name="network1") network2 = network_factory(vlan_name="network2") - interface1_n1 = interface_factory(network=network1) - interface2_n1 = interface_factory(network=network1) - interface1_n2 = interface_factory(network=network2) - host1_n1 = host_factory(name="host1", interfaces=[interface1_n1]) - host2_n1 = host_factory(name="host2", interfaces=[interface2_n1]) - host1_n2 = host_factory(interfaces=[interface1_n2]) - group_n1 = ansible_group_factory( + host1 = host_factory(name="host1") + host2 = host_factory(name="host2") + host3 = host_factory(name="host3") + interface_factory(name="host1", host=host1, network=network1) + interface_factory(name="host2", host=host2, network=network1) + interface_factory(name="host2-2", host=host2, network=network2) + interface_factory(name="host3", host=host3, network=network2) + group1 = ansible_group_factory( name="network1", type=models.AnsibleGroupType.NETWORK ) - group_n2 = ansible_group_factory( + group2 = ansible_group_factory( name="network2", type=models.AnsibleGroupType.NETWORK ) - group_n3 = ansible_group_factory( - name="unknown", type=models.AnsibleGroupType.NETWORK - ) - assert group_n1.hosts == [host1_n1, host2_n1] - assert group_n2.hosts == [host1_n2] - assert group_n3.hosts == [] + group3 = ansible_group_factory(name="unknown", type=models.AnsibleGroupType.NETWORK) + # host2 has an interface on network1 and one on network2. + # It's only in group1, because its main interface (same name as host) is on network1. + assert group1.hosts == [host1, host2] + assert group2.hosts == [host3] + assert group3.hosts == [] def test_ansible_dynamic_network_scope_group( @@ -300,27 +301,30 @@ def test_ansible_dynamic_network_scope_group( ): scope1 = network_scope_factory(name="scope1") scope2 = network_scope_factory(name="scope2") - network1_s1 = network_factory(scope=scope1) - network2_s1 = network_factory(scope=scope1) - network1_s2 = network_factory(scope=scope2) - interface1_s1 = interface_factory(network=network1_s1) - interface2_s1 = interface_factory(network=network2_s1) - interface1_s2 = interface_factory(network=network1_s2) - host1_s1 = host_factory(name="host1", interfaces=[interface1_s1]) - host2_s1 = host_factory(name="host2", interfaces=[interface2_s1]) - host1_s2 = host_factory(interfaces=[interface1_s2]) - group_s1 = ansible_group_factory( + network1 = network_factory(scope=scope1) + network2 = network_factory(scope=scope1) + network3 = network_factory(scope=scope2) + host1 = host_factory(name="host1") + host2 = host_factory(name="host2") + host3 = host_factory(name="host3") + interface_factory(name="host1", host=host1, network=network1) + interface_factory(name="host2", host=host2, network=network2) + interface_factory(name="host2-2", host=host2, network=network3) + interface_factory(name="host3", host=host3, network=network3) + group1 = ansible_group_factory( name="scope1", type=models.AnsibleGroupType.NETWORK_SCOPE ) - group_s2 = ansible_group_factory( + group2 = ansible_group_factory( name="scope2", type=models.AnsibleGroupType.NETWORK_SCOPE ) - group_s3 = ansible_group_factory( + group3 = ansible_group_factory( name="unknown", type=models.AnsibleGroupType.NETWORK_SCOPE ) - assert group_s1.hosts == [host1_s1, host2_s1] - assert group_s2.hosts == [host1_s2] - assert group_s3.hosts == [] + # host2 has an interface on scope1 and one on scope2. + # It's only in group1, because its main interface (same name as host) is on scope1. + assert group1.hosts == [host1, host2] + assert group2.hosts == [host3] + assert group3.hosts == [] @pytest.mark.parametrize("status", [None, "FINISHED", "FAILED", "STARTED"]) -- GitLab