From 37ee55b1f8aa6c89d0120c38541f6e99d414574e Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Wed, 24 Apr 2019 02:54:26 +0300 Subject: [PATCH] test: test ssh Signed-off-by: Ruslan Kuprieiev --- tests/unit/remote/ssh/test_connection.py | 88 +++++++++++++++++------- 1 file changed, 65 insertions(+), 23 deletions(-) diff --git a/tests/unit/remote/ssh/test_connection.py b/tests/unit/remote/ssh/test_connection.py index a570f3213..775cd79df 100644 --- a/tests/unit/remote/ssh/test_connection.py +++ b/tests/unit/remote/ssh/test_connection.py @@ -1,6 +1,9 @@ +from __future__ import unicode_literals + import mockssh import os -from pytest import yield_fixture +import posixpath +import pytest from dvc.remote.ssh.connection import SSHConnection @@ -10,35 +13,74 @@ user = "user" key_path = os.path.join(here, "{0}.key".format(user)) -@yield_fixture() -def server(): +@pytest.yield_fixture() +def ssh(): users = {user: key_path} with mockssh.Server(users) as s: - yield s + yield SSHConnection( + s.host, username=user, port=s.port, key_filename=key_path + ) -def _client(server): - client = SSHConnection( - server.host, username=user, port=server.port, key_filename=key_path - ) - return client +def test_connection(ssh): + assert ssh.execute("ls /") -def test_connection(server): - client = _client(server) - ls = client.execute("ls /") - assert ls +def test_isdir(ssh): + assert ssh.isdir(here) + assert not ssh.isdir(__file__) -def test_isdir(server): - client = _client(server) - path = here - isdir = client.isdir(path=path) - assert isdir is True +def test_exists(ssh): + assert not ssh.exists("/path/to/non/existent/file") + assert ssh.exists(__file__) -def test_exists(server): - client = _client(server) - path = "/path/to/file" - file_path = client.exists(path=path) - assert file_path is False +def test_isfile(ssh): + assert ssh.isfile(__file__) + assert not ssh.isfile(here) + + +def test_makedirs(tmp_path, ssh): + tmp = tmp_path.absolute().as_posix() + path = posixpath.join(tmp, "dir", "subdir") + ssh.makedirs(path) + assert os.path.isdir(path) + + +def test_walk(tmp_path, ssh): + root_path = tmp_path + dir_path = root_path / "dir" + subdir_path = dir_path / "subdir" + + dir_path.mkdir() + subdir_path.mkdir() + + root_data_path = root_path / "root_data" + dir_data_path = dir_path / "dir_data" + subdir_data_path = subdir_path / "subdir_data" + + with root_data_path.open("w+") as fobj: + fobj.write("") + + with dir_data_path.open("w+") as fobj: + fobj.write("") + + with subdir_data_path.open("w+") as fobj: + fobj.write("") + + entries = [ + dir_path, + subdir_path, + root_data_path, + dir_data_path, + subdir_data_path, + ] + expected = set([entry.absolute().as_posix() for entry in entries]) + + paths = set() + for root, dirs, files in ssh.walk(root_path.absolute().as_posix()): + for entry in dirs + files: + paths.add(posixpath.join(root, entry)) + + assert paths == expected