1 # Licensed to the Apache ftware Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
21 from aria.storage.filesystem_rapi import FileSystemResourceAPI
22 from aria.storage import (
26 from . import TestFileSystem
29 class TestResourceStorage(TestFileSystem):
30 def _create(self, storage):
31 storage.register('service_template')
33 def _upload(self, storage, tmp_path, id):
34 with open(tmp_path, 'w') as f:
35 f.write('fake context')
37 storage.service_template.upload(entry_id=id, source=tmp_path)
39 def _upload_dir(self, storage, tmp_dir, tmp_file_name, id):
40 file_source = os.path.join(tmp_dir, tmp_file_name)
41 with open(file_source, 'w') as f:
42 f.write('fake context')
44 storage.service_template.upload(entry_id=id, source=tmp_dir)
46 def _create_storage(self):
47 return ResourceStorage(FileSystemResourceAPI,
48 api_kwargs=dict(directory=self.path))
51 api = FileSystemResourceAPI
52 storage = ResourceStorage(FileSystemResourceAPI,
53 items=['service_template'],
54 api_kwargs=dict(directory=self.path))
55 assert repr(storage) == 'ResourceStorage(api={api})'.format(api=api)
56 assert 'directory={resource_dir}'.format(resource_dir=self.path) in \
57 repr(storage.registered['service_template'])
59 def test_create(self):
60 storage = self._create_storage()
62 assert os.path.exists(os.path.join(self.path, 'service_template'))
64 def test_upload_file(self):
65 storage = ResourceStorage(FileSystemResourceAPI, api_kwargs=dict(directory=self.path))
67 tmpfile_path = tempfile.mkstemp(suffix=self.__class__.__name__, dir=self.path)[1]
68 self._upload(storage, tmpfile_path, id='service_template_id')
70 storage_path = os.path.join(
73 'service_template_id',
74 os.path.basename(tmpfile_path))
75 assert os.path.exists(storage_path)
77 with open(storage_path, 'rb') as f:
78 assert f.read() == 'fake context'
80 def test_download_file(self):
81 storage = self._create_storage()
83 tmpfile_path = tempfile.mkstemp(suffix=self.__class__.__name__, dir=self.path)[1]
84 tmpfile_name = os.path.basename(tmpfile_path)
85 self._upload(storage, tmpfile_path, 'service_template_id')
87 temp_dir = tempfile.mkdtemp(dir=self.path)
88 storage.service_template.download(
89 entry_id='service_template_id',
93 with open(os.path.join(self.path, os.path.join(temp_dir, tmpfile_name))) as f:
94 assert f.read() == 'fake context'
96 def test_download_non_existing_file(self):
97 storage = self._create_storage()
99 with pytest.raises(exceptions.StorageError):
100 storage.service_template.download(entry_id='service_template_id', destination='',
103 def test_data_non_existing_file(self):
104 storage = self._create_storage()
105 self._create(storage)
106 with pytest.raises(exceptions.StorageError):
107 storage.service_template.read(entry_id='service_template_id', path='fake_path')
109 def test_data_file(self):
110 storage = self._create_storage()
111 self._create(storage)
112 tmpfile_path = tempfile.mkstemp(suffix=self.__class__.__name__, dir=self.path)[1]
113 self._upload(storage, tmpfile_path, 'service_template_id')
115 assert storage.service_template.read(entry_id='service_template_id',
116 path=os.path.basename(tmpfile_path)) == 'fake context'
118 def test_upload_dir(self):
119 storage = self._create_storage()
120 self._create(storage)
121 tmp_dir = tempfile.mkdtemp(suffix=self.__class__.__name__, dir=self.path)
122 second_level_tmp_dir = tempfile.mkdtemp(dir=tmp_dir)
123 tmp_filename = tempfile.mkstemp(dir=second_level_tmp_dir)[1]
124 self._upload_dir(storage, tmp_dir, tmp_filename, id='service_template_id')
126 destination = os.path.join(
129 'service_template_id',
130 os.path.basename(second_level_tmp_dir),
131 os.path.basename(tmp_filename))
133 assert os.path.isfile(destination)
135 def test_upload_path_in_dir(self):
136 storage = self._create_storage()
137 self._create(storage)
138 tmp_dir = tempfile.mkdtemp(suffix=self.__class__.__name__, dir=self.path)
139 second_level_tmp_dir = tempfile.mkdtemp(dir=tmp_dir)
140 tmp_filename = tempfile.mkstemp(dir=second_level_tmp_dir)[1]
141 self._upload_dir(storage, tmp_dir, tmp_filename, id='service_template_id')
143 second_update_file = tempfile.mkstemp(dir=self.path)[1]
144 with open(second_update_file, 'w') as f:
145 f.write('fake context2')
147 storage.service_template.upload(
148 entry_id='service_template_id',
149 source=second_update_file,
150 path=os.path.basename(second_level_tmp_dir))
152 assert os.path.isfile(os.path.join(
155 'service_template_id',
156 os.path.basename(second_level_tmp_dir),
157 os.path.basename(second_update_file)))
159 def test_download_dir(self):
160 storage = self._create_storage()
161 self._create(storage)
162 tmp_dir = tempfile.mkdtemp(suffix=self.__class__.__name__, dir=self.path)
163 second_level_tmp_dir = tempfile.mkdtemp(dir=tmp_dir)
164 tmp_filename = tempfile.mkstemp(dir=second_level_tmp_dir)[1]
165 self._upload_dir(storage, tmp_dir, tmp_filename, id='service_template_id')
167 temp_destination_dir = tempfile.mkdtemp(dir=self.path)
168 storage.service_template.download(
169 entry_id='service_template_id',
170 destination=temp_destination_dir)
172 destination_file_path = os.path.join(
173 temp_destination_dir,
174 os.path.basename(second_level_tmp_dir),
175 os.path.basename(tmp_filename))
177 assert os.path.isfile(destination_file_path)
179 with open(destination_file_path) as f:
180 assert f.read() == 'fake context'
182 def test_data_dir(self):
183 storage = self._create_storage()
184 self._create(storage)
186 tmp_dir = tempfile.mkdtemp(suffix=self.__class__.__name__, dir=self.path)
187 tempfile.mkstemp(dir=tmp_dir)
188 tempfile.mkstemp(dir=tmp_dir)
190 storage.service_template.upload(entry_id='service_template_id', source=tmp_dir)
192 with pytest.raises(exceptions.StorageError):
193 storage.service_template.read(entry_id='service_template_id', path='')
195 def test_delete_resource(self):
196 storage = self._create_storage()
197 self._create(storage)
198 tmpfile_path = tempfile.mkstemp(suffix=self.__class__.__name__, dir=self.path)[1]
199 self._upload(storage, tmpfile_path, 'service_template_id')
200 tmpfile2_path = tempfile.mkstemp(suffix=self.__class__.__name__, dir=self.path)[1]
201 self._upload(storage, tmpfile2_path, 'service_template_id')
203 # deleting the first resource and expecting an error on read
204 storage.service_template.delete(entry_id='service_template_id',
205 path=os.path.basename(tmpfile_path))
206 with pytest.raises(exceptions.StorageError):
207 storage.service_template.read(entry_id='service_template_id',
208 path=os.path.basename(tmpfile_path))
209 # the second resource should still be available for reading
210 assert storage.service_template.read(
211 entry_id='service_template_id',
212 path=os.path.basename(tmpfile2_path)) == 'fake context'
214 def test_delete_directory(self):
215 storage = self._create_storage()
216 self._create(storage)
217 temp_destination_dir = tempfile.mkdtemp(dir=self.path)
219 tmp_dir = tempfile.mkdtemp(suffix=self.__class__.__name__, dir=self.path)
220 second_level_tmp_dir = tempfile.mkdtemp(dir=tmp_dir)
221 tmp_filename = tempfile.mkstemp(dir=second_level_tmp_dir)[1]
222 self._upload_dir(storage, tmp_dir, tmp_filename, id='service_template_id')
223 file_path_in_dir = os.path.join(
224 os.path.basename(second_level_tmp_dir),
225 os.path.basename(tmp_filename))
227 # should be able to read the file and download the directory..
228 assert storage.service_template.read(
229 entry_id='service_template_id',
230 path=file_path_in_dir) == 'fake context'
231 storage.service_template.download(
232 entry_id='service_template_id',
233 path=os.path.basename(second_level_tmp_dir),
234 destination=temp_destination_dir)
236 # after deletion, the file and directory should both be gone
237 storage.service_template.delete(
238 entry_id='service_template_id',
239 path=os.path.basename(second_level_tmp_dir))
240 with pytest.raises(exceptions.StorageError):
241 assert storage.service_template.read(
242 entry_id='service_template_id',
243 path=file_path_in_dir) == 'fake context'
244 with pytest.raises(exceptions.StorageError):
245 storage.service_template.download(
246 entry_id='service_template_id',
247 path=os.path.basename(second_level_tmp_dir),
248 destination=temp_destination_dir)
250 def test_delete_all_resources(self):
251 storage = self._create_storage()
252 self._create(storage)
253 temp_destination_dir = tempfile.mkdtemp(dir=self.path)
255 tmp_dir = tempfile.mkdtemp(suffix=self.__class__.__name__, dir=self.path)
256 second_level_tmp_dir = tempfile.mkdtemp(dir=tmp_dir)
257 tmp_filename = tempfile.mkstemp(dir=second_level_tmp_dir)[1]
258 self._upload_dir(storage, tmp_dir, tmp_filename, id='service_template_id')
259 file_path_in_dir = os.path.join(
260 os.path.basename(second_level_tmp_dir),
261 os.path.basename(tmp_filename))
263 # deleting without specifying a path - delete all resources of this entry
264 storage.service_template.delete(entry_id='service_template_id')
265 with pytest.raises(exceptions.StorageError):
266 assert storage.service_template.read(
267 entry_id='service_template_id',
268 path=file_path_in_dir) == 'fake context'
269 with pytest.raises(exceptions.StorageError):
270 storage.service_template.download(
271 entry_id='service_template_id',
272 path=os.path.basename(second_level_tmp_dir),
273 destination=temp_destination_dir)
275 def test_delete_nonexisting_resource(self):
276 storage = self._create_storage()
277 self._create(storage)
278 # deleting a nonexisting resource - no effect is expected to happen
279 assert storage.service_template.delete(entry_id='service_template_id',
280 path='fake-file') is False