def test_duplicate_hostname_in_different_networks_in_same_dc_should_not_pass( self): # noqa name = 'random.hostname.net' network = NetworkFactory( address='192.168.0.0/24', network_environment=NetworkEnvironmentFactory( data_center=DataCenterFactory(name='DC1'))) network1 = NetworkFactory( address='1.1.0.0/24', network_environment=NetworkEnvironmentFactory( data_center=DataCenterFactory(name='DC1'))) ip = IPAddressFactory(hostname=name, address='192.168.0.1', dhcp_expose=True) self.ip.hostname = name self.ip.address = '1.1.0.2' self.ip.dhcp_expose = True with self.assertRaises( ValidationError, msg='Hostname "{hostname}" is already exposed in DHCP in {dc}.' .format(hostname=name, dc=network.network_environment.data_center)): self.ip.validate_hostname_uniqueness_in_dc(name) with self.assertRaises( ValidationError, msg='Hostname "{hostname}" is already exposed in DHCP in {dc}.' .format(hostname=name, dc=network1.network_environment.data_center)): self.ip.clean()
def setUpClass(cls): super().setUpClass() cls.hypervisor = DataCenterAssetFactory() cls.rack = RackFactory() cls.net_env = NetworkEnvironmentFactory( hostname_template_prefix='server_1', hostname_template_postfix='.mydc.net', hostname_template_counter_length=5, ) cls.net_env_2 = NetworkEnvironmentFactory( hostname_template_prefix='server_2', hostname_template_postfix='.mydc2.net', hostname_template_counter_length=5, ) cls.net_env_3 = NetworkEnvironmentFactory( hostname_template_prefix='server_3', hostname_template_postfix='.mydc3.net', hostname_template_counter_length=5, ) cls.net = NetworkFactory( network_environment=cls.net_env, address='10.20.30.0/24', # reserve 10.20.30.1, 10.20.30.2, 10.20.30.3, 10.20.30.4, 10.20.30.5 reserved_from_beginning=5) cls.net_2 = NetworkFactory( network_environment=cls.net_env_2, address='11.20.30.0/24', ) cls.net.racks.add(cls.rack) cls.net_2.racks.add(cls.rack)
def setUp(self): from ralph.networks.tests.factories import NetworkFactory self.factory = RequestFactory() self.request = self.factory.get('/') self.private_network = NetworkFactory(name='private_network', address='10.0.0.0/8') self.public_network = NetworkFactory(name='public_network', address='5.0.0.0/21')
def setUp(self): super(NetworkFiltersTestCase, self).setUp() self.networks = (NetworkFactory(address='10.42.42.0/24'), NetworkFactory(address='10.42.0.0/16'), NetworkFactory(address='10.42.42.42/32'), NetworkFactory(address='10.43.42.0/24'), NetworkFactory(address='2001:db8:1234::/48'))
def test_get_available_networks_is_broadcasted_in_dhcp(self): self._prepare_rack(self.dc_asset, '192.168.1.1', '192.168.1.0/24') self.net3 = NetworkFactory(address='192.168.3.0/24', dhcp_broadcast=True) self.assertCountEqual( self.dc_asset._get_available_networks(is_broadcasted_in_dhcp=True), [self.net, self.net2])
def test_get_last_modified_should_return_ip_modified(self): network = NetworkFactory(address='192.168.1.0/24') ip = IPAddressFactory(address='192.168.1.2') returned = self.view.get_last_modified( Network.objects.filter(id__in=[network.id])) self.assertEqual(returned.strftime("%Y-%m-%d %H:%M:%S"), ip.modified.strftime("%Y-%m-%d %H:%M:%S"))
def test_filter_duplicated_hostnames(self): network = NetworkFactory(address='192.168.1.0/24') asset = DataCenterAssetFactory() ethernet = EthernetFactory(base_object=asset) ethernet.save() IPAddressFactory(hostname='host1.mydc.net', address='192.168.1.2', ethernet=ethernet, dhcp_expose=True) ethernet2 = EthernetFactory() ethernet2.save() IPAddressFactory(hostname='host1.mydc.net', address='192.168.1.3', ethernet=ethernet2, dhcp_expose=True) ethernet3 = EthernetFactory() ethernet3.save() ip = IPAddressFactory(hostname='host2.mydc.net', address='192.168.1.4', ethernet=ethernet3, dhcp_expose=True) entries = self.view._get_dhcp_entries( Network.objects.filter(id__in=[network.id])) self.assertEqual(DHCPEntry.objects.count(), 3) self.assertEqual(len(entries), 1) self.assertEqual(entries[0].pk, ip.pk)
def test_change_network_should_not_pass(self): net = NetworkFactory(address='192.168.1.0/24') data = {'network': net.id} url = reverse('ipaddress-detail', args=(self.ip1.id, )) response = self.client.patch(url, format='json', data=data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.ip1.refresh_from_db() self.assertEqual(self.ip1.network, self.net)
def test_config_endpoint_should_return_304(self): network = NetworkFactory(address='192.168.1.0/24') url = '{}?env={}'.format(reverse('dhcp_config_entries'), network.network_environment) response = self.client.get(url, HTTP_IF_MODIFIED_SINCE=http_date( network.modified.timestamp())) self.assertEqual(response.status_code, 304)
def test_config_endpoint_should_return_200(self): get_user_model().objects.create_superuser('test', '*****@*****.**', 'test') self.client.login(username='******', password='******') network = NetworkFactory(address='192.168.1.0/24') url = '{}?env={}'.format(reverse('dhcp_config_entries'), network.network_environment) response = self.client.get(url) self.assertEqual(response.status_code, 200)
def test_duplicate_hostnames_in_different_dcs_should_pass(self): name = 'random.hostname.net' network1 = NetworkFactory( address='1.1.0.0/24', network_environment=NetworkEnvironmentFactory( data_center=DataCenterFactory(name='DC1'))) network2 = NetworkFactory( address='192.168.0.0/24', network_environment=NetworkEnvironmentFactory( data_center=DataCenterFactory(name='DC2'))) ip = IPAddressFactory(hostname=name, address='1.1.0.1', dhcp_expose=True) self.ip.address = '192.168.0.1' self.ip.hostname = name self.ip.dhcp_expose = True self.ip.validate_hostname_uniqueness_in_dc(name) self.ip.clean()
def test_ip_inside_defined_network_should_pass(self): NetworkFactory(address='10.20.30.0/24') IPAddressFactory(address='10.20.30.40', ethernet__base_object=self.instance) validate_ip_address([self.instance], { 'ip_or_network': { 'value': '__other__', '__other__': '10.20.30.40' } })
def test_check_ipaddress_unique_with_ip_assigned_to_the_same_object_should_pass(self): NetworkFactory(address='10.20.30.0/24') IPAddressFactory( address='10.20.30.40', ethernet__base_object=self.instance ) validate_ip_address( [self.instance], {'ip_or_network': { 'value': '__other__', '__other__': '10.20.30.40' }} )
def setUp(self): self.network = NetworkFactory(address='10.20.30.0/24') self.ip1 = IPAddressFactory(address='10.20.30.40') self.ip2 = IPAddressFactory(address='10.20.30.41') self.ip_other = IPAddressFactory(address='10.30.30.40') self.user = get_user_model().objects.create_superuser( username='******', password='******', email='*****@*****.**') result = self.client.login(username='******', password='******') self.assertEqual(result, True) self.factory = RequestFactory()
def _prepare_rack(self): self.rack = RackFactory() self.net_env = NetworkEnvironmentFactory( hostname_template_prefix='server_1', hostname_template_postfix='.mydc.net', ) self.net = NetworkFactory( network_environment=self.net_env, address='10.20.30.0/24', ) self.net.racks.add(self.rack) return self.net_env
def _generate_rack_with_networks(self, num_networks=5): nets = [ NetworkFactory(address='10.0.{}.0/24'.format(i)) for i in range(num_networks) ] rack = RackFactory() for net in nets: rack.network_set.add(net) return rack, nets
def test_get_last_modified_should_return_assets_ethernet_modified(self): network = NetworkFactory(address='192.168.1.0/24') asset = DataCenterAssetFactory() ethernet = EthernetFactory(base_object=asset) ethernet.save() ip = IPAddressFactory(address='192.168.1.2', base_object=asset, ethernet=ethernet) ethernet.save() returned = self.view.get_last_modified( Network.objects.filter(id__in=[network.id])) self.assertEqual(returned.strftime("%Y-%m-%d %H:%M:%S"), ethernet.modified.strftime("%Y-%m-%d %H:%M:%S"))
def _prepare_rack(self, dc_asset, address, network_address, rack=None): self.rack = rack or RackFactory() self.net_env = NetworkEnvironmentFactory( hostname_template_prefix='server_1', hostname_template_postfix='.mydc.net', ) self.net_env2 = NetworkEnvironmentFactory( hostname_template_prefix='server_2', hostname_template_postfix='.mydc.net', ) self.net = NetworkFactory( network_environment=self.net_env, address=network_address, ) self.net2 = NetworkFactory( network_environment=self.net_env2, address='10.20.30.0/24', ) self.net.racks.add(self.rack) self.net2.racks.add(self.rack) dc_asset.rack = self.rack dc_asset.save() IPAddressFactory(ethernet__base_object=self.dc_asset, address=address)
def test_should_return_only_common_networks(self): rack100 = RackFactory() rack101 = RackFactory() rack_100_net = NetworkFactory(address='10.0.100.0/24') rack_101_net = NetworkFactory(address='10.0.101.0/24') common_net = NetworkFactory(address='10.0.0.0/24') rack_100_net.racks = [rack100] rack_101_net.racks = [rack101] common_net.racks = [rack100, rack101] self.dc_asset_2.rack = rack100 self.dc_asset_3.rack = rack101 rack_100_result = assign_additional_hostname_choices( None, [self.dc_asset_2]) common_result = assign_additional_hostname_choices( None, [self.dc_asset_2, self.dc_asset_3]) expected_rack100_result = [(str(rack_100_net.pk), rack_100_net), (str(common_net.pk), common_net)] expected_common_result = [(str(common_net.pk), common_net)] self.assertEqual(rack_100_result, expected_rack100_result) self.assertEqual(len(rack_100_result), 2) self.assertEqual(common_result, expected_common_result) self.assertEqual(len(common_result), 1)
def test_dhcp_expose_for_existing_record_duplicate_hostname_should_not_pass( self): # noqa network = NetworkFactory( address='192.168.0.0/24', network_environment=NetworkEnvironmentFactory( data_center=DataCenterFactory(name='DC1'))) name = 'some.hostname' ip = IPAddressFactory(dhcp_expose=True, hostname=name, address='192.168.0.7').save() self.ip1.hostname = name self.ip1.address = '192.168.0.12' self.ip1.save() inline_data = { 'TOTAL_FORMS': 2, 'INITIAL_FORMS': 1, '0-id': self.eth1.id, '0-base_object': self.obj1.id, '0-label': 'eth10', '1-base_object': self.obj1.id, '1-hostname': name, '1-address': '192.168.0.33', '1-mac': '10:10:10:10:10:10', '1-label': 'eth10', '1-dhcp_expose': 'on', } data = { 'hostname': self.obj1.hostname, 'id': self.obj1.id, } data.update(self._prepare_inline_data(inline_data)) response = self.client.post(self.obj1.get_absolute_url(), data, follow=True) msg = 'Hostname "{hostname}" is already exposed in DHCP in {dc}'.format( # noqa hostname=self.ip1.hostname, dc=network.network_environment.data_center) self.assertIn('errors', response.context_data) self.assertTrue( any([msg in err for err in response.context_data['errors']]))
def test_dhcp_expose_for_new_record_duplicate_hostname_should_not_pass( self): # noqa network = NetworkFactory( address='127.0.0.0/24', network_environment=NetworkEnvironmentFactory( data_center=DataCenterFactory(name='DC1'))) self.test_dhcp_expose_for_new_record_should_pass( ) # generate duplicate obj2 = PolymorphicTestModel.objects.create(hostname='xyz') inline_data = { 'TOTAL_FORMS': 2, 'INITIAL_FORMS': 1, '0-id': self.eth1.id, '0-base_object': obj2.id, '0-mac': 'ff:ff:ff:ff:ff:ff', '0-label': 'eth10', '1-base_object': obj2.id, '1-hostname': self.ip1.hostname, '1-address': '127.0.0.3', '1-mac': '11:11:11:11:11:11', '1-label': 'eth10', '1-dhcp_expose': 'on', } data = { 'hostname': obj2.hostname, 'id': obj2.id, } data.update(self._prepare_inline_data(inline_data)) response = self.client.post(obj2.get_absolute_url(), data, follow=True) self.assertEqual(response.status_code, 200) msg = 'Hostname "{hostname}" is already exposed in DHCP in {dc}'.format( # noqa hostname=self.ip1.hostname, dc=network.network_environment.data_center) self.assertIn('errors', response.context_data) self.assertTrue( any([msg in err for err in response.context_data['errors']]))
def test_get_last_modified_should_run_5_queries(self): network = NetworkFactory(address='192.168.1.0/24') with self.assertNumQueries(5): self.view.get_last_modified( Network.objects.filter(id__in=[network.id]))
def setUpClass(cls): super().setUpClass() cls.net1 = NetworkFactory(address='127.0.0.0/24') cls.net2 = NetworkFactory(address='128.0.0.0/24')
def test_config_endpoint_should_return_401_when_user_is_anonymous(self): network = NetworkFactory(address='192.168.1.0/24') url = '{}?env={}'.format(reverse('dhcp_config_entries'), network.network_environment) response = self.client.get(url) self.assertEqual(response.status_code, 401)
def test_get_available_networks(self): self._prepare_rack(self.dc_asset, '192.168.1.1', '192.168.1.0/24') self.net3 = NetworkFactory(address='192.168.3.0/24') self.assertCountEqual(self.dc_asset._get_available_networks(), [self.net, self.net2])
def setUpClass(cls): super().setUpClass() cls.net = NetworkFactory(address='127.0.0.0/24') cls.eth = EthernetFactory()
def test_get_available_networks_no_rack(self): NetworkFactory(address='192.168.1.0/24') NetworkFactory(address='192.168.2.0/24') self.assertEqual(self.dc_asset._get_available_networks(), [])