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 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 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_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_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 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 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)
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'])