Skip to content

Commit

Permalink
tests: Deprecate old 'testing' folder and implement django tests or
Browse files Browse the repository at this point in the history
system certificates and workflows
  • Loading branch information
frikilax committed Jan 17, 2025
1 parent a8687b5 commit ae98cef
Show file tree
Hide file tree
Showing 23 changed files with 608 additions and 591 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- [SYSTEM] [SETTINGS] Allow to configure project with django-environ
- [TESTS] [SYSTEM] Implement Network test cases
- [TESTS] [WORKFLOW] Implement Workflow test cases
### Removed
- [API_PARSER] [CORTEX_XDR] Remove fields used for backward compatibility
- [TESTS] Remove old 'testing' folder
### Changed
- [API_PARSER] [NETSKOPE] Add the possiblity to retrieve 3 new kinds of logs
- [SYSTEM] [SETTINGS] Use Hostname value from Django settings
Expand Down
File renamed without changes.
342 changes: 342 additions & 0 deletions vulture_os/system/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
#!/home/vlt-os/env/bin/python
"""This file is part of Vulture OS.
Vulture OS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vulture OS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vulture OS. If not, see http://www.gnu.org/licenses/.
"""

__author__ = "Théo BERTIN"
__credits__ = []
__license__ = "GPLv3"
__version__ = "4.0.0"
__maintainer__ = "Vulture OS"
__email__ = "[email protected]"
__doc__ = 'Tests for System App'

from django.test import TestCase
from unittest.mock import patch

from system.cluster.models import NetworkInterfaceCard, NetworkAddress, NetworkAddressNIC

class NetworkInterfaceCardTestCase(TestCase):
TEST_CASE_NAME=f"{__name__}"
def setUp(self):
from system.cluster.models import Node
self.node = Node.objects.create(
name=f"node_test_{self.TEST_CASE_NAME}",
)

def test_nic_creation(self):
self.assertIsNotNone(
NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)
)

@patch('subprocess.check_output')
def test_get_list(self, mocked_check_output):
mocked_check_output.return_value = b"vtnet0 lo0 lo1 lo2 em1 lo3 lo4 lo5 lo6 tun0 pflog0"
nic = NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)

interface_list = nic.get_list()
self.assertIsInstance(interface_list, list)
self.assertEquals(set(["vtnet0", "em1"]), set(interface_list))

def test_has_ipv4_simple(self):
nic = NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)
net_addr_ipv4 = NetworkAddress.objects.create(
name = f"net_addr_ipv4_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
)
nic_addr = NetworkAddressNIC.objects.create(
nic=nic,
network_address=net_addr_ipv4
)
self.assertIsNotNone(nic)
self.assertIsNotNone(net_addr_ipv4)
self.assertIsNotNone(nic_addr)

self.assertTrue(nic.has_ipv4)
self.assertFalse(nic.has_ipv6)

def test_has_ipv4_and_vlanv6(self):
nic = NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)
net_addr_ipv4 = NetworkAddress.objects.create(
name = f"net_addr_ipv4_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
)
net_addr_vlan = NetworkAddress.objects.create(
name = f"net_addr_vlan_test_{self.TEST_CASE_NAME}",
type="vlan",
ip="fe80::1",
prefix_or_netmask="64",
vlan=123
)
nic_addr = NetworkAddressNIC.objects.create(
nic=nic,
network_address=net_addr_ipv4
)
nic_addr2 = NetworkAddressNIC.objects.create(
nic=nic,
network_address=net_addr_vlan
)
self.assertIsNotNone(nic)
self.assertIsNotNone(net_addr_ipv4)
self.assertIsNotNone(net_addr_vlan)
self.assertIsNotNone(nic_addr)
self.assertIsNotNone(nic_addr2)

self.assertEquals(nic.networkaddress_set.count(), 2)

self.assertTrue(nic.has_ipv4)
self.assertFalse(nic.has_ipv6)

def test_has_ipv6_simple(self):
nic = NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)
net_addr_ipv6 = NetworkAddress.objects.create(
name = f"net_addr_ipv6_test_{self.TEST_CASE_NAME}",
type="system",
ip="fe80::1",
prefix_or_netmask="64",
)
nic_addr = NetworkAddressNIC.objects.create(
nic=nic,
network_address=net_addr_ipv6
)
self.assertIsNotNone(nic)
self.assertIsNotNone(net_addr_ipv6)
self.assertIsNotNone(nic_addr)

self.assertFalse(nic.has_ipv4)
self.assertTrue(nic.has_ipv6)


def test_has_ipv6_and_vlanv4(self):
nic = NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)
net_addr_ipv6 = NetworkAddress.objects.create(
name = f"net_addr_ipv6_test_{self.TEST_CASE_NAME}",
type="system",
ip="fe80::1",
prefix_or_netmask="64",
)
net_addr_vlan = NetworkAddress.objects.create(
name = f"net_addr_vlan_test_{self.TEST_CASE_NAME}",
type="vlan",
ip="127.0.0.1",
prefix_or_netmask="24",
vlan=123
)
nic_addr = NetworkAddressNIC.objects.create(
nic=nic,
network_address=net_addr_ipv6
)
nic_addr2 = NetworkAddressNIC.objects.create(
nic=nic,
network_address=net_addr_vlan
)
self.assertIsNotNone(nic)
self.assertIsNotNone(net_addr_ipv6)
self.assertIsNotNone(net_addr_vlan)
self.assertIsNotNone(nic_addr)
self.assertIsNotNone(nic_addr2)

self.assertEquals(nic.networkaddress_set.count(), 2)

self.assertFalse(nic.has_ipv4)
self.assertTrue(nic.has_ipv6)


