def test_remove_device(self): ci = CI.get_by_content_object(self.device) self.assertIsNotNone(ci) self.device.delete() ci = CI.get_by_content_object(self.device) self.assertFalse(ci)
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 test_create_puppet_change(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 create_cis(self): self.ci1 = CI( uid='uid-ci1', type=self.citype1, barcode='barcodeci1', name='ciname1', ) self.ci1.save() self.ci1.layers = [self.cilayer1, self.cilayer2] self.ci1.save() self.ci2 = CI( uid='uid-ci2', type=self.citype2, barcode='barcodeci2', name='ciname2', ) self.ci2.save() self.ci2.layers = [self.cilayer1] self.ci2.save() self.ci3 = CI( uid='other-ci3', type=self.citype2, barcode='otherbarcodeci3', name='otherci', ) self.ci3.save() self.ci3.layers = [self.cilayer2] self.ci3.save()
def test_remove_venture_role(self): ci = CI.get_by_content_object(self.venture_role) self.assertIsNotNone(ci) self.venture_role.delete() ci = CI.get_by_content_object(self.venture_role) self.assertFalse(ci)
def test_remove_datacenter(self): ci = CI.get_by_content_object(self.data_center) self.assertIsNotNone(ci) self.data_center.delete() ci = CI.get_by_content_object(self.data_center) self.assertFalse(ci)
def test_remove_network(self): ci = CI.get_by_content_object(self.network) self.assertIsNotNone(ci) self.network.delete() ci = CI.get_by_content_object(self.network) self.assertFalse(ci)
def test_remove_businessline(self): ci = CI.get_by_content_object(self.business_line) self.assertIsNotNone(ci) self.business_line.delete() ci = CI.get_by_content_object(self.business_line) self.assertFalse(ci)
def test_update_ci(self): self.venture.name = 'New Venture name' self.venture.save() venture = Venture.objects.get(id=self.venture.id) ci = CI.get_by_content_object(venture) self.assertEqual(venture.name, ci.name) self.assertEqual(ci.name, 'New Venture name') self.device.barcode = '1111-1111-2222-2222' self.device.save() device = Device.objects.get(id=self.device.id) ci = CI.get_by_content_object(device) self.assertEqual(device.barcode, ci.barcode) self.assertEqual(ci.barcode, '1111-1111-2222-2222')
def test_ci_relations_cycle(self): response_ci1 = self.add_ci(name="CI1") response_ci2 = self.add_ci(name="CI2") response_ci3 = self.add_ci(name="CI3") self.assertEqual(response_ci1.status_code, 302) self.assertEqual(response_ci2.status_code, 302) self.assertEqual(response_ci3.status_code, 302) ci1 = CI.objects.get(name="CI1") ci2 = CI.objects.get(name="CI2") ci3 = CI.objects.get(name="CI3") response_r = self.add_ci_relation( parent_ci=ci1, child_ci=ci2, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE ) self.assertEqual(response_r.status_code, 302) rel = CIRelation.objects.get(parent_id=ci1.id, child_id=ci2.id, type=CI_RELATION_TYPES.HASROLE) self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE) response_r = self.add_ci_relation( parent_ci=ci2, child_ci=ci3, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE ) self.assertEqual(response_r.status_code, 302) rel = CIRelation.objects.get(parent_id=ci2.id, child_id=ci3.id, type=CI_RELATION_TYPES.HASROLE) self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE) response_r = self.add_ci_relation( parent_ci=ci3, child_ci=ci1, relation_type="rel_parent", relation_kind=CI_RELATION_TYPES.HASROLE ) self.assertEqual(response_r.status_code, 302) rel = CIRelation.objects.get(parent_id=ci3.id, child_id=ci1.id, type=CI_RELATION_TYPES.HASROLE) self.assertEqual(rel.type, CI_RELATION_TYPES.HASROLE) cycle = CI.get_cycle() self.assertEqual(cycle, [1, 2, 3])
def test_add_ci(self): assets = [ self.venture, self.venture_role, self.data_center, self.network, self.device, self.service, self.business_line, ] for asset in assets: ci = CI.get_by_content_object(asset) self.assertIsNotNone(ci)
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 create_issue(self): bowner = get_business_owner(self.device) towner = get_technical_owner(self.device) params = dict( ci_uid = CI.get_uid_by_content_object(self.device), description = 'Please accept in order to continue deployment.', summary = '%s - acceptance request for deployment' % unicode(self.device), technical_assignee=towner, business_assignee=bowner, ) super(Deployment, self).create_issue(params, DEFAULT_ASSIGNEE)
def test_create_puppet_change(self): hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save() p = PuppetAgentsImporter() changed_yaml = open( os.path.join(CURRENT_DIR, 'cmdb/tests/samples/canonical.yaml'), ).read() p.import_contents(changed_yaml) unchanged_yaml = open( os.path.join( CURRENT_DIR, 'cmdb/tests/samples/canonical_unchanged.yaml'), ).read() p.import_contents(unchanged_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) # should drop request, when the same configuration ver. + hostname # is already present in the database. # Now, feed importer with the same yaml the second time... p.import_contents(changed_yaml) # No change should be registered at this time. self.assertEquals( chg, CIChange.objects.get(type=CI_CHANGE_TYPES.CONF_AGENT.id) ) self.assertEquals( CIChangePuppet.objects.count(), 1 )
def create_issue(self): bowner = get_business_owner(self.device) towner = get_technical_owner(self.device) params = dict( ci_uid=CI.get_uid_by_content_object(self.device), description='Please accept in order to continue deployment.', summary='%s - acceptance request for deployment' % unicode(self.device), technical_assignee=towner, business_assignee=bowner, ) super(Deployment, self).create_issue(params, DEFAULT_ASSIGNEE)
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_create_ci_generate_change(self): # TICKETS REGISTRATION IN THIS TEST IS DISABLED. # first case - automatic change hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save() # not registered, because not user - driven change self.assertEqual( set([(x.content_object.old_value, x.content_object.new_value, x.content_object.field_name, x.content_object.user_id, x.registration_type) for x in CIChange.objects.all()]), set([(u'None', u'Device', u'type', None, CI_CHANGE_REGISTRATION_TYPES.NOT_REGISTERED.id), (u'None', u'1', u'id', None, CI_CHANGE_REGISTRATION_TYPES.NOT_REGISTERED.id)]) ) hostci.delete() # second case - manual change user = User.objects.create_user( 'john', '*****@*****.**', 'johnpassword') # john reigstered change, change should be at WAITING because # registering is not enabled in config hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save(user=user) self.assertEqual( set([(x.content_object.old_value, x.content_object.new_value, x.content_object.field_name, x.content_object.user_id, x.registration_type) for x in CIChange.objects.all()]), set([ (u'None', u'Device', u'type', 1, CI_CHANGE_REGISTRATION_TYPES.WAITING.id), (u'None', u'1', u'id', 1, CI_CHANGE_REGISTRATION_TYPES.WAITING.id), ]) )
def setUp(self): # create Venture and CI self.venture = Venture.objects.create(name='TestVenture') uid = CI.get_uid_by_content_object(self.venture) self.venture_ci_id = CI.objects.create( name='TestVentureCI', uid=uid, type_id=4).pk # create VentureRole and CI v = Venture.objects.create(name='SomeAssignedVenture') self.venture_role = VentureRole.objects.create(name='TestVentureRole', venture=v) uid = CI.get_uid_by_content_object(self.venture_role) self.venture_role_ci_id = CI.objects.create( name='TestVentureRoleCI', uid=uid, type_id=5).pk # create DataCenter and CI self.data_center = DataCenter.objects.create(name='DC123') uid = CI.get_uid_by_content_object(self.data_center) self.data_center_ci_id = CI.objects.create( name='TestDataCenterCI', uid=uid, type_id=9).pk # create Network and CI dc = DataCenter.objects.create(name='SomeDC') self.network = Network.objects.create(name='TestNetwork', address='192.168.56.1', data_center=dc) uid = CI.get_uid_by_content_object(self.network) self.network_ci_id = CI.objects.create( name='TestNetworkCI', uid=uid, type_id=8).pk # create Device and CI device_model = DeviceModel.objects.create( name='SomeDeviceModel', type=DeviceType.rack_server.id) self.device = Device.create(name='TestDevice', sn='sn123', model=device_model) uid = CI.get_uid_by_content_object(self.device) self.device_ci_id = CI.objects.create( name='TestDeviceCI', uid=uid, type_id=2).pk # create Service and CI bl = BusinessLine.objects.create(name='Some Business Line') self.service = Service.objects.create(name='someservice.com', external_key='abc123', business_line=bl) uid = CI.get_uid_by_content_object(self.service) self.service_ci_id = CI.objects.create( name='TestServiceCI', uid=uid, type_id=7).pk # create BusinessLIne and CI self.business_line = BusinessLine.objects.create( name='TestBusinessLine') uid = CI.get_uid_by_content_object(self.business_line) self.business_line_ci_id = CI.objects.create( name='TestBusinessLineCI', uid=uid, type_id=6).pk
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)
def setUp(self): self.puppet_cv = "v%s" % random.randrange(0, 1000) puppet_bundle = Bundle( data={ 'configuration_version': self.puppet_cv, 'host': 's11111.dc2', 'kind': 'apply', 'status': 'failed', 'time': '2012-11-14 13:00:00' }) puppet_resource = CIChangePuppetResource() puppet_resource.obj_create(bundle=puppet_bundle) self.git_changeset = "change:%s" % random.randrange(0, 1000) self.git_comment = "comment:%s" % random.randrange(0, 1000) git_bundle = Bundle( data={ 'author': 'Jan Kowalski', 'changeset': self.git_changeset, 'comment': self.git_comment, 'file_paths': '/some/path', }) git_resource = CIChangeGitResource() git_resource.obj_create(bundle=git_bundle) temp_venture = Venture.objects.create(name='TempTestVenture') self.ci = CI.objects.create( name='TempTestVentureCI', uid=CI.get_uid_by_content_object(temp_venture), type_id=4) self.cmdb_new_value = 'nv_%s' % random.randrange(0, 1000) self.cmdb_old_value = 'ov_%s' % random.randrange(0, 1000) cmdb_bundle = Bundle( data={ 'ci': '/api/v0.9/ci/%d/' % self.ci.pk, 'comment': 'test api', 'field_name': 'child', 'new_value': self.cmdb_new_value, 'old_value': self.cmdb_old_value, 'time': '2012-11-15 12:00:00' }) cmdb_resource = CIChangeCMDBHistoryResource() cmdb_resource.obj_create(bundle=cmdb_bundle)
def get_context_data(self, **kwargs): ret = super(BaseMixin, self).get_context_data(**kwargs) details = self.kwargs.get('details', 'info') profile = self.request.user.get_profile() has_perm = profile.has_perm footer_items = [] mainmenu_items = [ MenuItem('Ventures', fugue_icon='fugue-store', view_name='ventures') ] if has_perm(Perm.read_dc_structure): mainmenu_items.append( MenuItem('Racks', fugue_icon='fugue-building', view_name='racks')) if has_perm(Perm.read_network_structure): mainmenu_items.append( MenuItem('Networks', fugue_icon='fugue-weather-clouds', view_name='networks')) if has_perm(Perm.read_device_info_reports): mainmenu_items.append( MenuItem('Reports', fugue_icon='fugue-report', view_name='reports')) if has_perm(Perm.edit_device_info_financial): mainmenu_items.append( MenuItem('Catalog', fugue_icon='fugue-paper-bag', view_name='catalog')) if ('ralph.cmdb' in settings.INSTALLED_APPS and has_perm(Perm.read_configuration_item_info_generic)): mainmenu_items.append( MenuItem('CMDB', fugue_icon='fugue-thermometer', href='/cmdb/changes/timeline')) if settings.BUGTRACKER_URL: mainmenu_items.append( MenuItem('Report a bug', fugue_icon='fugue-bug', pull_right=True, href=settings.BUGTRACKER_URL)) if self.request.user.is_staff: footer_items.append( MenuItem('Admin', fugue_icon='fugue-toolbox', href='/admin')) footer_items.append( MenuItem('%s (logout)' % self.request.user, fugue_icon='fugue-user', view_name='logout', view_args=[details or 'info', ''], pull_right=True, href=settings.LOGOUT_URL)) mainmenu_items.append( MenuItem('Advanced search', name='search', fugue_icon='fugue-magnifier', view_args=[details or 'info', ''], view_name='search', pull_right=True)) tab_items = [] venture = (self.venture if self.venture and self.venture != '*' else None) or (self.object.venture if self.object else None) def tab_href(name): return '../%s/%s?%s' % (name, self.object.id if self.object else '', self.request.GET.urlencode()) if has_perm(Perm.read_device_info_generic, venture): tab_items.extend([ MenuItem('Info', fugue_icon='fugue-wooden-box', href=tab_href('info')), MenuItem('Components', fugue_icon='fugue-box', href=tab_href('components')), MenuItem('Software', fugue_icon='fugue-disc', href=tab_href('software')), MenuItem('Addresses', fugue_icon='fugue-network-ip', href=tab_href('addresses')), ]) if has_perm(Perm.edit_device_info_financial, venture): tab_items.extend([ MenuItem('Prices', fugue_icon='fugue-money-coin', href=tab_href('prices')), ]) if has_perm(Perm.read_device_info_financial, venture): tab_items.extend([ MenuItem('Costs', fugue_icon='fugue-wallet', href=tab_href('costs')), ]) if has_perm(Perm.read_device_info_history, venture): tab_items.extend([ MenuItem('History', fugue_icon='fugue-hourglass', href=tab_href('history')), ]) if has_perm(Perm.read_device_info_support, venture): tab_items.extend([ MenuItem('Purchase', fugue_icon='fugue-baggage-cart-box', href=tab_href('purchase')), ]) if has_perm(Perm.run_discovery, venture): tab_items.extend([ MenuItem('Discover', fugue_icon='fugue-flashlight', href=tab_href('discover')), ]) if ('ralph.cmdb' in settings.INSTALLED_APPS and has_perm(Perm.read_configuration_item_info_generic)): ci = '' try: device = self.kwargs['device'] except KeyError: device = None if device: ci = CI.get_by_content_object(Device.objects.get(pk=device)) if ci: tab_items.extend([ MenuItem('CMDB', fugue_icon='fugue-thermometer', href='/cmdb/ci/view/%s' % ci.id), ]) if has_perm(Perm.read_device_info_reports, venture): tab_items.extend([ MenuItem('Reports', fugue_icon='fugue-reports-stack', href=tab_href('reports')), ]) if details == 'bulkedit': tab_items.extend([ MenuItem('Bulk edit', fugue_icon='fugue-pencil-field', name='bulkedit'), ]) ret.update({ 'section': self.section, 'details': details, 'mainmenu_items': mainmenu_items, 'footer_items': footer_items, 'url_query': self.request.GET, 'search_url': reverse('search', args=[details, '']), 'user': self.request.user, 'tab_items': tab_items, 'show_bulk': has_perm(Perm.bulk_edit), }) return ret
class CMDBApiTest(TestCase): def setUp(self): self.creatre_user() self.create_cilayers() self.create_citypes() self.create_owners() self.create_cis() self.create_ownerships() self.create_relations() def creatre_user(self): self.user = User.objects.create_user( 'api_user', '*****@*****.**', 'password' ) self.user.save() self.api_key = ApiKey.objects.get(user=self.user) self.data = { 'format': 'json', 'username': self.user.username, 'api_key': self.api_key.key } def create_cilayers(self): self.cilayer1 = CILayer(name='layer1') self.cilayer1.save() self.cilayer2 = CILayer(name='layer2') self.cilayer2.save() def create_citypes(self): self.citype1 = CIType(name='type1') self.citype1.save() self.citype2 = CIType(name='type2') self.citype2.save() def create_owners(self): self.owner1 = CIOwner( first_name='first_name_owner1', last_name='last_name_owner1', email='*****@*****.**' ) self.owner1.save() self.owner2 = CIOwner( first_name='first_name_owner2', last_name='last_name_owner2', email='*****@*****.**' ) self.owner2.save() def create_cis(self): self.ci1 = CI( uid='uid-ci1', type=self.citype1, barcode='barcodeci1', name='ciname1', ) self.ci1.save() self.ci1.layers = [self.cilayer1, self.cilayer2] self.ci1.save() self.ci2 = CI( uid='uid-ci2', type=self.citype2, barcode='barcodeci2', name='ciname2', ) self.ci2.save() self.ci2.layers = [self.cilayer1] self.ci2.save() self.ci3 = CI( uid='other-ci3', type=self.citype2, barcode='otherbarcodeci3', name='otherci', ) self.ci3.save() self.ci3.layers = [self.cilayer2] self.ci3.save() def create_ownerships(self): self.ciownership1 = CIOwnership( ci=self.ci1, owner=self.owner1, type=CIOwnershipType.technical ) self.ciownership1.save() self.ciownership2 = CIOwnership( ci=self.ci1, owner=self.owner2, type=CIOwnershipType.business ) self.ciownership2.save() self.ciownership3 = CIOwnership( ci=self.ci2, owner=self.owner2, type=CIOwnershipType.business ) self.ciownership3.save() def create_relations(self): self.relation1 = CIRelation( parent=self.ci1, child=self.ci2, type=CI_RELATION_TYPES.CONTAINS, ) self.relation1.save() self.relation2 = CIRelation( parent=self.ci2, child=self.ci3, type=CI_RELATION_TYPES.HASROLE, ) self.relation2.save() def test_layers(self): path = "/api/v0.9/cilayers/" response = self.client.get(path=path, data=self.data, format='json') json_string = response.content json_data = json.loads(json_string) resource_uris = [x['resource_uri'] for x in json_data['objects']] response = self.client.get( path=resource_uris[0], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['name'], self.cilayer1.name) response = self.client.get( path=resource_uris[1], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['name'], self.cilayer2.name) def test_types(self): path = "/api/v0.9/citypes/" response = self.client.get(path=path, data=self.data, format='json') json_string = response.content json_data = json.loads(json_string) resource_uris = [x['resource_uri'] for x in json_data['objects']] response = self.client.get( path=resource_uris[0], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['name'], self.citype1.name) response = self.client.get( path=resource_uris[1], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['name'], self.citype2.name) def test_ci(self): path = "/api/v0.9/ci/" response = self.client.get(path=path, data=self.data, format='json') json_string = response.content json_data = json.loads(json_string) resource_uris = [x['resource_uri'] for x in json_data['objects']] response = self.client.get( path=resource_uris[0], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name) self.assertEqual(json_data['layers'][1]['name'], self.cilayer2.name) self.assertEqual(json_data['barcode'], self.ci1.barcode) self.assertEqual(json_data['name'], self.ci1.name) self.assertEqual(json_data['type']['name'], self.ci1.type.name) self.assertEqual(json_data['uid'], self.ci1.uid) self.assertEqual( json_data['technical_owner'][0]['username'], '{}.{}'.format(self.owner1.first_name, self.owner1.last_name) ) response = self.client.get( path=resource_uris[1], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name) self.assertEqual(json_data['barcode'], self.ci2.barcode) self.assertEqual(json_data['name'], self.ci2.name) self.assertEqual(json_data['type']['name'], self.ci2.type.name) self.assertEqual(json_data['uid'], self.ci2.uid) self.assertEqual( json_data['technical_owner'][0]['username'], '{}.{}'.format(self.owner2.first_name, self.owner2.last_name) ) def test_relations(self): path = "/api/v0.9/cirelation/" response = self.client.get(path=path, data=self.data, format='json') json_string = response.content json_data = json.loads(json_string) resource_uris = [x['resource_uri'] for x in json_data['objects']] response = self.client.get( path=resource_uris[0], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['parent'], self.ci1.id) self.assertEqual(json_data['child'], self.ci2.id) self.assertEqual(json_data['type'], CI_RELATION_TYPES.CONTAINS) response = self.client.get( path=resource_uris[1], data=self.data, format='json', ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['parent'], self.ci2.id) self.assertEqual(json_data['child'], self.ci3.id) self.assertEqual(json_data['type'], CI_RELATION_TYPES.HASROLE) def test_ci_filter_exact(self): path = "/api/v0.9/ci/" self.data['name__exact'] = 'otherci' response = self.client.get(path=path, data=self.data, format='json') json_string = response.content json_data = json.loads(json_string) resource_uris = [x['resource_uri'] for x in json_data['objects']] self.assertEqual(len(resource_uris), 1) response = self.client.get( path=resource_uris[0], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['layers'][0]['name'], self.cilayer2.name) self.assertEqual(json_data['barcode'], self.ci3.barcode) self.assertEqual(json_data['name'], self.ci3.name) self.assertEqual(json_data['type']['name'], self.ci3.type.name) self.assertEqual(json_data['uid'], self.ci3.uid) del(self.data['name__exact']) def test_ci_filter_startswith(self): path = "/api/v0.9/ci/" self.data['name__startswith'] = 'ciname' response = self.client.get(path=path, data=self.data, format='json') json_string = response.content json_data = json.loads(json_string) resource_uris = [x['resource_uri'] for x in json_data['objects']] self.assertEqual(len(resource_uris), 2) response = self.client.get( path=resource_uris[0], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name) self.assertEqual(json_data['barcode'], self.ci1.barcode) self.assertEqual(json_data['name'], self.ci1.name) self.assertEqual(json_data['type']['name'], self.ci1.type.name) self.assertEqual(json_data['uid'], self.ci1.uid) response = self.client.get( path=resource_uris[1], data=self.data, format='json' ) json_string = response.content json_data = json.loads(json_string) self.assertEqual(json_data['layers'][0]['name'], self.cilayer1.name) self.assertEqual(json_data['barcode'], self.ci2.barcode) self.assertEqual(json_data['name'], self.ci2.name) self.assertEqual(json_data['type']['name'], self.ci2.type.name) self.assertEqual(json_data['uid'], self.ci2.uid) del(self.data['name__startswith'])
def get_context_data(self, **kwargs): ret = super(BaseMixin, self).get_context_data(**kwargs) details = self.kwargs.get('details', 'info') profile = self.request.user.get_profile() has_perm = profile.has_perm footer_items = [] mainmenu_items = [ MenuItem('Ventures', fugue_icon='fugue-store', view_name='ventures') ] if has_perm(Perm.read_dc_structure): mainmenu_items.append( MenuItem('Racks', fugue_icon='fugue-building', view_name='racks')) if has_perm(Perm.read_network_structure): mainmenu_items.append( MenuItem('Networks', fugue_icon='fugue-weather-clouds', view_name='networks')) if has_perm(Perm.read_device_info_reports): mainmenu_items.append( MenuItem('Reports', fugue_icon='fugue-report', view_name='reports')) if has_perm(Perm.edit_device_info_financial): mainmenu_items.append( MenuItem('Catalog', fugue_icon='fugue-paper-bag', view_name='catalog')) if ('ralph.cmdb' in settings.INSTALLED_APPS and has_perm(Perm.read_configuration_item_info_generic)): mainmenu_items.append( MenuItem('CMDB', fugue_icon='fugue-thermometer', href='/cmdb/changes/timeline') ) if settings.BUGTRACKER_URL: mainmenu_items.append( MenuItem( 'Report a bug', fugue_icon='fugue-bug', pull_right=True, href=settings.BUGTRACKER_URL) ) if self.request.user.is_staff: footer_items.append( MenuItem('Admin', fugue_icon='fugue-toolbox', href='/admin')) footer_items.append( MenuItem('%s (logout)' % self.request.user, fugue_icon='fugue-user', view_name='logout', view_args=[details or 'info', ''], pull_right=True, href=settings.LOGOUT_URL)) mainmenu_items.append( MenuItem('Advanced search', name='search', fugue_icon='fugue-magnifier', view_args=[details or 'info', ''], view_name='search', pull_right=True)) tab_items = [] venture = ( self.venture if self.venture and self.venture != '*' else None ) or ( self.object.venture if self.object else None ) def tab_href(name): return '../%s/%s?%s' % ( name, self.object.id if self.object else '', self.request.GET.urlencode() ) if has_perm(Perm.read_device_info_generic, venture): tab_items.extend([ MenuItem('Info', fugue_icon='fugue-wooden-box', href=tab_href('info')), MenuItem('Components', fugue_icon='fugue-box', href=tab_href('components')), MenuItem('Software', fugue_icon='fugue-disc', href=tab_href('software')), MenuItem('Addresses', fugue_icon='fugue-network-ip', href=tab_href('addresses')), ]) if has_perm(Perm.edit_device_info_financial, venture): tab_items.extend([ MenuItem('Prices', fugue_icon='fugue-money-coin', href=tab_href('prices')), ]) if has_perm(Perm.read_device_info_financial, venture): tab_items.extend([ MenuItem('Costs', fugue_icon='fugue-wallet', href=tab_href('costs')), ]) if has_perm(Perm.read_device_info_history, venture): tab_items.extend([ MenuItem('History', fugue_icon='fugue-hourglass', href=tab_href('history')), ]) if has_perm(Perm.read_device_info_support, venture): tab_items.extend([ MenuItem('Purchase', fugue_icon='fugue-baggage-cart-box', href=tab_href('purchase')), ]) if has_perm(Perm.run_discovery, venture): tab_items.extend([ MenuItem('Discover', fugue_icon='fugue-flashlight', href=tab_href('discover')), ]) if ('ralph.cmdb' in settings.INSTALLED_APPS and has_perm(Perm.read_configuration_item_info_generic)): ci = '' try: device = self.kwargs['device'] except KeyError: device = None if device: ci = CI.get_by_content_object(Device.objects.get(pk=device)) if ci: tab_items.extend([ MenuItem('CMDB', fugue_icon='fugue-thermometer', href='/cmdb/ci/view/%s' % ci.id), ]) if has_perm(Perm.read_device_info_reports, venture): tab_items.extend([ MenuItem('Reports', fugue_icon='fugue-reports-stack', href=tab_href('reports')), ]) if details == 'bulkedit': tab_items.extend([ MenuItem('Bulk edit', fugue_icon='fugue-pencil-field', name='bulkedit'), ]) ret.update({ 'section': self.section, 'details': details, 'mainmenu_items': mainmenu_items, 'footer_items': footer_items, 'url_query': self.request.GET, 'search_url': reverse('search', args=[details, '']), 'user': self.request.user, 'tab_items': tab_items, 'show_bulk': has_perm(Perm.bulk_edit), }) return ret
def test_create_ci_generate_change(self): # TICKETS REGISTRATION IN THIS TEST IS DISABLED. # first case - automatic change hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save() # not registered, because not user - driven change self.assertEqual( set([(x.content_object.old_value, x.content_object.new_value, x.content_object.field_name, x.content_object.user_id, x.registration_type) for x in CIChange.objects.all()]), set([(u'None', u'Device', u'type', None, CI_CHANGE_REGISTRATION_TYPES.NOT_REGISTERED.id), (u'None', u'1', u'id', None, CI_CHANGE_REGISTRATION_TYPES.NOT_REGISTERED.id)])) hostci.delete() # second case - manual change user = User.objects.create_user('john', '*****@*****.**', 'johnpassword') # john reigstered change, change should be at WAITING because # registering is not enabled in config hostci = CI(name='s11401.dc2', uid='mm-1') hostci.type_id = CI_TYPES.DEVICE.id hostci.save(user=user) self.assertEqual( set([(x.content_object.old_value, x.content_object.new_value, x.content_object.field_name, x.content_object.user_id, x.registration_type) for x in CIChange.objects.all()]), set([ (u'None', u'Device', u'type', 1, CI_CHANGE_REGISTRATION_TYPES.WAITING.id), (u'None', u'1', u'id', 1, CI_CHANGE_REGISTRATION_TYPES.WAITING.id), ]))