Merge ~cgrabowski/maas:do_not_create_duplicate_vlan_on_controller_report_neighbors into maas:master

Proposed by Christian Grabowski
Status: Merged
Approved by: Christian Grabowski
Approved revision: 7a7ce709f9baa2d6f480e9a6d56418a21e3c11f4
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~cgrabowski/maas:do_not_create_duplicate_vlan_on_controller_report_neighbors
Merge into: maas:master
Diff against target: 145 lines (+67/-5)
4 files modified
src/maasserver/models/interface.py (+16/-1)
src/maasserver/models/node.py (+1/-1)
src/maasserver/models/tests/test_interface.py (+17/-2)
src/maasserver/models/tests/test_node.py (+33/-1)
Reviewer Review Type Date Requested Status
MAAS Lander Approve
Alexsander de Souza Approve
Review via email: mp+423776@code.launchpad.net

Commit message

update vlan's fabric when observed neighbor's vlan already exists

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b do_not_create_duplicate_vlan_on_controller_report_neighbors lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/12769/console
COMMIT: 71e12ebe19fc7826d66919fd4f2876ea6af6e9de

review: Needs Fixing
Revision history for this message
Alexsander de Souza (alexsander-souza) wrote :

I think the other testcases in TestReportVID must be updated also, because it looks like `neighbour["ip"]` won't be None usually. If you do this change, I think test_creates_vlan_if_necessary() will fail

review: Needs Information
aa67806... by Christian Grabowski

pass IP to test_create_vlan_if_necessary report_vid calls

Revision history for this message
Christian Grabowski (cgrabowski) wrote :

> I think the other testcases in TestReportVID must be updated also, because it
> looks like `neighbour["ip"]` won't be None usually. If you do this change, I
> think test_creates_vlan_if_necessary() will fail

Ah good point, updated.

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b do_not_create_duplicate_vlan_on_controller_report_neighbors lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/12772/console
COMMIT: aa67806ec1598b8474ba1ec898a44598e0922fe4

review: Needs Fixing
9e81695... by Christian Grabowski

fix formatting

Revision history for this message
Alexsander de Souza (alexsander-souza) wrote :

+1

review: Approve
7a7ce70... by Christian Grabowski

remove unused var

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b do_not_create_duplicate_vlan_on_controller_report_neighbors lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/12774/console
COMMIT: 9e81695f9b3afb5a67cb0760862d6f8d5fd011dd

