def setUp(self): self.top_venture = Venture(name="top_venture") self.top_venture.save() self.child_venture = Venture(name="child_venture", parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name="role", venture=self.child_venture) self.role.save() self.child_role = VentureRole(name="child_role", venture=self.child_venture, parent=self.role) self.child_role.save() dm = self.add_model("DC model sample", DeviceType.data_center.id) self.dc = Device.create(sn="sn1", model=dm) self.dc.name = "dc" self.dc.save() dm = self.add_model("Rack model sample", DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn="sn2", model=dm) self.rack.parent = self.dc self.rack.name = "rack" self.rack.save() dm = self.add_model("Blade model sample", DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn="sn3", model=dm) self.blade.name = "blade" self.blade.parent = self.rack self.blade.save()
def test_fisheye(self): """ Create Venture/Role and import as CI/CIRelations. Now import fisheye xml from samples/* files and compare if changes are properly saved into the database, and reconcilated. """ x = Venture(symbol="test_venture") x.save() y = VentureRole(name="test_role", venture=x) y.save() allegro_ci = CI(name="Allegro", type_id=CI_TYPES.VENTURE.id) allegro_ci.save() ct = ContentType.objects.get_for_model(x) test_venture, = CIImporter().import_all_ci([ct], asset_id=x.id) ct = ContentType.objects.get_for_model(y) test_role, = CIImporter().import_all_ci([ct], asset_id=y.id) CIImporter().import_relations(ContentType.objects.get_for_model(y), asset_id=y.id) with mock.patch("ralph.cmdb.integration.lib.fisheye.Fisheye") as Fisheye: Fisheye.side_effect = MockFisheye x = pgi(fisheye_class=Fisheye) x.import_git() self.assertEqual( CIChangeGit.objects.filter( author__contains="*****@*****.**", # file_paths__contains='/test_venture' ).count(), 2, ) self.assertEqual(CIChange.objects.filter(ci=test_role, type=CI_CHANGE_TYPES.CONF_GIT.id).count(), 2)
def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole(name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', name='Rack 1', model=dm) dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn='sn2', name='DC', model=dm) self.rack.parent=self.dc self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', name='blade1', model=dm ) self.blade.parent=self.rack self.blade.save()
def setUp(self): engine = settings.ISSUETRACKERS['default']['ENGINE'] if engine != '': raise ImproperlyConfigured( '''Expected ISSUETRACKERS['default']['ENGINE']='' got: %s''' % engine) # usual stuff self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() to = VentureOwner(name='Bufallo Kudłaczek', venture=self.child_venture, type=OwnerType.technical.id) to.save() bo = VentureOwner(name='Bill Bąbelek', venture=self.child_venture, type=OwnerType.business.id) bo.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn='sn2', model=dm) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm) self.blade.name = 'blade' self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device = self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip = '192.168.1.1' self.deployment.hostname = 'test' self.deployment.save()
def test_validate_venture_and_role(self): with self.assertRaises(forms.ValidationError): _validate_venture_and_role('bang', 'klang', 0) v = Venture(name='Bang', symbol='bang') v.save() with self.assertRaises(forms.ValidationError): _validate_venture_and_role('bang', 'klang', 0) r = VentureRole(name='klang', venture=v) r.save() _validate_venture_and_role('bang', 'klang', 0)
def setUp(self): engine = settings.ISSUETRACKERS['default']['ENGINE'] if engine != '': raise ImproperlyConfigured('''Expected ISSUETRACKERS['default']['ENGINE']='' got: %s''' % engine) # usual stuff self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole(name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() to = VentureOwner(name='Bufallo Kudłaczek', venture=self.child_venture, type=OwnerType.technical.id) to.save() bo = VentureOwner(name='Bill Bąbelek', venture=self.child_venture, type=OwnerType.business.id) bo.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create( sn='sn1', model=dm ) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm ) self.rack.parent=self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm ) self.blade.name = 'blade' self.blade.parent=self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device=self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip='192.168.1.1' self.deployment.hostname='test' self.deployment.save()
def setUp(self): v = Venture(symbol='test_venture') v.save() r = VentureRole(name='test_role', venture=v) r.save() # ci for custom path mapping c = Venture(symbol='custom_ci', name='custom_ci') c.save() for i in (v, r, c): CIImporter().import_single_object(i) CIImporter().import_single_object_relations(i)
def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture( name='child_venture', parent=self.top_venture, ) self.child_venture.save() self.role = VentureRole( name='role', venture=self.child_venture, ) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm, ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm, ) self.blade.name = 'blade' self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device = self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip = '192.168.1.1' self.deployment.hostname = 'test' self.deployment.save()
def test_role_full_name(self): a = Venture(name='A', symbol='a') a.save() x = VentureRole(name='x', venture=a) x.save() y = VentureRole(name='y', venture=a, parent=x) y.save() self.assertEqual(y.full_name, 'x / y')
def setUp(self): engine = settings.ISSUETRACKERS["default"]["ENGINE"] if engine != "": raise ImproperlyConfigured("""Expected ISSUETRACKERS['default']['ENGINE']='' got: %r""" % engine) # usual stuff self.top_venture = Venture(name="top_venture") self.top_venture.save() self.child_venture = Venture(name="child_venture", parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name="role", venture=self.child_venture) self.role.save() self.child_role = VentureRole(name="child_role", venture=self.child_venture, parent=self.role) self.child_role.save() to = CIOwner(first_name="Bufallo", last_name="Kudłaczek") to.save() bo = CIOwner(first_name="Bill", last_name="Bąbelek") bo.save() ct = ContentType.objects.get_for_model(self.top_venture) CIImporter().import_all_ci([ct]) CIOwnership(owner=to, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.technical.id).save() CIOwnership(owner=bo, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.business.id).save() dm = self.add_model("DC model sample", DeviceType.data_center.id) self.dc = Device.create(sn="sn1", model=dm) self.dc.name = "dc" self.dc.save() dm = self.add_model("Rack model sample", DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn="sn2", model=dm) self.rack.parent = self.dc self.rack.name = "rack" self.rack.save() dm = self.add_model("Blade model sample", DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn="sn3", model=dm) self.blade.name = "blade" self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = "test_host2" self.deployment.device = self.blade self.deployment.mac = "10:9a:df:6f:af:01" self.deployment.ip = "192.168.1.1" self.deployment.hostname = "test" self.deployment.save()
def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole(name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create( sn='sn1', model=dm ) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm ) self.rack.parent=self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm ) self.blade.name = 'blade' self.blade.parent=self.rack self.blade.save()
def setUp(self): self.client = login_as_su() self.deprecation_kind = DeprecationKind(months=24, remarks='Default') self.deprecation_kind.save() self.margin = MarginKind(margin=100, remarks='100%') self.margin.save() self.venture = Venture(name='VenureName') self.venture.save() self.role = VentureRole(venture=self.venture, name='VentureRole') self.role.save() self.device = Device.create( sn=DEVICE['sn'], barcode=DEVICE['barcode'], remarks=DEVICE['remarks'], model_name=DEVICE['model_name'], model_type=DeviceType.unknown, rack=DEVICE['rack'], position=DEVICE['position'], dc=DATACENTER, ) self.device.name = DEVICE['name'] self.device.save() self.device2 = Device.create( sn=DEVICE2['sn'], barcode=DEVICE2['barcode'], remarks=DEVICE2['remarks'], model_name=DEVICE2['model_name'], model_type=DeviceType.unknown, rack=DEVICE2['rack'], position=DEVICE2['position'], dc=DATACENTER, ) self.device2.name = DEVICE2['name'] self.device2.save()
def setUp(self): self.client = login_as_su() venture = Venture(name='venture', symbol='ventureSymbol') venture.save() self.venture = venture venture_role = VentureRole(name='role', venture=self.venture) venture_role.save() self.venture_role = venture_role d_kind = DeprecationKind(name='12 months', months=12) d_kind.save() self.kind = DeprecationKind.objects.get(name='12 months') # Cross - devices self.device_after_deprecation = Device.create( sn='device_after_deprecation', deprecation_kind=self.kind, support_expiration_date=datetime.datetime(2003, 01, 02), purchase_date=datetime.datetime(2001, 01, 01), warranty_expiration_date=datetime.datetime(2005, 01, 02), venture=self.venture, venture_role=self.venture_role, model_name='xxx', model_type=DeviceType.unknown, ) self.device_after_deprecation.name = 'Device1' self.device_after_deprecation.save() self.device_with_blanks = Device.create( sn='device_with_blanks', deprecation_date=None, deprecation_kind=None, support_expiration_date=None, purchase_date=None, venture=None, venture_role=None, model_name='xxx', model_type=DeviceType.unknown, ) self.device_with_blanks.name = 'Device2' self.device_with_blanks.save()
def setUp(self): login = '******' password = '******' user = User.objects.create_user(login, '*****@*****.**', password) self.user = user self.user.is_staff = True self.user.is_superuser = True self.user.save() self.client = Client() self.client.login(username=login, password=password) venture = Venture(name=DEVICE_VENTURE, symbol=DEVICE_VENTURE_SYMBOL) venture.save() self.venture = venture venture_role = VentureRole(name=VENTURE_ROLE, venture=self.venture) venture_role.save() self.venture_role = venture_role self.device = Device.create( sn=DEVICE_SN, barcode=DEVICE_BARCODE, remarks=DEVICE_REMARKS, model_name='xxxx', model_type=DeviceType.unknown, venture=self.venture, venture_role=self.venture_role, rack=DEVICE_RACK, position=DEVICE_POSITION, dc=DATACENTER, ) self.device.name = DEVICE_NAME self.device.save() self.layer = db.CILayer(name='layer1') self.layer.save() self.citype = db.CIType(name='xxx') self.citype.save()
def test_fisheye(self): """ Create Venture/Role and import as CI/CIRelations. Now import fisheye xml from samples/* files and compare if changes are properly saved into the database, and reconcilated. """ x = Venture(symbol='test_venture') x.save() y = VentureRole(name='test_role', venture=x) y.save() allegro_ci = CI(name='Allegro', type_id=CI_TYPES.VENTURE.id) allegro_ci.save() ct=ContentType.objects.get_for_model(x) test_venture, = CIImporter().import_all_ci([ct],asset_id=x.id) ct=ContentType.objects.get_for_model(y) test_role, = CIImporter().import_all_ci([ct],asset_id=y.id) CIImporter().import_relations(ContentType.objects.get_for_model(y),asset_id=y.id) with mock.patch('ralph.cmdb.integration.lib.fisheye.Fisheye') as Fisheye: Fisheye.side_effect = MockFisheye x = pgi(fisheye_class=Fisheye) x.import_git() self.assertEqual(CIChangeGit.objects.filter( author__contains='*****@*****.**', #file_paths__contains='/test_venture' ).count(), 2) self.assertEqual(CIChange.objects.filter( ci=test_role, type=CI_CHANGE_TYPES.CONF_GIT.id, ).count(), 2)
def setUp(self): engine = settings.ISSUETRACKERS['default']['ENGINE'] if engine != '': raise ImproperlyConfigured( '''Expected ISSUETRACKERS['default']['ENGINE']='' got: %r''' % engine) # usual stuff self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() to = CIOwner( first_name='Bufallo', last_name='Kudłaczek', ) to.save() bo = CIOwner( first_name='Bill', last_name='Bąbelek', ) bo.save() ct = ContentType.objects.get_for_model(self.top_venture) CIImporter().import_all_ci([ct]) CIOwnership(owner=to, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.technical.id).save() CIOwnership(owner=bo, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.business.id).save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm, ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm) self.blade.name = 'blade' self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device = self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip = '192.168.1.1' self.deployment.hostname = 'test' self.deployment.save()
def setUp(self): self.client = login_as_su() venture = Venture(name=DEVICE["venture"], symbol=DEVICE["ventureSymbol"]) venture.save() self.venture = venture venture_role = VentureRole(name=DEVICE["venture_role"], venture=self.venture) venture_role.save() self.venture_role = venture_role self.device = Device.create( sn=DEVICE["sn"], barcode=DEVICE["barcode"], remarks=DEVICE["remarks"], model_name=DEVICE["model_name"], model_type=DeviceType.unknown, venture=self.venture, venture_role=self.venture_role, rack=DEVICE["rack"], position=DEVICE["position"], dc=DATACENTER, ) self.device.name = DEVICE["name"] self.device.save() self.ip = IPAddress(address=DEVICE["ip"], device=self.device) self.ip.save() self.db_ip = IPAddress.objects.get(address=DEVICE["ip"]) self.network_terminator = NetworkTerminator(name="simple_terminator") self.network_terminator.save() self.network_datacenter = DataCenter(name=DATACENTER) self.network_datacenter.save() self.network = Network(name=NETWORK["name"], address=NETWORK["address"], data_center=self.network_datacenter) self.diskshare_device = Device.create( sn=DISKSHARE["sn"], barcode=DISKSHARE["barcode"], model_name="xxx", model_type=DeviceType.storage ) self.diskshare_device.name = DISKSHARE["device"] self.diskshare_device.save() self.cm_generic = ComponentModel(name="GenericModel") self.cm_diskshare = ComponentModel(name="DiskShareModel") self.cm_processor = ComponentModel(name="ProcessorModel") self.cm_memory = ComponentModel(name="MemoryModel") self.cm_storage = ComponentModel(name="ComponentModel") self.cm_fibre = ComponentModel(name="FibreChannalMidel") self.cm_ethernet = ComponentModel(name="EthernetMidel") self.cm_software = ComponentModel(name="SoftwareModel") self.cm_splunkusage = ComponentModel(name="SplunkusageModel") self.cm_operatingsystem = ComponentModel(name="OperatingSystemModel") self.generic_component = GenericComponent( device=self.device, model=self.cm_generic, label=COMPONENT["GenericComponent"], sn=GENERIC["sn"] ) self.generic_component.save() self.diskshare = DiskShare( device=self.device, model=self.cm_diskshare, share_id=self.device.id, size=80, wwn=DISKSHARE["wwn"] ) self.diskshare.save() self.disksharemount = DiskShareMount.concurrent_get_or_create( share=self.diskshare, device=self.device, volume=COMPONENT["DiskShareMount"] ) self.processor = Processor(device=self.device, model=self.cm_processor, label=COMPONENT["Processor"]) self.processor.save() self.memory = Memory(device=self.device, model=self.cm_memory, label=COMPONENT["Memory"]) self.memory.save() self.storage = Storage(device=self.device, model=self.cm_storage, label=COMPONENT["Storage"]) self.storage.save() self.fibrechannel = FibreChannel( device=self.device, model=self.cm_fibre, label=COMPONENT["Fibre"], physical_id="01234" ) self.fibrechannel.save() self.ethernet = Ethernet(model=self.cm_ethernet, device=self.device, mac=DEVICE["mac"]) self.ethernet.save() self.software = Software(device=self.device, model=self.cm_software, label=COMPONENT["Software"]) self.software.save() self.splunkusage = SplunkUsage(device=self.device, model=self.cm_splunkusage) self.splunkusage.save() self.operatingsystem = OperatingSystem(device=self.device, model=self.cm_operatingsystem, label=COMPONENT["OS"]) self.operatingsystem.save()
class DeploymentTest(TestCase): fixtures=['0_types.yaml', '1_attributes.yaml', '2_layers.yaml', '3_prefixes.yaml'] def setUp(self): engine = settings.ISSUETRACKERS['default']['ENGINE'] if engine != '': raise ImproperlyConfigured('''Expected ISSUETRACKERS['default']['ENGINE']='' got: %s''' % engine) # usual stuff self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole(name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() to = VentureOwner(name='Bufallo Kudłaczek', venture=self.child_venture, type=OwnerType.technical.id) to.save() bo = VentureOwner(name='Bill Bąbelek', venture=self.child_venture, type=OwnerType.business.id) bo.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create( sn='sn1', model=dm ) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm ) self.rack.parent=self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm ) self.blade.name = 'blade' self.blade.parent=self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device=self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip='192.168.1.1' self.deployment.hostname='test' self.deployment.save() def test_acceptance(self): # using issue null engine self.assertEqual(self.deployment.status, DeploymentStatus.open.id) self.deployment.create_issue() self.assertEqual(self.deployment.issue_key, '#123456') # status not changed, until plugin is run self.assertEqual(self.deployment.status, DeploymentStatus.open.id) # run ticket acceptance plugin ticket(self.deployment.id) # ticket already accepted self.deployment = Deployment.objects.get(id=self.deployment.id) self.assertEqual(self.deployment.status, DeploymentStatus.in_deployment.id) def test_owners(self): self.assertEqual(get_technical_owner(self.deployment.device), 'bufallo.kudlaczek') self.assertEqual(get_business_owner(self.deployment.device), 'bill.babelek') def add_model(self, name, device_type): dm = DeviceModel(); dm.model_type=device_type, dm.name=name dm.save() return dm
def setUp(self): self.client = login_as_su() venture = Venture( name=DEVICE['venture'], symbol=DEVICE['ventureSymbol'] ) venture.save() self.venture = venture venture_role = VentureRole( name=DEVICE['venture_role'], venture=self.venture ) venture_role.save() self.venture_role = venture_role self.device = Device.create( sn=DEVICE['sn'], barcode=DEVICE['barcode'], remarks=DEVICE['remarks'], model_name=DEVICE['model_name'], model_type=DeviceType.unknown, venture=self.venture, venture_role=self.venture_role, rack=DEVICE['rack'], position=DEVICE['position'], dc=DATACENTER, ) self.device.name = DEVICE['name'] self.device.save() self.ip = IPAddress(address=DEVICE['ip'], device=self.device) self.ip.save() self.db_ip = IPAddress.objects.get(address=DEVICE['ip']) self.network_terminator = NetworkTerminator(name='simple_terminator') self.network_terminator.save() self.network_datacenter = DataCenter(name=DATACENTER) self.network_datacenter.save() self.network = Network( name=NETWORK['name'], address=NETWORK['address'], data_center=self.network_datacenter, ) self.diskshare_device = Device.create( sn=DISKSHARE['sn'], barcode=DISKSHARE['barcode'], model_name='xxx', model_type=DeviceType.storage, ) self.diskshare_device.name = DISKSHARE['device'] self.diskshare_device.save() self.cm_generic = ComponentModel(name='GenericModel') self.cm_diskshare = ComponentModel(name='DiskShareModel') self.cm_processor = ComponentModel(name='ProcessorModel') self.cm_memory = ComponentModel(name='MemoryModel') self.cm_storage = ComponentModel(name='ComponentModel') self.cm_fibre = ComponentModel(name='FibreChannalMidel') self.cm_ethernet = ComponentModel(name='EthernetMidel') self.cm_software = ComponentModel(name='SoftwareModel') self.cm_splunkusage = ComponentModel(name='SplunkusageModel') self.cm_operatingsystem = ComponentModel(name='OperatingSystemModel') self.generic_component = GenericComponent( device=self.device, model=self.cm_generic, label=COMPONENT['GenericComponent'], sn=GENERIC['sn'], ) self.generic_component.save() self.diskshare = DiskShare( device=self.device, model=self.cm_diskshare, share_id=self.device.id, size=80, wwn=DISKSHARE['wwn'], ) self.diskshare.save() self.disksharemount = DiskShareMount.concurrent_get_or_create( share=self.diskshare, device=self.device, defaults={ 'volume': COMPONENT['DiskShareMount'], }, ) self.processor = Processor( device=self.device, model=self.cm_processor, label=COMPONENT['Processor'], ) self.processor.save() self.memory = Memory( device=self.device, model=self.cm_memory, label=COMPONENT['Memory'], ) self.memory.save() self.storage = Storage( device=self.device, model=self.cm_storage, label=COMPONENT['Storage'], ) self.storage.save() self.fibrechannel = FibreChannel( device=self.device, model=self.cm_fibre, label=COMPONENT['Fibre'], physical_id='01234', ) self.fibrechannel.save() self.ethernet = Ethernet( model=self.cm_ethernet, device=self.device, mac=DEVICE['mac'], ) self.ethernet.save() self.software = Software( device=self.device, model=self.cm_software, label=COMPONENT['Software'], ) self.software.save() self.splunkusage = SplunkUsage( device=self.device, model=self.cm_splunkusage, ) self.splunkusage.save() self.operatingsystem = OperatingSystem( device=self.device, model=self.cm_operatingsystem, label=COMPONENT['OS'], ) self.operatingsystem.save() # device with strange name... self.strange_device = Device.create( sn='abcabc123123', model_name=DEVICE['model_name'], model_type=DeviceType.unknown, venture=self.venture, venture_role=self.venture_role, rack=DEVICE['rack'], position=DEVICE['position'], dc=DATACENTER, ) self.strange_device.name = 'śćź' self.strange_device.save()
def test_get_preboot_role(self): preboot = Preboot(name='test preboot') preboot.save() ven = Venture(name='test1', symbol='test1', preboot = preboot) ven.save() a = VentureRole(name='test1', preboot = preboot, venture_id = ven.id) a.save() b = VentureRole(name='test1 parent', parent_id = a.id, venture_id = ven.id) b.save() c = VentureRole(name='test1 parent parent', parent_id = b.id, venture_id = ven.id) c.save() self.assertEqual(a.get_preboot(), preboot) self.assertEqual(b.get_preboot(), preboot) self.assertEqual(c.get_preboot(), preboot)
class BulkeditTest(TestCase): """ Tests edit form Scenario: 1. Changes data in single device + test history change 2. Changes data in more than one device 3. Send form without select edit fields 4. Send form with empty save comment field 5. If data was added, check depreciation_date """ def setUp(self): self.client = login_as_su() self.deprecation_kind = DeprecationKind(months=24, remarks='Default') self.deprecation_kind.save() self.margin = MarginKind(margin=100, remarks='100%') self.margin.save() self.venture = Venture(name='VenureName') self.venture.save() self.role = VentureRole(venture=self.venture, name='VentureRole') self.role.save() self.device = Device.create( sn=DEVICE['sn'], barcode=DEVICE['barcode'], remarks=DEVICE['remarks'], model_name=DEVICE['model_name'], model_type=DeviceType.unknown, rack=DEVICE['rack'], position=DEVICE['position'], dc=DATACENTER, ) self.device.name = DEVICE['name'] self.device.save() self.device2 = Device.create( sn=DEVICE2['sn'], barcode=DEVICE2['barcode'], remarks=DEVICE2['remarks'], model_name=DEVICE2['model_name'], model_type=DeviceType.unknown, rack=DEVICE2['rack'], position=DEVICE2['position'], dc=DATACENTER, ) self.device2.name = DEVICE2['name'] self.device2.save() def test_single_device_edit(self): url = '/ui/search/bulkedit/' text_fields = ('position', 'chassis_position', 'remarks', 'verified') device_fields = [] device_fields.extend(text_fields) post_data = { 'select': [self.device.id], # 1 'edit': device_fields, 'verified': True, 'position': '9', 'chassis_position': 10, 'remarks': 'Hello Ralph', 'save_comment': 'Everything has changed', 'save': '', # save form } self.client.post(url, post_data) # Check if data from form is the same that data in database device = Device.objects.get(id=self.device.id) for field in device_fields: db_data = getattr(device, field) form_data = post_data[field] msg = 'FIELD: %s, DB: %s FORM: %s' % (field, db_data, form_data) self.assertEqual(unicode(db_data), unicode(form_data), msg) # Check if change can see in History change history_device = HistoryChange.objects.filter( device=self.device, old_value='Very important device', new_value='Hello Ralph', ) self.assertEqual(history_device.count(), 1) self.assertEqual( history_device[0].comment, 'Everything has changed' ) def test_many_devices_edit(self): url = '/ui/search/bulkedit/' remarks = 'change remarks in 2 devices' post_data = { 'select': [self.device.id, self.device2.id], 'edit': ['remarks'], 'remarks': remarks, 'save_comment': 'change remarks', 'save': '', # save form } self.client.post(url, post_data) device, device2 = Device.objects.filter(remarks=remarks) self.assertEqual = (device.remarks, remarks) self.assertEqual = (device.remarks, device2.remarks) def test_send_form_without_marking_edited_fields(self): url = '/ui/search/bulkedit/' data_post = { 'select': self.device.id, 'remarks': 'Here are some remarks without confirmation.', 'save': '', } response = self.client.post(url, data_post) self.assertEqual(response.status_code, 200) self.assertTrue(ERROR_MSG['no_mark_fields'] in response.content) def test_send_form_with_empty_save_comment_field(self): url = '/ui/search/bulkedit/?' post_data = { 'select': [self.device.id], 'edit': ['remarks'], 'remarks': 'Hello World!', 'save_comment': '', 'save': '', # save form } response = self.client.post(url, post_data) self.assertEqual(response.status_code, 200) self.assertTrue(ERROR_MSG['empty_save_comment'] in response.content) self.assertFormError( response, 'form', 'save_comment', ERROR_MSG['empty_save_comment_field'] )
def test_check_ip(self): terminator = NetworkTerminator(name='Test Terminator') terminator.save() data_center = DataCenter(name='Test date_center') data_center.save() network = Network(address='192.168.1.0/24',name='Test network', data_center=data_center) network.save() network.terminators = [terminator] network.save() subnetwork = Network(address='192.168.2.0/24',name='Test subnetwork', data_center=data_center) subnetwork.save() subnetwork.terminators = [terminator] subnetwork.save() main_venture = Venture(name='Main Venture') main_venture.save() main_venture.networks = [network, subnetwork] main_venture.save() second_network = Network(address='172.16.0.0/28',name='Test secound_network', data_center=data_center) second_network.save() second_network.terminators = [terminator] second_network.save() child_venture = Venture(name='Child Venture', parent=main_venture) child_venture.save() child_venture.networks = [second_network] child_venture.save() third_network = Network(address='66.6.6.0/29',name='Test third_network', data_center=data_center) third_network.save() third_network.terminators = [terminator] third_network.save() third_subnetwork = Network(address='66.6.7.0/29',name='Test third_subnetwork', data_center=data_center) third_subnetwork.save() third_subnetwork.terminators = [terminator] third_subnetwork.save() venture_role_main = VentureRole(name='Main Venture role', venture=child_venture) venture_role_main.save() venture_role_main.networks = [third_network, third_subnetwork] venture_role_main.save() fourth_network = Network(address='111.11.11.0/27',name='Test fourth_network', data_center=data_center) fourth_network.save() fourth_network.terminators = [terminator] fourth_network.save() venture_role_child = VentureRole(name='Child Venture role', venture=child_venture, parent=venture_role_main) venture_role_child.save() venture_role_child.networks = [fourth_network] venture_role_child.save() self.assertEqual(venture_role_child.check_ip("192.168.1.15"), True) self.assertEqual(venture_role_child.check_ip("192.168.2.15"), True) self.assertEqual(venture_role_child.check_ip("192.168.3.15"), False) self.assertEqual(venture_role_child.check_ip("172.16.0.10"), True) self.assertEqual(venture_role_child.check_ip("172.16.0.22"), False) self.assertEqual(venture_role_child.check_ip("66.6.6.5"), True) self.assertEqual(venture_role_child.check_ip("66.6.7.5"), True) self.assertEqual(venture_role_child.check_ip("66.6.8.10"), False) self.assertEqual(venture_role_child.check_ip("111.11.11.1"), True) self.assertEqual(venture_role_child.check_ip("111.11.11.44"), False)
class DeploymentTest(TestCase): def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture( name='child_venture', parent=self.top_venture, ) self.child_venture.save() self.role = VentureRole( name='role', venture=self.child_venture, ) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm, ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm, ) self.blade.name = 'blade' self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device = self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip = '192.168.1.1' self.deployment.hostname = 'test' self.deployment.save() def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = device_type, dm.name = name dm.save() return dm def test_archivization(self): id = self.deployment.id data = {} for field in self.deployment._meta.fields: data[field.name] = getattr(self.deployment, field.name) if field.name == 'mac': data[field.name] = MACAddressField.normalize(data[field.name]) self.deployment.archive() archive = ArchivedDeployment.objects.get(pk=id) archive_data = {} for field in archive._meta.fields: archive_data[field.name] = getattr(archive, field.name) if field.name == 'mac': archive_data[field.name] = MACAddressField.normalize( archive_data[field.name]) self.assertEqual(data, archive_data) with self.assertRaises(Deployment.DoesNotExist): Deployment.objects.get(pk=id)
def test_get_preboot_role_none(self): ven = Venture(name='test1', symbol='test1') ven.save() a = VentureRole(name='test1', venture_id=ven.id) a.save() b = VentureRole(name='test1 parent', parent_id=a.id, venture_id=ven.id) b.save() c = VentureRole(name='test1 parent parent', parent_id=b.id, venture_id=ven.id) c.save() self.assertEqual(a.get_preboot(), None) self.assertEqual(b.get_preboot(), None) self.assertEqual(c.get_preboot(), None)
def setUp(self): self.client = login_as_su() venture = Venture(name=DEVICE['venture'], symbol=DEVICE['ventureSymbol']) venture.save() self.venture = venture venture_role = VentureRole(name=DEVICE['venture_role'], venture=self.venture) venture_role.save() self.venture_role = venture_role self.device = Device.create( sn=DEVICE['sn'], barcode=DEVICE['barcode'], remarks=DEVICE['remarks'], model_name=DEVICE['model_name'], model_type=DeviceType.unknown, venture=self.venture, venture_role=self.venture_role, rack=DEVICE['rack'], position=DEVICE['position'], dc=DATACENTER, ) self.device.name = DEVICE['name'] self.device.save() self.ip = IPAddress(address=DEVICE['ip'], device=self.device) self.ip.save() self.db_ip = IPAddress.objects.get(address=DEVICE['ip']) self.network_terminator = NetworkTerminator(name='simple_terminator') self.network_terminator.save() self.network_datacenter = DataCenter(name=DATACENTER) self.network_datacenter.save() self.network = Network( name=NETWORK['name'], address=NETWORK['address'], data_center=self.network_datacenter, ) self.diskshare_device = Device.create( sn=DISKSHARE['sn'], barcode=DISKSHARE['barcode'], model_name='xxx', model_type=DeviceType.storage, ) self.diskshare_device.name = DISKSHARE['device'] self.diskshare_device.save() self.cm_generic = ComponentModel(name='GenericModel') self.cm_diskshare = ComponentModel(name='DiskShareModel') self.cm_processor = ComponentModel(name='ProcessorModel') self.cm_memory = ComponentModel(name='MemoryModel') self.cm_storage = ComponentModel(name='ComponentModel') self.cm_fibre = ComponentModel(name='FibreChannalMidel') self.cm_ethernet = ComponentModel(name='EthernetMidel') self.cm_software = ComponentModel(name='SoftwareModel') self.cm_splunkusage = ComponentModel(name='SplunkusageModel') self.cm_operatingsystem = ComponentModel(name='OperatingSystemModel') self.generic_component = GenericComponent( device=self.device, model=self.cm_generic, label=COMPONENT['GenericComponent'], sn=GENERIC['sn'], ) self.generic_component.save() self.diskshare = DiskShare( device=self.device, model=self.cm_diskshare, share_id=self.device.id, size=80, wwn=DISKSHARE['wwn'], ) self.diskshare.save() self.disksharemount = DiskShareMount.concurrent_get_or_create( share=self.diskshare, device=self.device, volume=COMPONENT['DiskShareMount'], ) self.processor = Processor( device=self.device, model=self.cm_processor, label=COMPONENT['Processor'], ) self.processor.save() self.memory = Memory( device=self.device, model=self.cm_memory, label=COMPONENT['Memory'], ) self.memory.save() self.storage = Storage( device=self.device, model=self.cm_storage, label=COMPONENT['Storage'], ) self.storage.save() self.fibrechannel = FibreChannel( device=self.device, model=self.cm_fibre, label=COMPONENT['Fibre'], physical_id='01234', ) self.fibrechannel.save() self.ethernet = Ethernet( model=self.cm_ethernet, device=self.device, mac=DEVICE['mac'], ) self.ethernet.save() self.software = Software( device=self.device, model=self.cm_software, label=COMPONENT['Software'], ) self.software.save() self.splunkusage = SplunkUsage( device=self.device, model=self.cm_splunkusage, ) self.splunkusage.save() self.operatingsystem = OperatingSystem( device=self.device, model=self.cm_operatingsystem, label=COMPONENT['OS'], ) self.operatingsystem.save()
class BulkeditTest(TestCase): """ Tests edit form Scenario: 1. Changes data in single device + test history change 2. Changes data in more than one device 3. Send form without select edit fields 4. Send form with empty save comment field 5. If data was added, check depreciation_date """ def setUp(self): self.client = login_as_su() self.deprecation_kind = DeprecationKind(months=24, remarks='Default') self.deprecation_kind.save() self.margin = MarginKind(margin=100, remarks='100%') self.margin.save() self.venture = Venture(name='VenureName') self.venture.save() self.role = VentureRole(venture=self.venture, name='VentureRole') self.role.save() self.device = Device.create( sn=DEVICE['sn'], barcode=DEVICE['barcode'], remarks=DEVICE['remarks'], model_name=DEVICE['model_name'], model_type=DeviceType.unknown, rack=DEVICE['rack'], position=DEVICE['position'], dc=DATACENTER, ) self.device.name = DEVICE['name'] self.device.save() self.device2 = Device.create( sn=DEVICE2['sn'], barcode=DEVICE2['barcode'], remarks=DEVICE2['remarks'], model_name=DEVICE2['model_name'], model_type=DeviceType.unknown, rack=DEVICE2['rack'], position=DEVICE2['position'], dc=DATACENTER, ) self.device2.name = DEVICE2['name'] self.device2.save() def test_single_device_edit(self): url = '/ui/search/bulkedit/' select_fields = ( 'venture', 'venture_role', 'margin_kind', 'deprecation_kind', ) date_fields = ( 'purchase_date', 'warranty_expiration_date', 'support_expiration_date', 'support_kind', ) text_fields = ( 'barcode', 'position', 'chassis_position', 'remarks', 'price', 'sn', 'verified' ) device_fields = [] for field_list in [select_fields, date_fields, text_fields]: device_fields.extend(field_list) post_data = { 'select': [self.device.id], # 1 'edit': device_fields, 'venture': self.venture.id, # 1 'venture_role': self.role.id, # 1 'verified': True, 'barcode': 'bc-2222-2222-2222-2222', 'position': '9', 'chassis_position': 10, 'remarks': 'Hello Ralph', 'margin_kind': self.margin.id, # 1 'deprecation_kind': self.deprecation_kind.id, # 1 'price': 100, 'sn': '2222-2222-2222-2222', 'purchase_date': datetime(2001, 1, 1, 0, 0), 'warranty_expiration_date': datetime(2001, 1, 2, 0, 0), 'support_expiration_date': datetime(2001, 1, 3, 0, 0), 'support_kind': datetime(2001, 1, 4, 0, 0), 'save_comment': 'Everything has changed', 'save': '', # save form } response = self.client.post(url, post_data) # Check if data from form is the same that data in database device = Device.objects.get(id=self.device.id) for field in device_fields: db_data = getattr(device, field) form_data = post_data[field] msg = 'FIELD: %s, DB: %s FORM: %s' % (field, db_data, form_data) if field in select_fields: self.assertEqual(db_data.id, form_data, msg) else: self.assertEqual(unicode(db_data), unicode(form_data), msg) # Check if change can see in History change history_device = HistoryChange.objects.filter( device=self.device, old_value='Very important device', new_value='Hello Ralph', ) self.assertEqual(history_device.count(), 1) self.assertEqual( history_device[0].comment, 'Everything has changed' ) def test_many_devices_edit(self): url = '/ui/search/bulkedit/' remarks = 'change remakrs in 2 devices' post_data = { 'select': [self.device.id, self.device2.id], 'edit': ['remarks'], 'remarks': remarks, 'save_comment': 'change remakrs', 'save': '', # save form } response = self.client.post(url, post_data) device, device2 = Device.objects.filter(remarks=remarks) self.assertEqual = (device.remarks, remarks) self.assertEqual = (device.remarks, device2.remarks) def test_send_form_without_selected_edit_fields(self): url = '/ui/search/bulkedit/' data_post = { 'select': self.device.id, 'bulk': '', # show form } response = self.client.post(url, data_post) self.assertEqual(response.status_code, 200) # form false response = self.client.post(url, {'select': self.device.id}) self.assertTrue(ERROR_MSG['no_mark_fields'] in response.content) def test_send_form_with_empty_save_comment_field(self): url = '/ui/search/bulkedit/?' post_data = { 'select': [self.device.id], 'edit': ['remarks'], 'remarks': 'Hello World!', 'save_comment': '', 'save': '', # save form } response = self.client.post(url, post_data) self.assertEqual(response.status_code, 200) # form false self.assertTrue(ERROR_MSG['empty_save_comment'] in response.content) self.assertFormError( response, 'form', 'save_comment', ERROR_MSG['empty_save_comment_field'] ) def test_calculation_depreciation_date(self): device = Device.objects.get(id=self.device.id) self.assertEqual(device.purchase_date, None) self.assertEqual(device.deprecation_kind, None) url = '/ui/search/bulkedit/?' post_data = { 'select': [self.device.id], # 1 'edit': ['purchase_date', 'deprecation_kind'], 'purchase_date': datetime(2001, 1, 4, 0, 0), 'deprecation_kind': self.deprecation_kind.id, # 1 'save_comment': 'Updated: purchase date and deprecation kind', 'save': '', # save form } response = self.client.post(url, post_data) updated_device = Device.objects.get(id=self.device.id) self.assertEqual( updated_device.purchase_date, datetime(2001, 1, 4, 0, 0) ) self.assertEqual(updated_device.deprecation_kind.months, 24) self.assertEqual( updated_device.deprecation_date, datetime(2003, 1, 4, 0, 0) ) # Check if purchase date,change deprecation date post_data = { 'select': [self.device.id], # 1 'edit': ['purchase_date'], 'purchase_date': datetime(2002, 1, 1, 0, 0), 'save_comment': 'Updated: purchase date', 'save': '', # save form } response = self.client.post(url, post_data, follow=True) updated_device2 = Device.objects.get(id=self.device.id) self.assertEqual( updated_device2.deprecation_date, datetime(2004, 1, 1, 0, 0) )
class DeploymentTest(TestCase): fixtures = [ '0_types.yaml', '1_attributes.yaml', '2_layers.yaml', '3_prefixes.yaml' ] def setUp(self): engine = settings.ISSUETRACKERS['default']['ENGINE'] if engine != '': raise ImproperlyConfigured( '''Expected ISSUETRACKERS['default']['ENGINE']='' got: %r''' % engine) # usual stuff self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() to = CIOwner( first_name='Bufallo', last_name='Kudłaczek', ) to.save() bo = CIOwner( first_name='Bill', last_name='Bąbelek', ) bo.save() ct = ContentType.objects.get_for_model(self.top_venture) CIImporter().import_all_ci([ct]) CIOwnership(owner=to, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.technical.id).save() CIOwnership(owner=bo, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.business.id).save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm, ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm) self.blade.name = 'blade' self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device = self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip = '192.168.1.1' self.deployment.hostname = 'test' self.deployment.save() def test_acceptance(self): # using issue null engine self.assertEqual(self.deployment.status, DeploymentStatus.open.id) self.deployment.create_issue() self.assertEqual(self.deployment.issue_key, '#123456') # status not changed, until plugin is run self.assertEqual(self.deployment.status, DeploymentStatus.open.id) # run ticket acceptance plugin ticket(self.deployment.id) # ticket already accepted self.deployment = Deployment.objects.get(id=self.deployment.id) self.assertEqual(self.deployment.status, DeploymentStatus.in_deployment.id) def test_owners(self): self.assertEqual(get_technical_owner(self.deployment.device), 'bufallo.kudlaczek') self.assertEqual(get_business_owner(self.deployment.device), 'bill.babelek') def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = device_type, dm.name = name dm.save() return dm
class CIImporterTest(TestCase): fixtures=['0_types.yaml', '1_attributes.yaml', '2_layers.yaml', '3_prefixes.yaml'] def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole(name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', name='Rack 1', model=dm) dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn='sn2', name='DC', model=dm) self.rack.parent=self.dc self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', name='blade1', model=dm ) self.blade.parent=self.rack self.blade.save() def add_model(self, name, device_type): dm = DeviceModel(); dm.model_type=device_type, dm.name=name dm.save() return dm def test_puppet_parser(self): hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save() p = PuppetAgentsImporter() yaml = open(os.getcwd()+'/cmdb/tests/samples/canonical.yaml').read() p.import_contents(yaml) yaml = open(os.getcwd()+'/cmdb/tests/samples/canonical_unchanged.yaml').read() p.import_contents(yaml) chg = CIChange.objects.all()[0] logs = PuppetLog.objects.filter(cichange=chg).order_by('id') self.assertEqual(chg.content_object.host, u's11401.dc2') self.assertEqual(chg.content_object.kind, u'apply') self.assertEqual(chg.ci,hostci) self.assertEqual(chg.type, 2) # check parsed logs self.assertEqual(len(logs), 16) self.assertEqual(logs[0].time, datetime.datetime(2010, 12, 31, 0, 56, 37, 290413)) # should not import puppet report which has 'unchanged' status self.assertEqual(CIChangePuppet.objects.filter(status='unchanged').count(), 0) def test_fisheye(self): """ Create Venture/Role and import as CI/CIRelations. Now import fisheye xml from samples/* files and compare if changes are properly saved into the database, and reconcilated. """ x = Venture(symbol='test_venture') x.save() y = VentureRole(name='test_role', venture=x) y.save() allegro_ci = CI(name='Allegro', type_id=CI_TYPES.VENTURE.id) allegro_ci.save() ct=ContentType.objects.get_for_model(x) test_venture, = CIImporter.import_all_ci([ct],asset_id=x.id) ct=ContentType.objects.get_for_model(y) test_role, = CIImporter.import_all_ci([ct],asset_id=y.id) CIImporter.import_relations(ContentType.objects.get_for_model(y),asset_id=y.id) with mock.patch('ralph.cmdb.integration.fisheye.Fisheye') as Fisheye: Fisheye.side_effect = MockFisheye x = pgi(fisheye_class=Fisheye) x.import_git() self.assertEqual(CIChangeGit.objects.filter( author__contains='*****@*****.**', #file_paths__contains='/test_venture' ).count(), 2) self.assertEqual(CIChange.objects.filter( ci=test_role, type=CI_CHANGE_TYPES.CONF_GIT.id, ).count(), 2) #todo # what if modified both core and venture? def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <---- dc <----rack <----blade """ # ventures and roles objs = [ self.top_venture, self.child_venture, self.role, self.child_role ] for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter.import_all_ci([ct],asset_id=o.id) for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter.import_relations(ct, asset_id=o.id) # devices objs = [ self.dc, self.rack, self.blade ] # create ci for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter.import_all_ci([ct],asset_id=o.id) # create relations for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter.import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(uid='dd-1') ci_rack = CI.objects.get(uid='dd-2') ci_blade = CI.objects.get(uid='dd-3') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, and should not change relations count cis=[] for o in objs: ct=ContentType.objects.get_for_model(o) cis.extend(CIImporter.import_all_ci([ct],asset_id=o.id)) # Rack should be inside DC try: CIRelation.objects.get(parent=ci_dc, child=ci_rack,type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack) ) # Blade should be inside Rack CIRelation.objects.get(parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id) # Relations count should be 6 self.assertEqual(len(CIRelation.objects.all()), 6)
def test_get_preboot_role(self): preboot = Preboot(name='test preboot') preboot.save() ven = Venture(name='test1', symbol='test1', preboot=preboot) ven.save() a = VentureRole(name='test1', preboot=preboot, venture_id=ven.id) a.save() b = VentureRole(name='test1 parent', parent_id=a.id, venture_id=ven.id) b.save() c = VentureRole(name='test1 parent parent', parent_id=b.id, venture_id=ven.id) c.save() self.assertEqual(a.get_preboot(), preboot) self.assertEqual(b.get_preboot(), preboot) self.assertEqual(c.get_preboot(), preboot)
class CIImporterTest(TestCase): fixtures = [ '0_types.yaml', '1_attributes.yaml', '2_layers.yaml', '3_prefixes.yaml' ] def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture( name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create( sn='sn1', model=dm ) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, sn='sn3', model=dm, ) self.blade.name = 'blade' self.blade.venture_role = self.child_role self.blade.parent = self.rack self.blade.save() def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = device_type dm.name = name dm.save() return dm def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <---- dc <----rack <----blade """ # ventures and roles objs = [self.top_venture, self.child_venture, self.role, self.child_role] for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [self.dc, self.rack, self.blade] # create ci for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') ci_venture = CI.objects.get(name='child_venture') ci_role = CI.objects.get(name='child_role') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, # and should not change relations count cis = [] for o in objs: ct = ContentType.objects.get_for_model(o) cis.extend( CIImporter().import_all_ci([ct], asset_id=o.id)) # Rack should be inside DC try: CIRelation.objects.get( parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get( parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id) # every device in composition chain should have relation # to Venture and Role as well. # test relations - # dc - no role no venture # rack - venture, no role # blade - venture and role venture_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_venture, type=CI_RELATION_TYPES.CONTAINS.id, ) # dc is *not* bound to venture self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in venture_rels]), set([(u'child_venture', u'rack', 1), (u'child_venture', u'blade', 1)]) ) role_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_role, type=CI_RELATION_TYPES.HASROLE.id, ) # only bottom level device has role, so one relation is made self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in role_rels]), set([(u'child_role', u'blade', 3)]), ) from ralph.cmdb.graphs import ImpactCalculator # summarize relations. self.assertEqual(CIRelation.objects.count(), 9) # calculate impact/spanning tree for CI structure root_ci = CI.objects.get(name='rack') calc = ImpactCalculator(root_ci) self.assertEqual( calc.find_affected_nodes(root_ci.id), ({2: 6, 5: 6, 6: None, 7: 6}, [6, 7, 2, 5]), )
def test_check_ip(self): terminator = NetworkTerminator(name='Test Terminator') terminator.save() data_center = DataCenter(name='Test date_center') data_center.save() network = Network(address='192.168.1.0/24', name='Test network', data_center=data_center) network.save() network.terminators = [terminator] network.save() subnetwork = Network(address='192.168.2.0/24', name='Test subnetwork', data_center=data_center) subnetwork.save() subnetwork.terminators = [terminator] subnetwork.save() main_venture = Venture(name='Main Venture') main_venture.save() main_venture.networks = [network, subnetwork] main_venture.save() second_network = Network(address='172.16.0.0/28', name='Test secound_network', data_center=data_center) second_network.save() second_network.terminators = [terminator] second_network.save() child_venture = Venture(name='Child Venture', parent=main_venture) child_venture.save() child_venture.networks = [second_network] child_venture.save() third_network = Network(address='66.6.6.0/29', name='Test third_network', data_center=data_center) third_network.save() third_network.terminators = [terminator] third_network.save() third_subnetwork = Network(address='66.6.7.0/29', name='Test third_subnetwork', data_center=data_center) third_subnetwork.save() third_subnetwork.terminators = [terminator] third_subnetwork.save() venture_role_main = VentureRole(name='Main Venture role', venture=child_venture) venture_role_main.save() venture_role_main.networks = [third_network, third_subnetwork] venture_role_main.save() fourth_network = Network(address='111.11.11.0/27', name='Test fourth_network', data_center=data_center) fourth_network.save() fourth_network.terminators = [terminator] fourth_network.save() venture_role_child = VentureRole(name='Child Venture role', venture=child_venture, parent=venture_role_main) venture_role_child.save() venture_role_child.networks = [fourth_network] venture_role_child.save() self.assertEqual(venture_role_child.check_ip("192.168.1.15"), True) self.assertEqual(venture_role_child.check_ip("192.168.2.15"), True) self.assertEqual(venture_role_child.check_ip("192.168.3.15"), False) self.assertEqual(venture_role_child.check_ip("172.16.0.10"), True) self.assertEqual(venture_role_child.check_ip("172.16.0.22"), False) self.assertEqual(venture_role_child.check_ip("66.6.6.5"), True) self.assertEqual(venture_role_child.check_ip("66.6.7.5"), True) self.assertEqual(venture_role_child.check_ip("66.6.8.10"), False) self.assertEqual(venture_role_child.check_ip("111.11.11.1"), True) self.assertEqual(venture_role_child.check_ip("111.11.11.44"), False)
class DeploymentTest(TestCase): fixtures = ["0_types.yaml", "1_attributes.yaml", "2_layers.yaml", "3_prefixes.yaml"] def setUp(self): engine = settings.ISSUETRACKERS["default"]["ENGINE"] if engine != "": raise ImproperlyConfigured("""Expected ISSUETRACKERS['default']['ENGINE']='' got: %r""" % engine) # usual stuff self.top_venture = Venture(name="top_venture") self.top_venture.save() self.child_venture = Venture(name="child_venture", parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name="role", venture=self.child_venture) self.role.save() self.child_role = VentureRole(name="child_role", venture=self.child_venture, parent=self.role) self.child_role.save() to = CIOwner(first_name="Bufallo", last_name="Kudłaczek") to.save() bo = CIOwner(first_name="Bill", last_name="Bąbelek") bo.save() ct = ContentType.objects.get_for_model(self.top_venture) CIImporter().import_all_ci([ct]) CIOwnership(owner=to, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.technical.id).save() CIOwnership(owner=bo, ci=CI.get_by_content_object(self.child_venture), type=CIOwnershipType.business.id).save() dm = self.add_model("DC model sample", DeviceType.data_center.id) self.dc = Device.create(sn="sn1", model=dm) self.dc.name = "dc" self.dc.save() dm = self.add_model("Rack model sample", DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn="sn2", model=dm) self.rack.parent = self.dc self.rack.name = "rack" self.rack.save() dm = self.add_model("Blade model sample", DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn="sn3", model=dm) self.blade.name = "blade" self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = "test_host2" self.deployment.device = self.blade self.deployment.mac = "10:9a:df:6f:af:01" self.deployment.ip = "192.168.1.1" self.deployment.hostname = "test" self.deployment.save() def test_acceptance(self): # using issue null engine self.assertEqual(self.deployment.status, DeploymentStatus.open.id) self.deployment.create_issue() self.assertEqual(self.deployment.issue_key, "#123456") # status not changed, until plugin is run self.assertEqual(self.deployment.status, DeploymentStatus.open.id) # run ticket acceptance plugin ticket(self.deployment.id) # ticket already accepted self.deployment = Deployment.objects.get(id=self.deployment.id) self.assertEqual(self.deployment.status, DeploymentStatus.in_deployment.id) def test_owners(self): self.assertEqual(get_technical_owner(self.deployment.device), "bufallo.kudlaczek") self.assertEqual(get_business_owner(self.deployment.device), "bill.babelek") def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = (device_type,) dm.name = name dm.save() return dm
class DeploymentTest(TestCase): def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture( name='child_venture', parent=self.top_venture, ) self.child_venture.save() self.role = VentureRole( name='role', venture=self.child_venture, ) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm, ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm, ) self.blade.name = 'blade' self.blade.parent = self.rack self.blade.save() self.deployment = Deployment() self.deployment.hostname = 'test_host2' self.deployment.device = self.blade self.deployment.mac = '10:9a:df:6f:af:01' self.deployment.ip = '192.168.1.1' self.deployment.hostname = 'test' self.deployment.save() def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = device_type, dm.name = name dm.save() return dm def test_archivization(self): id = self.deployment.id data = {} for field in self.deployment._meta.fields: data[field.name] = getattr(self.deployment, field.name) if field.name == 'mac': data[field.name] = MACAddressField.normalize(data[field.name]) self.deployment.archive() archive = ArchivedDeployment.objects.get(pk=id) archive_data = {} for field in archive._meta.fields: archive_data[field.name] = getattr(archive, field.name) if field.name == 'mac': archive_data[field.name] = MACAddressField.normalize( archive_data[field.name] ) self.assertEqual(data, archive_data) with self.assertRaises(Deployment.DoesNotExist): Deployment.objects.get(pk=id)
class CIImporterTest(TestCase): fixtures=['0_types.yaml', '1_attributes.yaml', '2_layers.yaml', '3_prefixes.yaml'] def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole(name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create( sn='sn1', model=dm ) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm ) self.rack.parent=self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, venturerole=self.child_role, sn='sn3', model=dm ) self.blade.name = 'blade' self.blade.parent=self.rack self.blade.save() def add_model(self, name, device_type): dm = DeviceModel(); dm.model_type=device_type, dm.name=name dm.save() return dm def test_puppet_parser(self): hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save() p = PuppetAgentsImporter() yaml = open(CURRENT_DIR + 'cmdb/tests/samples/canonical.yaml').read() p.import_contents(yaml) yaml = open(CURRENT_DIR + 'cmdb/tests/samples/canonical_unchanged.yaml').read() p.import_contents(yaml) chg = CIChange.objects.all()[0] logs = PuppetLog.objects.filter(cichange__host='s11401.dc2').order_by('id') self.assertEqual(chg.content_object.host, u's11401.dc2') self.assertEqual(chg.content_object.kind, u'apply') self.assertEqual(chg.ci,hostci) self.assertEqual(chg.type, 2) # check parsed logs self.assertEqual(len(logs), 16) time_iso = logs[0].time.isoformat().split('.')[0] self.assertEqual(time_iso, datetime.datetime(2010, 12, 31, 0, 56, 37).isoformat()) # should not import puppet report which has 'unchanged' status self.assertEqual(CIChangePuppet.objects.filter(status='unchanged').count(), 0) def test_fisheye(self): """ Create Venture/Role and import as CI/CIRelations. Now import fisheye xml from samples/* files and compare if changes are properly saved into the database, and reconcilated. """ x = Venture(symbol='test_venture') x.save() y = VentureRole(name='test_role', venture=x) y.save() allegro_ci = CI(name='Allegro', type_id=CI_TYPES.VENTURE.id) allegro_ci.save() ct=ContentType.objects.get_for_model(x) test_venture, = CIImporter().import_all_ci([ct],asset_id=x.id) ct=ContentType.objects.get_for_model(y) test_role, = CIImporter().import_all_ci([ct],asset_id=y.id) CIImporter().import_relations(ContentType.objects.get_for_model(y),asset_id=y.id) with mock.patch('ralph.cmdb.integration.lib.fisheye.Fisheye') as Fisheye: Fisheye.side_effect = MockFisheye x = pgi(fisheye_class=Fisheye) x.import_git() self.assertEqual(CIChangeGit.objects.filter( author__contains='*****@*****.**', #file_paths__contains='/test_venture' ).count(), 2) self.assertEqual(CIChange.objects.filter( ci=test_role, type=CI_CHANGE_TYPES.CONF_GIT.id, ).count(), 2) #todo # what if modified both core and venture? def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <---- dc <----rack <----blade """ # ventures and roles objs = [ self.top_venture, self.child_venture, self.role, self.child_role ] for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [ self.dc, self.rack, self.blade ] # create ci for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct=ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, and should not change relations count cis=[] for o in objs: ct=ContentType.objects.get_for_model(o) cis.extend(CIImporter().import_all_ci([ct],asset_id=o.id)) # Rack should be inside DC try: CIRelation.objects.get(parent=ci_dc, child=ci_rack,type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack) ) # Blade should be inside Rack CIRelation.objects.get(parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id) # Relations count should be 6 self.assertEqual(len(CIRelation.objects.all()), 6)
def test_get_preboot_role_none(self): ven = Venture(name='test1', symbol='test1') ven.save() a = VentureRole(name='test1', venture_id = ven.id) a.save() b = VentureRole(name='test1 parent', parent_id = a.id, venture_id = ven.id) b.save() c = VentureRole(name='test1 parent parent', parent_id = b.id, venture_id = ven.id) c.save() self.assertEqual(a.get_preboot(), None) self.assertEqual(b.get_preboot(), None) self.assertEqual(c.get_preboot(), None)
class CIImporterTest(TestCase): fixtures = ["0_types.yaml", "1_attributes.yaml", "2_layers.yaml", "3_prefixes.yaml"] def setUp(self): self.top_venture = Venture(name="top_venture") self.top_venture.save() self.child_venture = Venture(name="child_venture", parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name="role", venture=self.child_venture) self.role.save() self.child_role = VentureRole(name="child_role", venture=self.child_venture, parent=self.role) self.child_role.save() dm = self.add_model("DC model sample", DeviceType.data_center.id) self.dc = Device.create(sn="sn1", model=dm) self.dc.name = "dc" self.dc.save() dm = self.add_model("Rack model sample", DeviceType.rack_server.id) self.rack = Device.create(venture=self.child_venture, sn="sn2", model=dm) self.rack.parent = self.dc self.rack.name = "rack" self.rack.save() dm = self.add_model("Blade model sample", DeviceType.blade_server.id) self.blade = Device.create(venture=self.child_venture, venturerole=self.child_role, sn="sn3", model=dm) self.blade.name = "blade" self.blade.parent = self.rack self.blade.save() def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = (device_type,) dm.name = name dm.save() return dm def test_puppet_parser(self): hostci = CI(name="s11401.dc2", uid="mm-1") hostci.type_id = CI_TYPES.DEVICE.id hostci.save() p = PuppetAgentsImporter() yaml = open(CURRENT_DIR + "cmdb/tests/samples/canonical.yaml").read() p.import_contents(yaml) yaml = open(CURRENT_DIR + "cmdb/tests/samples/canonical_unchanged.yaml").read() p.import_contents(yaml) chg = CIChange.objects.all()[0] logs = PuppetLog.objects.filter(cichange__host="s11401.dc2").order_by("id") self.assertEqual(chg.content_object.host, "s11401.dc2") self.assertEqual(chg.content_object.kind, "apply") self.assertEqual(chg.ci, hostci) self.assertEqual(chg.type, 2) # check parsed logs self.assertEqual(len(logs), 16) time_iso = logs[0].time.isoformat().split(".")[0] self.assertEqual(time_iso, datetime.datetime(2010, 12, 31, 0, 56, 37).isoformat()) # should not import puppet report which has 'unchanged' status self.assertEqual(CIChangePuppet.objects.filter(status="unchanged").count(), 0) def test_fisheye(self): """ Create Venture/Role and import as CI/CIRelations. Now import fisheye xml from samples/* files and compare if changes are properly saved into the database, and reconcilated. """ x = Venture(symbol="test_venture") x.save() y = VentureRole(name="test_role", venture=x) y.save() allegro_ci = CI(name="Allegro", type_id=CI_TYPES.VENTURE.id) allegro_ci.save() ct = ContentType.objects.get_for_model(x) test_venture, = CIImporter().import_all_ci([ct], asset_id=x.id) ct = ContentType.objects.get_for_model(y) test_role, = CIImporter().import_all_ci([ct], asset_id=y.id) CIImporter().import_relations(ContentType.objects.get_for_model(y), asset_id=y.id) with mock.patch("ralph.cmdb.integration.lib.fisheye.Fisheye") as Fisheye: Fisheye.side_effect = MockFisheye x = pgi(fisheye_class=Fisheye) x.import_git() self.assertEqual( CIChangeGit.objects.filter( author__contains="*****@*****.**", # file_paths__contains='/test_venture' ).count(), 2, ) self.assertEqual(CIChange.objects.filter(ci=test_role, type=CI_CHANGE_TYPES.CONF_GIT.id).count(), 2) # todo # what if modified both core and venture? def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <---- dc <----rack <----blade """ # ventures and roles objs = [self.top_venture, self.child_venture, self.role, self.child_role] for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [self.dc, self.rack, self.blade] # create ci for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name="dc") ci_rack = CI.objects.get(name="rack") ci_blade = CI.objects.get(name="blade") self.assertEqual(ci_dc.layers.select_related()[0].name, "Hardware") self.assertEqual(ci_rack.layers.select_related()[0].name, "Hardware") self.assertEqual(ci_blade.layers.select_related()[0].name, "Hardware") # Reimport of relations should not raise Exception, and should not change relations count cis = [] for o in objs: ct = ContentType.objects.get_for_model(o) cis.extend(CIImporter().import_all_ci([ct], asset_id=o.id)) # Rack should be inside DC try: CIRelation.objects.get(parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail("Cant find relation %s %s %s" % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get(parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id) # Relations count should be 6 self.assertEqual(len(CIRelation.objects.all()), 6)
class CIImporterTest(TestCase): fixtures = [ '0_types.yaml', '1_attributes.yaml', '2_layers.yaml', '3_prefixes.yaml' ] def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture( name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center.id) self.dc = Device.create( sn='sn1', model=dm ) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server.id) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server.id) self.blade = Device.create( venture=self.child_venture, sn='sn3', model=dm, ) self.blade.name = 'blade' self.blade.venture_role = self.child_role self.blade.parent = self.rack self.blade.save() def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = device_type dm.name = name dm.save() return dm def test_puppet_parser(self): hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save() p = PuppetAgentsImporter() yaml = open( djoin(CURRENT_DIR, 'cmdb/tests/samples/canonical.yaml') ).read() p.import_contents(yaml) yaml = open( djoin(CURRENT_DIR, 'cmdb/tests/samples/canonical_unchanged.yaml') ).read() p.import_contents(yaml) chg = CIChange.objects.get(type=CI_CHANGE_TYPES.CONF_AGENT.id) logs = PuppetLog.objects.filter( cichange__host='s11401.dc2').order_by('id') self.assertEqual(chg.content_object.host, u's11401.dc2') self.assertEqual(chg.content_object.kind, u'apply') self.assertEqual(chg.ci, hostci) self.assertEqual(chg.type, 2) # check parsed logs self.assertEqual(len(logs), 16) time_iso = logs[0].time.isoformat().split('.')[0] self.assertEqual(time_iso, datetime.datetime( 2010, 12, 31, 0, 56, 37).isoformat()) # should not import puppet report which has 'unchanged' status self.assertEqual( CIChangePuppet.objects.filter(status='unchanged').count(), 0) def test_fisheye(self): """ Create Venture/Role and import as CI/CIRelations. Now import fisheye xml from samples/* files and compare if changes are properly saved into the database, and reconcilated. """ x = Venture(symbol='test_venture') x.save() y = VentureRole(name='test_role', venture=x) y.save() allegro_ci = CI(name='Allegro', type_id=CI_TYPES.VENTURE.id) allegro_ci.save() ct = ContentType.objects.get_for_model(x) test_venture, = CIImporter().import_all_ci([ct], asset_id=x.id) ct = ContentType.objects.get_for_model(y) test_role, = CIImporter().import_all_ci([ct], asset_id=y.id) CIImporter().import_relations( ContentType.objects.get_for_model(y), asset_id=y.id) with mock.patch( 'ralph.cmdb.integration.lib.fisheye.Fisheye') as Fisheye: Fisheye.side_effect = MockFisheye x = pgi(fisheye_class=Fisheye) x.import_git() self.assertEqual(CIChangeGit.objects.filter( author__contains='*****@*****.**', #file_paths__contains='/test_venture' ).count(), 2) self.assertEqual(CIChange.objects.filter( ci=test_role, type=CI_CHANGE_TYPES.CONF_GIT.id, ).count(), 2) #todo # what if modified both core and venture? def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <---- dc <----rack <----blade """ # ventures and roles objs = [self.top_venture, self.child_venture, self.role, self.child_role] for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [self.dc, self.rack, self.blade] # create ci for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_role = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') ci_venture = CI.objects.get(name='child_venture') ci_role = CI.objects.get(name='child_role') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, # and should not change relations count cis = [] for o in objs: ct = ContentType.objects.get_for_model(o) cis.extend( CIImporter().import_all_ci([ct], asset_id=o.id)) # Rack should be inside DC try: CIRelation.objects.get( parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get( parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id) # every device in composition chain should have relation # to Venture and Role as well. # test relations - # dc - no role no venture # rack - venture, no role # blade - venture and role venture_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_venture, type=CI_RELATION_TYPES.CONTAINS.id, ) # dc is *not* bound to venture self.assertEqual( set([(x.parent.name, x.child.name, x.type) for x in venture_rels]), set([(u'child_venture', u'rack', 1), (u'child_venture', u'blade', 1)]) ) role_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_role, type=CI_RELATION_TYPES.HASROLE.id, ) # only bottom level device has role, so one relation is made self.assertEqual( set([(x.parent.name, x.child.name, x.type) for x in role_rels]), set([(u'child_role', u'blade', 3)]), ) from ralph.cmdb.graphs import ImpactCalculator # summarize relations. self.assertEqual(len(CIRelation.objects.all()), 9) # calculate impact/spanning tree for CI structure calc = ImpactCalculator() self.assertEqual( calc.find_affected_nodes(1), ({1: None, 2: 1, 4: 7, 6: 2, 7: 2}, [1, 2, 6, 7, 4]) )
class CIImporterTest(TestCase): """Test creating CI's and relations between them from base ralph data types.""" def setUp(self): self.top_venture = Venture(name='top_venture') self.top_venture.save() self.child_venture = Venture(name='child_venture', parent=self.top_venture) self.child_venture.save() self.role = VentureRole(name='role', venture=self.child_venture) self.role.save() self.child_role = VentureRole( name='child_role', venture=self.child_venture, parent=self.role, ) self.child_role.save() dm = self.add_model('DC model sample', DeviceType.data_center) self.dc = Device.create(sn='sn1', model=dm) self.dc.name = 'dc' self.dc.save() dm = self.add_model('Rack model sample', DeviceType.rack_server) self.rack = Device.create( venture=self.child_venture, sn='sn2', model=dm, ) self.rack.parent = self.dc self.rack.name = 'rack' self.rack.save() dm = self.add_model('Blade model sample', DeviceType.blade_server) self.blade = Device.create( venture=self.child_venture, sn='sn3', model=dm, ) self.blade.name = 'blade' self.blade.venture_role = self.child_role self.blade.parent = self.rack self.blade.save() def add_model(self, name, device_type): dm = DeviceModel() dm.model_type = device_type dm.name = name dm.save() return dm def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <----dc <----rack <----blade """ # ventures and roles objs = [ self.top_venture, self.child_venture, self.role, self.child_role ] for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [self.dc, self.rack, self.blade] # create ci for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') ci_venture = CI.objects.get(name='child_venture') ci_role = CI.objects.get(name='child_role') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, # and should not change relations count cis = [] for o in objs: ct = ContentType.objects.get_for_model(o) cis.extend(CIImporter().import_all_ci([ct], asset_id=o.id), ) # Rack should be inside DC try: CIRelation.objects.get(parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get( parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id, ) # every device in composition chain should have relation # to Venture and Role as well. # test relations - # dc - no role no venture # rack - venture, no role # blade - venture and role venture_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_venture, type=CI_RELATION_TYPES.CONTAINS.id, ) # dc is *not* bound to venture self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in venture_rels]), set([(u'child_venture', u'rack', 1), (u'child_venture', u'blade', 1)])) role_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_role, type=CI_RELATION_TYPES.HASROLE.id, ) # only bottom level device has role, so one relation is made self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in role_rels]), set([(u'child_role', u'blade', 3)]), ) from ralph.cmdb.graphs import ImpactCalculator # summarize relations. self.assertEqual(CIRelation.objects.count(), 9) # calculate impact/spanning tree for CI structure root_ci = CI.objects.get(name='rack') calc = ImpactCalculator(root_ci) self.assertEqual( calc.find_affected_nodes(root_ci.id), ({ 2: 6, 5: 6, 6: None, 7: 6 }, [6, 7, 2, 5]), )