All the methods for a work with a files in a filesystem are tested.

packages
Иванов Денис 4 years ago
parent 1d2f3fad99
commit 3b08ce2deb

@ -159,6 +159,7 @@
"tests/templates/test_template_engine.py::TestTemplateEngine::test_if_value_of_variable_is_set_using_save_tag__the_new_value_of_variable_can_be_used_in_template": true,
"tests/templates/test_template_executor.py::TestTemplateAction": true,
"tests/templates/test_template_executor.py::TestTemplateAction::test_if_chown_value_is_not_correct__a_TemplateExecutor_object_throws_TemplateExecutorError_exception": true,
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_existing_directory_belongs_to_the_template_package__the_method_throws_TemplateCollisionError_error": true,
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_directory_method_s_input_is_a_path_to_an_unexisting_source_directory_and_a_target_path__the_method_does_nothing": true,
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_directory_method_s_input_is_a_path_to_an_unexisting_source_directory_and_a_target_path__the_method_raises_the_TemplateExecutorError_exception": true,
"tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_TemplateWrapper_object_contains_package_value__the_wrapper_can_use_Package_object_for_changing_CONTENTS_file_of_the_package": true,

@ -67,6 +67,7 @@
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_create_directory_method_input_is_a_template_with_a_target_path_to_an_unexisting_directory_from_an_unexisting_directory_and_chown_and_chmod_parameters_is_not_set__the_method_creates_new_directory_and_all_its_parents_with_a_mode_and_an_owner_of_its_existing_parent",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_create_directory_method_s_input_is_a_template_with_a_target_path_to_an_unexisting_directory_from_an_unexisting_directory_and_chown_and_chmod_parameters_is_set__the_method_creates_new_directory_and_all_its_parents_with_a_mode_and_an_owner_from_the_parameters",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_directory_method_s_input_is_a_path_to_an_existing_source_directory_and_a_target_path__the_method_creates_a_link_to_a_source_file",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_directory_method_s_input_is_a_path_to_an_existing_source_file_and_a_target_path__the_method_creates_a_link_to_a_source_file",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_directory_method_s_input_is_a_path_to_an_existing_source_directory_and_a_target_path_to_an_existing_link__the_method_raises_the_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_remove_directory_method_s_input_is_a_path_to_an_existing_empty_directory__the_method_will_remove_the_directory",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_remove_directory_method_s_input_is_a_path_to_an_existing_directory_that_contains_a_some_files__the_method_will_remove_the_directory",
@ -79,6 +80,21 @@
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_clear_directory_method_s_input_is_a_path_to_an_existing_directory_that_contains_some_directories__the_method_clears_the_target_directory",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_clear_directory_method_s_input_is_a_path_to_an_existing_directory_that_contains_some_directories_and_files__the_method_clears_the_target_directory",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_clear_directory_method_s_input_is_a_path_to_an_unexisting_directory__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_clear_directory_method_s_input_is_a_path_to_a_file__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_file_method_s_input_is_a_path_to_an_existing_source_file_and_a_target_path__the_method_creates_a_link_to_a_source_file",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_link_file_method_s_input_is_a_path_to_an_existing_source_file_and_a_target_path_to_an_existing_file__the_method_creates_a_link_to_a_source_file",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_remove_file_method_s_input_is_a_path_to_an_existing_file__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_remove_file_method_s_input_is_a_path_to_an_existing_link_to_an_existing_file__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_remove_file_method_s_input_is_a_path_to_an_unexisting_file__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_the_remove_file_method_s_input_is_a_path_to_an_existing_directory__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_clear_file_method_s_input_is_a_path_to_an_existing_empty_file__the_method_does_nothing",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_clear_file_method_s_input_is_a_path_to_an_existing_file_with_any_content__the_method_does_nothing",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_clear_file_method_s_input_is_a_path_to_an_unexisting_file__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_clear_file_method_s_input_is_a_path_to_an_existing_directory__the_method_raises_TemplateExecutorError_exception",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_unexisting_directory__the_method_creates_new_directory_and_adds_it_to_a_package_contents_file",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_existing_directory_that_does_not_belong_to_any_package__the_method_just_adds_new_directory_to_a_package_contents_file",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_existing_directory_belongs_to_the_template_package__the_method_does_nothing",
"tests/templates/test_template_executor.py::TestTemplateExecutor::test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_existing_directory_belongs_to_the_template_package__the_method_throws_TemplateCollisionError_error",
"tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_the_TemplateWrapper_object_has_already_been_created__it_contains_the_correct_list_of_protected_and_unprotected_paths",
"tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_template_type_is_FILE_and_target_file_does_not_exist__the_TemplateWrapper_target_type_is_None",
"tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_template_type_is_DIR_and_target_file_is_DIR__the_TemplateWrapper_target_type_is_DIR",

