forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_unittest.py
executable file
·73 lines (64 loc) · 2.61 KB
/
remote_unittest.py
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
#!/usr/bin/python
import unittest, os
import common, remote, data_dir
class RemoteFileTest(unittest.TestCase):
tmp_dir = data_dir.get_tmp_dir()
test_file_path = os.path.join(tmp_dir, "remote_file")
default_data = ["RemoteFile Test.\n", "Pattern Line."]
def __del__(self):
if os.path.exists(self.test_file_path):
os.remove(self.test_file_path)
def _new_remote_file(self):
if os.path.exists(self.test_file_path):
os.remove(self.test_file_path)
test_file = open(self.test_file_path, "w")
test_file.writelines(self.default_data)
test_file.close()
remote_file = remote.RemoteFile(None, "test", None, None, None,
self.test_file_path)
return remote_file
def _read_test_file(self):
test_file = open(self.test_file_path, "r")
test_data = test_file.readlines()
test_file.close()
return test_data
def testAdd(self):
remote_file = self._new_remote_file()
_add_list = ["add_line_1", "add_line_2", "add_line_3"]
remote_file.add(_add_list)
test_data = self._read_test_file()
except_data = ["RemoteFile Test.\n",
"Pattern Line.\n",
"add_line_1\n",
"add_line_2\n",
"add_line_3"]
for index in range(len(except_data)):
self.assertEqual(except_data[index], test_data[index])
del remote_file
test_data = self._read_test_file()
self.assertEqual(test_data, self.default_data)
def testSub(self):
remote_file = self._new_remote_file()
_pattern2repl = {r"Remote":"Local", r"^Pat.*$":"Replace Line"}
remote_file.sub(_pattern2repl)
test_data = self._read_test_file()
except_data = ["LocalFile Test.\n",
"Replace Line"]
for index in range(len(except_data)):
self.assertEqual(except_data[index], test_data[index])
del remote_file
test_data = self._read_test_file()
self.assertEqual(test_data, self.default_data)
def testRemove(self):
remote_file = self._new_remote_file()
_pattern_list = [r"^Pattern"]
remote_file.remove(_pattern_list)
test_data = self._read_test_file()
except_data = ["RemoteFile Test."]
for index in range(len(except_data)):
self.assertEqual(except_data[index], test_data[index])
del remote_file
test_data = self._read_test_file()
self.assertEqual(test_data, self.default_data)
if __name__ == "__main__":
unittest.main()