def test_ssh_execute_command(mock_ssh): """Test executing commands using the SSH client.""" with ssh.ssh_client('test') as client: assert client.exec_cmd('ls') == 'ls' # -- Simulate error case -------------------------------------------------- with ssh.ssh_client('test') as client: client._client.exit_status = -1 with pytest.raises(RuntimeError): client.exec_cmd('ls')
def test_remote_volume_load_file(mock_ssh, basedir, data_e): """Test loading a file from a remote storage volume.""" with ssh.ssh_client('test', sep=os.sep) as client: store = RemoteStorage(remotedir=basedir, client=client) with store.load(key='examples/data/data.json').open() as f: doc = json.load(f) assert doc == data_e
def test_ssh_walk(mock_ssh, tmpdir): """Test the remote directory walk method.""" # -- Setup ---------------------------------------------------------------- # # Create directory structure: # a.txt # b/ # b/c.txt # b/d.txt # b/e/ # b/e/f.txt # b/g os.makedirs(os.path.join(tmpdir, 'b', 'e')) os.makedirs(os.path.join(tmpdir, 'b', 'g')) Path(os.path.join(tmpdir, 'a.txt')).touch() Path(os.path.join(tmpdir, 'b', 'c.txt')).touch() Path(os.path.join(tmpdir, 'b', 'd.txt')).touch() Path(os.path.join(tmpdir, 'b', 'e', 'f.txt')).touch() Path(os.path.join(tmpdir, 'a.txt')).touch() # -- Test ----------------------------------------------------------------- with ssh.ssh_client('test', sep=os.sep) as client: files = client.walk(str(tmpdir)) assert len(files) == 4 assert 'a.txt' in files assert 'b/c.txt' in files assert 'b/d.txt' in files assert 'b/e/f.txt' in files
def test_sftp_file_handle(mock_ssh, basedir, data_a): """Test methods of the SFTPFile handle object.""" with ssh.ssh_client('test') as client: f = SFTPFile(filename=os.path.join(basedir, 'A.json'), client=client) with f.open() as b: doc = json.load(b) assert doc == data_a assert f.size() > 0
def test_remote_volume_erase(mock_ssh, basedir): """Test erasing the remote storage volume base directory.""" with ssh.ssh_client('test', sep=os.sep) as client: store = RemoteStorage(remotedir=basedir, client=client) assert basedir in store.describe() store.erase() store.close() assert not os.path.isdir(basedir)
def test_remote_volume_delete_file(mock_ssh, basedir): """Test downloading a file from a storage volume.""" with ssh.ssh_client('test', sep=os.sep) as client: store = RemoteStorage(remotedir=basedir, client=client) store.delete(key='examples/data/data.json') assert os.path.isdir(os.path.join(basedir, 'examples', 'data')) assert not os.path.isfile( os.path.join(basedir, 'examples', 'data', 'data.json'))
def test_remote_volume_copy_all(mock_ssh, basedir, emptydir, filenames_all, data_a): """Test copying the full directory of a storage volume.""" source = FileSystemStorage(basedir=basedir) with ssh.ssh_client('test', sep=os.sep) as client: target = RemoteStorage(remotedir=emptydir, client=client) source.copy(src=None, dst=None, store=target) files = {key: file for key, file in target.walk(src='')} assert set(files.keys()) == filenames_all with files['A.json'].open() as f: assert json.load(f) == data_a
def test_remote_volume_copy_file(mock_ssh, basedir, emptydir, data_e): """Test copying a file from a storage volume.""" source = FileSystemStorage(basedir=basedir) with ssh.ssh_client('test', sep=os.sep) as client: target = RemoteStorage(remotedir=emptydir, client=client) source.copy(src='examples/data/data.json', dst='static/examples/data/data.json', store=target) files = {key: file for key, file in target.walk(src='static')} assert set(files.keys()) == {'static/examples/data/data.json'} with files['static/examples/data/data.json'].open() as f: assert json.load(f) == data_e
def test_remote_volume_subfolder(mock_ssh, basedir, data_d, data_e): """Test creating a new storage volume for a sub-folder of the base directory of a remote file system storage volume. """ with ssh.ssh_client('test', sep=os.sep) as client: store = RemoteStorage(remotedir=basedir, client=client) substore = store.get_store_for_folder(key='docs', identifier='SUBSTORE') assert substore.identifier == 'SUBSTORE' with substore.load(key='D.json').open() as f: doc = json.load(f) assert doc == data_d substore.erase() with store.load(key='examples/data/data.json').open() as f: doc = json.load(f) assert doc == data_e
def test_remote_volume_serialization(mock_ssh, basedir): """Test uploading a full directory to a storage volume.""" with ssh.ssh_client('test', sep=os.sep) as client: store = RemoteStorage(remotedir=basedir, client=client) store_id = store.identifier basedir = store.remotedir doc = store.to_dict() assert doc == { 'type': SFTP_STORE, 'id': store_id, 'args': [{ 'key': 'basedir', 'value': basedir }, { 'key': 'hostname', 'value': 'test' }, { 'key': 'port', 'value': None }, { 'key': 'timeout', 'value': None }, { 'key': 'look_for_keys', 'value': False }, { 'key': 'sep', 'value': os.sep }] } fs = RemoteStorage.from_dict(doc) assert isinstance(fs, RemoteStorage) assert fs.identifier == store_id assert fs.remotedir == basedir
def test_remote_volume_mkdir(mock_ssh, basedir): """Test mkdir for remote storage volumes.""" with ssh.ssh_client('test', sep=os.sep) as client: store = RemoteStorage(remotedir=basedir, client=client) store.mkdir(path='docs') store.mkdir(path='docs/results')