def test_closed_stories(load_logs): """Test closed stories.""" owner_id = get_owner_id(load_logs) for story_type in ('job', ): stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=False) assert len(stories) is 2 story = stories[0] assert story['finished_at'] assert story['type'] == story_type for log in story['logs']: assert log['type'] in TYPES[story_type] assert log['owner_id'] == story['owner_id'] assert log['%s_id' % story_type] == story['story_id'] for story_type in ('shell', 'incident'): stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=False) assert len(stories) is 1 story = stories[0] assert story['finished_at'] assert story['type'] == story_type assert len(story['logs']) is 2 for log in story['logs']: assert log['type'] in TYPES[story_type] assert log['owner_id'] == story['owner_id'] assert log['%s_id' % story_type] == story['story_id']
def send_stories(self, stype): """Send open stories of the specified type.""" def callback(stories): email = self.auth_context.user.email ename = '%ss' % stype log.info('Will emit %d %s for %s', len(stories), ename, email) self.send(ename, stories) # Only send incidents for non-Owners. if not self.auth_context.is_owner() and stype != 'incident': return callback([]) # Fetch the latest open stories. kwargs = { 'story_type': stype, 'range': { '@timestamp': { 'gte': int((time.time() - 7 * 24 * 60 * 60) * 1000) } } } if self.enforce_logs_for is not None: kwargs['owner_id'] = self.enforce_logs_for get_stories(tornado_async=True, tornado_callback=callback, **kwargs)
def send_stories(self, stype): """Send open stories of the specified type.""" def callback(stories, pending=False): email = self.auth_context.user.email ename = '%s_%ss' % ('open' if pending else 'closed', stype) log.info('Will emit %d %s for %s', len(stories), ename, email) self.send(ename, stories) # Only send incidents for non-Owners. if not self.auth_context.is_owner() and stype != 'incident': self.send('open_%ss' % stype, []) return # Fetch the latest open stories. kwargs = { 'story_type': stype, 'pending': True, 'range': { '@timestamp': { 'gte': int((time.time() - 7 * 24 * 60 * 60) * 1000) } } } if self.enforce_logs_for is not None: kwargs['owner_id'] = self.enforce_logs_for get_stories(tornado_async=True, tornado_callback=callback, **kwargs) # Fetch also the latest, closed incidents. if stype == 'incident': kwargs.update({'limit': 10, 'pending': False}) get_stories(tornado_async=True, tornado_callback=callback, **kwargs)
def test_error_stories(load_logs): """Test stories that ended with an error.""" owner_id = get_owner_id(load_logs) for story_type in ('job', ): stories = get_stories(story_type, owner_id=owner_id, error=True, expand=True, pending=True) assert len(stories) is 0 for story_type in ('job', ): stories = get_stories(story_type, owner_id=owner_id, error=True, expand=True, pending=False) assert len(stories) is 1 for story_type in ('job', ): stories = get_stories(story_type, owner_id=owner_id, error=True, expand=True) assert len(stories) is 1 story = stories[0] assert story['error'] assert story['type'] == story_type for log in story['logs']: assert log['type'] in TYPES[story_type] assert log['owner_id'] == story['owner_id'] assert log['%s_id' % story_type] == story['story_id'] # Ensure there is a log with an error. for log in story['logs']: if log['error']: break else: assert False # Ensure all indexed logs are present in the story, if appropriate. logs = [] for log in load_logs[story_type]: if log['%s_id' % story_type] == story['story_id']: logs.append(log) assert len(story['logs']) == len(logs)
def show_script(request): """ Show script details and job history. READ permission required on script. --- script_id: type: string required: true in: path """ script_id = request.matchdict['script_id'] auth_context = auth_context_from_request(request) if not script_id: raise RequiredParameterMissingError('No script id provided') try: script = Script.objects.get(owner=auth_context.owner, id=script_id, deleted=None) except me.DoesNotExist: raise NotFoundError('Script id not found') # SEC require READ permission on SCRIPT auth_context.check_perm('script', 'read', script_id) ret_dict = script.as_dict() jobs = get_stories('job', auth_context.owner.id, script_id=script_id) ret_dict['jobs'] = [job['job_id'] for job in jobs] return ret_dict
def test_open_stories(load_logs): """Test open stories. Fetch open stories, ensure they are indeed pending, and verify their type. """ owner_id = get_owner_id(load_logs) for story_type in ('job', 'shell', 'session'): stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=True) assert len(stories) is 1 # Ensure stories are still pending. story = stories[0] assert not story['finished_at'] assert story['type'] == story_type # Cross-verify the story_id and type. for log in story['logs']: assert log['type'] in TYPES[story_type] assert log['owner_id'] == story['owner_id'] assert log['%s_id' % story_type] == story['story_id']
def test_close_story(load_logs): """Test closing stories. Fetch an open story and close it manually. """ owner_id = get_owner_id(load_logs) for story_type in ('incident', ): stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=True) assert len(stories) is 1 story = stories[0] assert len(story['logs']) is 1 assert not story['finished_at'] assert story['type'] == story_type for log in story['logs']: assert log['type'] in TYPES[story_type] assert log['owner_id'] == story['owner_id'] assert log['%s_id' % story_type] == story['story_id'] # Close the story. log_event(owner_id=owner_id, action='close_story', event_type='request', story_id=story['story_id']) # Wait for index refresh. time.sleep(1) # Ensure there are no more pending stories. stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=True) assert len(stories) is 0 # Verify the story's `finished_at` timestamp. story = get_story(owner_id=owner_id, story_id=story['story_id']) assert story['finished_at'] assert story['type'] == story_type assert len(story['logs']) is 2
def test_incidents(load_logs): """Test incidents.""" owner_id = get_owner_id(load_logs) for story_type in ('incident', ): stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=True) assert len(stories) is 2 story = stories[0] assert not story['finished_at'] assert story['type'] == story_type for log in story['logs']: assert log['type'] in TYPES[story_type] assert log['owner_id'] == story['owner_id'] assert log['%s_id' % story_type] == story['story_id'] logs = [] for log in load_logs[story_type]: if log['%s_id' % story_type] == story['story_id']: logs.append(log) assert len(story['logs']) == len(logs) # Publish new event, which is meant to close the open incident. for log in load_logs['request']: log_event(**log) # Wait to ensure log has actually been indexed. time.sleep(1) # Verify that the incident has closed. for story_type in ('incident', ): stories = get_stories(story_type, owner_id=owner_id, expand=True, pending=True) assert len(stories) is 1