def test_support_requirement(self): """ - add asset a1 with required_support=True - add asset a2 with required_support=False - send request without param *required_support* - assert found: all-assets - send request with required_support=yes - assert found: 1 - send request with required_support=no - assert found: (all-assets - 1) """ DCAssetFactory(**{'required_support': True}) DCAssetFactory(**{'required_support': False}) assets_count = Asset.objects.filter( type=AssetType.data_center.id).count() self._check_results_length( self.testing_urls['dc'], 'required_support', '', assets_count, ) self._check_results_length( self.testing_urls['dc'], 'required_support', 'yes', 1, ) self._check_results_length( self.testing_urls['dc'], 'required_support', 'no', assets_count - 1, )
def test_get_vizualization_returns_none_when_no_rack(self): asset = DCAssetFactory(device_info=DeviceInfoFactory()) asset.device_info.rack = None self.assertTrue(asset.device_info.data_center) self.assertFalse(asset.device_info.rack) url = asset.get_vizualization_url() self.assertFalse(url)
def test_asset_params_cleaning(self): asset_1 = DCAssetFactory(sn='sn_123123', barcode=None) name = 'Some Name - sn_123123 - ' self.assertEqual(get_asset_by_name(name).id, asset_1.id) asset_2 = DCAssetFactory(sn=None, barcode='barcode_321321') name = 'Some Name - - barcode_321321' self.assertEqual(get_asset_by_name(name).id, asset_2.id)
def test_support_assignment(self): """ - add asset a1 without supports - add asset a2 with support s1 - send request without param *support_assigned* - assert found: all-assets - send request with support_assigned=any - assert found: 1 - send request with support_assigned=false - assert found: (all-assets - 1) """ DCAssetFactory() DCAssetFactory(**dict(supports=(supports.DCSupportFactory(), ), )) assets_count = Asset.objects.filter( type=AssetType.data_center.id).count() self._check_results_length( self.testing_urls['dc'], 'support_assigned', '', assets_count, ) self._check_results_length( self.testing_urls['dc'], 'support_assigned', 'any', 1, ) self._check_results_length( self.testing_urls['dc'], 'support_assigned', 'none', assets_count - 1, )
def test_device_environment(self): DCAssetFactory( device_environment=DeviceEnvironmentFactory(name='dc-dev-env'), ) DCAssetFactory( device_environment=DeviceEnvironmentFactory(name='dc-dev-env2'), ) BOAssetFactory( device_environment=DeviceEnvironmentFactory(name='bo-dev-env'), ) BOAssetFactory( device_environment=DeviceEnvironmentFactory(name='bo-dev-env2'), ) field_name = 'device_environment' self._check_search_result_count([ # exact check (self.testing_urls['dc'], field_name, '"dc-dev-env"', 1), (self.testing_urls['dc'], field_name, 'fake-dc', 0), (self.testing_urls['bo'], field_name, '"bo-dev-env"', 1), (self.testing_urls['bo'], field_name, 'fake-bo', 0), # icontains check (self.testing_urls['dc'], field_name, 'dc', 2), (self.testing_urls['dc'], field_name, 'dc-dev-env2', 1), (self.testing_urls['dc'], field_name, 'none', 0), (self.testing_urls['bo'], field_name, 'bo', 2), (self.testing_urls['bo'], field_name, 'bo-dev-env2', 1), (self.testing_urls['bo'], field_name, 'none', 0), ])
def test_get_vizualization_returns_none_when_no_data_center(self): asset = DCAssetFactory() asset.device_info.data_center = None asset.device_info.save() self.assertTrue(asset.device_info.rack) self.assertFalse(asset.device_info.data_center) url = asset.get_vizualization_url() self.assertTrue(url)
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 test_device_with_asset(self): # Create asset DCAssetFactory( device_info=DeviceInfoFactory(ralph_device_id=self.device.id), ) url = reverse( 'search', kwargs={ 'details': 'info', 'device': self.device.id }, ) response = self.client.get(url, follow=True) from ralph.ui.widgets import ReadOnlyWidget, RackWidget self.assertTrue( isinstance( response.context['form'].fields['dc'].widget, RackWidget, ), ) self.assertTrue( isinstance( response.context['form'].fields['rack'].widget, RackWidget, ), ) self.assertTrue( isinstance( response.context['form'].fields['chassis_position'].widget, ReadOnlyWidget, ), ) self.assertTrue( isinstance( response.context['form'].fields['position'].widget, ReadOnlyWidget, ), )
def test_basic_data_with_asset(self): asset = DCAssetFactory() asset.model.category.is_blade = False asset.model.category.save() data = { 'serial_number': 'aaa123456789', 'hostname': 'mervin', 'data_center': 'chicago', 'barcode': '00000', 'rack': 'i31', 'chassis_position': '4', 'management': '10.10.10.2', 'asset': asset, } warnings = [] set_device_data(self.device, data, warnings=warnings) self.device.save() device = Device.objects.get(sn='aaa123456789') self.assertIsNone(device.dc) self.assertIsNone(device.rack) self.assertIsNone(device.chassis_position) self.assertIsNone(device.management) self.assertEqual(warnings, [ 'You can not set data for `chassis_position` here - skipped. ' 'Use assets module.', 'You can not set data for `data_center` here - skipped. ' 'Use assets module.', 'You can not set data for `rack` here - skipped. ' 'Use assets module.', 'Management IP address (10.10.10.2) has been ignored. To ' 'change them, please use the Assets module.', ])
def test_finding_device_to_link(self): device_to_check = DeviceFactory(barcode=None, sn=None) dc_asset = DCAssetFactory(device_info=None) self.assertEqual(dc_asset.find_device_to_link(), None) self.update_device(device_to_check, 'sn', dc_asset.sn) self.assertEqual(dc_asset.find_device_to_link(), device_to_check) self.update_device(device_to_check, 'sn', None) self.update_device(device_to_check, 'barcode', dc_asset.barcode) self.assertEqual(dc_asset.find_device_to_link(), device_to_check) self.update_device(device_to_check, 'sn', dc_asset.sn) self.update_device(device_to_check, 'barcode', dc_asset.barcode) self.assertEqual(dc_asset.find_device_to_link(), device_to_check)
def test_device_with_asset_ip_addresses(self): asset = DCAssetFactory() data = { 'system_ip_addresses': [ '127.0.0.1', '127.0.0.2', ], 'management_ip_addresses': [ '127.0.0.3', '127.0.0.4', ], 'asset': asset, } warnings = [] set_device_data(self.device, data, warnings=warnings) self.device.save() device = Device.objects.get(sn='123456789') self.assertEqual( device.ipaddress_set.filter(is_management=False).count(), 2, ) self.assertEqual( device.ipaddress_set.filter(is_management=True).count(), 0, ) self.assertEqual( warnings, [ 'Management IP addresses (127.0.0.3, 127.0.0.4) have been ' 'ignored. To change them, please use the Assets module.', ], )
def test_get_asset_with_empty_asset_source(self): """Getting an asset with empty 'source' field should also succeed.""" category = AssetCategoryFactory() model = AssetModelFactory(category=category) asset = DCAssetFactory(model=model, source=None) asset_data = get_asset(asset.device_info.ralph_device_id) self.assertEqual(asset_data['source'], None)
def test_get_assets_without_environment(self): DCAssetFactory( service=None, device_environment=None, ) today = date(2013, 11, 12) result = [a for a in api_scrooge.get_assets(today)] self.assertEquals(result, [])
def setUp(self): self.ralph_device = RalphDevice.objects.create( name="Name0", ) self.device_info = DeviceInfo.objects.create( ralph_device_id=1, ) self.asset = DCAssetFactory()
def test_asset_found_by_sn(self): sn = 'test-sn' DCAssetFactory(sn=sn) base_url = self._generate_url( 'ralph_assets.models', 'LinkedDeviceNameLookup', ) self._check_lookup_count(base_url, searched_term=sn, expected_count=1)
def test_report_linked_report(self): assets = [DCAssetFactory() for _ in xrange(6)] for asset in assets: self.create_device(sn=asset.sn, barcode=asset.barcode) asset.device_info.ralph_device_id = None asset.device_info.save() report = LinkedDevicesReport() results = report.execute(None) self.assertEqual(len(results), 3)
def test_asset_found_by_barcode(self): barcode = 'test-barcode' DCAssetFactory(barcode=barcode) base_url = self._generate_url( 'ralph_assets.models', 'LinkedDeviceNameLookup', ) self._check_lookup_count(base_url, searched_term=barcode, expected_count=1)
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_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 test_get_asset_without_hostname(self): asset = DCAssetFactory() asset.device_info.ralph_device_id = None asset.device_info.save() today = date(2013, 11, 12) result = [a for a in api_scrooge.get_assets(today)] self._compare_asset( asset, result[0], today, None, )
def test_service(self): DCAssetFactory(service=ServiceCatalogFactory(name='dc-service.com')) DCAssetFactory(service=ServiceCatalogFactory(name='dc-service2.com')) BOAssetFactory(service=ServiceCatalogFactory(name='bo-service.com')) BOAssetFactory(service=ServiceCatalogFactory(name='bo-service2.com')) field_name = 'service' self._check_search_result_count([ # exact check (self.testing_urls['dc'], field_name, '"dc-service.com"', 1), (self.testing_urls['dc'], field_name, 'fake-dc', 0), (self.testing_urls['bo'], field_name, '"bo-service.com"', 1), (self.testing_urls['bo'], field_name, 'fake-bo', 0), # icontains check (self.testing_urls['dc'], field_name, 'dc', 2), (self.testing_urls['dc'], field_name, 'dc-service2', 1), (self.testing_urls['dc'], field_name, 'none', 0), (self.testing_urls['bo'], field_name, 'bo', 2), (self.testing_urls['bo'], field_name, 'bo-service2', 1), (self.testing_urls['bo'], field_name, 'none', 0), ])
def test_get_asset_by_sn_or_barcode(self): category = AssetCategoryFactory() model = AssetModelFactory(category=category) asset = DCAssetFactory(model=model, ) # by sn asset_data = get_asset_by_sn_or_barcode(asset.sn) self.assertEqual(asset_data['sn'], asset.sn) self.assertEqual(asset_data['barcode'], asset.barcode) # by barcode asset_data = get_asset_by_sn_or_barcode(asset.barcode) self.assertEqual(asset_data['sn'], asset.sn) self.assertEqual(asset_data['barcode'], asset.barcode) # not exists self.assertEqual(get_asset_by_sn_or_barcode('foo_ziew_123'), None)
def test_lookups_bo_and_dc(self): """ - user type 'Model' in some ajax-selects field - user get assets with DC and BO type """ number_of_assets = 3 for _ in xrange(number_of_assets): BOAssetFactory() DCAssetFactory() url = self._generate_url('ralph_assets.models', 'AssetLookup') response = self.client.get(url + '?term=Model') self.assertEqual(len(json.loads(response.content)), number_of_assets * 2)
def _check_fields(self, search_query, required_fields, asset_data): for field in required_fields: asset = DCAssetFactory(**asset_data) setattr(asset.device_info, field, None) asset.device_info.save() results = self._query_results( self.testing_urls['dc'], search_query, ) self.assertEqual( len(results), 1, "Asset with {} = None not match got: {}, exp: {}".format( field, len(results), 1, ) ) self.assertEqual( results[0].id, asset.id, "Found asset with id {} instead of {} for {} = None".format( results[0].id, asset.id, field, ) ) asset.delete()
def test_check_if_can_edit_position(self): # No asset in saved data. self.assertTrue(check_if_can_edit_position({'key_1': 'value_1'})) # Asset is assigned. asset = DCAssetFactory() self.assertFalse( check_if_can_edit_position({ 'key_1': 'value_1', 'asset': asset }), ) # Asset string representation - asset does not exist. self.assertTrue( check_if_can_edit_position( { 'key_1': 'value_1', 'asset': 'Name - SN - BARCODE' }, ), )
def test_get_asset(self): """Test get asset information by ralph_device_id.""" support1 = DCSupportFactory() support2 = DCSupportFactory() category = AssetCategoryFactory() model = AssetModelFactory(category=category) asset = DCAssetFactory( model=model, supports=[support1, support2], ) asset_data = get_asset(asset.device_info.ralph_device_id) self.assertEqual(asset_data['sn'], asset.sn) self.assertEqual(asset_data['barcode'], asset.barcode) self.assertEqual(asset_data['supports'][0]['name'], support1.name) self.assertEqual(asset_data['supports'][0]['url'], support1.url) self.assertEqual(asset_data['supports'][1]['name'], support2.name) self.assertEqual(asset_data['supports'][1]['url'], support2.url) self.assertEqual( asset_data['required_support'], asset.required_support, )
def test_base_fields_when_asset_is_assigned(self): """Asset has not is_blade flag -> edition is not possible""" asset = DCAssetFactory(device_info=DeviceInfoFactory( ralph_device_id=self.device.id, ), ) asset.model.category.is_blade = False asset.model.category.save() response = self.client.get(self.url, follow=True) self.assertEqual(response.status_code, 200) self.assertTrue( isinstance( response.context_data['adminform'].form.fields['dc'].widget, ReadOnlyWidget, )) self.assertTrue( isinstance( response.context_data['adminform'].form.fields['rack'].widget, ReadOnlyWidget, )) self.assertFalse( 'parent' in response.context_data['adminform'].form.fields) self.assertFalse( 'management' in response.context_data['adminform'].form.fields)
def test_hostname(self): for hostname in ("POLPC10000", "POLPC10001"): DCAssetFactory(hostname=hostname) for hostname in ("POLPC20000", "POLPC20001"): BOAssetFactory(hostname=hostname) field_name = 'hostname' self._check_search_result_count([ # exact check (self.testing_urls['dc'], field_name, '"POLPC10001"', 1), (self.testing_urls['dc'], field_name, '"POLPC1000"', 0), (self.testing_urls['bo'], field_name, '"POLPC20001"', 1), (self.testing_urls['bo'], field_name, '"POLPC2000"', 0), # multi check (self.testing_urls['dc'], field_name, 'POLPC10000;POLPC10001', 2), (self.testing_urls['bo'], field_name, 'POLPC20000;POLPC20001', 2), # icontains check (self.testing_urls['dc'], field_name, 'POLPC1', 2), (self.testing_urls['dc'], field_name, '10001', 1), (self.testing_urls['dc'], field_name, 'none', 0), (self.testing_urls['bo'], field_name, 'POLPC2', 2), (self.testing_urls['bo'], field_name, '20001', 1), (self.testing_urls['bo'], field_name, 'none', 0), ])
def test_get_configuration_url_returns_none(self): asset = DCAssetFactory(device_info=None) url = asset.get_configuration_url() self.assertFalse(url)
def generate_data(self, data): blade_location = 3 service_name = Service.objects.create(name='Office infrastructure') warehouse = Warehouse.objects.create(name='Warsaw') last_month = datetime.date.today() - datetime.timedelta(days=30) return { 'rack_server': DCAssetFactory( model=data['assets_models']['rack_server'], device_info__ralph_device_id=data['devices']['device_1'].id, device_info__data_center=data['dc']['a'], device_info__orientation=Orientation.front, device_info__position=1, device_info__rack=data['racks']['a'], device_info__server_room=data['server_rooms']['a'], device_info__slot_no='', service=data['services']['backup_systems'], device_environment=data['envs']['prod'], service_name=service_name, warehouse=warehouse, ), 'rack_server': DCAssetFactory( model=data['assets_models']['rack_server'], device_info__ralph_device_id=data['devices']['device_2'].id, device_info__data_center=data['dc']['a'], device_info__orientation=Orientation.front, device_info__position=2, device_info__rack=data['racks']['a'], device_info__server_room=data['server_rooms']['a'], device_info__slot_no='', service=data['services']['databases'], device_environment=data['envs']['prod'], service_name=service_name, warehouse=warehouse, invoice_date=last_month, ), 'blade_chassis': DCAssetFactory( model=data['assets_models']['blade_chassis'], device_info__data_center=data['dc']['a'], device_info__position=blade_location, device_info__rack=data['racks']['a'], device_info__server_room=data['server_rooms']['a'], device_info__slot_no='', service=data['services']['backup_systems'], device_environment=data['envs']['prod'], service_name=service_name, warehouse=warehouse, invoice_date=last_month + datetime.timedelta(days=5), ), 'blade_servers': [ DCAssetFactory( model=data['assets_models']['blade_chassis'], device_info__data_center=data['dc']['a'], device_info__position=blade_location, device_info__rack=data['racks']['a'], device_info__server_room=data['server_rooms']['a'], device_info__slot_no=slot_no, service=data['services']['backup_systems'], device_environment=data['envs']['prod'], service_name=service_name, warehouse=warehouse, invoice_date=last_month + datetime.timedelta(days=7), ) for slot_no in xrange(1, 17) ], 'load_balancer': DCAssetFactory( model=data['assets_models']['rack_server'], device_info__ralph_device_id=data['devices']['device_3'].id, device_info__data_center=data['dc']['a'], device_info__orientation=Orientation.front, device_info__position=20, device_info__rack=data['racks']['a'], device_info__server_room=data['server_rooms']['a'], device_info__slot_no='', service=data['services']['load_balancing'], device_environment=data['envs']['prod'], service_name=service_name, warehouse=warehouse, ) }
def setUp(self): self.asset = DCAssetFactory() self.ralph_device = self.asset.get_ralph_device()
class TestPricingAssetModelsCommand(TestCase): def setUp(self): self.asset = DCAssetFactory() self.ralph_device = self.asset.get_ralph_device() def test_get_device_name_from_asset_when_device_info_is_null(self): self.asset.device_info = None self.assertEqual( Command().get_device_name_from_asset(self.asset, {1: 'test0'}), None, ) def test_get_device_name_from_asset_when_ralph_id_is_null(self): self.asset.device_info.ralph_device_id = None self.assertEqual( Command().get_device_name_from_asset(self.asset, {1: 'test0'}), None, ) def test_get_device_name_from_asset(self): self.assertEqual( Command().get_device_name_from_asset( self.asset, {self.asset.device_info.ralph_device_id: 'test0'}), 'test0', ) def test_get_device_ids_when_device_info_is_null(self): self.asset.device_info = None self.assertEqual(Command()._get_device_ids([self.asset]), []) def test_get_device_ids(self): self.assertEqual(Command()._get_device_ids([self.asset]), [self.ralph_device.id]) def test_get_ralph_devices(self): self.assertEqual( repr(Command()._get_ralph_devices([self.asset])), repr([self.ralph_device]), ) def test_get_device_ids_and_names(self): self.assertEqual(Command().get_device_ids_and_names([self.asset]), {self.ralph_device.id: self.ralph_device.name}) def test_get_deprecated_date_when_force_deprecation(self): self.asset.force_deprecation = True self.assertEqual(Command().get_deprecated_date(self.asset), 'Deprecated') def test_get_deprecated_date_when_no_invoice_date(self): self.asset.invoice_date = None self.asset.save() self.assertEqual(Command().get_deprecated_date(self.asset), 'No invoice date') def test_get_deprecated_date_when_deprecation_end_date(self): self.asset.invoice_date = datetime.date.today() self.asset.deprecation_rate = 25 self.asset.deprecation_end_date = datetime.date.today() self.asset.save() self.assertEqual(Command().get_deprecated_date(self.asset), datetime.date.today()) def test_get_deprecated_date(self): self.asset.invoice_date = datetime.date.today() self.asset.deprecation_end_date = None self.asset.deprecation_rate = 25 self.asset.save() self.assertEqual(Command().get_deprecated_date(self.asset), datetime.date.today() + relativedelta(years=4)) def test_get_data(self): self.assertEqual(Command().get_data(), [[ self.asset.id, self.asset.sn, self.asset.barcode, self.asset.venture, self.asset.get_ralph_device().name, self.asset.deprecation_end_date, ]])
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_get_vizualization_returns_none_when_no_device_info(self): asset = DCAssetFactory(device_info=None) url = asset.get_vizualization_url() self.assertFalse(url)
def test_dc_asset_without_linked_device(self): asset = DCAssetFactory(device_info=None) asset.save() self.assertEqual(asset.linked_device, None)
def test_get_configuration_url_returns_url(self): asset = DCAssetFactory() url = asset.get_configuration_url() self.assertTrue(url)
def test_get_assets_without_device_info(self): DCAssetFactory(device_info=None, ) today = date(2013, 11, 12) result = [a for a in api_scrooge.get_assets(today)] self.assertEquals(result, [])
def test_dc_asset_without_linked_device(self): asset = DCAssetFactory(device_info=None) asset.save() self.assertEqual(asset.linked_device, None)
def test_get_vizualization_returns_url(self): asset = DCAssetFactory() url = asset.get_vizualization_url() self.assertTrue(url)
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 test_get_assets_without_invoice(self): asset = DCAssetFactory(invoice_date=None, ) today = date(2013, 11, 12) result = [a for a in api_scrooge.get_assets(today)] self._compare_asset(asset, result[0], today, asset.device_info.get_ralph_device().name)
class TestPricingAssetModelsCommand(TestCase): def setUp(self): self.asset = DCAssetFactory() self.ralph_device = self.asset.get_ralph_device() def test_get_device_name_from_asset_when_device_info_is_null(self): self.asset.device_info = None self.assertEqual( Command().get_device_name_from_asset( self.asset, {1: 'test0'} ), None, ) def test_get_device_name_from_asset_when_ralph_id_is_null(self): self.asset.device_info.ralph_device_id = None self.assertEqual( Command().get_device_name_from_asset( self.asset, {1: 'test0'} ), None, ) def test_get_device_name_from_asset(self): self.assertEqual( Command().get_device_name_from_asset( self.asset, {self.asset.device_info.ralph_device_id: 'test0'} ), 'test0', ) def test_get_device_ids_when_device_info_is_null(self): self.asset.device_info = None self.assertEqual(Command()._get_device_ids([self.asset]), []) def test_get_device_ids(self): self.assertEqual( Command()._get_device_ids([self.asset]), [self.ralph_device.id] ) def test_get_ralph_devices(self): self.assertEqual( repr(Command()._get_ralph_devices([self.asset])), repr([self.ralph_device]), ) def test_get_device_ids_and_names(self): self.assertEqual( Command().get_device_ids_and_names([self.asset]), {self.ralph_device.id: self.ralph_device.name} ) def test_get_deprecated_date_when_force_deprecation(self): self.asset.force_deprecation = True self.assertEqual( Command().get_deprecated_date(self.asset), 'Deprecated' ) def test_get_deprecated_date_when_no_invoice_date(self): self.asset.invoice_date = None self.asset.save() self.assertEqual( Command().get_deprecated_date(self.asset), 'No invoice date' ) def test_get_deprecated_date_when_deprecation_end_date(self): self.asset.invoice_date = datetime.date.today() self.asset.deprecation_rate = 25 self.asset.deprecation_end_date = datetime.date.today() self.asset.save() self.assertEqual( Command().get_deprecated_date(self.asset), datetime.date.today() ) def test_get_deprecated_date(self): self.asset.invoice_date = datetime.date.today() self.asset.deprecation_end_date = None self.asset.deprecation_rate = 25 self.asset.save() self.assertEqual( Command().get_deprecated_date(self.asset), datetime.date.today() + relativedelta(years=4) ) def test_get_data(self): self.assertEqual( Command().get_data(), [[ self.asset.id, self.asset.sn, self.asset.barcode, self.asset.venture, self.asset.get_ralph_device().name, self.asset.deprecation_end_date, ]] )
def setUp(self): self.asset = DCAssetFactory() self.ralph_device = self.asset.get_ralph_device()