Skip to content

Commit bd97506

Browse files
committed
Fixup firewall tests.
1 parent 53c07f7 commit bd97506

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

sshuttle/tests/test_firewall.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
from mock import Mock, patch, call
22
import io
3-
import os
4-
import os.path
5-
import shutil
6-
import filecmp
73

84
import sshuttle.firewall
95

@@ -19,27 +15,27 @@ def setup_daemon():
1915
10,2404:6800:4004:80c::33
2016
PORTS 1024,1025,1026,1027
2117
GO 1
18+
HOST 1.2.3.3,existing
2219
""")
2320
stdout = Mock()
2421
return stdin, stdout
2522

2623

27-
@patch('sshuttle.firewall.HOSTSFILE', new='tmp/hosts')
28-
@patch('sshuttle.firewall.hostmap', new={
29-
'myhost': '1.2.3.4',
30-
'myotherhost': '1.2.3.5',
31-
})
32-
def test_rewrite_etc_hosts():
33-
if not os.path.isdir("tmp"):
34-
os.mkdir("tmp")
24+
def test_rewrite_etc_hosts(tmpdir):
25+
orig_hosts = tmpdir.join("hosts.orig")
26+
orig_hosts.write("1.2.3.3 existing\n")
3527

36-
with open("tmp/hosts.orig", "w") as f:
37-
f.write("1.2.3.3 existing\n")
28+
new_hosts = tmpdir.join("hosts")
29+
orig_hosts.copy(new_hosts)
3830

39-
shutil.copyfile("tmp/hosts.orig", "tmp/hosts")
31+
hostmap = {
32+
'myhost': '1.2.3.4',
33+
'myotherhost': '1.2.3.5',
34+
}
35+
with patch('sshuttle.firewall.HOSTSFILE', new=str(new_hosts)):
36+
sshuttle.firewall.rewrite_etc_hosts(hostmap, 10)
4037

41-
sshuttle.firewall.rewrite_etc_hosts(10)
42-
with open("tmp/hosts") as f:
38+
with new_hosts.open() as f:
4339
line = f.readline()
4440
s = line.split()
4541
assert s == ['1.2.3.3', 'existing']
@@ -57,39 +53,37 @@ def test_rewrite_etc_hosts():
5753
line = f.readline()
5854
assert line == ""
5955

60-
sshuttle.firewall.restore_etc_hosts(10)
61-
assert filecmp.cmp("tmp/hosts.orig", "tmp/hosts", shallow=False) is True
56+
with patch('sshuttle.firewall.HOSTSFILE', new=str(new_hosts)):
57+
sshuttle.firewall.restore_etc_hosts(10)
58+
assert orig_hosts.computehash() == new_hosts.computehash()
6259

6360

64-
@patch('sshuttle.firewall.HOSTSFILE', new='tmp/hosts')
61+
@patch('sshuttle.firewall.rewrite_etc_hosts')
6562
@patch('sshuttle.firewall.setup_daemon')
6663
@patch('sshuttle.firewall.get_method')
67-
def test_main(mock_get_method, mock_setup_daemon):
64+
def test_main(mock_get_method, mock_setup_daemon, mock_rewrite_etc_hosts):
6865
stdin, stdout = setup_daemon()
6966
mock_setup_daemon.return_value = stdin, stdout
7067

71-
if not os.path.isdir("tmp"):
72-
os.mkdir("tmp")
68+
mock_get_method("not_auto").name = "test"
69+
mock_get_method.reset_mock()
7370

74-
sshuttle.firewall.main("test", False)
71+
sshuttle.firewall.main("not_auto", False)
7572

76-
with open("tmp/hosts") as f:
77-
line = f.readline()
78-
s = line.split()
79-
assert s == ['1.2.3.3', 'existing']
80-
81-
line = f.readline()
82-
assert line == ""
73+
assert mock_rewrite_etc_hosts.mock_calls == [
74+
call({'1.2.3.3': 'existing'}, 1024),
75+
call({}, 1024),
76+
]
8377

84-
stdout.mock_calls == [
78+
assert stdout.mock_calls == [
8579
call.write('READY test\n'),
8680
call.flush(),
8781
call.write('STARTED\n'),
8882
call.flush()
8983
]
90-
mock_setup_daemon.mock_calls == [call()]
91-
mock_get_method.mock_calls == [
92-
call('test'),
84+
assert mock_setup_daemon.mock_calls == [call()]
85+
assert mock_get_method.mock_calls == [
86+
call('not_auto'),
9387
call().setup_firewall(
9488
1024, 1026,
9589
[(10, u'2404:6800:4004:80c::33')],
@@ -104,6 +98,7 @@ def test_main(mock_get_method, mock_setup_daemon):
10498
[(2, 24, False, u'1.2.3.0'), (2, 32, True, u'1.2.3.66')],
10599
True),
106100
call().setup_firewall()(),
101+
call().setup_firewall()(),
107102
call().setup_firewall(1024, 0, [], 10, [], True),
108103
call().setup_firewall(1025, 0, [], 2, [], True),
109104
]

0 commit comments

Comments
 (0)