review: Needs Fixing
Revision history for this message
Christian Grabowski (cgrabowski) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b do_not_create_duplicate_vlan_on_controller_report_neighbors lp:~cgrabowski/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 7a7ce709f9baa2d6f480e9a6d56418a21e3c11f4

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/models/interface.py b/src/maasserver/models/interface.py
2index 9392df7..7945291 100644
3--- a/src/maasserver/models/interface.py
4+++ b/src/maasserver/models/interface.py
5@@ -1533,7 +1533,7 @@ class Interface(CleanSave, TimestampedModel):
6 tags.remove(tag)
7 self.tags = tags
8
9- def report_vid(self, vid):
10+ def report_vid(self, vid, ip=None):
11 """Report that the specified VID was seen on this interface.
12
13 Automatically creates the related VLAN on this interface's associated
14@@ -1542,8 +1542,23 @@ class Interface(CleanSave, TimestampedModel):
15 if self.vlan is not None and vid is not None:
16 fabric = self.vlan.fabric
17 # Circular imports
18+ from maasserver.models.subnet import Subnet
19 from maasserver.models.vlan import VLAN
20
21+ if ip:
22+ subnet = Subnet.objects.get_best_subnet_for_ip(ip)
23+ if subnet:
24+ vlan = subnet.vlan
25+ vlan.fabric = fabric
26+ vlan.save()
27+ maaslog.info(
28+ "%s: Automatically updated VLAN %d (observed on %s).",
29+ self.get_log_string(),
30+ vid,
31+ vlan.fabric.get_name(),
32+ )
33+ return
34+
35 vlan, created = VLAN.objects.get_or_create(
36 fabric=fabric,
37 vid=vid,
38diff --git a/src/maasserver/models/node.py b/src/maasserver/models/node.py
39index 385c2d9..2a01c4b 100644
40--- a/src/maasserver/models/node.py
41+++ b/src/maasserver/models/node.py
42@@ -6416,7 +6416,7 @@ class Controller(Node):
43 vid=vid,
44 )
45 if vid is not None:
46- interface.report_vid(vid)
47+ interface.report_vid(vid, ip=neighbour["ip"])
48
49 def report_mdns_entries(self, entries):
50 """Update the mDNS entries on this controller.
51diff --git a/src/maasserver/models/tests/test_interface.py b/src/maasserver/models/tests/test_interface.py
52index ca9e1eb..fcfdc85 100644
53--- a/src/maasserver/models/tests/test_interface.py
54+++ b/src/maasserver/models/tests/test_interface.py
55@@ -4049,14 +4049,15 @@ class TestReportVID(MAASServerTestCase):
56 vlan = fabric.get_default_vlan()
57 iface = factory.make_Interface(vlan=vlan)
58 vid = random.randint(1, 4094)
59+ ip = factory.make_ip_address()
60 vlan_before = get_one(VLAN.objects.filter(fabric=fabric, vid=vid))
61 self.assertIsNone(vlan_before)
62- iface.report_vid(vid)
63+ iface.report_vid(vid, ip=ip)
64 vlan_after = get_one(VLAN.objects.filter(fabric=fabric, vid=vid))
65 self.assertIsNotNone(vlan_after)
66 # Report it one more time to make sure we can handle it if we already
67 # observed it. (expect nothing to happen.)
68- iface.report_vid(vid)
69+ iface.report_vid(vid, ip=ip)
70
71 def test_logs_vlan_creation_and_sets_description(self):
72 fabric = factory.make_Fabric()
73@@ -4075,6 +4076,20 @@ class TestReportVID(MAASServerTestCase):
74 new_vlan.description,
75 )
76
77+ def test_report_vid_handles_existing_vlan(self):
78+ fabric1 = factory.make_Fabric()
79+ fabric2 = factory.make_Fabric()
80+ observing_vlan = fabric1.get_default_vlan()
81+ neighbour_vlan = factory.make_VLAN(fabric=fabric2)
82+ subnet1 = factory.make_Subnet(vlan=observing_vlan)
83+ subnet2 = factory.make_Subnet(vlan=neighbour_vlan)
84+ iface = factory.make_Interface(subnet=subnet1)
85+ iface.report_vid(
86+ neighbour_vlan.vid, ip=subnet2.get_next_ip_for_allocation()
87+ )
88+ neighbour_vlan.refresh_from_db()
89+ self.assertEqual(observing_vlan.fabric, neighbour_vlan.fabric)
90+
91
92 class TestInterfaceGetDefaultBridgeName(MAASServerTestCase):
93
94diff --git a/src/maasserver/models/tests/test_node.py b/src/maasserver/models/tests/test_node.py
95index 33752b8..7a468c0 100644
96--- a/src/maasserver/models/tests/test_node.py
97+++ b/src/maasserver/models/tests/test_node.py
98@@ -103,6 +103,7 @@ from maasserver.models import (
99 StaticIPAddress,
100 Subnet,
101 UnknownInterface,
102+ VLAN,
103 VolumeGroup,
104 )
105 from maasserver.models import Bcache, BMC
106@@ -10913,7 +10914,38 @@ class TestReportNeighbours(MAASServerTestCase):
107 },
108 ]
109 rack.report_neighbours(neighbours)
110- report_vid.assert_has_calls([call(3), call(7)])
111+ report_vid.assert_has_calls(
112+ [call(3, ip=neighbours[0]["ip"]), call(7, ip=neighbours[1]["ip"])]
113+ )
114+
115+ def test_updates_fabric_of_existing_vlan(self):
116+ rack = factory.make_RackController()
117+ observing_fabric = factory.make_Fabric()
118+ other_fabric = factory.make_Fabric()
119+ vlan = factory.make_VLAN(fabric=observing_fabric)
120+ observed_vlan = factory.make_VLAN(fabric=other_fabric)
121+ subnet1 = factory.make_Subnet(vlan=vlan)
122+ subnet2 = factory.make_Subnet(vlan=observed_vlan)
123+ factory.make_Interface(name="eth0", subnet=subnet1, node=rack)
124+ iface2 = factory.make_Interface(name="eth1", node=rack)
125+ neighbours = [
126+ {
127+ "interface": "eth0",
128+ "ip": subnet2.get_next_ip_for_allocation(),
129+ "time": datetime.now(),
130+ "mac": iface2.mac_address,
131+ "vid": observed_vlan.vid,
132+ }
133+ ]
134+ rack.report_neighbours(neighbours)
135+ observed_vlan.refresh_from_db()
136+ self.assertEqual(observing_fabric, observed_vlan.fabric)
137+ self.assertEqual(
138+ VLAN.objects.filter(
139+ vid=observed_vlan.vid, fabric=observing_fabric
140+ ).count(),
141+ 1,
142+ )
143
144
145 class TestReportMDNSEntries(MAASServerTestCase):

Subscribers

People subscribed via source and target branches