annotate gcp_test.py @ 57:d4f1235c860b

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