@ -691,7 +691,7 @@ class TemplateExecutor:
if template_object.target_type is None:
self._create_directory(template_object)
template_object.add_to_contents()
template_object.add_to_contents()
def _append_remove_directory(self, template_object: TemplateWrapper):
'''Метод описывающий действия для append = "remove", если шаблон --
@ -1068,7 +1068,7 @@ class TemplateExecutor:
else:
error_message = "target directory does not exist"
raise TemplateExecutorError(("failed to delete directory: {},"
raise TemplateExecutorError(("failed to clear directory: {},"
" reason: {}").format(target_path,
error_message))
@ -1077,43 +1077,67 @@ class TemplateExecutor:
расположенную на пути, указанному в source.'''
try:
os.symlink(source, target_path, target_is_directory=True)
except OSError:
raise TemplateExecutorError("Failed to create symlink: {0} -> {1}".
format(target_path, source))
except OSError as error:
raise TemplateExecutorError(
"failed to create symlink: {0} -> {1}, reason: {2}".
format(target_path, source, str(error)))
def _remove_file(self, target_path):
'''Метод для удаления файлов.'''
if os.path.islink(target_path):
try:
os.unlink(target_path)
except OSError:
raise TemplateExecutorError('failed to delete the link: {}'.
format(target_path))
if os.path.isfile(target_path):
try:
os.remove(target_path)
except OSError:
raise TemplateExecutorError('failed to delete the file: {}'.
format(target_path))
if os.path.exists(target_path):
if os.path.isfile(target_path):
if os.path.islink(target_path):
try:
os.unlink(target_path)
except OSError as error:
raise TemplateExecutorError(
'failed to delete the link: {}, reason: {}'.
format(target_path, str(error)))
if os.path.isfile(target_path):
try:
os.remove(target_path)
except OSError as error:
raise TemplateExecutorError(
'failed to delete the file: {}, reason: {}'.
format(target_path, str(error)))
return
else:
error_message = 'target is not a file'
else:
error_message = 'target file does not exist'
raise TemplateExecutorError(("failed to remove the file: {0},"
"reason: {1}").format(target_path,
error_message))
def _clear_file(self, target_path):
'''Метод для очистки файлов.'''
try:
with open(target_path, 'w') as f:
f.truncate(0)
except IOError:
raise TemplateExecutorError("failed to clear the file: {}".
format(target_path))
if os.path.exists(target_path):
if os.path.isfile(target_path):
try:
with open(target_path, 'w') as f:
f.truncate(0)
return
except IOError as error:
error_message = str(error)
else:
error_message = 'target is not a file'
else:
error_message = 'target file does not exist'
raise TemplateExecutorError(("failed to clear the file: {0},"
"reason: {1}").format(target_path,
error_message))
def _link_file(self, source, target_path):
'''Метод для создания по целевому пути ссылки на файл расположенный на
пути, указанному в source.'''
try:
os.symlink(source, target_path)
except OSError:
except OSError as error:
raise TemplateExecutorError(
"Failed to create symlink to the file: {0} -> {1}".
format(target_path, self.source))
"failed to create symlink to the file: {0} -> {1}, reason: {2}".
format(target_path, source, str(error)))
def _run_template(self, template_object: TemplateWrapper):
'''Метод для сохранения текста шаблонов, который должен быть исполнен

@ -7,7 +7,8 @@ import shutil
from calculate.templates.template_processor import TemplateExecutor, DIR,\
FILE, CalculateConfigFile,\
TemplateExecutorError,\
TemplateWrapper
TemplateWrapper,\
TemplateCollisionError
from calculate.templates.template_engine import ParametersContainer
from calculate.utils.package import PackageAtomName, Version
from calculate.utils.files import join_paths
@ -35,6 +36,12 @@ test_package_name = PackageAtomName(
'var/db/pkg/test-category/test-package-1.0'),
'version': Version('1.0')})
other_package_name = PackageAtomName(
{'pkg_path': os.path.join(
CHROOT_PATH,
'var/db/pkg/test-category/other-package-1.1'),
'version': Version('1.1')})
@pytest.mark.template_executor
class TestTemplateExecutor:
@ -646,6 +653,19 @@ class TestTemplateExecutor:
assert not os.path.exists(target_path)
def test_if_the_link_directory_method_s_input_is_a_path_to_an_existing_source_file_and_a_target_path__the_method_creates_a_link_to_a_source_file(self):
target_path = join_paths(CHROOT_PATH,
'/etc/link_dir_testfiles/link_dir_1')
source_path = join_paths(CHROOT_PATH, '/etc/link_dir_testfiles/file_1')
template_executor._link_directory(source_path, target_path)
assert os.path.exists(target_path)
assert os.path.islink(target_path)
os.unlink(target_path)
assert not os.path.exists(target_path)
def test_if_the_link_directory_method_s_input_is_a_path_to_an_existing_source_directory_and_a_target_path_to_an_existing_link__the_method_raises_the_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/link_dir_testfiles/link_dir_2')
@ -676,10 +696,12 @@ class TestTemplateExecutor:
'/etc/remove_dir_testfiles/dir_2')
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join'})
files_owner = template_executor._get_file_owner(
os.path.join(target_path, 'file_1'))
files_mode = template_executor._get_file_mode(
os.path.join(target_path, 'file_1'))
template_wrapper = TemplateWrapper(
target_path,
parameters_object, DIR,
@ -764,6 +786,7 @@ class TestTemplateExecutor:
'/etc/clear_dir_testfiles/dir_0')
template_executor._clear_directory(target_path)
assert not os.listdir(target_path)
def test_if_the_clear_directory_method_s_input_is_a_path_to_an_existing_directory_that_contains_some_files__the_method_clears_the_target_directory(self):
target_path = join_paths(CHROOT_PATH,
@ -853,9 +876,181 @@ class TestTemplateExecutor:
with pytest.raises(TemplateExecutorError):
template_executor._clear_directory(target_path)
def test_if_the_clear_directory_method_s_input_is_a_path_to_an_unexisting_directory__the_method_raises_TemplateExecutorError_exception(self):
def test_if_the_clear_directory_method_s_input_is_a_path_to_a_file__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/clear_dir_testfiles/dir_4')
'/etc/clear_dir_testfiles/file')
with pytest.raises(TemplateExecutorError):
template_executor._clear_directory(target_path)
def test_if_the_link_file_method_s_input_is_a_path_to_an_existing_source_file_and_a_target_path__the_method_creates_a_link_to_a_source_file(self):
target_path = join_paths(CHROOT_PATH,
'/etc/link_file_testfiles/link_file_0')
source_path = join_paths(CHROOT_PATH,
'/etc/link_file_testfiles/file_0')
template_executor._link_file(source_path, target_path)
assert os.path.exists(target_path)
assert os.path.islink(target_path)
os.unlink(target_path)
assert not os.path.exists(target_path)
def test_if_the_link_file_method_s_input_is_a_path_to_an_existing_source_file_and_a_target_path_to_an_existing_file__the_method_creates_a_link_to_a_source_file(self):
target_path = join_paths(CHROOT_PATH,
'/etc/link_file_testfiles/link_file_1')
source_path = join_paths(CHROOT_PATH,
'/etc/link_file_testfiles/file_1')
with pytest.raises(TemplateExecutorError):
template_executor._link_file(source_path, target_path)
def test_if_the_remove_file_method_s_input_is_a_path_to_an_existing_file__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/remove_file_testfiles/file_0')
files_owner = template_executor._get_file_owner(target_path)
files_mode = template_executor._get_file_mode(target_path)
template_executor._remove_file(target_path)
assert not os.path.exists(target_path)
open(target_path, 'w')
template_executor._chown_file(target_path, files_owner)
template_executor._chmod_file(target_path, files_mode)
assert os.path.exists(target_path)
def test_if_the_remove_file_method_s_input_is_a_path_to_an_existing_link_to_an_existing_file__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/remove_file_testfiles/link_file_1')
source_path = join_paths(CHROOT_PATH,
'/etc/remove_file_testfiles/file_1')
template_executor._remove_file(target_path)
assert not os.path.exists(target_path)
assert os.path.exists(source_path)
template_executor._link_file(source_path, target_path)
assert os.path.exists(target_path)
def test_if_the_remove_file_method_s_input_is_a_path_to_an_unexisting_file__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/remove_file_testfiles/file_2')
with pytest.raises(TemplateExecutorError):
template_executor._remove_file(target_path)
def test_if_the_remove_file_method_s_input_is_a_path_to_an_existing_directory__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/remove_file_testfiles/dir_0')
with pytest.raises(TemplateExecutorError):
template_executor._remove_file(target_path)
def test_if_clear_file_method_s_input_is_a_path_to_an_existing_empty_file__the_method_does_nothing(self):
target_path = join_paths(CHROOT_PATH,
'/etc/clear_file_testfiles/file_0')
template_executor._clear_file(target_path)
with open(target_path, 'r') as target_file:
text = target_file.read()
assert not text
def test_if_clear_file_method_s_input_is_a_path_to_an_existing_file_with_any_content__the_method_does_nothing(self):
target_path = join_paths(CHROOT_PATH,
'/etc/clear_file_testfiles/file_1')
template_executor._clear_file(target_path)
with open(target_path, 'r') as target_file:
text = target_file.read()
assert not text
with open(target_path, 'w') as target_file:
target_file.write('MATURE CONTENT')
def test_if_clear_file_method_s_input_is_a_path_to_an_unexisting_file__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/clear_file_testfiles/file_2')
with pytest.raises(TemplateExecutorError):
template_executor._clear_file(target_path)
def test_if_clear_file_method_s_input_is_a_path_to_an_existing_directory__the_method_raises_TemplateExecutorError_exception(self):
target_path = join_paths(CHROOT_PATH,
'/etc/clear_file_testfiles/dir_0')
with pytest.raises(TemplateExecutorError):
template_executor._clear_file(target_path)
# Тестируем поведение исполнительного модуля при различных значениях
# параметра append.
def test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_unexisting_directory__the_method_creates_new_directory_and_adds_it_to_a_package_contents_file(self):
target_path = join_paths(CHROOT_PATH,
'/etc/append_join_dir_testfiles/dir_0')
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join'})
template_wrapper = TemplateWrapper(
target_path,
parameters_object, DIR,
chroot_path=CHROOT_PATH,
config_archive_path=CONFIG_ARCHIVE_PATH)
try:
template_executor._append_join_directory(template_wrapper)
except Exception as error:
if os.path.exists(target_path):
shutil.rmtree(target_path)
template_wrapper.remove_from_contents()
pytest.fail('Exception: {}'.format(str(error)))
assert os.path.exists(target_path)
assert '/etc/append_join_dir_testfiles/dir_0'\
in template_wrapper.target_package
shutil.rmtree(target_path)
template_wrapper.remove_from_contents()
assert not os.path.exists(target_path)
assert '/etc/append_join_dir_testfiles/dir_0'\
not in template_wrapper.target_package
def test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_existing_directory_that_does_not_belong_to_any_package__the_method_just_adds_new_directory_to_a_package_contents_file(self):
target_path = join_paths(CHROOT_PATH,
'/etc/append_join_dir_testfiles/dir_1')
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join'})
template_wrapper = TemplateWrapper(
target_path,
parameters_object, DIR,
chroot_path=CHROOT_PATH,
config_archive_path=CONFIG_ARCHIVE_PATH)
try:
template_executor._append_join_directory(template_wrapper)
except Exception as error:
template_wrapper.remove_from_contents()
pytest.fail('Exception: {}'.format(str(error)))
assert os.path.exists(target_path)
assert '/etc/append_join_dir_testfiles/dir_1'\
in template_wrapper.target_package
template_wrapper.remove_from_contents()
assert '/etc/append_join_dir_testfiles/dir_1'\
not in template_wrapper.target_package
def test_if_append_join_directory_method_s_input_is_a_template_with_a_target_path_to_an_existing_directory_belongs_to_the_template_package__the_method_does_nothing(self):
target_path = join_paths(CHROOT_PATH,
'/etc/append_join_dir_testfiles/dir_2')
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join'})
template_wrapper = TemplateWrapper(
target_path,
parameters_object, DIR,
chroot_path=CHROOT_PATH,
config_archive_path=CONFIG_ARCHIVE_PATH)
template_executor._append_join_directory(template_wrapper)
assert os.path.exists(target_path)
assert '/etc/append_join_dir_testfiles/dir_2'\
in template_wrapper.target_package

@ -0,0 +1 @@
/home/divanov/Home/development/calculate-lib/tests/templates/testfiles/test_executor_root/etc/remove_file_testfiles/file_1

@ -8,3 +8,6 @@ dir /etc/dir_2
obj /etc/dir_2/file 84bcceb2c8e6de79849ea5f3304f2411 1591343236
dir /not_protected
obj /not_protected/file.conf 687594ba5f93d0673abce62d0bd649ec 1591352448
dir /etc/append_join_dir_testfiles
dir /etc/append_join_dir_testfiles/dir_3
obj /etc/append_join_dir_testfiles/dir_3/file d41d8cd98f00b204e9800998ecf8427e 1592319654

@ -4,3 +4,6 @@ dir /etc/config_testfiles/dir
obj /etc/config_testfiles/dir/file.conf 0b87fea7f5b65cac5012baa2bf647e72 1591196541
dir /etc/terminfo
obj /etc/terminfo/info.json 7a374d90a202b969ee338ac4334fb14b 1591354184
dir /etc/append_join_dir_testfiles
dir /etc/append_join_dir_testfiles/dir_2
obj /etc/append_join_dir_testfiles/dir_2/file d41d8cd98f00b204e9800998ecf8427e 1592318164

Loading…
Cancel
Save