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.

299 lines
10 KiB

import pytest
from collections import OrderedDict
from calculate.templates.format.bind_format import BINDFormat
@pytest.mark.bind
class TestParsingMethods:
def test_if_input_document_contains_just_few_parameter_lines__the_initialised_object_contains_correct_dictionary(self):
document_text = '''directory "/var/bind";
pid-file "/run/named/named.pid";
disable-empty-zone "10.in-addr.arpa";
'''
result = OrderedDict({
('', 'directory'):
['"/var/bind"'],
('', 'pid-file'):
['"/run/named/named.pid"'],
('', 'disable-empty-zone'):
['"10.in-addr.arpa"']
})
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_if_input_document_contains_some_block_of_parameters__the_initialised_object_contains_correct_dictionary(self):
document_text = '''
acl "dns_servers" {
127.0.0.1;
10.0.1.3;
10.1.0.3;
};
options {
response-policy {
zone "rpz.zone";
};
recursion yes;
};
zone "localhost" IN {
type master;
file "pri/localhost.zone";
notify no;
}
'''
acl_section = OrderedDict({('', '127.0.0.1'): [''],
('', '10.0.1.3'): [''],
('', '10.1.0.3'): ['']})
response_section = OrderedDict({('', 'zone'): ['"rpz.zone"']})
options_section = OrderedDict({('', 'response-policy'): response_section,
('', 'recursion'): ['yes']})
zone_section = OrderedDict({('', 'type'): ['master'],
('', 'file'): ['"pri/localhost.zone"'],
('', 'notify'): ['no']})
result = OrderedDict({('', 'acl', '"dns_servers"'): acl_section,
('', 'options'): options_section,
('', 'zone', '"localhost"', 'IN'): zone_section})
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_if_input_document_contains_some_blocks_with_similar_names__the_blocks_join_recursively(self):
document_text = '''
acl "dns_servers" {
127.0.0.1;
10.0.1.3;
10.1.0.3;
};
options {
response-policy {
mood "almost.blue";
};
todo "drink.beer"
};
acl "dns_servers" {
10.3.0.3;
10.4.0.3;
};
options {
response-policy {
zone "rpz.zone";
};
}
'''
acl_section = OrderedDict({('', '127.0.0.1'): [''],
('', '10.0.1.3'): [''],
('', '10.1.0.3'): [''],
('', '10.3.0.3'): [''],
('', '10.4.0.3'): ['']})
response_section = OrderedDict({('', 'mood'): ['"almost.blue"'],
('', 'zone'): ['"rpz.zone"']})
options_section = OrderedDict({('', 'response-policy'):
response_section,
('', 'todo'): ['"drink.beer"']})
result = OrderedDict({('', 'acl', '"dns_servers"'): acl_section,
('', 'options'): options_section})
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_if_input_document_contains_blocks_and_parameters_with_action_marks__the_key_tuples_of_object_s_dictionary_have_it_as_its_first_element(self):
document_text = '''
!pid-file "/run/named/named.pid";
-disable-empty-zone "10.in-addr.arpa";
acl "dns_servers" {
!127.0.0.1;
10.0.1.3;
10.1.0.3;
};
-options {
!response-policy {
zone "rpz.zone";
};
!recursion yes;
}
'''
acl_section = OrderedDict({('!', '127.0.0.1'): [''],
('', '10.0.1.3'): [''],
('', '10.1.0.3'): ['']})
response_section = OrderedDict({('', 'zone'): ['"rpz.zone"']})
options_section = OrderedDict({('!', 'response-policy'):
response_section,
('!', 'recursion'): ['yes']})
result = OrderedDict({('!', 'pid-file'):
['"/run/named/named.pid"'],
('-', 'disable-empty-zone'):
['"10.in-addr.arpa"'],
('', 'acl', '"dns_servers"'): acl_section,
('-', 'options'): options_section})
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_if_parameters_and_blocks_in_input_document_has_some_comments__the_comments_will_be_collected_in_the_list_of_parameter_value_or_with_special_key_in_block_dictionary(self):
document_text = '''
// Comment 1
!pid-file "/run/named/named.pid";
/*
* A very big comment.
* Still here...
* The pure giant of the comment kind.
*/
-disable-empty-zone "10.in-addr.arpa";
# Comment 2
// Comment 3
acl "dns_servers" {
!127.0.0.1;
// Comment 4
10.0.1.3;
10.1.0.3;
};
-options {
!response-policy {
/*
* This comment is very important.
* And I have no idea, why this
* comment is so important.
*/
zone "rpz.zone";
};
!recursion yes;
}
'''
acl_section = OrderedDict({'#': ['# Comment 2', '// Comment 3'],
('!', '127.0.0.1'): [''],
('', '10.0.1.3'): ['// Comment 4', ''],
('', '10.1.0.3'): ['']})
response_section = OrderedDict({('', 'zone'):
['/*',
'* This comment is very important.',
'* And I have no idea, why this',
'* comment is so important.',
'*/',
'"rpz.zone"']})
options_section = OrderedDict({('!', 'response-policy'):
response_section,
('!', 'recursion'): ['yes']})
result = OrderedDict({('!', 'pid-file'):
['// Comment 1',
'"/run/named/named.pid"'],
('-', 'disable-empty-zone'):
['/*',
'* A very big comment.',
'* Still here...',
'* The pure giant of the comment kind.',
'*/',
'"10.in-addr.arpa"'],
('', 'acl', '"dns_servers"'): acl_section,
('-', 'options'): options_section})
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_if_the_IgnoreComments_flag_is_set__the_parser_ignores_all_comments(self):
document_text = '''
// Comment 1
!pid-file "/run/named/named.pid";
/*
* A very big comment.
* Still here...
* The pure giant of comment kind.
*/
-disable-empty-zone "10.in-addr.arpa";
# Comment 2
// Comment 3
acl "dns_servers" {
!127.0.0.1;
// Comment 4
10.0.1.3;
10.1.0.3;
};
-options {
!response-policy {
/*
* This comment is very important.
* And I have no idea, why this
* comment is so important.
*/
zone "rpz.zone";
};
!recursion yes;
}
'''
acl_section = OrderedDict({('!', '127.0.0.1'): [''],
('', '10.0.1.3'): [''],
('', '10.1.0.3'): ['']})
response_section = OrderedDict({('', 'zone'): ['"rpz.zone"']})
options_section = OrderedDict({('!', 'response-policy'):
response_section,
('!', 'recursion'): ['yes']})
result = OrderedDict({('!', 'pid-file'):
['"/run/named/named.pid"'],
('-', 'disable-empty-zone'):
['"10.in-addr.arpa"'],
('', 'acl', '"dns_servers"'): acl_section,
('-', 'options'): options_section})
bind_object = BINDFormat(document_text, ignore_comments=True)
assert bind_object._document_dictionary == result
def test_if_input_document_contains_parameters_to_delete_without_values_or_with_empty_block__the_document_object_contains_dictionary_with_item_to_delete(self):
document_text = '''
!pid-file;
!acl "dns_servers" {};
options {
!response-policy {};
!recursion;
}
'''
options_section = OrderedDict({('!', 'response-policy'):
OrderedDict(),
('!', 'recursion'): ['']})
result = OrderedDict({('!', 'pid-file'):
[''],
('!', 'acl', '"dns_servers"'): OrderedDict(),
('', 'options'): options_section})
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/bind_original.conf', 'r') as original_file:
original_text = original_file.read()
print(original_text)
bind_original_object = BINDFormat(original_text)
with open('./tests/format/testfiles/bind_template.conf', 'r') as template_file:
template_text = template_file.read()
bind_template_object = BINDFormat(template_text,
ignore_comments=True)
bind_original_object.join_template(bind_template_object)
with open('./tests/format/testfiles/bind_result.conf', 'r') as result_file:
result_text = result_file.read()
assert bind_original_object.get_document_text() == result_text