blob: f4ecea1afa6bc866f1fdda88a3f5835af6e59a12 [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for file utility functions."""
import cloudstorage
from melange.utils import files as files_utils
from tests import test_utils
TEST_FILE_PATH = 'tests/images/melange_logo.png'
TEST_OBJECT_NAME = 'test-object'
TEST_BUCKET_NAME = 'test-bucket'
def _getTestObjectPath():
"""Returns a test path for an object in Cloud Storage."""
return '/' + TEST_BUCKET_NAME + '/' + TEST_OBJECT_NAME
class SaveFileTest(test_utils.SoCTestCase):
"""Unit tests for saveFile function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testFileSaved(self):
"""Tests that the file is saved correctly."""
with open(TEST_FILE_PATH, 'r') as test_file:
object_path = files_utils.saveFile(
test_file, TEST_OBJECT_NAME, bucket_name=TEST_BUCKET_NAME)
test_file.seek(0)
expected_size = len(test_file.read())
# check that the returned path is correct
self.assertEqual(_getTestObjectPath(), object_path)
# check that the file is saved correctly
with cloudstorage.open(object_path) as file_object:
self.assertEqual(expected_size, len(file_object.read()))
class DeleteFileTest(test_utils.SoCTestCase):
"""Unit tests for deleteFile function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testFileDeleted(self):
"""Tests that a file is deleted correctly."""
with cloudstorage.open(_getTestObjectPath(), 'w') as gcs_file:
gcs_file.write('unused-file-content')
files_utils.deleteFile(_getTestObjectPath())
# check that the field is not found, because it is deleted
with self.assertRaises(cloudstorage.NotFoundError):
cloudstorage.open(_getTestObjectPath(), 'r')