def testRefresh_Plain(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: os.mkdir(os.path.join(temp_dirpath, 'dir1')) client.ls(temp_dirpath) stat_entries = vfs_obj.ls(temp_dirpath) self.assertLen(stat_entries, 1) self.assertEqual(stat_entries[0].pathspec.path, os.path.join(temp_dirpath, 'dir1')) os.mkdir(os.path.join(temp_dirpath, 'dir2')) vfs_obj.refresh(temp_dirpath) stat_entries = vfs_obj.ls(temp_dirpath) paths = sorted(_.pathspec.path for _ in stat_entries) self.assertLen(paths, 2) self.assertEqual(paths[0], os.path.join(temp_dirpath, 'dir1')) self.assertEqual(paths[1], os.path.join(temp_dirpath, 'dir2'))
def testRefresh_Recursive(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) dir_components = ['dir1', 'dir2', 'dir3', 'dir4', 'dir5'] api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: os.makedirs(os.path.join(temp_dirpath, dir_components[0])) client.ls(temp_dirpath) os.makedirs(os.path.join(temp_dirpath, *dir_components)) vfs_obj.refresh(temp_dirpath, max_depth=3) stat_entries = vfs_obj.ls(temp_dirpath, max_depth=10) paths = sorted(_.pathspec.path for _ in stat_entries) self.assertLen(paths, 3) self.assertEqual(paths[0], os.path.join(temp_dirpath, 'dir1')) self.assertEqual(paths[1], os.path.join(temp_dirpath, 'dir1', 'dir2')) self.assertEqual( paths[2], os.path.join(temp_dirpath, 'dir1', 'dir2', 'dir3'))
def testLs_ContainsFiles(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) dir_nodes = [ # name, content ('file1', b'foo'), ('file2', b'foo\nbar'), ] api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: for filename, file_content in dir_nodes: filepath = os.path.join(temp_dirpath, filename) with io.open(filepath, 'wb') as filedesc: filedesc.write(file_content) client.ls(temp_dirpath) stat_entries = vfs_obj.ls(temp_dirpath) stat_entries = sorted(stat_entries, key=lambda _: _.pathspec.path) self.assertLen(stat_entries, 2) self.assertEqual(stat_entries[0].pathspec.path, os.path.join(temp_dirpath, 'file1')) self.assertEqual(stat_entries[0].st_size, 3) self.assertEqual(stat_entries[1].pathspec.path, os.path.join(temp_dirpath, 'file2')) self.assertEqual(stat_entries[1].st_size, 7)
def testRefresh_DoesNotExist(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) vfs_obj = vfs.VFS(self._get_fake_api_client(), jobs_pb2.PathSpec.OS) with self.assertRaises(api_errors.ResourceNotFoundError): vfs_obj.refresh('/foo/bar')
def testWget_FileDoesNotExist(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) vfs_obj = vfs.VFS(self._get_fake_api_client(), jobs_pb2.PathSpec.OS) with flagsaver.flagsaver(grr_admin_ui_url=self.endpoint): with self.assertRaises(Exception): vfs_obj.wget('/non/existing/file')
def testLs_WithoutApproval(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) vfs_obj = vfs.VFS(self._get_fake_api_client(), jobs_pb2.PathSpec.OS) with self.assertRaises(errors.ApprovalMissingError) as context: vfs_obj.ls('/foo/bar') self.assertEqual(context.exception.client_id, VfsTest.FAKE_CLIENT_ID)
def testLs_NotDirectory(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempFilePath() as temp_file: client.glob(temp_file) with self.assertRaises(errors.NotDirectoryError): vfs_obj.ls(temp_file)
def testLs_EmptyDirectory(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: client.ls(temp_dirpath) stat_entries = vfs_obj.ls(temp_dirpath) self.assertEmpty(stat_entries)
def testWget_IsDirectory(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with flagsaver.flagsaver(grr_admin_ui_url=self.endpoint): with temp.AutoTempDirPath() as temp_dir: client.ls(temp_dir) with self.assertRaises(ValueError): vfs_obj.wget(temp_dir)
def testOpen_NotCollected(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) content = b'foo bar' api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempFilePath() as temp_filepath: with io.open(temp_filepath, 'wb') as filedesc: filedesc.write(content) client.ls(os.path.dirname(temp_filepath)) with self.assertRaises(api_errors.ResourceNotFoundError): vfs_obj.open(temp_filepath)
def testWget_NoAdminURLSpecified(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with flagsaver.flagsaver(grr_admin_ui_url=''): with temp.AutoTempFilePath() as temp_file: with io.open(temp_file, 'wb') as filedesc: filedesc.write(b'foo bar') with client.open(temp_file): pass with self.assertRaises(ValueError): vfs_obj.wget(temp_file)
def testOpen_ReadMore(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) content = b'foo bar' api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempFilePath() as temp_filepath: with io.open(temp_filepath, 'wb') as filedesc: filedesc.write(content) with client.open(temp_filepath): pass with vfs_obj.open(temp_filepath) as filedesc: self.assertEqual(filedesc.read(10), content)
def testOpen_SeekOutOfBuffer(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) size = 1024 * 512 api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempFilePath() as temp_filepath: with io.open(temp_filepath, 'wb') as filedesc: filedesc.write(b'a' * size) filedesc.write(b'b' * size) with client.open(temp_filepath): pass with vfs_obj.open(temp_filepath) as filedesc: self.assertEqual(filedesc.seek(size - 1), size - 1) self.assertEqual(filedesc.read(2), b'ab')
def testWget_LinkWorksWithOfflineClient(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) content = b'foo bar' with flagsaver.flagsaver(grr_admin_ui_url=self.endpoint): with temp.AutoTempFilePath() as temp_file: with io.open(temp_file, 'wb') as filedesc: filedesc.write(content) with client.open(temp_file): pass link = vfs_obj.wget(temp_file) self.assertEqual(requests.get(link).content, content)
def testOpen_Buffering(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) size = 1024 * 1024 api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempFilePath() as temp_filepath: with io.open(temp_filepath, 'wb') as filedesc: filedesc.write(b'a' * size) with client.open(temp_filepath): pass with vfs_obj.open(temp_filepath) as filedesc: self.assertEqual(filedesc.tell(), 0) self.assertLess(len(filedesc.read1()), size) self.assertGreater(filedesc.tell(), 0)
def testLs_Recursive(self): data_store.REL_DB.WriteClientMetadata(client_id=VfsTest.FAKE_CLIENT_ID, fleetspeak_enabled=False) dir_nodes = [ 'file0', os.path.join('dir1', 'file1'), os.path.join('dir2', 'file2'), os.path.join('dir2', 'file3'), ] api_client = self._get_fake_api_client() client = grr_colab.Client(api_client) vfs_obj = vfs.VFS(api_client, jobs_pb2.PathSpec.OS) with temp.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: os.mkdir(os.path.join(temp_dirpath, 'dir1')) os.mkdir(os.path.join(temp_dirpath, 'dir2')) for path in dir_nodes: with io.open(os.path.join(temp_dirpath, path), 'wb') as filedesc: filedesc.write(b'foo') client.ls(temp_dirpath, max_depth=10) stat_entries = vfs_obj.ls(temp_dirpath, max_depth=5) paths = sorted(_.pathspec.path for _ in stat_entries) self.assertLen(paths, 6) self.assertEqual(paths[0], os.path.join(temp_dirpath, 'dir1')) self.assertEqual(paths[1], os.path.join(temp_dirpath, 'dir1', 'file1')) self.assertEqual(paths[2], os.path.join(temp_dirpath, 'dir2')) self.assertEqual(paths[3], os.path.join(temp_dirpath, 'dir2', 'file2')) self.assertEqual(paths[4], os.path.join(temp_dirpath, 'dir2', 'file3')) self.assertEqual(paths[5], os.path.join(temp_dirpath, 'file0'))
def cached(self) -> vfs.VFS: return vfs.VFS(self._client, self._path_type)