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.
calculate-utils-4-lib/tests/templates/test_template_executor.py

296 lines
17 KiB

import pytest
import os
import stat
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
# Тестируем методы для работы непосредственно с файловой системой.
# Тестируем методы для chown и chmod.
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_owner(self):
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/dir_0'),
chown_value)
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/dir_0')).\
st_uid == chown_value['uid']
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/dir_0')).\
st_gid == chown_value['gid']
if os.getuid() == 0 and "SSH_connection" not in os.environ:
chown_value = {'uid': int(os.environ['SUDO_UID']),
'gid': int(os.environ['SUDO_GID'])}
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/dir_0'),
chown_value)
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/dir_0')).\
st_uid == chown_value['uid']
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/dir_0')).\
st_gid == chown_value['gid']
def test_if_the_chown_directory_method_input_is_path_to_the_unexisting_directory_and_correct_chown_value__the_chown_directory_method_does_nothing(self):
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_2'),
chown_value)
def test_if_the_chown_directory_method_input_is_a_path_to_the_directory_with_a_fat_filesystem_and_a_correct_chown_value__TemplateExecutor_object_throws_exception(self):
# Для тестирования нужно примонтировать файловую систему с fat32.
pass
def test_if_the_chown_file_method_input_is_a_path_to_an_existing_file_and_the_correct_chown_value__the_chown_file_method_succesfully_changes_file_owner(self):
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
template_executor._chown_file(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_0'),
chown_value)
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/file_0')).\
st_uid == chown_value['uid']
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/file_0')).\
st_gid == chown_value['gid']
if os.getuid() == 0 and "SSH_connection" not in os.environ:
chown_value = {'uid': int(os.environ['SUDO_UID']),
'gid': int(os.environ['SUDO_GID'])}
template_executor._chown_file(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_0'),
chown_value)
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/file_0')).\
st_uid == chown_value['uid']
assert os.stat(join_paths(CHROOT_PATH,
'/etc/chown_testfiles/file_0')).\
st_gid == chown_value['gid']
def test_if_the_chown_file_method_input_is_path_to_the_unexisting_file_and_correct_chown_value__the_chown_directory_method_does_nothing(self):
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
template_executor._chown_file(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_2'),
chown_value)
def test_if_the_chown_file_method_input_is_a_path_to_the_file_from_the_fat_filesystem_and_a_correct_chown_value__TemplateExecutor_object_throws_exception(self):
# Для тестирования нужно примонтировать файловую систему с fat32.
pass
def test_if_the_chmod_directory_method_input_is_path_to_the_existing_directory_and_correct_chmod_value__the_chmod_directory_method_succesfully_changes_directory_mode(self):
chmod_value = int(0o777)
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_0'),
chmod_value)
result = stat.S_IMODE(os.stat(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_0')).st_mode)
assert result == chmod_value
chmod_value = int(0o755)
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_0'),
chmod_value)
result = stat.S_IMODE(os.stat(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_0')).st_mode)
assert result == chmod_value
def test_if_the_chmod_directory_method_input_is_path_to_the_unexisting_directory_and_correct_chmod_value__the_chmod_directory_method_does_nothing(self):
chmod_value = int(0o777)
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_2'),
chmod_value)
def test_if_the_chmod_directory_method_input_is_a_path_to_the_directory_with_a_fat_filesystem_and_a_correct_chmod_value__TemplateExecutor_object_throws_exception(self):
# Для тестирования нужно примонтировать файловую систему с fat32.
pass
def test_if_the_chmod_file_method_input_is_a_path_to_an_existing_file_and_the_correct_chmod_value__the_chmod_file_method_succesfully_changes_file_owner(self):
chmod_value = int(0o777)
template_executor._chmod_file(join_paths(CHROOT_PATH,
'/etc/chmod_testfiles/dir_0'),
chmod_value)
result = stat.S_IMODE(os.stat(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_0')).st_mode)
assert result == chmod_value
chmod_value = int(0o755)
template_executor._chmod_file(join_paths(CHROOT_PATH,
'/etc/chmod_testfiles/dir_0'),
chmod_value)
result = stat.S_IMODE(os.stat(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_0')).st_mode)
assert result == chmod_value
# Тесты действий с директориями.
def test_create_directory(self):
pass