class NetworkAddressTestCase(TestCase):
TEST_CASE_NAME=f"{__name__}"
def setUp(self):
from system.cluster.models import Node
self.node = Node.objects.create(
name=f"node_test_{self.TEST_CASE_NAME}",
)
self.nic0 = NetworkInterfaceCard.objects.create(
dev = "vtnet0",
node=self.node,
)
self.nic1 = NetworkInterfaceCard.objects.create(
dev = "vtnet1",
node=self.node,
)

def test_is_carp(self):
net_addr_with_carp = NetworkAddress.objects.create(
name = f"net_addr_with_carp_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
carp_vhid=42
)
net_addr_without_carp = NetworkAddress.objects.create(
name = f"net_addr_without_carp_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
carp_vhid=0
)

self.assertIsNotNone(net_addr_with_carp)
self.assertIsNotNone(net_addr_without_carp)
self.assertTrue(net_addr_with_carp.is_carp)
self.assertFalse(net_addr_without_carp.is_carp)

def test_version(self):
net_addr_ipv4 = NetworkAddress.objects.create(
name = f"net_addr_ipv4_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
)
net_addr_ipv6 = NetworkAddress.objects.create(
name = f"net_addr_ipv6_test_{self.TEST_CASE_NAME}",
type="system",
ip="fe80::1",
prefix_or_netmask="64",
)
net_addr_lagg = NetworkAddress.objects.create(
name = f"net_addr_lagg_test_{self.TEST_CASE_NAME}",
type="lagg",
)

self.assertIsNotNone(net_addr_ipv4)
self.assertIsNotNone(net_addr_ipv6)
self.assertIsNotNone(net_addr_lagg)
self.assertEquals(net_addr_ipv4.version, 4)
self.assertEquals(net_addr_ipv6.version, 6)
# Oh that's ugly...
self.assertEquals(net_addr_lagg.version, '')

def test_ip_cidr(self):
net_addr_ipv4 = NetworkAddress.objects.create(
name = f"net_addr_ipv4_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
)
net_addr_ipv6 = NetworkAddress.objects.create(
name = f"net_addr_ipv6_test_{self.TEST_CASE_NAME}",
type="system",
ip="fe80::1",
prefix_or_netmask="64",
)
net_addr_lagg = NetworkAddress.objects.create(
name = f"net_addr_lagg_test_{self.TEST_CASE_NAME}",
type="lagg",
)

self.assertIsNotNone(net_addr_ipv4)
self.assertIsNotNone(net_addr_ipv6)
self.assertIsNotNone(net_addr_lagg)
self.assertEquals(net_addr_ipv4.ip_cidr, '127.0.0.1/24')
self.assertEquals(net_addr_ipv6.ip_cidr, 'fe80::1/64')
self.assertEquals(net_addr_lagg.ip_cidr, '')

def test_family(self):
net_addr_ipv4 = NetworkAddress.objects.create(
name = f"net_addr_ipv4_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
)
net_addr_ipv6 = NetworkAddress.objects.create(
name = f"net_addr_ipv6_test_{self.TEST_CASE_NAME}",
type="system",
ip="fe80::1",
prefix_or_netmask="64",
)
net_addr_lagg = NetworkAddress.objects.create(
name = f"net_addr_lagg_test_{self.TEST_CASE_NAME}",
type="lagg",
)

self.assertIsNotNone(net_addr_ipv4)
self.assertIsNotNone(net_addr_ipv6)
self.assertIsNotNone(net_addr_lagg)
self.assertEquals(net_addr_ipv4.family, 'inet')
self.assertEquals(net_addr_ipv6.family, 'inet6')
self.assertEquals(net_addr_lagg.family, '')

def test_main_iface(self):
net_addr_ipv4 = NetworkAddress.objects.create(
name = f"net_addr_ipv4_test_{self.TEST_CASE_NAME}",
type="system",
ip="127.0.0.1",
prefix_or_netmask="24",
iface_id=0
)
NetworkAddressNIC.objects.create(
nic=self.nic0,
network_address=net_addr_ipv4
)
net_addr_ipv6 = NetworkAddress.objects.create(
name = f"net_addr_ipv6_test_{self.TEST_CASE_NAME}",
type="alias",
ip="fe80::1",
prefix_or_netmask="64",
iface_id=1
)
NetworkAddressNIC.objects.create(
nic=self.nic1,
network_address=net_addr_ipv6
)
net_addr_lagg = NetworkAddress.objects.create(
name = f"net_addr_lagg_test_{self.TEST_CASE_NAME}",
type="lagg",
iface_id=2,
)
NetworkAddressNIC.objects.create(
nic=self.nic0,
network_address=net_addr_lagg
)
NetworkAddressNIC.objects.create(
nic=self.nic1,
network_address=net_addr_lagg
)
net_addr_vlan = NetworkAddress.objects.create(
name = f"net_addr_lagg_test_{self.TEST_CASE_NAME}",
type="vlan",
vlan=42,
iface_id=42,
)

self.assertIsNotNone(net_addr_ipv4)
self.assertIsNotNone(net_addr_ipv6)
self.assertIsNotNone(net_addr_lagg)
self.assertIsNotNone(net_addr_vlan)
self.assertEquals(net_addr_ipv4.main_iface, 'vtnet0')
self.assertEquals(net_addr_ipv6.main_iface, 'vtnet1_alias1')
self.assertEquals(net_addr_lagg.main_iface, 'lagg2')
self.assertEquals(net_addr_vlan.main_iface, 'vlan42')
29 changes: 0 additions & 29 deletions vulture_os/testing/config.py

This file was deleted.

Empty file.
Loading

0 comments on commit ae98cef

Please sign in to comment.