From 5d381da5d9efb3e90dc35fa20bd2969f1221ae8b Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Mon, 7 Jan 2019 15:07:10 +0200 Subject: [PATCH] ssh: change order of user configuration Unfortunately this breaks backward compatibility, but we have to do it, because current order is totally wrong. Configuring user with `dvc remote modify myssh user` is an explicit action and should have higher priority than implicit url. This is also important, since in the next patch in this PR we will introduce support for `port` configuration, that should also follow the same logic. Signed-off-by: Ruslan Kuprieiev --- dvc/remote/ssh.py | 7 +++---- tests/unit/remote/ssh.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/unit/remote/ssh.py diff --git a/dvc/remote/ssh.py b/dvc/remote/ssh.py index db349065b..04fdf8c3f 100644 --- a/dvc/remote/ssh.py +++ b/dvc/remote/ssh.py @@ -60,10 +60,9 @@ class RemoteSSH(RemoteBase): super(RemoteSSH, self).__init__(project, config) self.url = config.get(Config.SECTION_REMOTE_URL, '/') self.host = self.group('host') - self.user = self.group('user') - if not self.user: - self.user = config.get(Config.SECTION_REMOTE_USER, - getpass.getuser()) + self.user = (config.get(Config.SECTION_REMOTE_USER) + or self.group('user') + or getpass.getuser()) self.prefix = self.group('path') self.port = config.get(Config.SECTION_REMOTE_PORT, self.DEFAULT_PORT) self.keyfile = config.get(Config.SECTION_REMOTE_KEY_FILE, None) diff --git a/tests/unit/remote/ssh.py b/tests/unit/remote/ssh.py new file mode 100644 index 000000000..0eed57b3a --- /dev/null +++ b/tests/unit/remote/ssh.py @@ -0,0 +1,24 @@ +import getpass + +from unittest import TestCase + +from dvc.remote.ssh import RemoteSSH + + +class TestRemoteSSH(TestCase): + def test_user(self): + url = 'ssh://127.0.0.1:/path/to/dir' + config = {'url': url} + remote = RemoteSSH(None, config) + self.assertEqual(remote.user, getpass.getuser()) + + user = 'test1' + url = 'ssh://{}@127.0.0.1:/path/to/dir'.format(user) + config = {'url': url} + remote = RemoteSSH(None, config) + self.assertEqual(remote.user, user) + + user = 'test2' + config['user'] = user + remote = RemoteSSH(None, config) + self.assertEqual(remote.user, user)