(tmpdir)
| 27 | |
| 28 | |
| 29 | def test_rewrite_etc_hosts(tmpdir): |
| 30 | orig_hosts = tmpdir.join("hosts.orig") |
| 31 | orig_hosts.write("1.2.3.3 existing\n") |
| 32 | |
| 33 | new_hosts = tmpdir.join("hosts") |
| 34 | orig_hosts.copy(new_hosts) |
| 35 | |
| 36 | hostmap = { |
| 37 | 'myhost': '1.2.3.4', |
| 38 | 'myotherhost': '1.2.3.5', |
| 39 | } |
| 40 | with patch('sshuttle.firewall.HOSTSFILE', new=str(new_hosts)): |
| 41 | sshuttle.firewall.rewrite_etc_hosts(hostmap, 10) |
| 42 | |
| 43 | with new_hosts.open() as f: |
| 44 | line = f.readline() |
| 45 | s = line.split() |
| 46 | assert s == ['1.2.3.3', 'existing'] |
| 47 | |
| 48 | line = f.readline() |
| 49 | s = line.split() |
| 50 | assert s == ['1.2.3.4', 'myhost', |
| 51 | '#', 'sshuttle-firewall-10', 'AUTOCREATED'] |
| 52 | |
| 53 | line = f.readline() |
| 54 | s = line.split() |
| 55 | assert s == ['1.2.3.5', 'myotherhost', |
| 56 | '#', 'sshuttle-firewall-10', 'AUTOCREATED'] |
| 57 | |
| 58 | line = f.readline() |
| 59 | assert line == "" |
| 60 | |
| 61 | with patch('sshuttle.firewall.HOSTSFILE', new=str(new_hosts)): |
| 62 | sshuttle.firewall.restore_etc_hosts(hostmap, 10) |
| 63 | assert orig_hosts.computehash() == new_hosts.computehash() |
| 64 | |
| 65 | |
| 66 | @patch('os.link') |
no test coverage detected