#!/usr/bin/env python # Copyright 2010-2015 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . '''Test SftpFS. This can't be part of the normal unit tests, since it requires access to a (real) ssh server. To run these tests, you must arrange for localhost to be able to accept ssh connections using the ssh agent. ''' import logging import os import pwd import shutil import tempfile import unittest import obnamlib import obnamlib.plugins.sftp_plugin class SftpTests(unittest.TestCase, obnamlib.VfsTests): def setUp(self): self.basepath = tempfile.mkdtemp() baseurl = 'sftp://localhost%s' % self.basepath settings = { 'pure-paramiko': False, 'create': True, 'sftp-delay': 0, 'ssh-key': '', 'strict-ssh-host-keys': False, 'ssh-known-hosts': os.path.expanduser('~/.ssh/known_hosts'), 'ssh-command': None, 'ssh-host-keys-check': 'no', } self.fs = obnamlib.plugins.sftp_plugin.SftpFS( baseurl, settings=settings) self.fs.connect() def tearDown(self): self.fs.close() shutil.rmtree(self.basepath) def test_connect_to_remote_root_dir(self): url = 'sftp://localhost/' settings = { 'pure-paramiko': False, 'create': True, 'sftp-delay': 0, 'ssh-key': '', 'strict-ssh-host-keys': False, 'ssh-known-hosts': os.path.expanduser('~/.ssh/known_hosts'), 'ssh-command': None, 'ssh-host-keys-check': 'no', } fs = obnamlib.plugins.sftp_plugin.SftpFS(url, settings=settings) fs.connect() cwd = fs.getcwd() self.assertEqual(cwd, '/') def test_connect_to_remote_root_dir_using_ipv6_address(self): url = 'sftp://[::1]/' settings = { 'pure-paramiko': False, 'create': True, 'sftp-delay': 0, 'ssh-key': '', 'strict-ssh-host-keys': False, 'ssh-known-hosts': os.path.expanduser('~/.ssh/known_hosts'), 'ssh-command': None, 'ssh-host-keys-check': 'no', } fs = obnamlib.plugins.sftp_plugin.SftpFS(url, settings=settings) fs.connect() cwd = fs.getcwd() self.assertEqual(cwd, '/') def test_sets_path_to_absolute_path(self): self.assertTrue(self.fs.path.startswith('/')) def test_resolves_magic_homedir_prefix(self): baseurl = 'sftp://localhost/~/' settings = { 'pure-paramiko': False, 'create': True, 'sftp-delay': 0, 'ssh-key': '', 'strict-ssh-host-keys': False, 'ssh-known-hosts': os.path.expanduser('~/.ssh/known_hosts'), 'ssh-command': None, 'ssh-host-keys-check': 'no', } fs = obnamlib.plugins.sftp_plugin.SftpFS(baseurl, settings=settings) fs.connect() homedir = pwd.getpwuid(os.getuid()).pw_dir self.assertEqual(fs._initial_dir, homedir) self.assertEqual(fs.getcwd(), homedir) def test_initial_cwd_is_basepath(self): self.assertEqual(self.fs.getcwd(), self.fs.path) def test_link_creates_hard_link(self): pass # sftp does not support hardlinking, so not testing it def test_mknod_creates_fifo(self): self.assertRaises(NotImplementedError, self.fs.mknod, 'foo', 0) # Override method from the VfsTests class. SFTP doesn't do sub-second # timestamps, so we fix the test here to not set those fields to nonzero. def test_lutimes_sets_times_correctly(self): self.fs.mkdir('foo') self.fs.lutimes('foo', 1, 2*1000, 3, 4*1000) self.assertEqual(self.fs.lstat('foo').st_atime_sec, 1) self.assertEqual(self.fs.lstat('foo').st_atime_nsec, 0) self.assertEqual(self.fs.lstat('foo').st_mtime_sec, 3) self.assertEqual(self.fs.lstat('foo').st_mtime_nsec, 0) def test_get_username_returns_None_for_zero(self): self.assertEqual(self.fs.get_username(0), None) def test_get_groupname_returns_None_for_zero(self): self.assertEqual(self.fs.get_groupname(0), None) if __name__ == '__main__': logging.basicConfig(filename='/dev/null') unittest.main()