class TestLookupEditor(LookupEditorTestCase): """ This tests the class which manages lookup backups. """ def setUp(self): self.lookup_editor = LookupEditor(logger=logger) @skipIfCantAuthenticate def test_resolve_lookup_filename(self): """ Test resolve_lookup_filename() to resolve a lookup file name. """ file_path = self.lookup_editor.resolve_lookup_filename('test.csv', 'search', 'nobody', False, None, session_key=self.get_session_key()) self.assertEquals(self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/test.csv') @skipIfCantAuthenticate def test_resolve_lookup_filename_version(self): """ Test resolve_lookup_filename() to resolve a lookup file name to the backed up version. """ file_path = self.lookup_editor.resolve_lookup_filename('test.csv', 'search', 'nobody', False, '1234', session_key=self.get_session_key()) self.assertEquals(self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv/1234') @skipIfCantAuthenticate def test_resolve_lookup_filename_version_no_user(self): """ Test resolve_lookup_filename() to resolve a lookup file name to the backed up version without providing a user. """ file_path = self.lookup_editor.resolve_lookup_filename('test.csv', 'search', None, False, '1234', session_key=self.get_session_key()) self.assertEquals(self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv/1234') @skipIfLookupTestNotInstalled def test_get_kv_fields_from_transform(self): """ Test getting the fields of a lookup from a transform. """ fields = self.lookup_editor.get_kv_fields_from_transform(self.get_session_key(), 'test_kv_store_transform_fields', 'lookup_test', None) self.assertEquals(len(fields), 4)
class LookupBackupsHandler(rest_handler.RESTHandler): """ This is a REST handler that supports backing up lookup files. This is broken out as a separate handler so that this handler can be replayed on other search heads via the allowRestReplay setting in restmap.conf. """ def __init__(self, command_line, command_arg): super(LookupBackupsHandler, self).__init__(command_line, command_arg, logger) self.lookup_editor = LookupEditor(logger) def post_backup(self, request_info, lookup_file=None, namespace="lookup_editor", owner=None, file_time=None, **kwargs): """ Make a backup of the given lookup file. """ try: # Determine the final path of the file resolved_file_path = self.lookup_editor.resolve_lookup_filename( lookup_file, namespace, owner, session_key=request_info.session_key, throw_not_found=True) # Backup the file passing the time so that the REST handler will use the same time for # all lookups even when the REST call is being replayed in an SHC cluster file_path = self.lookup_editor.backup_lookup_file( request_info.session_key, lookup_file, namespace, resolved_file_path, owner, file_time) self.logger.info("Created a backup of a lookup file, file_path=%s", file_path) # Everything worked, return accordingly return { 'payload': str(file_path), # Payload of the request. 'status': 200 # HTTP status code } except ResourceNotFound: self.logger.warn("Unable to find the lookup to backup") return self.render_error_json( "Unable to find the lookup to backup", 404) except: self.logger.exception( "Exception generated when attempting to backup a lookup file") return self.render_error_json("Unable to backup the lookup")
class LookupBackupsHandler(rest_handler.RESTHandler): """ This is a REST handler that supports backing up lookup files. This is broken out as a separate handler so that this handler can be replayed on other search heads via the allowRestReplay setting in restmap.conf. """ def __init__(self, command_line, command_arg): super(LookupBackupsHandler, self).__init__(command_line, command_arg, logger) self.lookup_editor = LookupEditor(logger) def post_backup(self, request_info, lookup_file=None, namespace="lookup_editor", owner=None, file_time=None, **kwargs): """ Make a backup of the given lookup file. """ try: # Determine the final path of the file resolved_file_path = self.lookup_editor.resolve_lookup_filename(lookup_file, namespace, owner, session_key=request_info.session_key, throw_not_found=True) # Backup the file passing the time so that the REST handler will use the same time for # all lookups even when the REST call is being replayed in an SHC cluster file_path = self.lookup_editor.backup_lookup_file(request_info.session_key, lookup_file, namespace, resolved_file_path, owner, file_time) self.logger.info("Created a backup of a lookup file, file_path=%s", file_path) # Everything worked, return accordingly return { 'payload': str(file_path), # Payload of the request. 'status': 200 # HTTP status code } except ResourceNotFound: self.logger.warn("Unable to find the lookup to backup") return self.render_error_json("Unable to find the lookup to backup", 404) except: self.logger.exception("Exception generated when attempting to backup a lookup file") return self.render_error_json("Unable to backup the lookup")
class TestLookupBackups(LookupEditorTestCase): """ This tests the class which manages lookup backups. """ def setUp(self): self.lookup_backups = lookup_backups.LookupBackups(logger=logger) @skipIfCantAuthenticate def test_get_backup_directory(self): """ Ensure that get_backup_directory() returns the correct directory. """ dir = self.lookup_backups.get_backup_directory(self.get_session_key(), "test.csv", namespace="search", owner="nobody") self.assertEquals( self.strip_splunk_path(dir), "/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv" ) @skipIfCantAuthenticate def test_get_backup_directory_with_resolved(self): """ Ensure that get_backup_directory() returns the correct directory when given a value for resolved_lookup_path. """ self.lookup_editor = LookupEditor(logger=logger) resolved_lookup_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', 'nobody', False, None, session_key=self.get_session_key()) dir = self.lookup_backups.get_backup_directory( self.get_session_key(), "test.csv", namespace="search", owner="nobody", resolved_lookup_path=resolved_lookup_path) self.assertEquals( self.strip_splunk_path(dir), "/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv" )
class TestLookupBackups(LookupEditorTestCase): """ This tests the class which manages lookup backups. """ def setUp(self): self.lookup_backups = lookup_backups.LookupBackups(logger=logger) @skipIfCantAuthenticate def test_get_backup_directory(self): """ Ensure that get_backup_directory() returns the correct directory. """ dir = self.lookup_backups.get_backup_directory(self.get_session_key(), "test.csv", namespace="search", owner="nobody") self.assertEquals(self.strip_splunk_path(dir), "/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv") @skipIfCantAuthenticate def test_get_backup_directory_with_resolved(self): """ Ensure that get_backup_directory() returns the correct directory when given a value for resolved_lookup_path. """ self.lookup_editor = LookupEditor(logger=logger) resolved_lookup_path = self.lookup_editor.resolve_lookup_filename('test.csv', 'search', 'nobody', False, None, session_key=self.get_session_key()) dir = self.lookup_backups.get_backup_directory(self.get_session_key(), "test.csv", namespace="search", owner="nobody", resolved_lookup_path=resolved_lookup_path) self.assertEquals(self.strip_splunk_path(dir), "/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv")
class TestLookupEditor(LookupEditorTestCase): """ This tests the class which manages lookup backups. """ def setUp(self): self.lookup_editor = LookupEditor(logger=logger) @skipIfCantAuthenticate def test_resolve_lookup_filename(self): """ Test resolve_lookup_filename() to resolve a lookup file name. """ file_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', 'nobody', False, None, session_key=self.get_session_key()) self.assertEquals(self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/test.csv') @skipIfCantAuthenticate def test_resolve_lookup_filename_version(self): """ Test resolve_lookup_filename() to resolve a lookup file name to the backed up version. """ file_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', 'nobody', False, '1234', session_key=self.get_session_key()) self.assertEquals( self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv/1234' ) @skipIfCantAuthenticate def test_resolve_lookup_filename_version_no_user(self): """ Test resolve_lookup_filename() to resolve a lookup file name to the backed up version without providing a user. """ file_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', None, False, '1234', session_key=self.get_session_key()) self.assertEquals( self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv/1234' ) @skipIfLookupTestNotInstalled def test_get_kv_fields_from_transform(self): """ Test getting the fields of a lookup from a transform. """ fields = self.lookup_editor.get_kv_fields_from_transform( self.get_session_key(), 'test_kv_store_transform_fields', 'lookup_test', None) self.assertEquals(len(fields), 4)
class TestLookupEditor(LookupEditorTestCase): """ This tests the class which manages lookup backups. """ def setUp(self): self.lookup_editor = LookupEditor(logger=logger) @skipIfCantAuthenticate def test_resolve_lookup_filename(self): """ Test resolve_lookup_filename() to resolve a lookup file name. """ file_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', 'nobody', False, None, session_key=self.get_session_key()) self.assertEquals(self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/test.csv') @skipIfCantAuthenticate def test_resolve_lookup_filename_version(self): """ Test resolve_lookup_filename() to resolve a lookup file name to the backed up version. """ file_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', 'nobody', False, '1234', session_key=self.get_session_key()) self.assertEquals( self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv/1234' ) @skipIfCantAuthenticate def test_resolve_lookup_filename_version_no_user(self): """ Test resolve_lookup_filename() to resolve a lookup file name to the backed up version without providing a user. """ file_path = self.lookup_editor.resolve_lookup_filename( 'test.csv', 'search', None, False, '1234', session_key=self.get_session_key()) self.assertEquals( self.strip_splunk_path(file_path), '/etc/apps/lookup_editor/lookups/lookup_file_backups/search/nobody/test.csv/1234' )