You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
387 lines
22 KiB
387 lines
22 KiB
import pytest
|
|
import shutil
|
|
import hashlib
|
|
import os
|
|
from calculate.templates.template_processor import DirectoryProcessor
|
|
from calculate.utils.package import PackageAtomName, Version, Package
|
|
from calculate.utils.files import join_paths
|
|
from calculate.utils.io_module import IOModule
|
|
from calculate.templates.template_engine import Variables
|
|
|
|
|
|
CHROOT_PATH = os.path.join(os.getcwd(),
|
|
'tests/templates/testfiles/test_dir_processor_root')
|
|
CONFIG_PATH = os.path.join(CHROOT_PATH, 'var/lib/calculate/config')
|
|
CONFIG_ARCHIVE_PATH = os.path.join(CHROOT_PATH,
|
|
'var/lib/calculate/config-archive')
|
|
EXECUTE_ARCHIVE_PATH = os.path.join(CHROOT_PATH,
|
|
'var/lib/calculate/.execute')
|
|
|
|
test_package_name = PackageAtomName(
|
|
{'pkg_path': os.path.join(
|
|
CHROOT_PATH,
|
|
'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')})
|
|
|
|
new_package_name = PackageAtomName(
|
|
{'pkg_path': os.path.join(
|
|
CHROOT_PATH,
|
|
'var/db/pkg/test-category/new-package-0.1.1'),
|
|
'version': Version('1.0')})
|
|
|
|
|
|
# Вместо модуля переменных.
|
|
group = Variables({'bool': True,
|
|
'list': [1, 2, 3]})
|
|
|
|
variables = Variables({'variable_1': 'value_1',
|
|
'variable_2': 'value_2',
|
|
'group': group})
|
|
|
|
install = Variables({'os_disk_dev': 'os_disk_dev_value',
|
|
'version': 1.5,
|
|
'number': 128,
|
|
'boolean': False,
|
|
'type': 'static',
|
|
'path': '/usr/sbin'})
|
|
|
|
merge = Variables({'var_1': 674,
|
|
'var_2': 48,
|
|
'version': 1.0,
|
|
'calculate_domains': 'lists.calculate-linux.org',
|
|
'ip_value': '127.0.0.0/8'})
|
|
|
|
main = Variables({'cl_template_path':
|
|
'{0},{1}'.format(os.path.join(CHROOT_PATH,
|
|
'templates'),
|
|
os.path.join(CHROOT_PATH,
|
|
'var/calculate/templates')),
|
|
'cl_chroot_path': CHROOT_PATH,
|
|
'cl_config_path': CONFIG_PATH,
|
|
'cl_config_archive': CONFIG_ARCHIVE_PATH,
|
|
'cl_exec_dir_path': EXECUTE_ARCHIVE_PATH,
|
|
'cl_ignore_files': '*.swp'})
|
|
|
|
test = ({'test_root': CHROOT_PATH})
|
|
|
|
datavars = Variables({'install': install,
|
|
'merge': merge,
|
|
'variables': variables,
|
|
'main': main,
|
|
'test': test,
|
|
'custom': Variables()})
|
|
|
|
|
|
def show_tree(dir_path, indent=0):
|
|
print('{}{}/'.format(' ' * 4 * indent, os.path.basename(dir_path)))
|
|
indent += 1
|
|
for node in os.scandir(dir_path):
|
|
if node.is_dir():
|
|
show_tree(node.path, indent=indent)
|
|
else:
|
|
print('{}{}'.format(' ' * 4 * indent, node.name))
|
|
|
|
|
|
@pytest.mark.directory_processor
|
|
class TestDirectoryProcessor:
|
|
def test_create_testfiles(self):
|
|
shutil.copytree(os.path.join(CHROOT_PATH, 'etc.backup'),
|
|
os.path.join(CHROOT_PATH, 'etc'),
|
|
symlinks=True)
|
|
shutil.copytree(os.path.join(CHROOT_PATH, 'var.backup'),
|
|
os.path.join(CHROOT_PATH, 'var'),
|
|
symlinks=True)
|
|
|
|
def test_if_templates_consist_only_one_directory_with_a_single_calculate_directory_file_with_the_append_skip_parameter__the_directory_processor_does_nothing(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_0')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_and_a_single_template_file__the_directory_processor_creates_new_file_and_adds_one_in_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_1')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_0'))
|
|
test_package = Package(test_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/file_0' in test_package
|
|
with open(join_paths(CHROOT_PATH, '/etc/file_0'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_and_a_single_other_directory_with_same_a_file__the_directory_processor_creates_new_directory(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_2')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_1'))
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_and_a_single_other_directory_without_calculate_directory_file__the_directory_processor_creates_new_directory(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_4')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_3'))
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_with_a_single_directory_with_a_single_template_file__the_directory_processor_creates_new_directory_and_file_and_adds_one_in_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_3')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_2'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_2/file_0'))
|
|
test_package = Package(test_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/dir_2' in test_package
|
|
assert '/etc/dir_2/file_0' in test_package
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/dir_2/file_0'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_and_with_two_directories_with_a_template_files__the_directory_processor_creates_all_new_directories_and_files_and_adds_them_in_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_5')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_4'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_4/file_0'))
|
|
test_package = Package(test_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/dir_4' in test_package
|
|
assert '/etc/dir_4/file_0' in test_package
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/dir_4/file_0'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
|
|
# Для подробностей см. шаблон.
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_5'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_5/dir_6'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH,
|
|
'/etc/dir_5/dir_6/file_0'))
|
|
test_package = Package(test_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/dir_5' in test_package
|
|
assert '/etc/dir_5/dir_6' in test_package
|
|
assert '/etc/dir_5/dir_6/file_0' in test_package
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/dir_5/dir_6/file_0'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_and_a_single_template_file_and_there_is_a_file_without_user_changes_on_its_target_path__the_directory_processor_joins_a_template_file_with_a_target_file_and_updates_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_6')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-0 yes;\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_1'))
|
|
test_package = Package(new_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/file_1' in test_package
|
|
assert test_package.is_md5_equal('/etc/file_1',
|
|
hashlib.md5(
|
|
output_text.encode()).hexdigest())
|
|
with open(join_paths(CHROOT_PATH, '/etc/file_1'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_with_a_single_directory_with_a_single_template_file_and_there_is_a_file_without_user_changes_on_its_target_path__the_directory_processor_joins_a_template_file_with_a_target_file_and_updates_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_7')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-0 yes;\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_6'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_6/file_0'))
|
|
test_package = Package(new_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/dir_6' in test_package
|
|
assert '/etc/dir_6/file_0' in test_package
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/dir_6/file_0'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
assert test_package.is_md5_equal('/etc/dir_6/file_0',
|
|
hashlib.md5(
|
|
output_text.encode()).hexdigest())
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_and_a_single_template_file_and_there_is_a_file_with_user_changes_on_its_target_path__the_directory_processor_joins_a_template_file_with_a_target_file_and_updates_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_8')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-0 yes;\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_2'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/._cfg0001_file_2'))
|
|
|
|
test_package = Package(new_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/file_2' in test_package
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/._cfg0001_file_2'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
assert test_package.is_md5_equal('/etc/._cfg0001_file_2',
|
|
hashlib.md5(
|
|
output_text.encode()).hexdigest())
|
|
|
|
def if_templates_consist_only_one_directory_with_a_calculate_directory_file_one_template_directory_and_a_single_template_file_with_a_target_path_to_a_file_removed_by_user_in_the_last_one__the_directory_processor_creates_a_new_empty_cfg_file__joins_template_with_it_and_updates_the_CONTENTS_file(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_9')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text = ("options {\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert not os.path.exists(join_paths(CHROOT_PATH, '/etc/file_3'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/._cfg0001_file_3'))
|
|
|
|
test_package = Package(new_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/file_3' in test_package
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/._cfg0001_file_3'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text
|
|
assert test_package.is_md5_equal('/etc/._cfg0001_file_3',
|
|
hashlib.md5(
|
|
output_text.encode()).hexdigest())
|
|
|
|
def if_templates_are_hierarchy_of_a_multiple_template_files_with_a_removed_or_changed_by_user_targets_and_there_is_the_autoupdate_parameter_in_the_root_calculate_directory_template__the_directory_processor_uses_autoupdate_parameter_for_all_templates_and_joins_all_templates_as_if_target_files_have_no_user_changes(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_10')
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars)
|
|
directory_processor.process_template_directories()
|
|
output_text_1 = ("options {\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
output_text_2 = ("options {\n parameter-0 no;\n parameter-1 "
|
|
+ datavars.variables.variable_1
|
|
+ ";\n parameter-2 "
|
|
+ datavars.variables.variable_2 + ";\n};\n")
|
|
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_4'))
|
|
assert not os.path.exists(join_paths(CHROOT_PATH,
|
|
'/etc/._cfg0001_file_4'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_5'))
|
|
assert not os.path.exists(join_paths(CHROOT_PATH,
|
|
'/etc/._cfg0001_file_5'))
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_7/file_0'))
|
|
assert not os.path.exists(join_paths(CHROOT_PATH,
|
|
'/etc/dir_7/._cfg0001_file_0'))
|
|
|
|
test_package = Package(new_package_name, chroot_path=CHROOT_PATH)
|
|
assert '/etc/file_4' in test_package
|
|
assert '/etc/file_5' in test_package
|
|
assert '/etc/dir_7/file_0' in test_package
|
|
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/file_4'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text_2
|
|
assert test_package.is_md5_equal('/etc/file_4',
|
|
hashlib.md5(
|
|
output_text_2.encode()).hexdigest())
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/file_5'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text_1
|
|
assert test_package.is_md5_equal('/etc/file_5',
|
|
hashlib.md5(
|
|
output_text_1.encode()).hexdigest())
|
|
with open(join_paths(CHROOT_PATH,
|
|
'/etc/dir_7/file_0'), 'r') as output_file:
|
|
output_file_text = output_file.read()
|
|
assert output_file_text == output_text_2
|
|
assert test_package.is_md5_equal('/etc/dir_7/file_0',
|
|
hashlib.md5(
|
|
output_text_2.encode()).hexdigest())
|
|
|
|
def if_the_template_directory_have_no_the_action_parameter_value_and_append_parameter_is_not_skip__the_directory_processor_skips_this_template_branch_and_sets_warning(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_11')
|
|
io_module = IOModule()
|
|
warning_message = ("Action parameter is not set for template:"
|
|
" {0}").format(join_paths(
|
|
CHROOT_PATH,
|
|
'/templates_11/root/dir_8'))
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars,
|
|
output_module=io_module)
|
|
directory_processor.process_template_directories()
|
|
assert not os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_8'))
|
|
assert io_module.messages[-1] == ('warning', warning_message)
|
|
|
|
def if_the_template_has_two_root_directories_with_different_action_values_and_directory_processor_intialized_for_the_one_of_this_actions__the_directory_processor_skips_one_this_template_s_roots_and_processed_a_template_s_root_with_the_same_action_parameter_value(self):
|
|
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
|
|
'templates_12')
|
|
io_module = IOModule()
|
|
warning_message = ("Action parameter value '{0}' does not match its"
|
|
" current value '{1}'.\nTemplate: {2}").format(
|
|
'update',
|
|
'install',
|
|
join_paths(
|
|
CHROOT_PATH,
|
|
'/templates_12/root_1'))
|
|
directory_processor = DirectoryProcessor('install',
|
|
datavars_module=datavars,
|
|
output_module=io_module)
|
|
directory_processor.process_template_directories()
|
|
assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_6'))
|
|
assert not os.path.exists(join_paths(CHROOT_PATH, '/etc/file_7'))
|
|
assert io_module.messages[-1] == ('warning', warning_message)
|
|
|
|
def test_view_tree(self):
|
|
list_path = join_paths(CHROOT_PATH, '/etc')
|
|
show_tree(list_path)
|
|
assert False
|
|
|
|
def test_for_removing_testfile(self):
|
|
shutil.rmtree(os.path.join(CHROOT_PATH, 'etc'))
|
|
shutil.rmtree(os.path.join(CHROOT_PATH, 'var'))
|