def generate_data(self, data): model = RackServerModelFactory() return { 'device_1': Device.get_or_create_by_mac( '02:42:ac:11:ff:ff', parent=data['deprecated_dc']['a'], dc=data['deprecated_dc']['a'].name, rack=data['deprecated_racks']['a'].name, service=data['services']['backup_systems'], device_environment=data['envs']['prod'], model=model, )[0], 'device_2': DeviceFactory( parent=data['deprecated_dc']['b'], dc=data['deprecated_dc']['b'].name, rack=data['deprecated_racks']['b'].name, service=data['services']['databases'], device_environment=data['envs']['prod'], model=model, ), 'device_3': DeviceFactory( parent=data['deprecated_dc']['b'], dc=data['deprecated_dc']['b'].name, rack=data['deprecated_racks']['b'].name, service=data['services']['load_balancing'], device_environment=data['envs']['prod'], model=DeviceModelFactory(name='F5', type=DeviceType.load_balancer), ), }
def test_change_service_state(self): """Change Service state from active to another state when Service have connected Devices""" data = json.dumps({'state': CI_STATE_TYPES.INACTIVE.id}) service = ServiceCatalogFactory(state=CI_STATE_TYPES.ACTIVE.id) device = DeviceFactory(service=service) response = self.patch( '/api/v0.9/ci/{}/'.format(service.id), data, CONTENT_TYPE='application/json', ) self.assertEqual( json.loads(response.content)['ci']['state'], 'You can not change state because this service has linked devices.', ) device.service = None device.save() response = self.patch( '/api/v0.9/ci/{}/'.format(service.id), data, CONTENT_TYPE='application/json', ) chenged_service = CI.objects.get(id=service.id) self.assertEqual(chenged_service.state, CI_STATE_TYPES.INACTIVE.id)
def test_change_service_state(self): """Change Service state from active to another state when Service have connected Devices""" service = ServiceCatalogFactory(state=CI_STATE_TYPES.ACTIVE.id) device = DeviceFactory(service=service) data = { 'base-state': CI_STATE_TYPES.INACTIVE.id, 'base-status': service.status, 'base-layers': 1, 'base-name': service.name, 'base-type': CI_TYPES.SERVICE.id, } url = reverse('ci_edit', args=(service.id,)) response = self.client.post(url, data, follow=True) msg = response.context['form'].errors['state'][0] self.assertTrue('You can not change state to INACTIVE' in msg) device.service = None device.save() url = reverse('ci_edit', args=(service.id,)) response = self.client.post(url, data, follow=True) self.assertEqual(response.context['form'].errors, {})
def test_change_service_state(self): """Change Service state from active to another state when Service have connected Devices""" data = json.dumps({'state': CI_STATE_TYPES.INACTIVE.id}) service = ServiceCatalogFactory(state=CI_STATE_TYPES.ACTIVE.id) device = DeviceFactory(service=service) response = self.patch( '/api/v0.9/ci/{}/'.format(service.id), data, CONTENT_TYPE='application/json', ) self.assertEqual( json.loads(response.content)['ci']['state'], 'You can not change state because this service has linked devices.', ) device.service = None device.save() response = self.patch( '/api/v0.9/ci/{}/'.format(service.id), data, CONTENT_TYPE='application/json', ) chenged_service = CI.objects.get(id=service.id) self.assertEqual(chenged_service.state, CI_STATE_TYPES.INACTIVE.id)
def test_migrate(self): """Resetting the management_ip should migrate the data from legacy way to the preferred way""" dev = DeviceFactory() dev.management = IPAddressFactory(is_management=True) dev.management_ip = dev.management_ip self.assertEqual(dev.ipaddress_set.all()[0], dev.management_ip) self.assertIsNone(dev.management)
def test_migrate(self): """Resetting the management_ip should migrate the data from legacy way to the preferred way""" dev = DeviceFactory() dev.management = IPAddressFactory(is_management=True) dev.management_ip = dev.management_ip self.assertEqual(dev.ipaddress_set.all()[0], dev.management_ip) self.assertIsNone(dev.management)
def test_set_tuple(self): """Setting the management_ip by tuple""" dev = DeviceFactory() data = ('hostname.dc1', '10.1.2.3') dev.management_ip = data self.assertEqual( (dev.management_ip.hostname, dev.management_ip.address), data, )
def test_set_tuple(self): """Setting the management_ip by tuple""" dev = DeviceFactory() data = ('hostname.dc1', '10.1.2.3') dev.management_ip = data self.assertEqual( (dev.management_ip.hostname, dev.management_ip.address), data, )
def setUp(self): sample_role = VentureRoleFactory() RolePropertyFactory( symbol='my_custom_property_1', type=RolePropertyTypeFactory(symbol='STRING'), role=sample_role, ) self.sample_device = DeviceFactory(venture=sample_role.venture, venture_role=sample_role) self.sample_user = UserFactory()
def test_orientation_property(self): dev_1 = DeviceFactory(name='h101.dc') dev_2 = DeviceFactory(name='h101.dc') DCAssetFactory(device_info=DeviceInfoFactory( ralph_device_id=dev_2.id, orientation=Orientation.middle.id, ), ) self.assertEqual(dev_1.orientation, '') self.assertEqual(dev_2.orientation, 'middle') with self.assertRaises(AttributeError): dev_2.orientation = Orientation.back.id
def test_orientation_property(self): dev_1 = DeviceFactory(name='h101.dc') dev_2 = DeviceFactory(name='h101.dc') DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=dev_2.id, orientation=Orientation.middle.id, ), ) self.assertEqual(dev_1.orientation, '') self.assertEqual(dev_2.orientation, 'middle') with self.assertRaises(AttributeError): dev_2.orientation = Orientation.back.id
def setUp(self): self.client = login_as_su() self.device = DeviceFactory() self.url = reverse( 'admin:discovery_device_change', args=(self.device.id, ), )
def test_position_edit_blocking(self): url = reverse( 'racks', kwargs={ 'rack': '-', 'details': 'add_device', 'device': '', }, ) dev = DeviceFactory() asset = DCAssetFactory(device_info=DeviceInfoFactory( ralph_device_id=dev.id, ), ) post_data = { 'asset': asset.id, } response = self.client.post(url, post_data, follow=True) self.assertFalse(response.context['form'].is_valid()) from ralph.ui.widgets import ReadOnlyWidget self.assertTrue( isinstance( response.context['form'].fields['chassis_position'].widget, ReadOnlyWidget, ), ) self.assertTrue( isinstance( response.context['form'].fields['position'].widget, ReadOnlyWidget, ), )
def setUp(self): self.sample_dev = DeviceFactory() self.sample_ip_1 = IPAddressFactory( address="127.0.0.1", device=self.sample_dev, ) self.sample_ip_2 = IPAddressFactory(address="127.0.0.2")
def test_when_device_does_not_exist_and_there_is_no_asset_identity(self): device = DeviceFactory() util._create_device = lambda x: device util.create_deployments([self.data], self.user, self.mass_deployment) deployments = Deployment.objects.all() self.assertEqual(deployments.count(), 1) self.assertEqual(deployments[0].device, device)
def test_model_is_empty(self): device = DeviceFactory() url = reverse( 'search', kwargs={'device': device.id, 'details': 'info'}, ) self.assertEqual(device.model, None) response = self.client.get(url) self.assertEqual(response.status_code, 200)
def setUp(self): sample_role = VentureRoleFactory() RolePropertyFactory( symbol='my_custom_property_1', type=RolePropertyTypeFactory(symbol='STRING'), role=sample_role, ) self.sample_device = DeviceFactory( venture=sample_role.venture, venture_role=sample_role) self.sample_user = UserFactory()
class DeviceSignalTest(TestCase): """Device should send a signal when created.""" def setUp(self): self.dev = DeviceFactory(service=ServiceCatalogFactory()) def test_signal_sent(self): with mock_signal_receiver(fields_synced_signal) as rec: old_name = self.dev.name author = UserFactory() old_service = self.dev.service service = ServiceCatalogFactory() self.dev.service = service self.dev.save(user=author) rec.assert_called_with( signal=mock.ANY, sender=self.dev, changes=[ChangeTuple('service', old_service, service)], change_author=author, )
class DevicePropertiesTest(TestCase): def setUp(self): sample_role = VentureRoleFactory() RolePropertyFactory( symbol='my_custom_property_1', type=RolePropertyTypeFactory(symbol='STRING'), role=sample_role, ) self.sample_device = DeviceFactory(venture=sample_role.venture, venture_role=sample_role) self.sample_user = UserFactory() def test_successful_save(self): self.sample_device.set_property( symbol='my_custom_property_1', value='Test 123', user=self.sample_user, ) self.assertEqual(self.sample_device.get_property_set(), {'my_custom_property_1': 'Test 123'})
class DeviceSignalTest(TestCase): """Device should send a signal when created.""" def setUp(self): self.dev = DeviceFactory(service=ServiceCatalogFactory()) def test_signal_sent(self): with mock_signal_receiver(fields_synced_signal) as rec: old_name = self.dev.name author = UserFactory() old_service = self.dev.service service = ServiceCatalogFactory() self.dev.service = service self.dev.save(user=author) rec.assert_called_with( signal=mock.ANY, sender=self.dev, changes=[ChangeTuple('service', old_service, service)], change_author=author, )
def setUp(self): self.sample_dev = DeviceFactory() self.sample_ip_1 = IPAddressFactory( address="10.0.0.1", device=self.sample_dev, hostname="dev1.dc", ) self.sample_ip_2 = IPAddressFactory( address="10.0.0.2", hostname='dev2.dc', )
def test_asset_found_by_device_hostname(self): hostname = 'blade-408-1-sw1.dc4.local' device = DeviceFactory(name=hostname) DCAssetFactory(device_info__ralph_device_id=device.id) base_url = self._generate_url( 'ralph_assets.models', 'LinkedDeviceNameLookup', ) self._check_lookup_count(base_url, searched_term=hostname, expected_count=1)
def generate_data(self, data): model = DeviceModel.objects.create(name='Generic rack', type=DeviceType.rack) return { 'a': DeviceFactory.create( model=model, sn='S4A2FD39I', ), 'b': DeviceFactory.create( model=model, sn='D8K9OKD7K', ), 'c': DeviceFactory.create( model=model, sn='FI8LJ0DA4', ), 'd': DeviceFactory.create( model=model, sn='U798DA32D', ), }
def setUp(self): self.device = DeviceFactory() self.regular_ipaddr = IPAddressFactory() self.management_ipaddr = IPAddressFactory(is_management=True) self.device.ipaddress_set.add(self.regular_ipaddr) self.device.ipaddress_set.add(self.management_ipaddr) self.url = reverse('search', kwargs={ 'details': 'addresses', 'device': str(self.device.pk), }) self.client = login_as_su()
class DevicePropertiesTest(TestCase): def setUp(self): sample_role = VentureRoleFactory() RolePropertyFactory( symbol='my_custom_property_1', type=RolePropertyTypeFactory(symbol='STRING'), role=sample_role, ) self.sample_device = DeviceFactory( venture=sample_role.venture, venture_role=sample_role) self.sample_user = UserFactory() def test_successful_save(self): self.sample_device.set_property( symbol='my_custom_property_1', value='Test 123', user=self.sample_user, ) self.assertEqual( self.sample_device.get_property_set(), {'my_custom_property_1': 'Test 123'} )
def test_when_device_does_not_exist_and_asset_identity_is_given(self): device = DeviceFactory() device_info = DeviceInfoFactory(ralph_device_id=device.id) asset = AssetFactory( barcode="testbarcode", device_info=device_info, ) self.data.update({"asset_identity": asset.barcode}) util.create_deployments([self.data], self.user, self.mass_deployment) deployments = Deployment.objects.all() self.assertEqual(deployments.count(), 1) self.assertEqual(deployments[0].device, device)
def generate_data(self, data): model = DeviceModel.objects.create( name='Generic rack', type=DeviceType.rack ) return { 'a': DeviceFactory.create( model=model, sn='S4A2FD39I', ), 'b': DeviceFactory.create( model=model, sn='D8K9OKD7K', ), 'c': DeviceFactory.create( model=model, sn='FI8LJ0DA4', ), 'd': DeviceFactory.create( model=model, sn='U798DA32D', ), }
def generate_data(self, data): from itertools import cycle services = cycle([ data['services']['backup_systems'], data['services']['databases'] ]) envs = cycle(list(data['envs'].values())) model = DeviceModel.objects.create(name='XEN', type=DeviceType.virtual_server) result = {} for i in range(5): result['virtual_{}'.format(i)] = DeviceFactory( model=model, service=services.next(), device_environment=envs.next(), parent=data['devices']['device_1'], name='XEN-{}'.format(i), ) return result
def setUp(self): super(TestChangeIPAddress, self).setUp() self.user = create_user() self.sample_dev = DeviceFactory() self.sample_ip_1 = IPAddressFactory( address="10.0.1.1", device=self.sample_dev, hostname="d001.dc", ) self.sample_dhcp_entry_1 = DHCPEntryFactory( ip="10.0.1.1", mac="aa:cc:bb:11:22:33", ) self.sample_dns_domain = DNSDomainFactory(name='dc') self.sample_dns_record = DNSRecordFactory( name='d001.dc', type='A', content='10.0.1.1', domain=self.sample_dns_domain, )
def generate_data(self, data): from uuid import uuid1 from itertools import cycle services = cycle([ data['services']['backup_systems'], data['services']['databases'] ]) envs = cycle(list(data['envs'].values())) model = DeviceModel.objects.create(name='OpenStack Juno Tenant', type=DeviceType.cloud_server) result = {} for i in range(5): result['tenant_{}'.format(i)] = DeviceFactory( model=model, service=services.next(), device_environment=envs.next(), sn='openstack-{}'.format(uuid1()), name='Tenant-{}'.format(i), ) return result
def test_set_string(self): """Setting the management_ip by string""" dev = DeviceFactory() dev.management_ip = '10.1.2.3' self.assertEqual(dev.management_ip.address, '10.1.2.3')
def test_set_string(self): """Setting the management_ip by string""" dev = DeviceFactory() dev.management_ip = '10.1.2.3' self.assertEqual(dev.management_ip.address, '10.1.2.3')
def test_dc_asset_with_linked_device(self): core_device = DeviceFactory() device_info = DeviceInfoFactory(ralph_device_id=core_device.id) asset = DCAssetFactory(device_info=device_info) self.assertEqual(asset.linked_device, core_device)
def setUp(self): self.dev = DeviceFactory(service=ServiceCatalogFactory())
def test_preferred(self): """Preferred way of setting management_ip is readable.""" dev = DeviceFactory() management = IPAddressFactory(is_management=True) dev.ipaddress_set.add(management) self.assertEqual(management, dev.management_ip)
def test_legacy(self): """Legacy way of setting management_ip is readable.""" dev = DeviceFactory() dev.management = IPAddressFactory(is_management=True) self.assertEqual(dev.management, dev.management_ip)
def test_mass_deployment(self): ip = '10.80.80.101' self.test_prepare_mass_deployment() ip_address = IPAddressFactory() device_model = DeviceModelFactory( type=DeviceType.rack, name="testrack", ) device = DeviceFactory( name="testdevice", model=device_model, sn="testsn", ) device.ipaddress.add(ip_address) ethernet = EthernetFactory.create( mac="0022b0000000", device=device, ) self.network.racks.add(device) mass_deployment = MassDeployment.objects.all()[0] dns_domain = DNSDomainFactory(name='dc') dns_record = DNSRecordFactory( name='d001.dc', type='A', content=ip_address.address, domain=dns_domain, ) url = reverse( 'mass_deploy', kwargs={"deployment": mass_deployment.id}, ) csv = ('{0}; {1}; {2}; ' '{3}; 10.80.80.102; {4}; {5} ; {6} ; {7} ; {8} ; {9}'.format( dns_record.name, ip, device.sn, ethernet.mac, self.network.name, self.venture_role.venture.symbol.upper(), self.venture_role.name, self.service.name, self.device_environment.name, self.preboot.name, )) response = self.client.post(url, { 'csv': csv, }) self._base_check_for_mass_deployment(response, 'generated_csv', csv) deployment = Deployment.objects.get(device=device) self.assertEqual(deployment.venture, self.venture_role.venture) self.assertEqual(deployment.venture_role, self.venture_role) self.assertEqual(deployment.service, self.service) self.assertEqual( deployment.device_environment, self.device_environment, ) self.assertEqual(deployment.preboot, self.preboot) self.assertEqual(deployment.ip, ip) self.assertEqual( deployment.mac, MACAddressField.normalize(ethernet.mac), )
def setUp(self): # core side self.dc_model = DeviceModelFactory( name="DC", type=DeviceType.data_center, ) self.rack_model = DeviceModelFactory( name="Rack", type=DeviceType.rack, ) self.dc_1 = DeviceFactory(name='DC1', sn='DC1', model=self.dc_model) self.dc_2 = DeviceFactory(name='DC2', sn='DC2', model=self.dc_model) self.rack_1_1 = DeviceFactory( name='Rack 1 DC1', sn='Rack 1 DC1', model=self.rack_model, parent=self.dc_1, ) self.rack_1_2 = DeviceFactory( name='Rack 2 DC1', sn='Rack 2 DC1', model=self.rack_model, parent=self.dc_1, ) self.rack_2_1 = DeviceFactory( name='Rack 1 DC2', sn='Rack 1 DC2', model=self.rack_model, parent=self.dc_1, ) self.rack_2_2 = DeviceFactory( name='Rack 2 DC2', sn='Rack 2 DC2', model=self.rack_model, parent=self.dc_2, ) self.dev_1 = DeviceFactory(name="h101.dc1", parent=self.rack_1_1) self.dev_2 = DeviceFactory(name="h201.dc1", parent=self.rack_1_2) self.dev_3 = DeviceFactory(name="h101.dc2", parent=self.rack_2_1) self.dev_4 = DeviceFactory(name="h201.dc2", parent=self.rack_2_2) self.dev_5 = DeviceFactory(name="h201-1.dc2", parent=self.dev_4) # assets side self.assets_dc_1 = DataCenterFactory( name='DC1', deprecated_ralph_dc_id=self.dc_1.id, ) self.assets_dc_2 = DataCenterFactory( name='DC2', deprecated_ralph_dc_id=self.dc_2.id, ) self.assets_sr_1 = ServerRoomFactory( name="DC1_1", data_center=self.assets_dc_1, ) self.assets_sr_2 = ServerRoomFactory( name="DC2_1", data_center=self.assets_dc_2, ) self.assets_rack_1_1 = RackFactory( name="Rack 1 DC1", deprecated_ralph_rack_id=self.rack_1_1.id, data_center=self.assets_dc_1, ) self.assets_rack_1_2 = RackFactory( name="Rack 2 DC1", deprecated_ralph_rack_id=self.rack_1_2.id, data_center=self.assets_dc_1, ) self.assets_rack_2_1 = RackFactory( name="Rack 1 DC2", deprecated_ralph_rack_id=self.rack_2_1.id, data_center=self.assets_dc_2, ) self.assets_rack_2_2 = RackFactory( name="Rack 2 DC2", deprecated_ralph_rack_id=self.rack_2_2.id, data_center=self.assets_dc_2, ) self.assets_dev_1 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_1.id, data_center=self.assets_dc_1, server_room=self.assets_sr_1, rack=self.assets_rack_1_1, ), ) self.assets_dev_2 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_2.id, data_center=self.assets_dc_1, server_room=self.assets_sr_1, rack=self.assets_rack_1_2, ) ) self.assets_dev_3 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_3.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=self.assets_rack_2_1, ) ) self.assets_dev_4 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_4.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=self.assets_rack_2_2, position=10, orientation=Orientation.front, ) ) self.assets_dev_5 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_5.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=self.assets_rack_2_2, position=10, orientation=Orientation.front, slot_no=1, ) )
def test_legacy(self): """Legacy way of setting management_ip is readable.""" dev = DeviceFactory() dev.management = IPAddressFactory(is_management=True) self.assertEqual(dev.management, dev.management_ip)
class AssetDevInfoPostSaveTest(TestCase): def setUp(self): # core side self.dc_model = DeviceModelFactory( name="DC", type=DeviceType.data_center, ) self.rack_model = DeviceModelFactory( name="Rack", type=DeviceType.rack, ) self.dc_1 = DeviceFactory(name='DC1', sn='DC1', model=self.dc_model) self.dc_2 = DeviceFactory(name='DC2', sn='DC2', model=self.dc_model) self.rack_1_1 = DeviceFactory( name='Rack 1 DC1', sn='Rack 1 DC1', model=self.rack_model, parent=self.dc_1, ) self.rack_1_2 = DeviceFactory( name='Rack 2 DC1', sn='Rack 2 DC1', model=self.rack_model, parent=self.dc_1, ) self.rack_2_1 = DeviceFactory( name='Rack 1 DC2', sn='Rack 1 DC2', model=self.rack_model, parent=self.dc_1, ) self.rack_2_2 = DeviceFactory( name='Rack 2 DC2', sn='Rack 2 DC2', model=self.rack_model, parent=self.dc_2, ) self.dev_1 = DeviceFactory(name="h101.dc1", parent=self.rack_1_1) self.dev_2 = DeviceFactory(name="h201.dc1", parent=self.rack_1_2) self.dev_3 = DeviceFactory(name="h101.dc2", parent=self.rack_2_1) self.dev_4 = DeviceFactory(name="h201.dc2", parent=self.rack_2_2) self.dev_5 = DeviceFactory(name="h201-1.dc2", parent=self.dev_4) # assets side self.assets_dc_1 = DataCenterFactory( name='DC1', deprecated_ralph_dc_id=self.dc_1.id, ) self.assets_dc_2 = DataCenterFactory( name='DC2', deprecated_ralph_dc_id=self.dc_2.id, ) self.assets_sr_1 = ServerRoomFactory( name="DC1_1", data_center=self.assets_dc_1, ) self.assets_sr_2 = ServerRoomFactory( name="DC2_1", data_center=self.assets_dc_2, ) self.assets_rack_1_1 = RackFactory( name="Rack 1 DC1", deprecated_ralph_rack_id=self.rack_1_1.id, data_center=self.assets_dc_1, ) self.assets_rack_1_2 = RackFactory( name="Rack 2 DC1", deprecated_ralph_rack_id=self.rack_1_2.id, data_center=self.assets_dc_1, ) self.assets_rack_2_1 = RackFactory( name="Rack 1 DC2", deprecated_ralph_rack_id=self.rack_2_1.id, data_center=self.assets_dc_2, ) self.assets_rack_2_2 = RackFactory( name="Rack 2 DC2", deprecated_ralph_rack_id=self.rack_2_2.id, data_center=self.assets_dc_2, ) self.assets_dev_1 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_1.id, data_center=self.assets_dc_1, server_room=self.assets_sr_1, rack=self.assets_rack_1_1, ), ) self.assets_dev_2 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_2.id, data_center=self.assets_dc_1, server_room=self.assets_sr_1, rack=self.assets_rack_1_2, ) ) self.assets_dev_3 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_3.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=self.assets_rack_2_1, ) ) self.assets_dev_4 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_4.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=self.assets_rack_2_2, position=10, orientation=Orientation.front, ) ) self.assets_dev_5 = DCAssetFactory( device_info=DeviceInfoFactory( ralph_device_id=self.dev_5.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=self.assets_rack_2_2, position=10, orientation=Orientation.front, slot_no=1, ) ) def test_update_cached_localization(self): _update_cached_localization( device=self.dev_1, asset_dev_info=self.assets_dev_1.device_info, ) self.assertEqual(self.dev_1.rack, 'Rack 1 DC1') self.assertEqual(self.dev_1.dc, 'DC1') _update_cached_localization( device=self.dev_3, asset_dev_info=self.assets_dev_3.device_info, ) self.assertEqual(self.dev_3.rack, 'Rack 1 DC2') self.assertEqual(self.dev_3.dc, 'DC2') def test_update_localization(self): # case: device_info without deprecated_ralph_rack rack = RackFactory( name="Rack 4 DC2", data_center=self.assets_dc_2, ) old_device_info = self.assets_dev_2.device_info self.assets_dev_2.device_info = None self.assets_dev_2.save() old_device_info.delete() device_info = DeviceInfoFactory( ralph_device_id=self.dev_2.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=rack, ) self.assets_dev_2.device_info = device_info self.assets_dev_2.save() _update_localization(device=self.dev_2, asset_dev_info=device_info) self.assertEqual(self.dev_2.parent_id, self.rack_1_2.id) # case: rack and dc changed rack = RackFactory( name="Rack 5 DC2", data_center=self.assets_dc_2, deprecated_ralph_rack_id=self.rack_2_2.id ) old_device_info = self.assets_dev_2.device_info self.assets_dev_2.device_info = None self.assets_dev_2.save() old_device_info.delete() device_info = DeviceInfoFactory( ralph_device_id=self.dev_2.id, data_center=self.assets_dc_2, server_room=self.assets_sr_2, rack=rack, ) self.assets_dev_2.device_info = device_info self.assets_dev_2.save() _update_localization(device=self.dev_2, asset_dev_info=device_info) self.assertEqual(self.dev_2.parent_id, self.rack_2_2.id) self.assertEqual(self.dev_2.parent.parent_id, self.dc_2.id) def test_update_localization_details(self): _update_localization_details( device=self.dev_5, asset_dev_info=self.assets_dev_5.device_info, ) self.assertEqual(self.dev_5.chassis_position, 10) self.assertEqual(self.dev_5.position, 1) def test_get_core_parent(self): # normal device parent, is_blade_system = _get_core_parent( self.assets_dev_4.device_info, ) self.assertEqual(parent.id, self.rack_2_2.id) self.assertFalse(is_blade_system) # blade server self.assets_dev_5.model.category.is_blade = True self.assets_dev_5.model.category.save() parent, is_blade_system = _get_core_parent( self.assets_dev_5.device_info, ) self.assertEqual(parent.id, self.dev_4.id) self.assertTrue(is_blade_system) # asset without rack self.assets_dev_2.device_info.rack = None self.assets_dev_2.device_info.save() self.assertEqual( _get_core_parent(self.assets_dev_2.device_info), (None, False), ) @patch('ralph_assets.models_signals._update_localization_details') @patch('ralph_assets.models_signals._update_localization') @patch('ralph_assets.models_signals._update_cached_localization') def test_update_core_localization( self, mock_update_cached_localization, mock_update_localization, mock_update_localization_details, ): # case: ralph device doesn't exist self.dev_1.delete() update_core_localization( asset_dev_info=self.assets_dev_1.device_info, ) self.assertFalse(mock_update_cached_localization.called) self.assertFalse(mock_update_localization.called) self.assertFalse(mock_update_localization_details.called) # case: ralph device exists update_core_localization( asset_dev_info=self.assets_dev_2.device_info, ) self.assertTrue(mock_update_cached_localization.called) self.assertTrue(mock_update_localization.called) self.assertTrue(mock_update_localization_details.called) @patch('ralph_assets.models_signals.update_core_localization') def test_asset_device_info_post_save(self, mock): asset_device_info_post_save( sender=DeviceInfo, instance=self.assets_dev_2.device_info, ) mock.assert_called_with(asset_dev_info=self.assets_dev_2.device_info)
def setUp(self): self.dev = DeviceFactory(service=ServiceCatalogFactory())
def setUp(self): self.client = login_as_su() self.device = DeviceFactory() self.url = reverse('admin:discovery_ipaddress_add')