|
|
|
|
import pytest
|
|
|
|
|
import os
|
|
|
|
|
from calculate.templates.template_processor import TemplateExecutor, DIR,\
|
|
|
|
|
FILE, CalculateConfigFile,\
|
|
|
|
|
TemplateExecutorError
|
|
|
|
|
from calculate.templates.template_engine import ParametersContainer
|
|
|
|
|
from calculate.utils.files import join_paths
|
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHROOT_PATH = os.path.join(os.getcwd(),
|
|
|
|
|
'tests/templates/testfiles/test_executor_root')
|
|
|
|
|
CL_CONFIG_PATH = os.path.join(CHROOT_PATH, 'var/lib/calculate/config')
|
|
|
|
|
CL_CONFIG_ARCHIVE_PATH = os.path.join(CHROOT_PATH,
|
|
|
|
|
'var/lib/calculate/config-archive')
|
|
|
|
|
CL_EXECUTE_ARCHIVE_PATH = os.path.join(CHROOT_PATH,
|
|
|
|
|
'var/lib/calculate/.execute')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template_executor = TemplateExecutor(
|
|
|
|
|
chroot_path=CHROOT_PATH,
|
|
|
|
|
cl_config_archive=CL_CONFIG_ARCHIVE_PATH,
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
execute_archive_path=CL_EXECUTE_ARCHIVE_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.template_executor
|
|
|
|
|
class TestTemplateAction:
|
|
|
|
|
# Сначала протестируем класс для работы с /var/lib/calculate/config файлом.
|
|
|
|
|
def test_if_config_file_does_not_exist__a_CalculateConfigFile_object_will_create_one_while_its_initialization(self):
|
|
|
|
|
temporary_chroot = join_paths(
|
|
|
|
|
os.getcwd(),
|
|
|
|
|
'tests/templates/testfiles/test_config_root_1')
|
|
|
|
|
temporary_config_path = join_paths(temporary_chroot,
|
|
|
|
|
'var/lib/calculate/config')
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=temporary_config_path,
|
|
|
|
|
cl_chroot_path=temporary_chroot)
|
|
|
|
|
assert os.path.exists(calculate_config_file.cl_config_path)
|
|
|
|
|
|
|
|
|
|
os.remove(calculate_config_file.cl_config_path)
|
|
|
|
|
|
|
|
|
|
def test_if_config_directory_is_on_the_config_path_instead_of_config_file__a_CalculateConfigFile_object_throws_TemplateExecutorError_exception(self):
|
|
|
|
|
temporary_chroot = join_paths(
|
|
|
|
|
os.getcwd(),
|
|
|
|
|
'tests/templates/testfiles/test_config_root_2')
|
|
|
|
|
temporary_config_path = join_paths(temporary_chroot,
|
|
|
|
|
'var/lib/calculate/config')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TemplateExecutorError):
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=temporary_config_path,
|
|
|
|
|
cl_chroot_path=temporary_chroot)
|
|
|
|
|
|
|
|
|
|
def test_if_a_CalculateConfigFile_object_is_initialized__the_object_can_be_used_for_setting_hash_sum_for_the_any_file(self):
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
cl_chroot_path=CHROOT_PATH)
|
|
|
|
|
md5_value = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/file'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
calculate_config_file.set_files_md5('/etc/file', md5_value)
|
|
|
|
|
assert '/etc/file' in calculate_config_file
|
|
|
|
|
assert calculate_config_file._config_dictionary['/etc/file'] ==\
|
|
|
|
|
md5_value
|
|
|
|
|
|
|
|
|
|
def test_if_a_CalculateConfigFile_object_is_initialized__the_object_can_be_used_for_removing_any_file_from_config_file(self):
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
cl_chroot_path=CHROOT_PATH)
|
|
|
|
|
md5_value = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/file'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
calculate_config_file.set_files_md5('/etc/file', md5_value)
|
|
|
|
|
assert '/etc/file' in calculate_config_file
|
|
|
|
|
|
|
|
|
|
calculate_config_file.remove_file('/etc/file')
|
|
|
|
|
assert '/etc/file' not in calculate_config_file
|
|
|
|
|
|
|
|
|
|
def test_if_a_CalculateConfigFile_object_contains_file_with_its_hash_and_the_hash_compared_with_equal_hash__the_compare_md5_method_returns_True(self):
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
cl_chroot_path=CHROOT_PATH)
|
|
|
|
|
md5_value = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/file'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
calculate_config_file.set_files_md5('/etc/file', md5_value)
|
|
|
|
|
|
|
|
|
|
assert calculate_config_file.compare_md5('/etc/file', md5_value)
|
|
|
|
|
|
|
|
|
|
def test_if_a_CalculateConfigFile_object_contains_file_with_its_hash_and_the_hash_compared_with_not_equal_hash__the_compare_md5_method_returns_False(self):
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
cl_chroot_path=CHROOT_PATH)
|
|
|
|
|
md5_value_1 = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/file'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
calculate_config_file.set_files_md5('/etc/file', md5_value_1)
|
|
|
|
|
|
|
|
|
|
md5_value_2 = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/dir/file.conf'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
assert not calculate_config_file.compare_md5('/etc/file', md5_value_2)
|
|
|
|
|
|
|
|
|
|
def test_if_a_CalculateConfigFile_object_is_used_for_comparing_a_hash_with_a_hash_of_a_file_that_is_not_in_the_config_file__the_compare_md5_method_returns_False(self):
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
cl_chroot_path=CHROOT_PATH)
|
|
|
|
|
md5_value = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/file'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
|
|
|
|
|
assert not calculate_config_file.compare_md5('/etc/file', md5_value)
|
|
|
|
|
|
|
|
|
|
def test_if_a_CalculateConfigFile_object_was_used_to_make_changes_in_the_config_file__the_save_changes_method_will_save_the_changes(self):
|
|
|
|
|
temporary_chroot = join_paths(
|
|
|
|
|
os.getcwd(),
|
|
|
|
|
'tests/templates/testfiles/test_config_root_1')
|
|
|
|
|
temporary_config_path = join_paths(temporary_chroot,
|
|
|
|
|
'var/lib/calculate/config')
|
|
|
|
|
calculate_config_file = CalculateConfigFile(
|
|
|
|
|
cl_config_path=temporary_config_path,
|
|
|
|
|
cl_chroot_path=temporary_chroot)
|
|
|
|
|
|
|
|
|
|
md5_value_1 = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/file'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
calculate_config_file.set_files_md5('/etc/file', md5_value_1)
|
|
|
|
|
|
|
|
|
|
md5_value_2 = hashlib.md5(open(join_paths(CHROOT_PATH,
|
|
|
|
|
'/etc/dir/file.conf'),
|
|
|
|
|
'r').read().encode()).hexdigest()
|
|
|
|
|
calculate_config_file.set_files_md5('/etc/dir/file.conf', md5_value_2)
|
|
|
|
|
calculate_config_file.save_changes()
|
|
|
|
|
|
|
|
|
|
output_text = '/etc/file {}\n/etc/dir/file.conf {}\n'.format(
|
|
|
|
|
md5_value_1,
|
|
|
|
|
md5_value_2)
|
|
|
|
|
assert open(temporary_config_path, 'r').read() == output_text
|
|
|
|
|
os.remove(calculate_config_file.cl_config_path)
|
|
|
|
|
|
|
|
|
|
# Тестируем исполнительный модуль.
|
|
|
|
|
# Тестируем сначала кое-какие вспомогательные методы.
|
|
|
|
|
def test_if_a_TemplateExecutor_object_is_initialized__the_object_can_return_the_set_of_the_supported_values_of_the_append_parameter(self):
|
|
|
|
|
template_executor = TemplateExecutor(
|
|
|
|
|
chroot_path=CHROOT_PATH,
|
|
|
|
|
cl_config_archive=CL_CONFIG_ARCHIVE_PATH,
|
|
|
|
|
cl_config_path=CL_CONFIG_PATH,
|
|
|
|
|
execute_archive_path=CL_EXECUTE_ARCHIVE_PATH)
|
|
|
|
|
|
|
|
|
|
assert template_executor.available_appends
|
|
|
|
|
|
|
|
|
|
assert 'join' in template_executor.available_appends
|
|
|
|
|
assert 'skip' in template_executor.available_appends
|
|
|
|
|
assert 'remove' in template_executor.available_appends
|
|
|
|
|
|
|
|
|
|
# Тестируем методы для работы непосредственно с файловой системой.
|
|
|
|
|
def test_if_the_chown_directory_method_input_is_path_to_the_existing_directory_and_correct_chown_value__the_chown_directory_method_succesfully_changes_directory_chown(self):
|
|
|
|
|
template_executor._chown_directory(join_paths(
|
|
|
|
|
CHROOT_PATH,
|
|
|
|
|
'/etc/chown_testfiles/dir_0'),
|
|
|
|
|
{'uid': 0, 'gid': 0})
|
|
|
|
|
|
|
|
|
|
def test_chown_directory(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_chmod_directory(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_create_directory(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_link_directory(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_remove_directory(self):
|
|
|
|
|
pass
|