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/utils/test_package.py

184 lines
9.0 KiB

import pytest
import os
from calculate.utils.package import Package, PackageAtom, PackageAtomError
from pprint import pprint
from collections import OrderedDict
import re
BASE_DIRECTORY = os.path.join(os.getcwd(), 'tests/utils/testfiles/')
@pytest.mark.package_utils
class TestContents:
def test_if_PackageContents_object_initialized_by_existing_package__it_contains_dictionary_of_items_from_contents_file(self):
result = OrderedDict({
'/usr':
OrderedDict({'type': 'dir'}),
'/usr/bin':
OrderedDict({'type': 'dir'}),
'/usr/bin/rview':
OrderedDict({'type': 'sym',
'target': 'vim',
'mtime': '1573538053'}),
'/usr/bin/rvim':
OrderedDict({'type': 'sym',
'target': 'vim',
'mtime': '1573538053'}),
'/usr/bin/vim':
OrderedDict({'type': 'obj',
'md5': '30acc0f256e11c1ecdb1bd80b688d238',
'mtime': '1573538056'}),
'/usr/bin/vimdiff':
OrderedDict({'type': 'sym',
'target': 'vim',
'mtime': '1573538053'})})
contents_object = Package('category_1/package_1',
chroot_path=BASE_DIRECTORY)
assert contents_object.contents_dictionary == result
def test_if_PackageContents_object_contains_contents_dictionary__it_renders_CONTENTS_file_correctly(self):
contents_object = Package('category_1/package_1',
chroot_path=BASE_DIRECTORY)
result = '''dir /usr
dir /usr/bin
sym /usr/bin/rview -> vim 1573538053
sym /usr/bin/rvim -> vim 1573538053
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
sym /usr/bin/vimdiff -> vim 1573538053
'''
assert contents_object.render_contents_file() == result
def test_if_new_directory_is_added_in_contents_file_using_add_dir_method__the_PackageContents_object_renders_the_contents_file_with_new_dir(self):
contents_object = Package('category_1/package_1',
chroot_path=BASE_DIRECTORY)
result = '''dir /usr
dir /usr/bin
sym /usr/bin/rview -> vim 1573538053
sym /usr/bin/rvim -> vim 1573538053
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
sym /usr/bin/vimdiff -> vim 1573538053
dir /etc
dir /etc/test_dir_1
'''
contents_object.add_dir('/etc/test_dir_1')
assert contents_object.render_contents_file() == result
def test_if_new_object_is_added_in_contents_file_using_add_obj_method__the_PackageContents_object_renders_the_contents_file_with_new_obj(self):
contents_object = Package('category_1/package_1',
chroot_path=BASE_DIRECTORY)
result = '''dir /usr
dir /usr/bin
sym /usr/bin/rview -> vim 1573538053
sym /usr/bin/rvim -> vim 1573538053
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
sym /usr/bin/vimdiff -> vim 1573538053
dir /etc
dir /etc/test_dir_2
obj /etc/test_dir_2/file_2.cfg a371f4d456d471ac0ed0e8befff1cb6d 1586531028
'''
contents_object.add_obj('/etc/test_dir_2/file_2.cfg')
print('RESULT:')
print(contents_object.render_contents_file())
assert contents_object.render_contents_file() == result
def test_if_new_link_is_added_in_contents_file_using_add_sym_method__the_PackageContents_object_renders_the_contents_file_with_new_sym(self):
contents_object = Package('category_1/package_1',
chroot_path=BASE_DIRECTORY)
result = '''dir /usr
dir /usr/bin
sym /usr/bin/rview -> vim 1573538053
sym /usr/bin/rvim -> vim 1573538053
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
sym /usr/bin/vimdiff -> vim 1573538053
dir /etc
dir /etc/test_dir_2
sym /etc/test_dir_2/symlink -> file_2.cfg 1587117567
'''
contents_object.add_sym('/etc/test_dir_2/symlink')
print('RESULT:')
print(contents_object.render_contents_file())
assert contents_object.render_contents_file() == result
def test_if_the_PackageAtom_object_parsed_a_correct_package_atom_name_but_without_a_slot_and_use_flags__the_PackageAtom_object_contains_name_and_category_values(self):
package_atom = PackageAtom()
package_atom.parse_package_parameter('dev-lang/python-3.6')
parsing_result = {
'category': 'dev-lang',
'contents': '/var/db/pkg/dev-lang/python-3.6.9/CONTENTS',
'name': 'python-3.6.9'
}
assert package_atom._atom_dictionary == parsing_result
def test_if_the_PackageAtom_object_parsed_a_correct_package_atom_name_with_a_slot_value__the_PackageAtom_object_contains_name_category_values_and_slot_value(self):
package_atom = PackageAtom()
package_atom.parse_package_parameter('dev-lang/python-3.6:3.6/3.6m')
parsing_result = {
'category': 'dev-lang',
'contents': '/var/db/pkg/dev-lang/python-3.6.9/CONTENTS',
'name': 'python-3.6.9',
'slot': '3.6/3.6m'
}
assert package_atom._atom_dictionary == parsing_result
def test_if_the_PackageAtom_object_parsed_a_correct_package_atom_name_with_an_empty_slot_value__the_PackageAtom_object_contains_name_category_values_and_slot_value(self):
package_atom = PackageAtom()
package_atom.parse_package_parameter('dev-lang/python-3.6:')
parsing_result = {
'category': 'dev-lang',
'contents': '/var/db/pkg/dev-lang/python-3.6.9/CONTENTS',
'name': 'python-3.6.9'
}
assert package_atom._atom_dictionary == parsing_result
def test_if_the_PackageAtom_object_parsed_a_correct_package_atom_name_with_a_uses_value__the_PackageAtom_object_contains_name_category_values_and_slot_value(self):
package_atom = PackageAtom()
package_atom.parse_package_parameter('dev-lang/python-3.6 abi_x86_64 ssl')
parsing_result = {
'category': 'dev-lang',
'contents': '/var/db/pkg/dev-lang/python-3.6.9/CONTENTS',
'name': 'python-3.6.9',
'uses': ['abi_x86_64', 'ssl']
}
assert package_atom._atom_dictionary == parsing_result
def test_if_the_PackageAtom_object_parsed_a_correct_package_atom_name_and_add_slot_and_add_use_flags_is_set__the_PackageAtom_object_contains_name_category_slot_and_uses_values(self):
package_atom = PackageAtom()
package_atom.parse_package_parameter('dev-lang/python-3.6',
add_slot=True,
add_uses=True)
parsing_result = {
'category': 'dev-lang',
'contents': '/var/db/pkg/dev-lang/python-3.6.9/CONTENTS',
'name': 'python-3.6.9'
}
assert parsing_result
def test_if_the_PackageAtom_object_tried_to_parse_an_incorrect_package_atom_name__the_PackageAtom_object_throws_the_PackageAtomError_exception(self):
package_atom = PackageAtom()
with pytest.raises(PackageAtomError):
package_atom.parse_package_parameter(
'dev-lang/something_i_can_never_have:3.6/3.6m')
def test_if_the_PackageAtom_object_tried_to_parse_an_correct_package_atom_name_that_matches_multiple_packages__the_PackageAtom_object_throws_the_PackageAtomError_exception(self):
package_atom = PackageAtom()
with pytest.raises(PackageAtomError):
package_atom.parse_package_parameter(
'dev-lang/python')
def test_if_the_get_file_package_method_of_the_PackageAtom_object_is_called_with_a_name_of_a_file_that_belongs_to_any_package__the_PackageAtom_object_contains_dictionary_with_an_owner_package(self):
package_atom = PackageAtom()
package_atom_regex = re.compile(r'dev-lang/python-3.6.\d+')
try:
file_package = package_atom.get_file_package('/usr/bin/python3.6')
assert package_atom_regex.search(file_package)
except Exception as error:
pytest.fail('Unexpected exception: {0}'.
format(str(error)))
def test_if_the_get_file_package_method_of_the_PackageAtom_object_is_called_with_a_name_of_a_file_that_does_not_belong_to_any_package__the_PackageAtom_object_throws_the_PackageAtomError_exception(self):
package_atom = PackageAtom()
with pytest.raises(PackageAtomError):
file_package = package_atom.get_file_package('/etc/shadow')
print('package = {}'.format(file_package))