|
14 | 14 | import ipaddress
|
15 | 15 |
|
16 | 16 | from unittest.mock import patch
|
| 17 | +from django.db import models |
17 | 18 | from django.test import TestCase
|
18 | 19 | from scionlab.defines import (
|
19 | 20 | DISPATCHER_PORT,
|
@@ -119,6 +120,82 @@ def test_update_keys(self):
|
119 | 120 | )
|
120 | 121 | self.assertEqual(new_certificate.version, prev_certificate.version + 1)
|
121 | 122 |
|
| 123 | + def test_is_core_not_modified(self): |
| 124 | + # if the is_core property is not modified, do not expect new keys or certs. |
| 125 | + as_ = AS.objects.get(as_id='ffaa:0:1304') |
| 126 | + prev_certs = list(as_.certificates()) |
| 127 | + prev_keys = list(as_.keys.all()) |
| 128 | + as_.mtu = 800 # modify a property but not is_core |
| 129 | + as_.save() |
| 130 | + |
| 131 | + got_certs = list(as_.certificates().all()) |
| 132 | + got_keys = list(as_.keys.all()) |
| 133 | + self.assertEqual(got_certs, prev_certs) |
| 134 | + self.assertEqual(got_keys, prev_keys) |
| 135 | + |
| 136 | + def test_is_core_modification(self): |
| 137 | + # Subfunction to perform the repetitive test. |
| 138 | + def _change_is_core_and_test(as_, core_state: bool): |
| 139 | + # Store state of ffaa:0:1304. |
| 140 | + prev_certs = list(as_.certificates()) |
| 141 | + prev_keys = list(as_.keys.all()) |
| 142 | + prev_trcs = list(as_.isd.trcs.all()) |
| 143 | + prev_ifaces = as_.interfaces.all() |
| 144 | + prev_links = Link.objects.filter( |
| 145 | + models.Q(interfaceA__in=prev_ifaces) | |
| 146 | + models.Q(interfaceB__in=prev_ifaces)) |
| 147 | + self.assertGreater(prev_links.count(), 0) |
| 148 | + prev_links = list(prev_links) |
| 149 | + prev_ifaces = list(prev_ifaces) |
| 150 | + |
| 151 | + # now promote AS 19-ffaa:0:1304 to core AS. We expect the following to happen: |
| 152 | + # - existing links starting or ending at that AS are removed. |
| 153 | + # - new keys and certificates are issued (as core) for that AS. |
| 154 | + # - all certificates in that ISD are reissued. |
| 155 | + # - the ISD issues a new TRC |
| 156 | + as_.mtu = 800 |
| 157 | + as_.is_core = core_state |
| 158 | + as_.save() |
| 159 | + |
| 160 | + # Verify the previous links do not exist anymore. |
| 161 | + self.assertEqual(Link.objects.filter(pk__in=[ |
| 162 | + link.pk for link in prev_links]).count(), 0) |
| 163 | + |
| 164 | + # Same with interfaces. |
| 165 | + self.assertEqual(Interface.objects.filter(pk__in=[ |
| 166 | + iface.pk for iface in prev_ifaces]).count(), 0) |
| 167 | + |
| 168 | + # This AS doesn't have interfaces or links. |
| 169 | + got_ifaces = as_.interfaces.all() |
| 170 | + self.assertEqual(got_ifaces.count(), 0) |
| 171 | + got_links = Link.objects.filter( |
| 172 | + models.Q(interfaceA__in=got_ifaces) | |
| 173 | + models.Q(interfaceB__in=got_ifaces)) |
| 174 | + self.assertEqual(got_links.count(), 0) |
| 175 | + |
| 176 | + # We have now more certificates and keys. |
| 177 | + got_certs = list(as_.certificates().all()) |
| 178 | + self.assertTrue(set(prev_certs).issubset(got_certs)) |
| 179 | + got_keys = list(as_.keys.all()) |
| 180 | + self.assertTrue(set(prev_keys).issubset(got_keys)) |
| 181 | + |
| 182 | + # We have a new TRC. |
| 183 | + got_trcs = list(as_.isd.trcs.all()) |
| 184 | + self.assertTrue(set(prev_trcs).issubset(got_trcs)) |
| 185 | + |
| 186 | + # Get two ASes from the fixture, one core and one non-core. |
| 187 | + as1 = AS.objects.get(as_id='ffaa:0:1301') |
| 188 | + as2 = AS.objects.get(as_id='ffaa:0:1304') |
| 189 | + # test sanity check: the fixture should have: |
| 190 | + # ffaa:0:1301 core. |
| 191 | + # ffaa:0:1304 non-core. |
| 192 | + self.assertTrue(as1.is_core) |
| 193 | + self.assertFalse(as2.is_core) |
| 194 | + |
| 195 | + # Test. |
| 196 | + _change_is_core_and_test(as1, False) |
| 197 | + _change_is_core_and_test(as2, True) |
| 198 | + |
122 | 199 |
|
123 | 200 | class LinkModificationTests(TestCase):
|
124 | 201 | fixtures = []
|
|
0 commit comments