Пример #1
0
def test_sorting_position():
    entries = [{
        "address": "1.1.1.254",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "10.0.8.1",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }, {
        "address": "1.1.1.4",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "5.5.8.1",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }]

    nc_in = NetworkCollection('1.1.1.0/24', entries)
    nc_sorted = NetworkCollection('1.1.1.0/24', entries)
    nc_sorted.sort_records()
    for i in nc_sorted.entries:
        print(i.address)
    assert nc_sorted.entries[0].address == "1.1.1.4" and nc_sorted.entries[
        2].address == "5.5.8.1", "Should be 1.1.1.4 first and 5.5.8.1 third in list"
 def test_all_ok(self):
     n = NetworkCollection('192.168.0.0/24',
                           [{
                               'address': '192.168.0.1',
                               'available': False,
                               'last_used': "30/01/20 17:00:00"
                           }])
     self.assertIsInstance(n, NetworkCollection)
     n.remove_invalid_records()
     self.assertIs(len(n.entries), 1)
def test_is_address_in_network_returns_proper_value(network, addr):
    # GIVEN a network collection instance based on the input network
    network_collection = NetworkCollection(network, [])

    # WHEN calling is_address_in_network on the input address
    valid = network_collection.is_address_in_network(IPv4Address(addr))

    # THEN it returns proper value
    expected = __VALID_IP_NETWORK_PAIR[(network, addr)]
    assert valid is expected,\
        f"Address {addr} should {'' if expected else 'not '}be part of network {network}."
 def test_outside_network(self):
     with self.assertLogs() as cm:
         n = NetworkCollection('192.168.0.0/24',
                               [{
                                   'address': '192.168.1.1',
                                   'available': False,
                                   'last_used': "30/01/20 17:00:00"
                               }])
         self.assertIsInstance(n, NetworkCollection)
         n.remove_invalid_records()
         self.assertIs(len(n.entries), 0)
     self.assertEqual(len(cm.output), 1)
     self.assertRegex(cm.output[0], r'ip .* not in')
Пример #5
0
def test_rem_one_invalid_ip():
    entries = [{
        "address": "1.a.a.254",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "1.1.1.1",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }]

    nc_out = NetworkCollection('1.1.1.0/24', entries)
    nc_out.remove_invalid_records()
    assert nc_out.entries[0].address == "1.1.1.1", "Should be 1.1.1.1"
Пример #6
0
def test_rem_two_invalid_ip():
    entries = [{
        "address": "1..2.25",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "1.1.1.0",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }]

    nc_out = NetworkCollection('1.1.1.0/24', entries)
    nc_out.remove_invalid_records()
    with pytest.raises(IndexError):
        nc_out.entries[0].address, "Should be empty list -> IndexError"
 def test_sort(self):
     n = NetworkCollection('192.168.0.0/24',
                           [{
                               'address': '192.168.0.2',
                               'available': False,
                               'last_used': "30/01/20 17:00:00"
                           }, {
                               'address': '192.168.0.1',
                               'available': False,
                               'last_used': "30/01/20 17:00:00"
                           }])
     self.assertIsInstance(n, NetworkCollection)
     self.assertIs(n.entries[0].address, '192.168.0.2')
     n.sort_records()
     self.assertIs(n.entries[0].address, '192.168.0.1')
Пример #8
0
def test_rem_not_in_subnet():
    entries = [{
        "address": "1.1.1.254",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "10.0.8.1",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }]
    nc_in = NetworkCollection('1.1.1.0/24', entries)
    nc_out = NetworkCollection('1.1.1.0/24', entries)
    nc_out.remove_invalid_records()
    assert len(
        nc_in.entries) == len(nc_out.entries) + 1, "Should remove one record"
Пример #9
0
    def __init__(self, name, network_dict, security_level):
        """
        Constructor for Cluster data structure.

        self.name -> str
        self.security_level -> int
        self.networks -> list(NetworkCollection)
        """

        try:
            self.name = str(name)
        except Exception:
            raise TypeError(
                f"Cannot convert 'name' argument {name} for Cluster to "
                f"str")

        self.networks = []
        if isinstance(network_dict, dict):
            for ipv4_prefix, network_list in network_dict.items():
                try:
                    self.networks.append(
                        NetworkCollection(ipv4_network=ipv4_prefix,
                                          raw_entry_list=network_list))
                except Exception as e:
                    logger.error(str(e))
        else:
            raise TypeError(f"Invalid 'network_dict' argument for Cluster.  "
                            f"Expected 'dict', got {type(network_dict)}")

        # ToDo: bool is subclass of int; isinstance(bool) -> int
        if isinstance(security_level, int):
            self.security_level = security_level
        else:
            raise TypeError(f"Invalid 'security_level' argument for Cluster.  "
                            f"Expected 'int', got {type(security_level)}")
