|
|
|
@ -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
|
|
|
|
|