-
Notifications
You must be signed in to change notification settings - Fork 0
/
HsCopierTest.py
91 lines (67 loc) · 2.36 KB
/
HsCopierTest.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
"""
Test HsCopier
"""
from __future__ import print_function
import getpass
import os
import shutil
import socket
import unittest
from HsCopier import CopyUsingRSync, CopyUsingSCP
class HsCopierTest(unittest.TestCase):
"Test HsCopier"
SOURCE_DIR = "/tmp/testsrc"
TARGET_DIR = "/tmp/testtgt"
@classmethod
def __create_files(cls, num, topdir=None):
if topdir is None:
raise Exception("Top directory has not been set")
if not os.path.exists(topdir):
os.mkdir(topdir, 0o755)
created = []
for idx in range(num):
path = os.path.join(topdir, "file%d" % idx)
created.append(path)
if os.path.exists(path):
continue
with open(path, "w") as fout:
for cnt in range(100):
print("x"*cnt, file=fout)
return created
@classmethod
def __file_size(cls, files):
return sum([os.path.getsize(x) for x in files])
@property
def __is_access(self):
fullname = socket.gethostname()
return "access" in fullname
def tearDown(self):
if os.path.exists(self.SOURCE_DIR):
shutil.rmtree(self.SOURCE_DIR)
if os.path.exists(self.TARGET_DIR):
shutil.rmtree(self.TARGET_DIR)
def test_rsync(self):
"Test 'rsync' copy"
source_list = self.__create_files(3, topdir=self.SOURCE_DIR)
source_size = self.__file_size(source_list)
copier = CopyUsingRSync(getpass.getuser(), "localhost",
self.TARGET_DIR, "ignored")
copier.copy(source_list)
self.assertEqual(copier.size, source_size,
"Expected size %s, not %s" %
(source_size, copier.size))
def test_scp(self):
"Test 'scp' copy"
if self.__is_access:
return
source_list = self.__create_files(3, topdir=self.SOURCE_DIR)
source_size = self.__file_size(source_list)
copier = CopyUsingSCP(getpass.getuser(), "localhost", self.TARGET_DIR,
"ignored")
copier.copy(source_list)
self.assertEqual(copier.size, source_size,
"Expected size %s, not %s" %
(source_size, copier.size))
if __name__ == '__main__':
unittest.main()