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

494 lines
28 KiB

import pytest
import os
import stat
import grp
import getpass
from calculate.templates.template_processor import TemplateExecutor, DIR,\
FILE, CalculateConfigFile,\
TemplateExecutorError,\
TemplateWrapper
from calculate.templates.template_engine import ParametersContainer
from calculate.utils.package import PackageAtomName, Version
from calculate.utils.files import join_paths
import hashlib
CHROOT_PATH = os.path.join(os.getcwd(),
'tests/templates/testfiles/test_executor_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')
template_executor = TemplateExecutor(
chroot_path=CHROOT_PATH,
cl_config_archive=CONFIG_ARCHIVE_PATH,
cl_config_path=CONFIG_PATH,
execute_archive_path=EXECUTE_ARCHIVE_PATH)
test_package_name = PackageAtomName(
{'pkg_path': os.path.join(
CHROOT_PATH,
'var/db/pkg/test-category/test-package-1.0'),
'version': Version('1.0')})
@pytest.mark.template_executor
class TestTemplateExecutor:
# Сначала протестируем класс для работы с /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)
calculate_config_file.save_changes()
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=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=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=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=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=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=CONFIG_ARCHIVE_PATH,
cl_config_path=CONFIG_PATH,
execute_archive_path=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_translate_uid_gid_method_s_input_is_uid_and_gid_of_an_existing_user__the_method_returns_user_s_name_and_usser_s_groupname(self):
if os.getuid() == 0 and "SUDO_COMMAND" in os.environ:
chown_value = {'uid': int(os.environ.get('SUDO_UID', 0)),
'gid': int(os.environ.get('SUDO_GID', 0))}
user_name = os.environ.get('SUDO_USER', 0)
group_name = grp.getgrgid(
int(os.environ.get('SUDO_GID', 0))).gr_name
else:
chown_value = {'uid': os.getuid(),
'gid': os.getgid()}
user_name = getpass.getuser()
group_name = grp.getgrgid(int(os.getgid())).gr_name
username_and_group = template_executor._translate_uid_gid(
chown_value['uid'],
chown_value['gid'])
assert username_and_group == "{}:{}".format(user_name, group_name)
# Тестируем методы для работы непосредственно с файловой системой.
# Тестируем методы для изменения владельца файла.
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()}
with pytest.raises(TemplateExecutorError):
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/dir_1'),
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()}
with pytest.raises(TemplateExecutorError):
template_executor._chown_file(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_1'),
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)
with pytest.raises(TemplateExecutorError):
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_1'),
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(0o666)
template_executor._chmod_file(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_0'),
chmod_value)
result = stat.S_IMODE(os.stat(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_0')).st_mode)
assert result == chmod_value
chmod_value = int(0o644)
template_executor._chmod_file(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_0'),
chmod_value)
result = stat.S_IMODE(os.stat(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_0')).st_mode)
assert result == chmod_value
def test_if_the_chmod_file_method_input_is_path_to_the_unexisting_file_and_correct_chmod_value__the_chmod_file_method_does_nothing(self):
chmod_value = int(0o777)
with pytest.raises(TemplateExecutorError):
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_1'),
chmod_value)
def test_if_the_chmod_file_method_input_is_a_path_to_the_file_with_a_fat_filesystem_and_a_correct_chmod_value__TemplateExecutor_object_throws_exception(self):
# Для тестирования нужно примонтировать файловую систему с fat32.
pass
# Тестируем методы для получения информации о владельце файла.
def test_if_the_get_file_owner_method_is_called_with_a_path_to_a_directory_that_exists__the_method_returns_a_dictionary_with_its_owner_s_and_group_s_id(self):
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/dir_2'),
chown_value)
owner = template_executor._get_file_owner(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/dir_2'))
assert owner == chown_value
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_2'),
chown_value)
def test_if_the_get_file_owner_method_is_called_with_a_path_to_a_directory_that_does_not_exists__the_method_throws_TemplateExecutorError_exception(self):
with pytest.raises(TemplateExecutorError):
template_executor._get_file_owner(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/dir_1'))
def test_if_the_get_file_owner_method_is_called_with_a_path_to_a_file_that_exists__the_method_returns_a_dictionary_with_its_owner_s_and_group_s_id(self):
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_2'),
chown_value)
owner = template_executor._get_file_owner(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_2'))
assert owner == chown_value
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/file_2'),
chown_value)
def test_if_the_get_file_owner_method_is_called_with_a_path_to_a_file_that_does_not_exists__the_method_throws_TemplateExecutorError_exception(self):
with pytest.raises(TemplateExecutorError):
template_executor._get_file_owner(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_1'))
# Тестируем методы для получения информации о доступе к файлу.
def test_if_the_get_file_mode_method_is_called_with_a_path_to_a_directory_that_exists__the_method_returns_its_mode_value(self):
chmod_value = int(0o777)
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_2'),
chmod_value)
mode = template_executor._get_file_mode(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_2'))
assert mode == chmod_value
chmod_value = int(0o755)
template_executor._chmod_directory(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_2'),
chmod_value)
def test_if_the_get_file_mode_method_is_called_with_a_path_to_a_directory_that_does_not_exists__the_method_throws_TemplateExecutorError_exception(self):
with pytest.raises(TemplateExecutorError):
template_executor._get_file_mode(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/dir_1'))
def test_if_the_get_file_mode_method_is_called_with_a_path_to_a_file_that_exists__the_method_returns_its_mode_value(self):
chmod_value = int(0o666)
template_executor._chmod_file(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_2'),
chmod_value)
mode = template_executor._get_file_mode(
join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_2'))
assert mode == chmod_value
chmod_value = int(0o644)
template_executor._chmod_file(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_2'),
chmod_value)
def test_if_the_get_file_mode_method_is_called_with_a_path_to_a_file_that_does_not_exists__the_method_throws_TemplateExecutorError_exception(self):
with pytest.raises(TemplateExecutorError):
template_executor._get_file_mode(join_paths(
CHROOT_PATH,
'/etc/chmod_testfiles/file_1'))
# Тесты действий с директориями.
def test_if_the_create_directory_method_input_is_a_template_with_a_target_path_to_an_existing_directory_and_chown_and_chmod_parameters_is_not_set__the_method_does_nothing(self):
target_directory = join_paths(CHROOT_PATH, '/etc/dir')
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join'})
template_wrapper = TemplateWrapper(
target_directory,
parameters_object, DIR,
chroot_path=CHROOT_PATH,
config_archive_path=CONFIG_ARCHIVE_PATH)
dir_owner = template_executor._get_file_owner(target_directory)
dir_mode = template_executor._get_file_mode(target_directory)
template_executor._create_directory(template_wrapper)
assert dir_owner == template_executor._get_file_owner(target_directory)
assert dir_mode == template_executor._get_file_mode(target_directory)
def test_if_the_create_directory_method_input_is_a_template_with_a_target_path_to_an_existing_directory_and_chown_and_chmod_parameters_is_set__the_method_does_nothing(self):
target_directory = join_paths(CHROOT_PATH, '/etc/dir')
chown_value = {'uid': os.getuid(), 'gid': os.getgid()}
chmod_value = int(0o777)
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join',
'chown': chown_value,
'chmod': chmod_value})
template_wrapper = TemplateWrapper(
target_directory,
parameters_object, DIR,
chroot_path=CHROOT_PATH,
config_archive_path=CONFIG_ARCHIVE_PATH)
dir_owner = template_executor._get_file_owner(target_directory)
dir_mode = template_executor._get_file_mode(target_directory)
template_executor._create_directory(template_wrapper)
assert chown_value == template_executor._get_file_owner(
target_directory)
assert chmod_value == template_executor._get_file_mode(
target_directory)
template_executor._chmod_directory(target_directory,
dir_mode)
if os.getuid() == 0 and "SSH_CONNECTION" not in os.environ:
template_executor._chown_directory(join_paths(
CHROOT_PATH,
'/etc/chown_testfiles/file_0'),
dir_owner)
def test_if_the_create_directory_method_input_is_a_template_with_a_target_path_to_an_unexisting_directory_and_chown_and_chmod_parameters_is_not_set__the_method_creates(self):
pass