57
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 gcp: Goffi's CoPier |
|
6 Copyright (C) 2010, 2011 Jérôme Poisson <goffi@goffi.org> |
|
7 (c) 2011 Thomas Preud'homme <robotux@celest.fr> |
|
8 |
|
9 This program is free software: you can redistribute it and/or modify |
|
10 it under the terms of the GNU General Public License as published by |
|
11 the Free Software Foundation, either version 3 of the License, or |
|
12 (at your option) any later version. |
|
13 |
|
14 This program is distributed in the hope that it will be useful, |
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 GNU General Public License for more details. |
|
18 |
|
19 You should have received a copy of the GNU General Public License |
|
20 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 """ |
|
22 |
|
23 from __future__ import with_statement |
|
24 import tempfile |
|
25 import unittest |
|
26 from os import getcwd, chdir, system |
|
27 from shutil import rmtree |
|
28 from random import randrange |
|
29 from hashlib import sha1 |
|
30 |
|
31 #size shorcuts |
|
32 S10K = 1024 * 10 |
|
33 S100K = 1024 * 100 |
|
34 S1M = 1024 * 1024 |
|
35 S10M = 1024 * 1024 * 10 |
|
36 S100M = 1024 * 1024 * 100 |
|
37 |
|
38 |
|
39 def sha1sum(filename, buf_size=4096): |
|
40 csum = sha1() |
|
41 with open(filename) as fd: |
|
42 data = fd.read(buf_size) |
|
43 while data: |
|
44 csum.update(data) |
|
45 data = fd.read(buf_size) |
|
46 return csum.digest() |
|
47 |
|
48 #def makeRandomFile(path, size, buf_size=4096): |
|
49 # """Create a fake file |
|
50 # @param path: where the file is created |
|
51 # @param size: size of the file to create in bytes""" |
|
52 # def seq(size): |
|
53 # return ''.join(chr(randrange(256)) for i in range(size)) |
|
54 # fd = open(path, 'w') |
|
55 # for byte in range(size//buf_size): |
|
56 # fd.write(seq(buf_size)) |
|
57 # fd.write(seq(size%buf_size)) |
|
58 # fd.close() |
|
59 |
|
60 def makeRandomFile(path, size, buf_size=4096): |
|
61 """Create a fake file using /dev/urandom |
|
62 @param path: where the file is created |
|
63 @param size: size of the file to create in bytes""" |
|
64 source = open('/dev/urandom','r') |
|
65 dest = open(path, 'w') |
|
66 for byte in range(size//buf_size): |
|
67 dest.write(source.read(buf_size)) |
|
68 dest.write(source.read(size%buf_size)) |
|
69 dest.close() |
|
70 |
|
71 |
|
72 class TestCopyCases(unittest.TestCase): |
|
73 """Test basic copy use cases, using gcp externally |
|
74 gcp must be available in the PATH""" |
|
75 #TODO: check journal |
|
76 |
|
77 def setUp(self): |
|
78 self.ori_dir = getcwd() |
|
79 self.tmp_dir = tempfile.mkdtemp() |
|
80 chdir(self.tmp_dir) |
|
81 |
|
82 def tearDown(self): |
|
83 chdir(self.ori_dir) |
|
84 rmtree(self.tmp_dir) |
|
85 |
|
86 |
|
87 def test_one_file_copy(self): |
|
88 """Copy one file and test the result""" |
|
89 makeRandomFile('file_1', S10K) |
|
90 ori_sum = sha1sum('file_1') |
|
91 ret = system("gcp file_1 file_2") |
|
92 self.assertEqual(ret,0) |
|
93 dest_sum = sha1sum('file_2') |
|
94 self.assertEqual(ori_sum, dest_sum) |
|
95 |
|
96 def test_one_file_copy_already_exists(self): |
|
97 """Check that an existing file is not overwritten""" |
|
98 makeRandomFile('file_1', S10K) |
|
99 makeRandomFile('file_2', S10K) |
|
100 file_2_sum = sha1sum('file_2') |
|
101 ret = system("gcp file_1 file_2") |
|
102 self.assertNotEqual(ret,0) |
|
103 file_2_sum_bis = sha1sum('file_2') |
|
104 self.assertEqual(file_2_sum, file_2_sum_bis) |
|
105 |
|
106 def test_one_file_copy_already_exists_force(self): |
|
107 """Check that an existing file is overwritten with --force""" |
|
108 makeRandomFile('file_1', S10K) |
|
109 makeRandomFile('file_2', S10K) |
|
110 file_1_sum = sha1sum('file_1') |
|
111 ret = system("gcp -f file_1 file_2") |
|
112 self.assertEqual(ret,0) |
|
113 file_2_sum_bis = sha1sum('file_2') |
|
114 self.assertEqual(file_1_sum, file_2_sum_bis) |
|
115 |
|
116 |
|
117 if __name__ == '__main__': |
|
118 unittest.main() |