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 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 test_should_pass_non_integer_counter(self): prefix = 't4' postfix = '.dc.local' non_integer_counter = 'non-integer-counter' ne = NetworkEnvironmentFactory(hostname_template_prefix=prefix, hostname_template_postfix='.dc.local', hostname_template_counter_length=5, use_hostname_counter=True) dc_asset = DataCenterAssetFactory( hostname=''.join([prefix, non_integer_counter, postfix])) self.assertEqual(ne.current_counter_without_model(), 0)
def test_issue_next_hostname_overflow(self): alhg = AssetLastHostname.objects.create( prefix='s123', postfix='.dc.local', counter=99998, ) ne = NetworkEnvironmentFactory( hostname_template_prefix='s123', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) self.assertEqual(ne.issue_next_free_hostname(), 's12399999.dc.local') self.assertEqual(ne.issue_next_free_hostname(), 's123100000.dc.local')
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_get_next_hostname(self): ne = NetworkEnvironmentFactory( hostname_template_prefix='s123', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) # check if hostname is not increased self.assertEqual(ne.next_free_hostname, 's12300001.dc.local') self.assertEqual(ne.next_free_hostname, 's12300001.dc.local')
def test_next_hostname_without_counter(self): prefix = 'test.' postfix = '.ralph.pl' network_env = NetworkEnvironmentFactory( hostname_template_prefix=prefix, hostname_template_postfix=postfix, hostname_template_counter_length=6, use_hostname_counter=False) self.assertEqual(network_env.next_hostname_without_model_counter(), 'test.000001.ralph.pl') for i in ['000009', '000001', '000008', '000007', '000050', '000044']: DataCenterAssetFactory(hostname="".join([prefix, i, postfix])) self.assertEqual(network_env.next_hostname_without_model_counter(), 'test.000051.ralph.pl') for i in ['000019', '000011', '000098', '000053', '000444']: VirtualServerFactory(hostname="".join([prefix, i, postfix])) self.assertEqual(network_env.next_hostname_without_model_counter(), 'test.000445.ralph.pl')
def test_issue_next_hostname_when_a_few_next_hostnames_are_taken(self): hostname_model_factories = [ ClusterFactory, DataCenterAssetFactory, IPAddressFactory, VirtualServerFactory, ] for i, model_factory in enumerate(hostname_model_factories): model_factory(hostname='s1230000{}.dc.local'.format(i)) ne = NetworkEnvironmentFactory( hostname_template_prefix='s123', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) self.assertEqual( ne.issue_next_free_hostname(), 's1230000{}.dc.local'.format(len(hostname_model_factories)))
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 test_use_hostname_counter_updates_last_hostname_counter(self): prefix = 'test.' postfix = '.ralph.pl' network_env = NetworkEnvironmentFactory( hostname_template_prefix=prefix, hostname_template_postfix=postfix, hostname_template_counter_length=6, use_hostname_counter=False, ) current = 10 DataCenterAssetFactory( hostname="".join([prefix, str(current), postfix])) ok_next_hostname = 'test.{host_num:0{fill}}.ralph.pl'.format( host_num=current + 1, fill=network_env.hostname_template_counter_length, ) self.assertEqual(network_env.next_free_hostname, ok_next_hostname) network_env.use_hostname_counter = True network_env.save() self.assertEqual(network_env.next_free_hostname, ok_next_hostname)
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_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_a_dhcp_servers_wait(self): start_date = datetime.datetime(2016, 8, 5, 1, 1, 1) end_date = datetime.datetime(2016, 8, 5, 1, 1, 2) net = Network.objects.create( name='net', address='192.169.58.0/24', network_environment=NetworkEnvironmentFactory()) ip = IPAddress.objects.create(address='192.169.58.1', status=IPAddressStatus.reserved) DHCPServer.objects.create(ip='10.0.0.1', network_environment=net.network_environment, last_synchronized=end_date) kwargs = { 'shared_params': { 'dhcp_entry_created_date': start_date, 'ip_addresses': { self.instance.pk: ip } } } self.instance.__class__.wait_for_dhcp_servers([self.instance], **kwargs)
def test_get_next_hostname_considers_other_models(self): hostname_model_factories = [ ClusterFactory, DataCenterAssetFactory, IPAddressFactory, VirtualServerFactory, ] for i, model_factory in enumerate(hostname_model_factories): model_factory(hostname='s1230000{}.dc.local'.format(i)) ne = NetworkEnvironmentFactory( hostname_template_prefix='s123', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) expected_hostname = 's1230000{}.dc.local'.format( len(hostname_model_factories)) # check if hostname is not increased self.assertEqual(ne.next_free_hostname, expected_hostname) self.assertEqual(ne.next_free_hostname, expected_hostname)
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_issue_next_hostname(self): ne = NetworkEnvironmentFactory( hostname_template_prefix='s123', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) ne2 = NetworkEnvironmentFactory( # same params as ne hostname_template_prefix='s123', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) ne3 = NetworkEnvironmentFactory( # other params comparing to ne hostname_template_prefix='s1', hostname_template_postfix='.dc.local', hostname_template_counter_length=5, ) self.assertEqual(ne.issue_next_free_hostname(), 's12300001.dc.local') self.assertEqual(ne.issue_next_free_hostname(), 's12300002.dc.local') self.assertEqual(ne.issue_next_free_hostname(), 's12300003.dc.local') self.assertEqual(ne2.issue_next_free_hostname(), 's12300004.dc.local') self.assertEqual(ne.issue_next_free_hostname(), 's12300005.dc.local') self.assertEqual(ne3.issue_next_free_hostname(), 's100001.dc.local')