diff --git a/.pytest_cache/v/cache/nodeids b/.pytest_cache/v/cache/nodeids index 5d1533e..cbfa503 100644 --- a/.pytest_cache/v/cache/nodeids +++ b/.pytest_cache/v/cache/nodeids @@ -68,6 +68,30 @@ "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_template_does_not_have_package_parameter_but_target_file_belongs_to_the_package__the_TemplateWrapper_uses_package_from_parameter", "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_template_contains_package_parameter_and_target_file_belongs_to_the_package_from_package_parameter__the_TemplateWrapper_uses_package_from_them", "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_template_contains_package_parameter_but_target_file_belongs_to_the_other_package__the_TemplateWrapper_throws_TemplateCollisionError", + "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", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_directory_contains_no_cfg_files_but_it_is_necessary_to_create_one__the_first_cfg_file_should_be_named_cfg0001_file", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_directory_contains_some_cfg_files_including_the_first_one__the_next_cfg_file_should_be_named_with_the_number_following_the_number_of_the_last_one", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_directory_contains_some_cfg_files_but_some_of_them_are_missed__the_next_cfg_file_should_be_named_with_the_number_following_the_number_of_the_last_one", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_get_archive_path_method_has_the_file_path_as_its_argument__it_returns_the_path_to_the_archive_version_of_the_input_file", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_is_located_in_a_protected_directory__the_TemplateWrapper_object_sets_the_protected_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_is_located_in_a_directory_that_is_not_protected__the_TemplateWrapper_object_sets_the_protected_flag_as_False", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_is_located_in_a_directory_from_the_protected_mask__the_TemplateWrapper_object_sets_the_protected_flag_as_False", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_is_not_protected__the_TemplateWrapper_sets_the_md5_matching_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_template_contains_the_unbound_parameter__the_TemplateWrapper_sets_the_md5_matching_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_does_not_exist_and_CONTENTS_file_of_the_file_s_package_does_not_contain_this_file__the_TemplateWrapper_sets_the_md5_matching_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_was_deleted_by_a_user__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_False", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_does_not_belong_to_any_package__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_False", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_template_contains_the_autoupdate_parameter__the_TemplateWrapper_sets_the_md5_matching_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_template_contains_the_autoupdate_parameter_and_a_target_file_does_not_belong_to_any_package__the_TemplateWrapper_sets_the_md5_matching_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_s_hash_matches_the_hash_from_the_package_s_CONTENTS_file__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_True", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_file_s_hash_does_not_match_the_hash_from_the_package_s_CONTENTS_file__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_False", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_after_the_target_file_check_md5_matching_flag_is_set_as_True_and_a_template_does_not_contain_source_parameter__the_output_and_the_input_paths_for_the_TemplateExecutor_is_the_same_and_it_is_target_path", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_after_the_target_file_check_md5_matching_flag_is_set_as_False_and_a_template_does_not_contain_source_parameter__the_output_path_is_the_cfg_file_path_and_the_input_path_is_the_calculate_archive_path", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_after_the_target_file_check_md5_matching_flag_is_set_as_True_and_a_template_contains_source_parameter__the_output_and_the_input_paths_for_the_TemplateExecutor_is_the_same_and_it_is_target_path", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_after_the_target_file_check_md5_matching_flag_is_set_as_False_and_a_template_contains_source_parameter__the_output_path_is_the_cfg_file_path_and_the_input_path_is_the_calculate_archive_path", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_path_is_FILE_and_a_template_is_executable__the_target_path_is_replaced_with_the_path_to_the_cwd_directory_and_there_is_no_package_type_conflicts_and_other_checks", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_target_path_is_DIR_and_a_template_is_executable__the_target_path_is_the_same_and_there_is_no_package_type_conflicts_and_other_checks", + "tests/templates/test_template_wrapper.py::TestTemplateWrapper::test_if_a_template_is_executable_and_contains_package_parameter__the_template_updates_the_CONTENTS_file_for_package_from_parameter", "tests/templates/format/test_base.py::TestJoinMethod::test_if_inputs_are_dictionaries_with_string_keys_without_any_action_marks__the_dictionaties_just_merged", "tests/templates/format/test_base.py::TestJoinMethod::test_if_inputs_are_dictionaries_with_tuple_keys_without_any_action_marks_as_their_keys__the_dictionaries_just_merged", "tests/templates/format/test_base.py::TestJoinMethod::test_if_inputs_are_dictionaries_with_same_sections_which_contain_different_parameters__a_section_from_the_template_added_to_the_same_section_of_original_dictionary", diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index 736af1c..6624da3 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -176,6 +176,9 @@ class TemplateWrapper: # Флаг, указывающий, что файл по целевому пути является ссылкой. self.target_is_link = False + # Пакет, к которому относится файл. + self.target_package = None + # Флаг, указывающий, что файл является PROTECTED. self.protected = False @@ -198,15 +201,12 @@ class TemplateWrapper: # Здесь будет детектор форматов. pass - print('is exist = {}'.format(os.path.exists(target_file_path))) # Если по этому пути что-то есть -- проверяем конфликты. if os.path.exists(target_file_path): - print("target type is ") for file_type, checker in self.type_checks.items(): if checker(target_file_path): self.target_type = file_type break - print("{} = {}".format(file_type, checker(target_file_path))) self.target_is_link = os.path.islink(target_file_path) # Если установлен параметр mirror и есть параметр source, # содержащий несуществующий путь -- удаляем целевой файл. @@ -219,14 +219,21 @@ class TemplateWrapper: self.target_type = None if self.format_class is not None and self.format_class.EXECUTABLE: - # Если формат исполняемый, но проверяем, существует ли директория, + # Если формат исполняемый -- проверяем, существует ли директория, # из которой будет выполняться шаблон. if not os.path.exists(self.target_path): - if self.template_type == DIR: - os.makedirs(self.target_path) - else: - self.target_path = os.path.dirname(self.target_path) - os.makedirs(self.target_path) + # Если не существует -- создаем ее. + os.makedirs(self.target_path) + elif os.path.isfile(self.target_path): + # Если вместо директории файл -- определяем по файлу + # директорию. + self.target_path = os.path.dirname(self.target_path) + + # Если есть параметр package, определяем по нему пакет. + if self.parameters.package: + self.target_package_name = self.parameters.package + self.target_package = Package(self.parameters.package, + chroot_path=self.chroot_path) return self.check_conflicts() @@ -247,13 +254,6 @@ class TemplateWrapper: def check_conflicts(self): '''Проверка конфликтов типов.''' - print('check conflicts') - print('template type = {}'.format('FILE' if self.template_type == FILE - else 'DIR')) - print('target type = {}'.format('FILE' if self.target_type == FILE - else 'DIR' if self.target_type == DIR - else 'None')) - print('target is link = {}'.format(self.target_is_link)) if self.parameters.append == 'link': if self.parameters.force or self.target_is_link: self.remove_original = True @@ -325,7 +325,6 @@ class TemplateWrapper: try: file_package = self.package_atom_parser.get_file_package( self.target_path) - print('file package = {}'.format(file_package)) except PackageNotFound: file_package = None self.target_without_package = True @@ -404,11 +403,7 @@ class TemplateWrapper: elif self.target_without_package: # Если файл по целевому пути не относится к какому-либо пакету. - if self.parameters.unbound: - self.md5_matching = True - else: - self.md5_matching = False - + self.md5_matching = False else: # Если файл есть и он относится к текущему пакету. target_md5 = self.target_package.get_md5(self.target_path) @@ -423,7 +418,7 @@ class TemplateWrapper: self.md5_matching = self.md5_matching or self.parameters.autoupdate - # Определяем путей входных и выходных файлов. + # Определяем пути входных и выходных файлов. if self.md5_matching: # Приоритет отдаем пути из параметра source. if self.parameters.source: @@ -449,6 +444,7 @@ class TemplateWrapper: def _get_cfg_path(self, file_path): '''Метод для получения пути для создания нового ._cfg????_ файла.''' + print(self.cfg_list) if self.cfg_list: last_cfg_name = os.path.basename(self.cfg_list[-1]) @@ -737,8 +733,8 @@ class TemplateExecutor: template_format = template_object.format_class # Задаемся значениями chmod и chown в зависимости от наличия или - # отсутствия файла, принадлежности его пакету и наличию дефолтных - # значений параметров. + # отсутствия файла, принадлежности его пакету и наличию значений + # параметров по умолчанию. try: chmod = template_object.parameters.chmod if not chmod: @@ -746,8 +742,7 @@ class TemplateExecutor: not template_object.target_without_package): chmod = self._get_file_mode(template_object.target_path) else: - chmod = self.file_default_parameters.get('chmod', - False) + chmod = self.file_default_parameters.get('chmod', False) chown = template_object.parameters.chown if not chown: @@ -755,8 +750,7 @@ class TemplateExecutor: not template_object.target_without_package): chown = self._get_file_owner(template_object.target_path) else: - chown = self.file_default_parameters.get('chown', - False) + chown = self.file_default_parameters.get('chown', False) except OSError: raise TemplateExecutorError('No access to the directory: {}'. format(template_object.target_path)) @@ -764,10 +758,12 @@ class TemplateExecutor: if template_object.format_class.EXECUTABLE or\ template_object.md5_matching: # Действия при совпадении md5 из CONTENTS и md5 целевого файла. + # А также если шаблон просто исполнительный. output_paths = [output_path] # Если целевой файл защищен, а шаблон не userspace. if template_object.protected and not template_object.is_userspace: + # Тогда также обновляем архив. output_paths.append(template_object.archive_path) if template_object.target_type is not None and not replace: @@ -792,6 +788,10 @@ class TemplateExecutor: output_text_md5 = hashlib.md5(output_text.encode()).hexdigest() for save_path in output_paths: + if not os.path.exists(os.path.dirname(save_path)): + self._create_directory( + template_object, + path_to_create=os.path.dirname(save_path)) with open(save_path, 'w') as output_file: output_file.write(output_text) @@ -828,8 +828,9 @@ class TemplateExecutor: # Удаляем форматный объект входного файла. del(parsed_template) # Если исполняемый формат выдал список измененных файлов для - # изменения CONTENTS -- обновляем CONTENTS. - if changed_files: + # изменения CONTENTS и при этом задан пакет -- обновляем + # CONTENTS. + if changed_files and template_object.target_package: template_object.update_contents_from_list(changed_files) else: if template_object.target_type is not None and not replace: @@ -853,6 +854,10 @@ class TemplateExecutor: if not self.calculate_config_file.compare_md5( template_object.target_path, output_text_md5): + if not os.path.exists(os.path.dirname(output_path)): + self._create_directory( + template_object, + path_to_create=os.path.dirname(output_path)) with open(output_path, 'w') as output_file: output_file.write(output_text) @@ -944,10 +949,15 @@ class TemplateExecutor: template_object.add_to_contents() - def _create_directory(self, template_object: TemplateWrapper): + def _create_directory(self, template_object: TemplateWrapper, + path_to_create=None): '''Метод для создания директории и, при необходимости, изменения владельца и доступа все директорий на пути к целевой.''' - target_path = template_object.target_path + if path_to_create is None: + target_path = template_object.target_path + else: + target_path = path_to_create + template_parameters = template_object.parameters # Если файл есть, но указан chmod или chown -- используем их. diff --git a/calculate/utils/package.py b/calculate/utils/package.py index cd2926a..5efdc32 100644 --- a/calculate/utils/package.py +++ b/calculate/utils/package.py @@ -566,7 +566,6 @@ class Package: def __init__(self, package_atom, pkg_path='/var/db/pkg', chroot_path='/'): self.chroot_path = chroot_path - print('chroot_path = {}'.format(self.chroot_path)) self.contents_file_path = self._get_contents_path(package_atom) if (chroot_path != '/' and diff --git a/template_action_draft.py b/template_action_draft.py index 573680b..495e70a 100644 --- a/template_action_draft.py +++ b/template_action_draft.py @@ -46,6 +46,31 @@ options { } ''' +another_template_text = '''{% calculate append = 'join', format = 'bind', +autoupdate, package = 'test-category/other-package' -%} +options { + parameter no; + parameter_1 yes; +}; +awful_section { + parameter_2 12; + inner_section { + parameter_4 "grief"; + } +} +''' + +one_more_template_text = '''{% calculate append = 'join', format = 'json', +autoupdate, package = 'test-category/test-package' -%} +{ + "Band": "Summoning", + "Song": "Rotting Horse on the Deadly Ground", + "Genre": "Black metal", + "Status": "Unspeakably good song", + "isRecommended": true +} +''' + class TemplateExecutorError(Exception): pass @@ -769,6 +794,10 @@ class TemplateExecutor: output_text_md5 = hashlib.md5(output_text.encode()).hexdigest() for save_path in output_paths: + if not os.path.exists(os.path.dirname(save_path)): + self._create_directory( + template_object, + path_to_create=os.path.dirname(save_path)) with open(save_path, 'w') as output_file: output_file.write(output_text) @@ -829,6 +858,10 @@ class TemplateExecutor: if not self.calculate_config_file.compare_md5(target_path, output_text_md5): + if not os.path.exists(os.path.dirname(output_path)): + self._create_directory( + template_object, + path_to_create=os.path.dirname(output_path)) with open(output_path, 'w') as output_file: output_file.write(output_text) @@ -838,7 +871,7 @@ class TemplateExecutor: self._chown_file(output_path, chown, check_existation=False) if chmod: - self._chmod_file(output_file, chmod, + self._chmod_file(output_path, chmod, check_existation=False) # Обновляем CL. @@ -920,10 +953,15 @@ class TemplateExecutor: template_object.add_to_contents() - def _create_directory(self, template_object: TemplateWrapper): + def _create_directory(self, template_object: TemplateWrapper, + path_to_create=None): '''Метод для создания директории и, при необходимости, изменения владельца и доступа все директорий на пути к целевой.''' - target_path = template_object.target_path + if path_to_create is None: + target_path = template_object.target_path + else: + target_path = path_to_create + template_parameters = template_object.parameters # Если файл есть, но указан chmod или chown -- используем их. @@ -1266,18 +1304,20 @@ template_engine = TemplateEngine(datavars_module=DATAVARS_MODULE, target_path = os.path.join(CHROOT_PATH, 'etc/dir/file.conf') other_target_path = os.path.join(CHROOT_PATH, 'etc/other_file.conf') +another_target_path = os.path.join(CHROOT_PATH, 'not_protected/file.conf') +one_more_target_path = os.path.join(CHROOT_PATH, 'etc/dir/deleted.json') run_target_path = os.path.join(CHROOT_PATH, 'file_to_run.py') # Применение основного шаблона: # template_engine.process_template_from_string(template_text, FILE) # template_parameters = template_engine.parameters # template_text = template_engine.template_text -# + # template_executor_obj.execute_template(target_path, # template_parameters, # FILE, template_text=template_text) # template_executor_obj.save_changes() -# + # input() # template_engine.process_template_from_string(template_to_run, FILE) # template_parameters = template_engine.parameters @@ -1286,7 +1326,7 @@ run_target_path = os.path.join(CHROOT_PATH, 'file_to_run.py') # template_parameters, # FILE, template_text=template_text) # template_executor_obj.save_changes() -# + # input() # template_engine.process_template_from_string(backup_template_text, FILE) # template_parameters = template_engine.parameters @@ -1296,10 +1336,18 @@ run_target_path = os.path.join(CHROOT_PATH, 'file_to_run.py') # FILE, template_text=template_text) # template_executor_obj.save_changes() -template_engine.process_template_from_string(other_template_text, FILE) +# template_engine.process_template_from_string(other_template_text, FILE) +# template_parameters = template_engine.parameters +# template_text = template_engine.template_text +# template_executor_obj.execute_template(other_target_path, +# template_parameters, +# FILE, template_text=template_text) +# template_executor_obj.save_changes() + +template_engine.process_template_from_string(one_more_template_text, FILE) template_parameters = template_engine.parameters template_text = template_engine.template_text -template_executor_obj.execute_template(other_target_path, +template_executor_obj.execute_template(one_more_target_path, template_parameters, FILE, template_text=template_text) template_executor_obj.save_changes() diff --git a/tests/templates/test_template_wrapper.py b/tests/templates/test_template_wrapper.py index c9e5e41..0ea62de 100644 --- a/tests/templates/test_template_wrapper.py +++ b/tests/templates/test_template_wrapper.py @@ -452,7 +452,8 @@ class TestTemplateWrapper: # Тестируем определитель пакетов и проверку на коллизии. def test_if_template_contains_append_parameters_but_does_not_have_package_parameter_and_target_file_does_not_belong_to_any_package__the_TemplateWrapper_throws_TemplateCollisionError_exception(self): - parameters_object = ParametersContainer({'append': 'join'}) + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) with pytest.raises(TemplateCollisionError): template_wrapper = TemplateWrapper( join_paths(CHROOT_PATH, @@ -463,7 +464,8 @@ class TestTemplateWrapper: def test_if_template_contains_package_parameter_and_target_file_does_not_belong_to_any_package__the_TemplateWrapper_uses_package_from_parameter_and_sets_target_without_package_flag(self): parameters_object = ParametersContainer({'package': test_package_name, - 'append': 'join'}) + 'append': 'join', + 'format': 'samba'}) try: template_wrapper = TemplateWrapper( join_paths(CHROOT_PATH, @@ -477,7 +479,8 @@ class TestTemplateWrapper: assert template_wrapper.target_without_package def test_if_template_does_not_have_package_parameter_but_target_file_belongs_to_the_package__the_TemplateWrapper_uses_package_from_parameter(self): - parameters_object = ParametersContainer({'append': 'join'}) + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) try: template_wrapper = TemplateWrapper( join_paths(CHROOT_PATH, @@ -493,7 +496,8 @@ class TestTemplateWrapper: def test_if_template_contains_package_parameter_and_target_file_belongs_to_the_package_from_package_parameter__the_TemplateWrapper_uses_package_from_them(self): parameters_object = ParametersContainer({'package': test_package_name, - 'append': 'join'}) + 'append': 'join', + 'format': 'samba'}) try: template_wrapper = TemplateWrapper( join_paths(CHROOT_PATH, @@ -508,7 +512,8 @@ class TestTemplateWrapper: def test_if_template_contains_package_parameter_but_target_file_belongs_to_the_other_package__the_TemplateWrapper_throws_TemplateCollisionError(self): parameters_object = ParametersContainer({'package': test_package_name, - 'append': 'join'}) + 'append': 'join', + 'format': 'samba'}) with pytest.raises(TemplateCollisionError): template_wrapper = TemplateWrapper( join_paths(CHROOT_PATH, @@ -517,7 +522,393 @@ class TestTemplateWrapper: chroot_path=CHROOT_PATH, config_archive_path=CONFIG_ARCHIVE_PATH) - def test_contents_init(self): - pass + def test_if_TemplateWrapper_object_contains_package_value__the_wrapper_can_use_Package_object_for_changing_CONTENTS_file_of_the_package(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + + assert '/etc/dir/file.conf' in template_wrapper.target_package # Тестируем проверку наличия пользовательских изменений. + # Сначала вспомогательные функции. + def test_if_a_target_file_directory_contains_no_cfg_files_but_it_is_necessary_to_create_one__the_first_cfg_file_should_be_named_cfg0001_file(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'bind'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_0/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + + cfg_name = template_wrapper._get_cfg_path(template_wrapper.target_path) + assert cfg_name == join_paths(CHROOT_PATH, '/etc/dir_0/._cfg0001_file') + + def test_if_a_target_file_directory_contains_some_cfg_files_including_the_first_one__the_next_cfg_file_should_be_named_with_the_number_following_the_number_of_the_last_one(self): + parameters_object = ParametersContainer({'append': 'bind'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_1/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + + cfg_name = template_wrapper._get_cfg_path(join_paths( + CHROOT_PATH, + '/etc/dir_1/file')) + assert cfg_name == join_paths(CHROOT_PATH, '/etc/dir_1/._cfg0003_file') + + def test_if_a_target_file_directory_contains_some_cfg_files_but_some_of_them_are_missed__the_next_cfg_file_should_be_named_with_the_number_following_the_number_of_the_last_one(self): + parameters_object = ParametersContainer({'append': 'bind'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_2/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + + cfg_name = template_wrapper._get_cfg_path(join_paths( + CHROOT_PATH, + '/etc/dir_2/file')) + assert cfg_name == join_paths(CHROOT_PATH, '/etc/dir_2/._cfg0003_file') + + def test_if_get_archive_path_method_has_the_file_path_as_its_argument__it_returns_the_path_to_the_archive_version_of_the_input_file(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + + assert template_wrapper.archive_path == join_paths( + CONFIG_ARCHIVE_PATH, + '/etc/dir/file.conf') + + # Тестируем результат установки флага protected. + def test_if_a_target_file_is_located_in_a_protected_directory__the_TemplateWrapper_object_sets_the_protected_flag_as_True(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.protected + + def test_if_a_target_file_is_located_in_a_directory_that_is_not_protected__the_TemplateWrapper_object_sets_the_protected_flag_as_False(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/not_protected/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert not template_wrapper.protected + + def test_if_a_target_file_is_located_in_a_directory_from_the_protected_mask__the_TemplateWrapper_object_sets_the_protected_flag_as_False(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/terminfo/info.json'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert not template_wrapper.protected + + # Тестируем проверку хэш-сумм и флаг получаемый в результате нее. + def test_if_a_target_file_is_not_protected__the_TemplateWrapper_sets_the_md5_matching_flag_as_True(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/not_protected/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.md5_matching + + def test_if_a_template_contains_the_unbound_parameter__the_TemplateWrapper_sets_the_md5_matching_flag_as_True(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba', + 'unbound': True}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_0/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.md5_matching + + def test_if_a_target_file_does_not_exist_and_CONTENTS_file_of_the_file_s_package_does_not_contain_this_file__the_TemplateWrapper_sets_the_md5_matching_flag_as_True(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/none'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.md5_matching + + def test_if_a_target_file_was_deleted_by_a_user__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_False(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/deleted.json'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert not template_wrapper.md5_matching + + def test_if_a_target_file_does_not_belong_to_any_package__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_False(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert not template_wrapper.md5_matching + + def test_if_a_template_contains_the_autoupdate_parameter__the_TemplateWrapper_sets_the_md5_matching_flag_as_True(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'samba', + 'autoupdate': True}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_0/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.md5_matching + + def test_if_a_template_contains_the_autoupdate_parameter_and_a_target_file_does_not_belong_to_any_package__the_TemplateWrapper_sets_the_md5_matching_flag_as_True(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'samba', + 'autoupdate': True}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.target_without_package + assert template_wrapper.md5_matching + + def test_if_a_target_file_s_hash_matches_the_hash_from_the_package_s_CONTENTS_file__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_True(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.md5_matching + + def test_if_a_target_file_s_hash_does_not_match_the_hash_from_the_package_s_CONTENTS_file__the_TemplateWrapper_object_sets_the_md5_matching_flag_as_False(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_0/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert not template_wrapper.md5_matching + + # Тестируем вывод путей к входному и выходному файлам исполнительного + # модуля. + def test_if_after_the_target_file_check_md5_matching_flag_is_set_as_True_and_a_template_does_not_contain_source_parameter__the_output_and_the_input_paths_for_the_TemplateExecutor_is_the_same_and_it_is_target_path(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.input_path == join_paths(CHROOT_PATH, + '/etc/dir/file.conf') + assert template_wrapper.output_path == join_paths(CHROOT_PATH, + '/etc/dir/file.conf') + + def test_if_after_the_target_file_check_md5_matching_flag_is_set_as_False_and_a_template_does_not_contain_source_parameter__the_output_path_is_the_cfg_file_path_and_the_input_path_is_the_calculate_archive_path(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'json'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_0/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.input_path == join_paths(CONFIG_ARCHIVE_PATH, + '/etc/dir_0/file') + assert template_wrapper.output_path == join_paths( + CHROOT_PATH, + '/etc/dir_0/._cfg0001_file') + + def test_if_after_the_target_file_check_md5_matching_flag_is_set_as_True_and_a_template_contains_source_parameter__the_output_and_the_input_paths_for_the_TemplateExecutor_is_the_same_and_it_is_target_path(self): + source = join_paths(CHROOT_PATH, '/etc/file') + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'json', + 'source': source}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.input_path == source + assert template_wrapper.output_path == join_paths(CHROOT_PATH, + '/etc/dir/file.conf') + + def test_if_after_the_target_file_check_md5_matching_flag_is_set_as_False_and_a_template_contains_source_parameter__the_output_path_is_the_cfg_file_path_and_the_input_path_is_the_calculate_archive_path(self): + source = join_paths(CHROOT_PATH, '/etc/file') + parameters_object = ParametersContainer({'append': 'join', + 'format': 'json', + 'source': source}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir_0/file'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.input_path == source + assert template_wrapper.output_path == join_paths( + CHROOT_PATH, + '/etc/dir_0/._cfg0001_file') + + # Тестируем поведение, если формат исполняемый. + def test_if_a_target_path_is_FILE_and_a_template_is_executable__the_target_path_is_replaced_with_the_path_to_the_cwd_directory_and_there_is_no_package_type_conflicts_and_other_checks(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'patch'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/file.conf'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.target_path == join_paths(CHROOT_PATH, + '/etc/dir') + assert template_wrapper.target_package is None + + def test_if_a_target_path_is_DIR_and_a_template_is_executable__the_target_path_is_the_same_and_there_is_no_package_type_conflicts_and_other_checks(self): + parameters_object = ParametersContainer({'append': 'join', + 'format': 'patch'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.target_path == join_paths(CHROOT_PATH, + '/etc/dir') + assert template_wrapper.target_package is None + + def test_if_a_template_is_executable_and_contains_package_parameter__the_template_updates_the_CONTENTS_file_for_package_from_parameter(self): + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'format': 'patch'}) + try: + template_wrapper = TemplateWrapper( + join_paths(CHROOT_PATH, + '/etc/dir/'), + parameters_object, FILE, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + except Exception as error: + pytest.fail("Unexpected exception: {}".format(str(error))) + assert template_wrapper.target_path == join_paths(CHROOT_PATH, + '/etc/dir/') + assert template_wrapper.target_package_name == test_package_name + + # Тестируем работу с CONTENTS через объект обертки. diff --git a/tests/templates/testfiles/test_wrapper_root/etc/dir_0/file b/tests/templates/testfiles/test_wrapper_root/etc/dir_0/file new file mode 100644 index 0000000..71d6666 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/dir_0/file @@ -0,0 +1,6 @@ +options { + parameter_1 no; + parameter_2 yes; + + user_change "blah"; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/etc/dir_1/._cfg0001_file b/tests/templates/testfiles/test_wrapper_root/etc/dir_1/._cfg0001_file new file mode 100644 index 0000000..cf1a045 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/dir_1/._cfg0001_file @@ -0,0 +1,12 @@ +options { + parameter_1 no; + parameter_2 yes; +}; + +new_section { + parameter_3 12; + + inner_section { + parameter_4 "value"; + }; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/etc/dir_1/._cfg0002_file b/tests/templates/testfiles/test_wrapper_root/etc/dir_1/._cfg0002_file new file mode 100644 index 0000000..395e7a0 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/dir_1/._cfg0002_file @@ -0,0 +1,12 @@ +options { + parameter_1 no; + parameter_2 yes; +}; + +new_section { + parameter_3 12; + + inner_section { + parameter_4 "grief"; + }; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/etc/dir_1/file b/tests/templates/testfiles/test_wrapper_root/etc/dir_1/file new file mode 100644 index 0000000..71d6666 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/dir_1/file @@ -0,0 +1,6 @@ +options { + parameter_1 no; + parameter_2 yes; + + user_change "blah"; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/etc/dir_2/._cfg0002_file b/tests/templates/testfiles/test_wrapper_root/etc/dir_2/._cfg0002_file new file mode 100644 index 0000000..395e7a0 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/dir_2/._cfg0002_file @@ -0,0 +1,12 @@ +options { + parameter_1 no; + parameter_2 yes; +}; + +new_section { + parameter_3 12; + + inner_section { + parameter_4 "grief"; + }; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/etc/dir_2/file b/tests/templates/testfiles/test_wrapper_root/etc/dir_2/file new file mode 100644 index 0000000..71d6666 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/dir_2/file @@ -0,0 +1,6 @@ +options { + parameter_1 no; + parameter_2 yes; + + user_change "blah"; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/etc/terminfo/info.json b/tests/templates/testfiles/test_wrapper_root/etc/terminfo/info.json new file mode 100644 index 0000000..3f3f7ec --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/etc/terminfo/info.json @@ -0,0 +1,7 @@ +{ + "Band": "Summoning", + "Song": "Flight of the Nazgul", + "Genre": "Black metal", + "Status": "Damn good song", + "isRecommended": true +} \ No newline at end of file diff --git a/tests/templates/testfiles/test_wrapper_root/not_protected/file.conf b/tests/templates/testfiles/test_wrapper_root/not_protected/file.conf new file mode 100644 index 0000000..3d39843 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/not_protected/file.conf @@ -0,0 +1,12 @@ +options { + parameter no; + parameter_1 yes; +}; + +awful_section { + parameter_2 12; + + inner_section { + parameter_4 "grief"; + }; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/other-package-1.1/CONTENTS b/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/other-package-1.1/CONTENTS index 998e638..a0fecd6 100644 --- a/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/other-package-1.1/CONTENTS +++ b/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/other-package-1.1/CONTENTS @@ -1,2 +1,10 @@ dir /etc -obj /etc/other_file.conf 61b82a7891c5233b92a5bde68535a71d 1591282672 +obj /etc/other_file.conf 61b82a7891c5233b92a5bde68535a71d 1591343936 +dir /etc/dir_0 +obj /etc/dir_0/file 84bcceb2c8e6de79849ea5f3304f2411 1591343236 +dir /etc/dir_1 +obj /etc/dir_1/file 84bcceb2c8e6de79849ea5f3304f2411 1591343236 +dir /etc/dir_2 +obj /etc/dir_2/file 84bcceb2c8e6de79849ea5f3304f2411 1591343236 +dir /not_protected +obj /not_protected/file.conf 687594ba5f93d0673abce62d0bd649ec 1591352448 diff --git a/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS b/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS index 8be0c84..348a5c1 100644 --- a/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS +++ b/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS @@ -1,3 +1,6 @@ dir /etc dir /etc/dir obj /etc/dir/file.conf 0b87fea7f5b65cac5012baa2bf647e72 1591105584 +dir /etc/terminfo +obj /etc/terminfo/info.json 7a374d90a202b969ee338ac4334fb14b 1591354184 +obj /etc/dir/deleted.json 11e3a79fe51cce828d973dba8702adaa 1591356795 diff --git a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config index e69de29..12c7963 100644 --- a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config +++ b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config @@ -0,0 +1,3 @@ +/etc/dir_0/file 84bcceb2c8e6de79849ea5f3304f2411 +/etc/dir_1/file 84bcceb2c8e6de79849ea5f3304f2411 +/etc/dir_2/file 84bcceb2c8e6de79849ea5f3304f2411 diff --git a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir/deleted.json b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir/deleted.json new file mode 100644 index 0000000..21625da --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir/deleted.json @@ -0,0 +1,7 @@ +{ + "Band": "Summoning", + "Song": "Rotting Horse on the Deadly Ground", + "Genre": "Black metal", + "Status": "Unspeakably good song", + "isRecommended": true +} \ No newline at end of file diff --git a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_1/file b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_1/file new file mode 100644 index 0000000..83825f2 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_1/file @@ -0,0 +1,4 @@ +options { + parameter_1 no; + parameter_2 yes; +}; diff --git a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/terminfo/info.json b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/terminfo/info.json new file mode 100644 index 0000000..3f3f7ec --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/terminfo/info.json @@ -0,0 +1,7 @@ +{ + "Band": "Summoning", + "Song": "Flight of the Nazgul", + "Genre": "Black metal", + "Status": "Damn good song", + "isRecommended": true +} \ No newline at end of file diff --git a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/not_protected/file.conf b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/not_protected/file.conf new file mode 100644 index 0000000..3d39843 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/not_protected/file.conf @@ -0,0 +1,12 @@ +options { + parameter no; + parameter_1 yes; +}; + +awful_section { + parameter_2 12; + + inner_section { + parameter_4 "grief"; + }; +};