forked from bit-team/backintime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
168 lines (151 loc) · 8.07 KB
/
test_config.py
File metadata and controls
168 lines (151 loc) · 8.07 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Back In Time
# Copyright (C) 2016 Taylor Raack
#
# 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 2 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, write to the Free Software Foundation,Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os
import stat
import sys
from test import generic
from tempfile import TemporaryDirectory
from unittest.mock import patch
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import config
class TestConfig(generic.TestCaseCfg):
def test_set_snapshots_path_test_writes(self):
with TemporaryDirectory() as dirpath:
self.assertTrue(self.cfg.setSnapshotsPath(dirpath))
def test_set_snapshots_path_fails_on_ro(self):
with TemporaryDirectory() as dirpath:
# set directory to read only
with generic.mockPermissions(dirpath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH):
self.assertFalse(self.cfg.setSnapshotsPath(dirpath))
@patch('os.chmod')
def test_set_snapshots_path_permission_fail(self, mock_chmod):
mock_chmod.side_effect = PermissionError()
with TemporaryDirectory() as dirpath:
self.assertTrue(self.cfg.setSnapshotsPath(dirpath))
class TestSshCommand(generic.SSHTestCase):
def test_full_command(self):
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'])
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user()),
'echo', 'foo'])
def test_custom_args(self):
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'],
custom_args = ['-o', 'PreferredAuthentications=publickey'])
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'-o', 'PreferredAuthentications=publickey',
'{}@localhost'.format(self.cfg.user()),
'echo', 'foo'])
def test_cipher(self):
self.cfg.setSshCipher('aes256-cbc')
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'])
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'-o', 'Ciphers=aes256-cbc',
'{}@localhost'.format(self.cfg.user()),
'echo', 'foo'])
# disable cipher
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'], cipher = False)
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user()),
'echo', 'foo'])
def test_without_command(self):
cmd = self.cfg.sshCommand()
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user())])
def test_nice(self):
self.cfg.setNiceOnRemote(True)
self.cfg.setIoniceOnRemote(True)
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'])
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user()),
'ionice', '-c2', '-n7',
'nice', '-n19',
'echo', 'foo'])
# without cmd no io-/nice
cmd = self.cfg.sshCommand()
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user())])
def test_quote(self):
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'], quote = True)
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user()),
"'", 'echo', 'foo', "'"])
# without cmd no quotes
cmd = self.cfg.sshCommand(quote = True)
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user())])
def test_prefix(self):
self.cfg.setSshPrefix(True, 'echo bar')
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'])
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user()),
'echo', 'bar',
'echo', 'foo'])
# disable prefix
cmd = self.cfg.sshCommand(cmd = ['echo', 'foo'], prefix = False)
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE),
'-p', '22',
'{}@localhost'.format(self.cfg.user()),
'echo', 'foo'])
def test_disable_args(self):
cmd = self.cfg.sshCommand(port = False, user_host = False)
self.assertListEqual(cmd, ['ssh',
'-o', 'ServerAliveInterval=240',
'-o', 'LogLevel=Error',
'-o', 'IdentityFile={}'.format(generic.PRIV_KEY_FILE)])