class SimpleNetworkTest(RalphTestCase): def setUp(self): self.net1 = Network.objects.create( name='net1', address='192.168.0.0/16', ) self.net2 = Network.objects.create( parent=self.net1, name='net2', address='192.168.0.0/17', ) self.net3 = Network.objects.create( parent=self.net2, name='net3', address='192.168.128.0/17', ) self.net4 = Network.objects.create( parent=self.net3, name='net4', address='192.168.133.0/24', ) self.net5 = Network.objects.create( name='net5', address='192.169.133.0/24', ) self.ip1 = IPAddress(address='192.168.128.10') self.ip1.save() self.ip2 = IPAddress(address='192.168.133.10') self.ip2.save() self.ip3 = IPAddress(address='192.168.128.11') self.ip3.save() Network.objects.rebuild() @unpack @data( ('net1', 16), ('net2', 17), ) def test_get_netmask(self, net, netmask): self.assertEquals(getattr(self, net).netmask, netmask) @unpack @data( ('net1', ['net2', 'net3', 'net4']), ('net3', ['net4']), ) def test_get_subnetworks(self, net, correct): net_obj = getattr(self, net) res = Network.objects.get(pk=net_obj.pk).get_subnetworks() self.assertEquals(list(res), list(Network.objects.filter(name__in=correct))) def test_subnetworks_count_cast_to_integer(self): net = Network.objects.get(pk=self.net1.pk) count = Network.objects.filter(id=self.net1.id).annotate( subnetworks_count=(CastToInteger(F('rght')) - CastToInteger(F('lft')))).values_list( 'subnetworks_count', flat=True)[0] self.assertTrue(count)
def setUp(self): self.net1 = Network.objects.create( name='net1', address='192.168.0.0/16', ) self.net2 = Network.objects.create( parent=self.net1, name='net2', address='192.168.0.0/17', ) self.net3 = Network.objects.create( parent=self.net2, name='net3', address='192.168.128.0/17', ) self.net4 = Network.objects.create( parent=self.net3, name='net4', address='192.168.133.0/24', ) self.net5 = Network.objects.create( name='net5', address='192.169.133.0/24', ) self.ip1 = IPAddress(address='192.168.128.10') self.ip1.save() self.ip2 = IPAddress(address='192.168.133.10') self.ip2.save() self.ip3 = IPAddress(address='192.168.128.11') self.ip3.save() Network.objects.rebuild()
def ip_addresses(self, value): # value is a list (of ips) or dict (of ip:hostname pairs) # when value is a dict, set will work on keys only for ip in set(value) - set(self.ip_addresses): try: new_ip = IPAddress.objects.get(address=ip) if new_ip.base_object is None: new_ip.base_object = self new_ip.save() else: logger.warning( 'Cannot assign IP %s to %s - it is already in use by ' 'another asset', ip, self.hostname) except ObjectDoesNotExist: logger.info('Creating new IP {} for {}'.format(ip, self)) new_ip = IPAddress( ethernet=Ethernet.objects.create(base_object=self), address=ip) new_ip.save() # refresh hostnames if isinstance(value, dict): for address, hostname in value.items(): try: ip = IPAddress.objects.get(address=address) except IPAddress.DoesNotExist: logger.debug('IP {} not found'.format(address)) else: if ip.hostname != hostname: logger.info( 'Setting {} for IP {} (previous value: {})'.format( hostname, address, ip.hostname)) ip.hostname = hostname ip.save() to_delete = set(self.ip_addresses) - set(value) for ip in to_delete: logger.warning('Deleting %s from %s', ip, self) Ethernet.objects.filter(base_object=self, ipaddress__address__in=to_delete).delete()
def validate_vip_event_data(data): """Performs some basic sanity checks (e.g. missing values) on the incoming event data. Returns list of errors (if any). """ name = data['name'] ip = data['ip'] port = data['port'] protocol = data['protocol'] service = data['service'] if service: service_uid = service.get('uid') else: service_uid = None environment = data['environment'] errors = [] if not name: err = 'missing name' errors.append(err) try: IPAddress(address=ip).clean_fields() except (TypeError, ValidationError): err = 'invalid IP address "{}"'.format(ip) errors.append(err) if not port or port < 0 or port > 65535: err = 'invalid port "{}"'.format(port) errors.append(err) if not protocol: err = 'missing protocol' errors.append(err) if not service_uid: err = 'missing service UID' errors.append(err) if not environment: err = 'missing environment' errors.append(err) return errors
def test_ip_is_public_or_no(self, ip, is_public): new_ip_address = IPAddress(address=ip) new_ip_address.save() self.assertEquals(new_ip_address.is_public, is_public)