From 0e2e328f42fb384b2d63dcec05ae44c0dd1ea0d5 Mon Sep 17 00:00:00 2001 From: ag613915cao Date: Sat, 20 Apr 2019 18:26:11 +0700 Subject: [PATCH] Add test for ssh connection This commit intends to mock SSH server that not depends on OS which the test running on for testing purpose. --- tests/requirements.txt | 1 + tests/unit/remote/ssh/test_connection.py | 44 ++++++++++++++++++++++++ tests/unit/remote/ssh/user.key | 27 +++++++++++++++ tests/unit/remote/ssh/user.key.pub | 1 + 4 files changed, 73 insertions(+) create mode 100644 tests/unit/remote/ssh/test_connection.py create mode 100644 tests/unit/remote/ssh/user.key create mode 100644 tests/unit/remote/ssh/user.key.pub diff --git a/tests/requirements.txt b/tests/requirements.txt index fade9c8b13..2bd83a2753 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -14,3 +14,4 @@ black==19.3b0 ; python_version >= '3.6' flake8 flake8-docstrings jaraco.windows==3.9.2 +mock-ssh-server >= 0.5.0 diff --git a/tests/unit/remote/ssh/test_connection.py b/tests/unit/remote/ssh/test_connection.py new file mode 100644 index 0000000000..94a1d3eb4a --- /dev/null +++ b/tests/unit/remote/ssh/test_connection.py @@ -0,0 +1,44 @@ +import mockssh +import os +from pytest import yield_fixture + +from dvc.remote.ssh.connection import SSHConnection + +here = os.path.abspath(os.path.dirname(__file__)) + +user = "user" +key_path = os.path.join(here, "{0}.key".format(user)) + + +@yield_fixture() +def server(): + users = {user: key_path} + with mockssh.Server(users) as s: + yield s + + +def _client(server): + client = SSHConnection( + server.host, username=user, port=server.port, key_filename=key_path + ) + return client + + +def test_connection(server): + client = _client(server) + ls = client.execute("ls /") + assert ls + + +def test_isdir(server): + client = _client(server) + path = here + isdir = client.isdir(path=path) + assert isdir is True + + +def test_file_exists(server): + client = _client(server) + path = "/path/to/file" + file_path = client.file_exists(path=path) + assert file_path is False diff --git a/tests/unit/remote/ssh/user.key b/tests/unit/remote/ssh/user.key new file mode 100644 index 0000000000..874786f6e7 --- /dev/null +++ b/tests/unit/remote/ssh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T6wAkSItgVnlU6t6FLuKjLXoD +Buw3oM/+i2pDy8oSsxueBv1GyMZG0ixcShkeXPuVAoGBAMFFHMo5st1hcXBeTBUX +J/s7F4duBZQdXWVkRrAX3WVVheqS30miE2OVp3nObmGbIQk5FRZi/HUO2BsHI6Km +/c+AiJl3m90e91ZJ9nDmLJf9U+fYoCXgR4d/FcJtN2eV99ThmjumisvBMyIXVyy4 +zibVtC3i7cPes2P2nD83ZlHxAoGBAN+VSCckx4HXjAJH/ZSuvnriVdyaceD2ARF0 +jJxtCYzkoAAk3l6PaLMjUiw2exgcAkov2RbPkB/DKkqBSPHDliiAijWS3FpoHwFY +XYaflj5yRHtdjYcwyWhaZvuLzvdeZppg7c3E14CMFn792IFSvTvW7AjWZBFbGdj8 +qpc+zY15AoGBANvsGFHmurP+VUu1ibegREBySts0WGNyq5VOTCrkN9S7AKR1pZNI +I6W7KrRcXEvbBM7B47ykbE40hAachxN1Rpk+9qEom6etTaw/yMewgFNjZXYJw06z +4bq6ofjKK8VqCWx41pWcmXj7Fa2A43RvZWg8TlX7Q8uc4wTBpTkuzfZC +-----END RSA PRIVATE KEY----- diff --git a/tests/unit/remote/ssh/user.key.pub b/tests/unit/remote/ssh/user.key.pub new file mode 100644 index 0000000000..98cfd20580 --- /dev/null +++ b/tests/unit/remote/ssh/user.key.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD8eUxHRyGAUvHoD8GZiL1imP2evSRndajqfdl1Jc5Elofp7t0tLD7R6gPU0MLEM5FmXBg0CDjnOd1Tmw83gi9VwC8L8UPSpIrUfSS7WnhBXzbLhrVI6fNGUNQhWF1DGeVBryAj7MM7wDLM4nihn0svsfL6YgOobFHAd4p8Nhe4eQT5oz0IKSJ0+pH9SrFqDTf7LnUwSmELTuavuasNviU6Z6rLxerRlxSMiSYqIYJVVEk7qDiRgXCvRTCkoLAvM5/DgXLZKnNZ97ch1VSoiuj3t3UAYL705RAROJWkFg5sEjWp9Ch5/oxP4IVxNXjRzIp0IbrHnt/qCk8xiph90dPJ user@unicorn