|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 |
|
18 """ |
|
19 Test the import export functionality |
|
20 """ |
|
21 |
|
22 import unittest |
|
23 import sys, os, shutil |
|
24 import __init__ |
|
25 |
|
26 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
27 |
|
28 from cone.public.api import Resource |
|
29 from cone.storage import filestorage, zipstorage |
|
30 |
|
31 from testautomation.base_testcase import BaseTestCase |
|
32 from testautomation.unzip_file import unzip_file |
|
33 |
|
34 def abspath(p): |
|
35 return os.path.normpath(os.path.join(ROOT_PATH, p)) |
|
36 |
|
37 DATA_ZIP = abspath("file_vs_zip_data.zip") |
|
38 TEMP_DIR = abspath("temp/file_vs_zip") |
|
39 |
|
40 EXPECTED = [ |
|
41 # These two are not used every time |
|
42 ('folder', 'dir'), |
|
43 ('folder', 'empty2'), |
|
44 |
|
45 ('folder', 'dir/emptysub'), |
|
46 ('folder', 'empty'), |
|
47 ('folder', 'empty2/sub'), |
|
48 ('resource', 'root.txt'), |
|
49 ('resource', 'dir/test1.txt'), |
|
50 ('resource', 'dir/test2.txt'), |
|
51 ] |
|
52 |
|
53 class TestFileStorageVsZipStorage(BaseTestCase): |
|
54 def setUp(self): |
|
55 pass |
|
56 |
|
57 def get_temp_zip_storage(self, path, empty=False): |
|
58 """ |
|
59 Get a new zip storage located in the temp/ directory. |
|
60 |
|
61 @param path: Path of the storage zip file relative to the temp/ directory. |
|
62 @param empty: If False, the DATA_ZIP is copied to the location specified |
|
63 by 'path' and returned as a read-only storage. If True, a new writable |
|
64 empty storage is returned. |
|
65 """ |
|
66 full_path = os.path.join(TEMP_DIR, path) |
|
67 if not empty: |
|
68 self.create_dir_for_file_path(full_path) |
|
69 shutil.copy2(DATA_ZIP, full_path) |
|
70 return zipstorage.ZipStorage(full_path, 'r') |
|
71 else: |
|
72 if os.path.exists(full_path): |
|
73 os.remove(full_path) |
|
74 return zipstorage.ZipStorage(full_path, 'w') |
|
75 |
|
76 def get_temp_file_storage(self, path, empty=False): |
|
77 """ |
|
78 Get a new file storage located in the temp/ directory. |
|
79 |
|
80 @param path: Path of the storage directory relative to the temp/ directory. |
|
81 @param empty: If False, the DATA_ZIP is extracted to the location specified |
|
82 by 'path' and returned as a read-only storage. If True, a new writable |
|
83 empty storage is returned. |
|
84 """ |
|
85 full_path = os.path.join(TEMP_DIR, path) |
|
86 if not empty: |
|
87 unzip_file(DATA_ZIP, full_path, delete_if_exists=True) |
|
88 return filestorage.FileStorage(full_path, 'r') |
|
89 else: |
|
90 self.recreate_dir(full_path) |
|
91 return filestorage.FileStorage(full_path, 'w') |
|
92 |
|
93 def assert_storage_contains_expected(self, storage, empty_folders): |
|
94 # Always list also empty folders, because empty_folders is False, |
|
95 # they should not have been exported in the first place |
|
96 resource_list = storage.list_resources('', recurse=True, empty_folders=True) |
|
97 |
|
98 expected = list(EXPECTED) |
|
99 |
|
100 # Remove empty folders from expected if necessary |
|
101 if not empty_folders: |
|
102 for i in reversed(xrange(len(expected))): |
|
103 if expected[i][0] == 'folder': |
|
104 del expected[i] |
|
105 |
|
106 # If the actual list contains exactly 2 less entries, they are |
|
107 # probably the root folders that are not present in every case |
|
108 # (e.g. there is a resource 'my_dir/file.txt', so sometimes there |
|
109 # is a folder 'my_dir' in the resource list, but not always) |
|
110 if len(resource_list) == len(expected) - 2: |
|
111 del expected[0:2] |
|
112 |
|
113 # Check that there is the same amount of resources/folders |
|
114 self.assertEquals(len(expected), len(resource_list), "Expected %r, actual %r" % (expected, resource_list)) |
|
115 |
|
116 # Check that all expected resources/folders are present in the storage |
|
117 for type, ref in expected: |
|
118 if type == 'resource': |
|
119 self.assertTrue(storage.is_resource(ref), "(%r, %r) expected, but not a resource" % (type, ref)) |
|
120 elif type == 'folder': |
|
121 self.assertTrue(storage.is_folder(ref), "(%r, %r) expected, but not a folder" % (type, ref)) |
|
122 else: |
|
123 raise RuntimeError("Invalid type field in expected data") |
|
124 |
|
125 def _run_test_storage_to_storage(self, source_storage, target_storage, empty_folders): |
|
126 try: |
|
127 # Check that the source storage contains all expected in the first place |
|
128 self.assert_storage_contains_expected(source_storage, True) |
|
129 |
|
130 # Export resources |
|
131 resources = source_storage.list_resources('', recurse=True, empty_folders=empty_folders) |
|
132 source_storage.export_resources(resources, target_storage, empty_folders=empty_folders) |
|
133 |
|
134 # Check that resources have been exported properly |
|
135 self.assert_storage_contains_expected(target_storage, empty_folders) |
|
136 finally: |
|
137 source_storage.close() |
|
138 target_storage.close() |
|
139 |
|
140 def test_export_file_to_file(self): |
|
141 self._run_test_storage_to_storage( |
|
142 source_storage = self.get_temp_file_storage('f2f/source'), |
|
143 target_storage = self.get_temp_file_storage('f2f/target', empty=True), |
|
144 empty_folders = False) |
|
145 |
|
146 self._run_test_storage_to_storage( |
|
147 source_storage = self.get_temp_file_storage('f2f/ef_source'), |
|
148 target_storage = self.get_temp_file_storage('f2f/ef_target', empty=True), |
|
149 empty_folders = True) |
|
150 |
|
151 def test_export_zip_to_zip(self): |
|
152 self._run_test_storage_to_storage( |
|
153 source_storage = self.get_temp_zip_storage('z2z/source.zip'), |
|
154 target_storage = self.get_temp_zip_storage('z2z/target.zip', empty=True), |
|
155 empty_folders = False) |
|
156 |
|
157 self._run_test_storage_to_storage( |
|
158 source_storage = self.get_temp_zip_storage('z2z/ef_source.zip'), |
|
159 target_storage = self.get_temp_zip_storage('z2z/ef_target.zip', empty=True), |
|
160 empty_folders = True) |
|
161 |
|
162 def test_export_zip_to_file(self): |
|
163 self._run_test_storage_to_storage( |
|
164 source_storage = self.get_temp_zip_storage('z2f/source.zip'), |
|
165 target_storage = self.get_temp_file_storage('z2f/target', empty=True), |
|
166 empty_folders = False) |
|
167 |
|
168 self._run_test_storage_to_storage( |
|
169 source_storage = self.get_temp_zip_storage('z2f/ef_source.zip'), |
|
170 target_storage = self.get_temp_file_storage('z2f/ef_target', empty=True), |
|
171 empty_folders = True) |
|
172 |
|
173 def test_export_file_to_zip(self): |
|
174 self._run_test_storage_to_storage( |
|
175 source_storage = self.get_temp_file_storage('f2z/source'), |
|
176 target_storage = self.get_temp_zip_storage('f2z/target.zip', empty=True), |
|
177 empty_folders = False) |
|
178 |
|
179 self._run_test_storage_to_storage( |
|
180 source_storage = self.get_temp_file_storage('f2z/ef_source'), |
|
181 target_storage = self.get_temp_zip_storage('f2z/ef_target.zip', empty=True), |
|
182 empty_folders = True) |
|
183 |
|
184 |
|
185 if __name__ == '__main__': |
|
186 unittest.main() |