|
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 Test the CPF configuration |
|
19 """ |
|
20 import unittest |
|
21 import string |
|
22 import sys,os,shutil |
|
23 import difflib, zipfile |
|
24 |
|
25 import __init__ |
|
26 |
|
27 from cone.public import exceptions,utils, api |
|
28 from cone.storage.filestorage import FileStorage |
|
29 from cone.storage.zipstorage import ZipStorage |
|
30 from testautomation import unzip_file |
|
31 from testautomation.base_testcase import BaseTestCase |
|
32 |
|
33 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
34 temp_dir = os.path.join(ROOT_PATH,"temp/export") |
|
35 test_cpf = os.path.join(ROOT_PATH,"testdata/test_project.cpf") |
|
36 datafolder = os.path.join(ROOT_PATH,"../../storage/tests/data") |
|
37 tempzip = os.path.join(temp_dir, "exported.zip") |
|
38 |
|
39 class TestConeProjectExport(BaseTestCase): |
|
40 def setUp(self): |
|
41 if not os.path.exists(temp_dir): |
|
42 os.makedirs(temp_dir) |
|
43 |
|
44 def test_export_to_zipstorage(self): |
|
45 test_project_dir = os.path.join(temp_dir, "test_project_1") |
|
46 unzip_file.unzip_file(test_cpf, test_project_dir, delete_if_exists=True) |
|
47 |
|
48 export_zip = os.path.join(temp_dir, "configexport.zip") |
|
49 |
|
50 fs = FileStorage(test_project_dir) |
|
51 p = api.Project(fs) |
|
52 conf = p.get_configuration('root5.confml') |
|
53 zs = ZipStorage(export_zip,"w") |
|
54 zp = api.Project(zs) |
|
55 conf_files = conf.list_resources() |
|
56 files1 = ['.metadata'] |
|
57 files1.extend(conf_files) |
|
58 p.export_configuration(conf,zs) |
|
59 zp.close() |
|
60 self.assertTrue(os.path.exists(export_zip)) |
|
61 zfile = zipfile.ZipFile(export_zip,"r") |
|
62 files2 = zfile.namelist() |
|
63 zfile.close() |
|
64 files1.sort() |
|
65 files2.sort() |
|
66 for i in range(len(files1)): |
|
67 self.assertEquals(files1[i],files2[i]) |
|
68 os.unlink(export_zip) |
|
69 |
|
70 def test_export_from_files_to_zipstorage_add(self): |
|
71 test_project_dir = os.path.join(temp_dir, "test_project_1") |
|
72 unzip_file.unzip_file(test_cpf, test_project_dir, delete_if_exists=True) |
|
73 |
|
74 export_zip = os.path.join(temp_dir, "configexport2.zip") |
|
75 |
|
76 fs = FileStorage(test_project_dir) |
|
77 p = api.Project(fs) |
|
78 exportconf = p.get_configuration('root4.confml') |
|
79 compareconf = p.get_configuration('root5.confml') |
|
80 zs = ZipStorage(export_zip,"w") |
|
81 zp = api.Project(zs) |
|
82 conf_files = compareconf.list_resources() |
|
83 files1 = ['.metadata'] |
|
84 files1.extend(conf_files) |
|
85 p.export_configuration(exportconf,zs) |
|
86 zp.close() |
|
87 self.assertTrue(os.path.exists(export_zip)) |
|
88 |
|
89 #Re-opening in append mode for adding new layers. |
|
90 zs = ZipStorage(export_zip,"a") |
|
91 zp = api.Project(zs) |
|
92 conf = p.get_configuration('Layer5/root.confml') |
|
93 p.export_configuration(conf,zs) |
|
94 zp.save() |
|
95 zp.close() |
|
96 |
|
97 zfile = zipfile.ZipFile(export_zip,"r") |
|
98 files2 = zfile.namelist() |
|
99 #Root file renaming. |
|
100 files2[files2.index('root4.confml')] = 'root5.confml' |
|
101 zfile.close() |
|
102 files1.sort() |
|
103 files2.sort() |
|
104 |
|
105 for i in range(len(files1)): |
|
106 self.assertEquals(files1[i],files2[i]) |
|
107 os.unlink(export_zip) |
|
108 |
|
109 |
|
110 def test_export_from_zip_to_zipstorage_add(self): |
|
111 export_zip = os.path.join(temp_dir, "configexport2.zip") |
|
112 zs_source = ZipStorage(test_cpf,'r') |
|
113 p = api.Project(zs_source) |
|
114 exportconf = p.get_configuration('root4.confml') |
|
115 compareconf = p.get_configuration('root5.confml') |
|
116 |
|
117 zs_target = ZipStorage(export_zip,"w") |
|
118 zp = api.Project(zs_target) |
|
119 conf_files = compareconf.list_resources() |
|
120 files1 = ['.metadata'] |
|
121 files1.extend(conf_files) |
|
122 p.export_configuration(exportconf,zs_target) |
|
123 zp.close() |
|
124 self.assertTrue(os.path.exists(export_zip)) |
|
125 |
|
126 #Re-opening in append mode for adding new layers. |
|
127 zs_target = ZipStorage(export_zip,"a") |
|
128 zp = api.Project(zs_target) |
|
129 conf = p.get_configuration('Layer5/root.confml') |
|
130 p.export_configuration(conf,zs_target) |
|
131 zp.save() |
|
132 zp.close() |
|
133 |
|
134 zfile = zipfile.ZipFile(export_zip,"r") |
|
135 files2 = zfile.namelist() |
|
136 #Root file renaming. |
|
137 files2[files2.index('root4.confml')] = 'root5.confml' |
|
138 zfile.close() |
|
139 files1.sort() |
|
140 files2.sort() |
|
141 |
|
142 for i in range(len(files1)): |
|
143 self.assertEquals(files1[i],files2[i]) |
|
144 os.unlink(export_zip) |
|
145 |
|
146 |
|
147 def test_export_from_zipstorage(self): |
|
148 output_dir = os.path.join(temp_dir, "export_from_zipstorage") |
|
149 self.remove_if_exists(output_dir) |
|
150 |
|
151 zs = ZipStorage(test_cpf,"r") |
|
152 p = api.Project(zs) |
|
153 fs = FileStorage(output_dir,"w") |
|
154 r = api.Project(fs) |
|
155 conf = p.get_configuration('root5.confml') |
|
156 conf_files = conf.list_resources() |
|
157 conf_files.append('.metadata') |
|
158 p.export_configuration(conf,fs) |
|
159 p.close() |
|
160 r.close() |
|
161 self.assertTrue(os.path.exists(output_dir)) |
|
162 |
|
163 # Temporarly hacked to pass, because feature is not supported |
|
164 # def test_export_to_zipstorage_multiple_configurations(self): |
|
165 # try: |
|
166 # fs = FileStorage(datafolder) |
|
167 # p = api.Project(fs) |
|
168 # zs = ZipStorage(tempzip,"w") |
|
169 # zp = api.Project(zs) |
|
170 # conf = p.get_configuration('morestuff.confml') |
|
171 # conf_files = conf.list_resources() |
|
172 # p.export_configuration(conf,zs) |
|
173 # conf = p.get_configuration('prodX.confml') |
|
174 # conf_files2 = conf.list_resources() |
|
175 # conf_files.extend(conf_files2) |
|
176 # conf_files = utils.distinct_array(conf_files) |
|
177 # p.export_configuration(conf,zs) |
|
178 # zp.close() |
|
179 # self.assertTrue(os.path.exists(tempzip)) |
|
180 # zfile = zipfile.ZipFile(tempzip,"r") |
|
181 # files = zfile.namelist() |
|
182 # files.remove('.metadata') |
|
183 # self.assertEquals(sorted(conf_files),sorted(files)) |
|
184 # except exceptions.NotSupportedException: |
|
185 # pass |
|
186 |
|
187 def test_export_to_filestorage_multiple_configurations(self): |
|
188 fs = FileStorage(datafolder) |
|
189 p = api.Project(fs) |
|
190 fs2 = FileStorage("temp/exported","w") |
|
191 p2 = api.Project(fs2) |
|
192 conf = p.get_configuration('morestuff.confml') |
|
193 conf_files = conf.list_resources() |
|
194 p.export_configuration(conf,fs2) |
|
195 conf = p.get_configuration('prodX.confml') |
|
196 conf_files.extend(conf.list_resources()) |
|
197 |
|
198 p.export_configuration(conf,fs2) |
|
199 p2.close() |
|
200 self.assertTrue(os.path.exists("temp/exported")) |
|
201 |
|
202 files = fs2.list_resources("/",True) |
|
203 conf_files = utils.distinct_array(conf_files) |
|
204 files.sort() |
|
205 conf_files.append('.metadata') |
|
206 conf_files.sort() |
|
207 self.assertEquals(conf_files,files) |
|
208 shutil.rmtree("temp") |
|
209 |
|
210 |
|
211 |
|
212 if __name__ == '__main__': |
|
213 unittest.main() |
|
214 |