generated from simonw/python-lib-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Toggled IXP active status based on active membership count
- Loading branch information
Showing
5 changed files
with
186 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import datetime | ||
|
||
from ixp_tracker.models import ASN, IXP | ||
|
||
|
||
def create_asn_fixture(as_number: int, country: str = "CH"): | ||
asn = ASN.objects.filter(number=as_number) | ||
if len(asn) > 0: | ||
return asn.first() | ||
asn = ASN( | ||
name="Network Org", | ||
number=as_number, | ||
peeringdb_id=5, | ||
network_type="other", | ||
registration_country_code=country, | ||
created="2019-01-01", | ||
last_updated="2024-05-01" | ||
) | ||
asn.save() | ||
return asn | ||
|
||
|
||
def create_ixp_fixture(peering_db_id: int, country = "MM"): | ||
ixp = IXP( | ||
name="Old name", | ||
long_name="Network Name", | ||
city="Aberdeen", | ||
website="", | ||
active_status=True, | ||
peeringdb_id=peering_db_id, | ||
country_code=country, | ||
created=datetime(year=2020,month=10,day=1), | ||
last_updated=datetime(year=2023,month=10,day=1), | ||
last_active=datetime(year=2024, month=4, day=1) | ||
) | ||
ixp.save() | ||
return ixp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import pytest | ||
from datetime import datetime, timedelta, timezone | ||
|
||
from ixp_tracker.importers import toggle_ixp_active_status | ||
from ixp_tracker.models import IXP, IXPMember, IXPMembershipRecord | ||
from tests.fixtures import create_asn_fixture, create_ixp_fixture | ||
|
||
pytestmark = pytest.mark.django_db | ||
processing_date = datetime.utcnow().replace(tzinfo=timezone.utc) | ||
|
||
|
||
def test_active_ixp_with_members_remains_active(): | ||
ixp = create_ixp_fixture(1) | ||
asn = create_asn_fixture(12345) | ||
member = IXPMember( | ||
ixp=ixp, | ||
asn=asn, | ||
last_updated=ixp.last_updated, | ||
last_active=ixp.last_active | ||
) | ||
member.save() | ||
membership = IXPMembershipRecord( | ||
member=member, | ||
start_date=ixp.created, | ||
is_rs_peer=True, | ||
speed=1000, | ||
end_date=None | ||
) | ||
membership.save() | ||
|
||
processing_date = datetime.utcnow().replace(tzinfo=timezone.utc) | ||
toggle_ixp_active_status(processing_date) | ||
|
||
ixp = IXP.objects.get(peeringdb_id=1) | ||
|
||
assert ixp.active_status | ||
|
||
|
||
def test_active_ixp_with_member_marked_ended_is_marked_inactive(): | ||
ixp = create_ixp_fixture(1) | ||
asn = create_asn_fixture(12345) | ||
member = IXPMember( | ||
ixp=ixp, | ||
asn=asn, | ||
last_updated=ixp.last_updated, | ||
last_active=ixp.last_active | ||
) | ||
member.save() | ||
end_date = processing_date.replace(day=1) - timedelta(days=1) | ||
membership = IXPMembershipRecord( | ||
member=member, | ||
start_date=ixp.created, | ||
is_rs_peer=True, | ||
speed=1000, | ||
end_date=end_date | ||
) | ||
membership.save() | ||
|
||
toggle_ixp_active_status(processing_date) | ||
|
||
ixp = IXP.objects.get(peeringdb_id=1) | ||
|
||
assert ixp.active_status is False | ||
|
||
|
||
def test_inactive_ixp_with_no_active_members_remains_inactive(): | ||
ixp = create_ixp_fixture(1) | ||
ixp.active_status = False | ||
ixp.save() | ||
asn = create_asn_fixture(12345) | ||
member = IXPMember( | ||
ixp=ixp, | ||
asn=asn, | ||
last_updated=ixp.last_updated, | ||
last_active=ixp.last_active | ||
) | ||
member.save() | ||
end_date = processing_date.replace(day=1) - timedelta(days=1) | ||
membership = IXPMembershipRecord( | ||
member=member, | ||
start_date=ixp.created, | ||
is_rs_peer=True, | ||
speed=1000, | ||
end_date=end_date | ||
) | ||
membership.save() | ||
|
||
toggle_ixp_active_status(processing_date) | ||
|
||
ixp = IXP.objects.get(peeringdb_id=1) | ||
|
||
assert ixp.active_status is False | ||
|
||
|
||
def test_inactive_ixp_with_active_member_marked_active(): | ||
ixp = create_ixp_fixture(1) | ||
ixp.active_status = False | ||
ixp.save() | ||
asn = create_asn_fixture(12345) | ||
member = IXPMember( | ||
ixp=ixp, | ||
asn=asn, | ||
last_updated=ixp.last_updated, | ||
last_active=ixp.last_active | ||
) | ||
member.save() | ||
membership = IXPMembershipRecord( | ||
member=member, | ||
start_date=ixp.created, | ||
is_rs_peer=True, | ||
speed=1000, | ||
end_date=None | ||
) | ||
membership.save() | ||
|
||
toggle_ixp_active_status(processing_date) | ||
|
||
ixp = IXP.objects.get(peeringdb_id=1) | ||
|
||
assert ixp.active_status |