def two_entries_collection():
    yield NetworkCollection("192.168.0.0/24", [{
        "address": "255.255.255.0",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "192.168.0.1",
        "available": False,
        "last_used": "20/12/19 17:10:01"
    }])
Пример #11
0
    def __init__(self, name, network_dict, security_level):
        """
        Constructor for Cluster data structure.

        self.name -> str
        self.security_level -> int
        self.networks -> list(NetworkCollection)
        """
        self.name = name
        self.networks = [
            NetworkCollection(network_addr, entries)
            for network_addr, entries in network_dict.items()
        ]
        self.security_level = security_level
Пример #12
0
def test_sorting_length():
    entries = [{
        "address": "1.1.1.254",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "10.0.8.1",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }, {
        "address": "1.1.1.4",
        "available": True,
        "last_used": "30/01/20 17:00:00"
    }, {
        "address": "5.5.8.1",
        "available": False,
        "last_used": "30/01/20 16:00:00"
    }]
    nc_in = NetworkCollection('1.1.1.0/24', entries)
    nc_sorted = NetworkCollection('1.1.1.0/24', entries)
    nc_sorted.sort_records()
    assert len(nc_in.entries) == len(
        nc_sorted.entries), "Should be equal to original"
Пример #13
0
    def __init__(self, name, network_dict, security_level):
        """
        Constructor for Cluster data structure.

        self.name -> str
        self.security_level -> int
        self.networks -> list(NetworkCollection)
        """

        self.name = name
        self.security_level = int(security_level)
        self.networks = []
        for key, value in network_dict.items():
            self.networks.append(NetworkCollection(key, value))
 def test_invalid_formats(self):
     with self.assertLogs() as cm:
         n = NetworkCollection('192.168.0.0/24', [
             {
                 'address': '192.168.0.400',
                 'available': False,
                 'last_used': "30/01/20 17:00:00"
             },
             {
                 'address': '192.168.400',
                 'available': False,
                 'last_used': "30/01/20 17:00:00"
             },
             {
                 'address': 'invalid',
                 'available': False,
                 'last_used': "30/01/20 17:00:00"
             },
         ])
         self.assertIsInstance(n, NetworkCollection)
         n.remove_invalid_records()
         self.assertIs(len(n.entries), 0)
     self.assertEqual(len(cm.output), 3)
     self.assertRegex(cm.output[0], r'invalid')
Пример #15
0
def verify_method(file, func):
    net_col = [
        NetworkCollection(key, value)
        for key, value in read_collection(file + ".json").items()
    ]
    output = read_expected_results(file + ".txt")
    if output:
        if len(net_col) == len(output):
            for i in range(len(net_col)):
                getattr(net_col[i], func)()
                if output[i] != str(net_col[i]):
                    return False, "Network collection no. {} in {} has a different output than " \
                                  "xpected!".format(i+1, file)
        else:
            return False, "In {}.txt we have {} collection(s), while in {}.json we have {} " \
                          "collection(s)!".format(file, len(output), file, len(net_col))
    else:
        return False, "Missing {}.txt file from resources!".format(file)
    return True, "ok"
def test_network_collection_inits_from_json_example(response_json_example):
    # GIVEN some data from the example json
    some_networks_data = response_json_example["Berlin"]["BER-1"]["networks"]

    # WHEN creating network collection instances based on these entries
    network_collections = [
        NetworkCollection(network, entries)
        for network, entries in some_networks_data.items()
    ]

    # THEN all netowrk collections are valid
    assert len(network_collections) == 2
    assert all([
        isinstance(network_collection.ipv4_network, IPv4Network)
        for network_collection in network_collections
    ])
    assert all([
        isinstance(network_collection.entries, list) and all(
            [isinstance(entry, Entry) for entry in network_collection.entries])
        for network_collection in network_collections
    ])
def test_network_collection_inits_network_ip():
    # WHEN creating a NetworkCollection instance
    network_collection = NetworkCollection("192.168.0.0/24", [])

    # THEN the ipv4_network field is a valid ip field
    assert isinstance(network_collection.ipv4_network, IPv4Network)