def test_to_dict(self): """ """ enterprise = tstdk.GAEnterprise(name='enterprise1') event = GAPushEvent(action=GARequest.ACTION_CREATE, entity=enterprise) expected_result = { 'entities': [{ 'description': None, 'zipcode': None, 'parentType': None, 'lastUpdatedDate': None, 'parentID': None, 'owner': None, 'creationDate': None, 'ID': None, 'name': 'enterprise1' }], 'entityType': 'enterprise', 'type': 'CREATE', 'updateMechanism': 'DEFAULT' } converted = event.to_dict() del converted['eventReceivedTime'] self.assertEqual(converted, expected_result)
def test_set_entity(self): """ """ event = GAPushEvent(action=GARequest.ACTION_CREATE) event.entity = 'test' self.assertEquals(event.entities, ['test']) self.assertEquals(event.entity, 'test')
def test_set_entity(self): """ """ event = GAPushEvent(action=GARequest.ACTION_CREATE) event.entity = 'test' self.assertEquals(event.entities, ['test']) self.assertEquals(event.entity, 'test')
def test_copy(self): """ """ session = GASession(garuda_uuid='xxx-xxx-xxx-xxx') request = GARequest(action=GARequest.ACTION_READ) context = GAContext(session=session, request=request) context.add_error( GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1')) context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2')) context.add_error( GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3')) context.add_event( GAPushEvent(action=GARequest.ACTION_UPDATE, entity=tstdk.GAEnterprise())) context.add_event( GAPushEvent(action=GARequest.ACTION_CREATE, entity=tstdk.GAEnterprise())) context.add_event( GAPushEvent(action=GARequest.ACTION_DELETE, entity=tstdk.GAEnterprise())) context.object = tstdk.GAEnterprise(name='enterprise1') context.objects = [ tstdk.GAEnterprise(name='enterprise2'), tstdk.GAEnterprise(name='enterprise3') ] context_copy = context.copy() self.assertEquals(context_copy.session.uuid, session.uuid) self.assertEquals(context_copy.request.action, GARequest.ACTION_READ) self.assertEquals([obj.name for obj in context_copy.objects], [obj.name for obj in context.objects]) self.assertEquals(context_copy.object.name, context.object.name) self.assertEquals(context_copy.has_errors, True) self.assertEquals(len(context_copy.errors), 3) self.assertEquals(context_copy.has_events, True) self.assertEquals(len(context_copy.events), 3)
def _perform_push(self): """ """ if self.context.request.action == GARequest.ACTION_ASSIGN: self.context.add_event( GAPushEvent(action=self.context.request.action, entity=self.context.parent_object)) else: self.context.add_event( GAPushEvent(action=self.context.request.action, entity=self.context.object))
def test_to_dict(self): """ """ enterprise = tstdk.GAEnterprise(name='enterprise1') event = GAPushEvent(action=GARequest.ACTION_CREATE, entity=enterprise) expected_result = {'entities': [{'description': None, 'zipcode': None, 'parentType': None, 'lastUpdatedDate': None, 'parentID': None, 'owner': None, 'creationDate': None, 'ID': None, 'name': 'enterprise1'}], 'entityType': 'enterprise', 'type': 'CREATE', 'updateMechanism': 'DEFAULT'} converted = event.to_dict() del converted['eventReceivedTime'] self.assertEqual(converted, expected_result)
def test_push_multiple_events_order(self): """ """ entity1 = tstdk.GAEnterprise(name='enterprise1') entity2 = tstdk.GAEnterprise(name='enterprise2') event1 = GAPushEvent(action=GARequest.ACTION_UPDATE, entity=entity1) event2 = GAPushEvent(action=GARequest.ACTION_DELETE, entity=entity2) notif = GAPushNotification(events=[event1, event2]) self.assertEquals(notif.events[0], event1) self.assertEquals(notif.events[1], event2)
def did_perform_write(self, context): """ """ job = context.object repository = context.parent_object session_username = context.session.root_object.id command = job.command if command == 'commit': repository.status = 'NEEDS_PULL' self._storage_controller.update(user_identifier=session_username, resource=repository) self._push_controller.push_events(events=[GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)]) response = self._storage_controller.get_all(user_identifier=session_username, parent=repository, resource_name=self._sdk.SDSpecification.rest_name) for specification in response.data: self._github_operations_controller.commit_specification(repository=repository, specification=specification, commit_message="Bulk saved specification %s" % specification.name, session_username=session_username) response = self._storage_controller.get_all(user_identifier=session_username, parent=repository, resource_name=self._sdk.SDAbstract.rest_name) for abstract in response.data: self._github_operations_controller.commit_specification(repository=repository, specification=abstract, commit_message="Bulk saved abstract %s" % abstract.name, session_username=session_username) else: repository.status = 'QUEUED' self._storage_controller.update(user_identifier=session_username, resource=repository) self._push_controller.push_events(events=[GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)]) if command == 'merge_master': self._github_operations_controller.merge_upstream_master(repository=repository, job=job, commit_message="Merged upstream master into %s" % repository.branch, session_username=session_username) elif command == 'pull': self._github_operations_controller.checkout_repository(repository=repository, job=job, session_username=session_username) return context
def test_from_dict(self): """ """ data = {'entities': [{'description': None, 'zipcode': None, 'parentType': None, 'lastUpdatedDate': None, 'parentID': None, 'owner': None, 'creationDate': None, 'ID': None, 'name': 'enterprise1'}], 'entityType': 'enterprise', 'type': 'CREATE', 'updateMechanism': 'DEFAULT', 'eventReceivedTime': None} GASDKLibrary().register_sdk('default', tstdk) event = GAPushEvent.from_dict(data) self.assertEqual(event.entity.name, 'enterprise1')
def get_next_event(self, session, timeout=None): """ """ event_queue_key = 'eventqueue:%s' % session.redis_key logger.debug('Waiting for event to be popped out of event queue: %s' % event_queue_key) blob = self.redis.brpop([event_queue_key], timeout=timeout) logger.debug('Popping one event from the session event queue %s' % event_queue_key) if not blob: return key, pack = blob event = GAPushEvent.from_dict(data=msgpack.unpackb(pack)) resources = [ GAResource(name=event.entity.rest_name, value=event.entity.id) ] request = GARequest(action=GARequest.ACTION_READ, resources=resources) context = GAContext(request=request, session=session) context.object = event.entity # operation_manager = GAOperationsController(context=context, # logic_controller=self.core_controller.logic_controller, # storage_controller=self.core_controller.storage_controller) # operation_manager.run() # if not context.has_errors: # logger.debug('Returning one event to session %s' % session.redis_key) return event
def test_events_gets_deleted_with_session_expiration(self): """ """ with patch.object(self.core_controller.sessions_controller, 'get_all_sessions', return_value=[self.session]): self.core_controller.sessions_controller._default_session_ttl = 1 session = self.core_controller.sessions_controller.create_session( request='fake-request') entity = tstdk.GAEnterprise(name='name', owner='user') self.push_controller.push_events( [GAPushEvent(action=GARequest.ACTION_CREATE, entity=entity)]) self.assertEquals( self.push_controller.redis.llen(self.session_event_queue_key), 1) self.assertFalse( self.push_controller.is_event_queue_empty( session=self.session)) import time time.sleep(1.5) self.assertEquals( self.push_controller.redis.llen('eventqueue:%s' % session.redis_key), 0) self.assertTrue( self.push_controller.is_event_queue_empty(session=session))
def test_from_dict(self): """ """ data = { 'entities': [{ 'description': None, 'zipcode': None, 'parentType': None, 'lastUpdatedDate': None, 'parentID': None, 'owner': None, 'creationDate': None, 'ID': None, 'name': 'enterprise1' }], 'entityType': 'enterprise', 'type': 'CREATE', 'updateMechanism': 'DEFAULT', 'eventReceivedTime': None } GASDKLibrary().register_sdk('default', tstdk) event = GAPushEvent.from_dict(data) self.assertEqual(event.entity.name, 'enterprise1')
def get_next_event(self, session, timeout=None): """ """ event_queue_key = 'eventqueue:%s' % session.redis_key logger.debug('Waiting for event to be popped out of event queue: %s' % event_queue_key) blob = self.redis.brpop([event_queue_key], timeout=timeout) logger.debug('Popping one event from the session event queue %s' % event_queue_key) if not blob: return key, pack = blob event = GAPushEvent.from_dict(data=msgpack.unpackb(pack)) resources = [GAResource(name=event.entity.rest_name, value=event.entity.id)] request = GARequest(action=GARequest.ACTION_READ, resources=resources) context = GAContext(request=request, session=session) context.object = event.entity # operation_manager = GAOperationsController(context=context, # logic_controller=self.core_controller.logic_controller, # storage_controller=self.core_controller.storage_controller) # operation_manager.run() # if not context.has_errors: # logger.debug('Returning one event to session %s' % session.redis_key) return event
def _perform_merge_upstream_master(self, repository, job, commit_message, session_username): """ """ repository.status = 'MERGING' self._storage_controller.update(user_identifier=session_username, resource=repository) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository) ]) try: manager = self._get_repository_manager_for_repository( repository=repository, session_username=session_username) manager.merge_upstream_master( local_branch=repository.branch, upstream_branch="master" if repository.parent_branch is None else repository.parent_branch, commit_message="Merged upstream master branch") job.progress = 1.0 job.status = 'SUCCESS' repository.status = 'NEEDS_PULL' except Exception as ex: logger.error('Unable to merge repository from upstream: %s' % ex) job.progress = 1.0 job.status = 'FAILED' job.result = 'Unable to merge repository.\n\nReason:\n%s' % ex repository.status = 'ERROR' finally: self._storage_controller.update(user_identifier=session_username, resource=job) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=job) ]) self._storage_controller.update(user_identifier=session_username, resource=repository) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository) ])
def test_push_notification_initialization(self): """ """ entity1 = tstdk.GAEnterprise(name='enterprise1') event1 = GAPushEvent(action=GARequest.ACTION_CREATE, entity=entity1) notif = GAPushNotification(events=[event1]) self.assertEquals(notif.events[0], event1) self.assertIsNotNone(notif.uuid)
def _set_specification_syncing(self, specification, syncing, session_username): """ """ specification.syncing = syncing self._storage_controller.update(user_identifier=session_username, resource=specification) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=specification) ])
def test_add_events(self): """ """ session = GASession(garuda_uuid='xxx-xxx-xxx-xxx') request = GARequest(action=GARequest.ACTION_READ) context = GAContext(session=session, request=request) event1 = GAPushEvent(action=GARequest.ACTION_UPDATE, entity=tstdk.GAEnterprise()) event2 = GAPushEvent(action=GARequest.ACTION_CREATE, entity=tstdk.GAEnterprise()) event3 = GAPushEvent(action=GARequest.ACTION_DELETE, entity=tstdk.GAEnterprise()) context.add_event(event1) self.assertEquals(len(context.events), 1) self.assertEquals(context.has_events, True) context.add_events([event2, event3]) self.assertEquals(len(context.events), 3) self.assertEquals(context.has_events, True)
def test_multiple_pushes(self): """ """ with patch.object(self.core_controller.sessions_controller, 'get_all_sessions', return_value=[self.session]): self.assertEquals( self.push_controller.redis.llen(self.session_event_queue_key), 0) self.assertTrue( self.push_controller.is_event_queue_empty( session=self.session)) entity = tstdk.GAEnterprise(name='name', owner='user') self.push_controller.push_events( [GAPushEvent(action=GARequest.ACTION_CREATE, entity=entity)]) entity.name = 'modified' self.push_controller.push_events( [GAPushEvent(action=GARequest.ACTION_UPDATE, entity=entity)]) event1 = self.push_controller.get_next_event(session=self.session) self.assertIsNotNone(event1) self.assertEquals(event1.action, GARequest.ACTION_CREATE) self.assertEquals(event1.entity.name, 'name') self.assertFalse( self.push_controller.is_event_queue_empty( session=self.session)) event2 = self.push_controller.get_next_event(session=self.session) self.assertIsNotNone(event2) self.assertEquals(event2.action, GARequest.ACTION_UPDATE) self.assertEquals(event2.entity.name, 'modified') self.assertTrue( self.push_controller.is_event_queue_empty( session=self.session))
def test_delete_push(self): """ """ with patch.object(self.core_controller.sessions_controller, 'get_all_sessions', return_value=[self.session]): self.assertEquals( self.push_controller.redis.llen(self.session_event_queue_key), 0) self.assertTrue( self.push_controller.is_event_queue_empty( session=self.session)) entity = tstdk.GAEnterprise(name='name', owner='user') self.push_controller.push_events( [GAPushEvent(action=GARequest.ACTION_DELETE, entity=entity)]) event = self.push_controller.get_next_event(session=self.session) self.assertIsNotNone(event) self.assertEquals(event.action, GARequest.ACTION_DELETE) self.assertEquals(entity.to_dict(), event.entity.to_dict())
def test_push_event_creates_event_queue(self): """ """ with patch.object(self.core_controller.sessions_controller, 'get_all_sessions', return_value=[self.session]): self.assertEquals( self.push_controller.redis.llen(self.session_event_queue_key), 0) self.assertTrue( self.push_controller.is_event_queue_empty( session=self.session)) entity = tstdk.GAEnterprise(name='name', owner='user') self.push_controller.push_events( [GAPushEvent(action=GARequest.ACTION_CREATE, entity=entity)]) self.assertEquals( self.push_controller.redis.llen(self.session_event_queue_key), 1) self.assertFalse( self.push_controller.is_event_queue_empty( session=self.session))
def did_perform_update(self, context): """ """ token = context.object session_username = context.session.root_object.id events = [] response = self._storage_controller.get_all( user_identifier=session_username, resource_name=self._sdk.SDRepository.rest_name, parent=None, filter="associatedTokenID == '%s'" % token.id) for repository in response.data: repository.status = 'NEEDS_PULL' self._storage_controller.update(user_identifier=session_username, resource=repository) events.append( GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)) if len(events): self._push_controller.push_events(events=events) return context
def _peform_checkout_repository(self, repository, job, session_username): """ """ repository.status = 'PULLING' self._storage_controller.update(user_identifier=session_username, resource=repository) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository) ]) try: manager = self._get_repository_manager_for_repository( repository=repository, session_username=session_username) self._specification_importer.clean_repository( repository=repository, session_username=session_username) self._specification_importer.import_repository_info( repository=repository, manager=manager, session_username=session_username) self._specification_importer.import_apiinfo( repository=repository, manager=manager, session_username=session_username) self._specification_importer.import_abstracts( repository=repository, manager=manager, session_username=session_username) self._specification_importer.import_specifications( repository=repository, manager=manager, session_username=session_username) self._specification_importer.import_monolitheconfig( repository=repository, manager=manager, session_username=session_username) job.progress = 1.0 job.status = 'SUCCESS' repository.status = 'READY' except Exception as ex: logger.error("Unable to checkout repository: %s" % ex) job.progress = 1.0 job.status = 'FAILED' job.result = 'Unable to pull repository.\n\nReason:\n%s' % ex repository.status = 'ERROR' finally: self._storage_controller.update(user_identifier=session_username, resource=job) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=job) ]) self._storage_controller.update(user_identifier=session_username, resource=repository) self._push_controller.push_events(events=[ GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository) ])
def test_set_action_assign_converted_to_update(self): """ """ event = GAPushEvent() event.action = GARequest.ACTION_ASSIGN self.assertEquals(event.action, GARequest.ACTION_UPDATE)
def test_set_action_assign_converted_to_update(self): """ """ event = GAPushEvent() event.action = GARequest.ACTION_ASSIGN self.assertEquals(event.action, GARequest.ACTION_UPDATE)
def test_set_action(self): """ """ event = GAPushEvent(action=GARequest.ACTION_CREATE) self.assertEquals(event.action, GARequest.ACTION_CREATE)
def did_perform_write(self, context): """ """ action = context.request.action specification = context.object repository = context.parent_object session_username = context.session.root_object.id if action == GARequest.ACTION_CREATE: self._github_operations_controller.commit_specification(repository=repository, specification=specification, commit_message="Added specification %s" % specification.name, session_username=session_username) elif action == GARequest.ACTION_UPDATE: if context.request.uuid in self._old_names: old_name = self._old_names[context.request.uuid] del self._old_names[context.request.uuid] self._github_operations_controller.rename_specification(repository=repository, specification=specification, old_name=old_name, commit_message="Renamed specification from %s to %s" % (old_name, specification.name), session_username=session_username) apis, specs = self._get_all_related_child_api(context=context) for api in apis: components = api.path.split('/') components[-1] = specification.object_resource_name api.path = "/".join(components) self._storage_controller.update(user_identifier=context.session.root_object.id, resource=api) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=api)) for spec in specs: self._github_operations_controller.commit_specification(repository=repository, specification=spec, commit_message="Updated api %s" % specification.object_resource_name, session_username=session_username) else: self._github_operations_controller.commit_specification(repository=repository, specification=specification, commit_message="Updated specification %s" % specification.name, session_username=session_username) elif action == GARequest.ACTION_DELETE: self._github_operations_controller.delete_specification(repository=repository, specification=specification, commit_message="Deleted specification %s" % specification.name, session_username=session_username) apis, specs = self._get_all_related_child_api(context=context) if len(apis): self._storage_controller.delete_multiple(user_identifier=context.session.root_object.id, resources=apis) context.add_events([GAPushEvent(action=GARequest.ACTION_DELETE, entity=api) for api in apis]) for spec in specs: self._github_operations_controller.commit_specification(repository=repository, specification=spec, commit_message="Removed api %s" % specification.object_resource_name, session_username=session_username) return context
def test_serialize(self): """ """ self.maxDiff = None entity1 = tstdk.GAEnterprise(name='enterprise1') entity2 = tstdk.GAEnterprise(name='enterprise2') event1 = GAPushEvent(action=GARequest.ACTION_UPDATE, entity=entity1) event2 = GAPushEvent(action=GARequest.ACTION_DELETE, entity=entity2) notif = GAPushNotification(events=[event1, event2]) expected = { 'events': [{ 'entities': [{ 'ID': None, 'creationDate': None, 'description': None, 'lastUpdatedDate': None, 'name': 'enterprise1', 'owner': None, 'parentID': None, 'parentType': None, 'zipcode': None }], 'entityType': 'enterprise', 'type': 'UPDATE', 'updateMechanism': 'DEFAULT' }, { 'entities': [{ 'ID': None, 'creationDate': None, 'description': None, 'lastUpdatedDate': None, 'name': 'enterprise2', 'owner': None, 'parentID': None, 'parentType': None, 'zipcode': None }], 'entityType': 'enterprise', 'type': 'DELETE', 'updateMechanism': 'DEFAULT' }] } data = notif.to_dict() self.assertIsNotNone(data['events'][0]['eventReceivedTime']) self.assertIsNotNone(data['events'][1]['eventReceivedTime']) self.assertIsNotNone(data['uuid']) del data['events'][0]['eventReceivedTime'] del data['events'][1]['eventReceivedTime'] del data['uuid'] self.assertEquals(data, expected)
def will_perform_write(self, context): """ """ if context.request.action in (GARequest.ACTION_DELETE): return context repository = context.parent_object specification = context.object action = context.request.action specification.name = '%s.spec' % (specification.object_rest_name.strip() if specification.object_rest_name and specification.object_rest_name.strip() else specification.entity_name.strip().lower()) response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, parent=repository, resource_name=self._sdk.SDSpecification.rest_name, filter='name == "%s"' % specification.name) if response.count and response.data[0].id != specification.id: context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Duplicate Name', description='Another specification exists with the name %s' % specification.name, property_name='name')) if not specification.entity_name or not len(specification.entity_name): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute entityName is mandatory.', property_name='entityName')) if action == GARequest.ACTION_UPDATE: response = self._storage_controller.get(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDSpecification.rest_name, identifier=specification.id) if response.data and response.data.name != specification.name: self._old_names[context.request.uuid] = response.data.name response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDAPIInfo.rest_name, parent=repository) apiinfo = response.data[0] if response.count else None if specification.root: response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDSpecification.rest_name, parent=repository, filter='name == "%s.spec"' % apiinfo.root) current_root_specification = response.data[0] if response.count else None if current_root_specification and current_root_specification.id != specification.id: current_root_specification.root = False response = self._storage_controller.update(user_identifier=context.session.root_object.id, resource=current_root_specification) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=current_root_specification)) response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDChildAPI.rest_name, parent=current_root_specification) for api in response.data: api.relationship = 'child' response = self.core_controller.storage_controller.get(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDSpecification.rest_name, identifier=api.associated_specification_id) api.path = '/%s/id/%s' % (current_root_specification.object_resource_name, response.data.object_resource_name) self._storage_controller.update(user_identifier=context.session.root_object.id, resource=api) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=api)) self._github_operations_controller.commit_specification(repository=repository, specification=current_root_specification, commit_message="Updated specification %s" % current_root_specification.name, session_username=context.session.root_object.id) response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDChildAPI.rest_name, parent=specification) for api in response.data: api.relationship = 'root' api.path = '/%s' % api.path.split('/')[3] self._storage_controller.update(user_identifier=context.session.root_object.id, resource=api) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=api)) if apiinfo and apiinfo.root != specification.object_rest_name: apiinfo.root = specification.object_rest_name self._storage_controller.update(user_identifier=context.session.root_object.id, resource=apiinfo) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=apiinfo)) self._github_operations_controller.commit_apiinfo(repository=repository, apiinfo=apiinfo, commit_message="Updated api.info", session_username=context.session.root_object.id) return context