summaryrefslogtreecommitdiff
path: root/test-sftpfs
blob: 33d841466b2578c61eed1af7a9cb1670a8760136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/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 <http://www.gnu.org/licenses/>.


'''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()