| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
from neutron_lib.api.validators import dns |
| 16 |
from neutron_lib.db import constants as db_constants |
| 17 |
from neutron_lib.tests import _base as base |
| 18 |
|
| 19 |
|
| 20 |
class TestDnsValidators(base.BaseTestCase): |
| 21 |
|
| 22 |
@mock.patch('oslo_config.cfg.CONF') |
| 23 |
def test_validate_dns_name(self, CONF): |
| 24 |
CONF.dns_domain = '' |
| 25 |
msg = dns.validate_dns_name('') |
| 26 |
self.assertIsNone(msg) |
| 27 |
|
| 28 |
CONF.dns_domain = 'example.org.' |
| 29 |
dns_name = 'host' |
| 30 |
msg = dns.validate_dns_name(dns_name) |
| 31 |
self.assertIsNone(msg) |
| 32 |
|
| 33 |
invalid_data = 'A' * 256 |
| 34 |
max_len = 255 |
| 35 |
expected = (f"'{invalid_data}' not a valid PQDN or FQDN. Reason: " |
| 36 |
f"'{invalid_data}' exceeds the {max_len} character FQDN " |
| 37 |
"limit") |
| 38 |
msg = dns.validate_dns_name(invalid_data, max_len) |
| 39 |
self.assertEqual(expected, msg) |
| 40 |
|
| 41 |
invalid_data = '.hostname' |
| 42 |
expected = (f"'{invalid_data}' not a valid PQDN or FQDN. Reason: " |
| 43 |
"Encountered an empty component") |
| 44 |
msg = dns.validate_dns_name(invalid_data) |
| 45 |
self.assertEqual(expected, msg) |
| 46 |
|
| 47 |
invalid_data = 'hostname-' |
| 48 |
expected = (f"'{invalid_data}' not a valid PQDN or FQDN. Reason: " |
| 49 |
f"Name '{invalid_data}' must not start or end with a " |
| 50 |
"hyphen") |
| 51 |
msg = dns.validate_dns_name(invalid_data) |
| 52 |
self.assertEqual(expected, msg) |
| 53 |
|
| 54 |
invalid_data = 'hostname@host' |
| 55 |
expected = (f"'{invalid_data}' not a valid PQDN or FQDN. Reason: " |
| 56 |
f"Name '{invalid_data}' must be 1-63 characters long, " |
| 57 |
"each of which can only be alphanumeric or a " |
| 58 |
"hyphen") |
| 59 |
msg = dns.validate_dns_name(invalid_data) |
| 60 |
self.assertEqual(expected, msg) |
| 61 |
invalid_suffix = '1234' |
| 62 |
invalid_data = 'hostname.' + invalid_suffix |
| 63 |
expected = (f"'{invalid_data}' not a valid PQDN or FQDN. Reason: " |
| 64 |
f"TLD '{invalid_suffix}' must not be all " |
| 65 |
"numeric") |
| 66 |
msg = dns.validate_dns_name(invalid_data) |
| 67 |
self.assertEqual(expected, msg) |
| 68 |
|
| 69 |
# len(dns_name + dns_domain) > 255 |
| 70 |
invalid_domain = 'A' * 250 + '.org.' |
| 71 |
CONF.dns_domain = invalid_domain |
| 72 |
dns_name = 'hostname' |
| 73 |
expected = ("The dns_name passed is a PQDN and its size is " |
| 74 |
f"'{len(dns_name)}'. The dns_domain option in " |
| 75 |
f"neutron.conf is set to {invalid_domain}, with a " |
| 76 |
f"length of '{len(invalid_domain) + 1}'. When the two are " |
| 77 |
"concatenated to form a FQDN (with a '.' at the end), " |
| 78 |
"the resulting length exceeds the maximum size " |
| 79 |
f"of '{db_constants.FQDN_FIELD_SIZE}'" |
| 80 |
) |
| 81 |
msg = dns.validate_dns_name(dns_name) |
| 82 |
self.assertEqual(expected, msg) |
| 83 |
|
| 84 |
dns_name = 'host.' |
| 85 |
dns_domain = 'example.com.' |
| 86 |
CONF.dns_domain = dns_domain |
| 87 |
expected = ("The dns_name passed is a FQDN. Its higher level labels " |
| 88 |
"must be equal to the dns_domain option in neutron.conf, " |
| 89 |
f"that has been set to '{dns_domain}'. It must also " |
| 90 |
"include one or more valid DNS labels to the left " |
| 91 |
f"of '{dns_domain}'") |
| 92 |
msg = dns.validate_dns_name(dns_name) |
| 93 |
self.assertEqual(expected, msg) |
| 94 |
|
| 95 |
def test_validate_fip_dns_name(self): |
| 96 |
# Don't run tests duplicated to validate_dns_name() |
| 97 |
|
| 98 |
msg = dns.validate_fip_dns_name('') |
| 99 |
self.assertIsNone(msg) |
| 100 |
|
| 101 |
msg = dns.validate_fip_dns_name('host') |
| 102 |
self.assertIsNone(msg) |
| 103 |
|
| 104 |
invalid_data = 1234 |
| 105 |
expected = f"'{invalid_data}' is not a valid string" |
| 106 |
msg = dns.validate_fip_dns_name(invalid_data) |
| 107 |
self.assertEqual(expected, msg) |
| 108 |
|
| 109 |
invalid_data = 'host.' |
| 110 |
expected = (f"'{invalid_data}' is a FQDN. It should be a relative " |
| 111 |
"domain name") |
| 112 |
msg = dns.validate_fip_dns_name(invalid_data) |
| 113 |
self.assertEqual(expected, msg) |
| 114 |
|
| 115 |
length = 10 |
| 116 |
invalid_data = 'a' * length |
| 117 |
max_len = 12 |
| 118 |
expected = (f"'{invalid_data}' contains {length} characters. Adding a " |
| 119 |
"domain name will cause it to exceed the maximum length " |
| 120 |
f"of a FQDN of '{max_len}'") |
| 121 |
msg = dns.validate_fip_dns_name(invalid_data, max_len) |
| 122 |
self.assertEqual(expected, msg) |
| 123 |
|
| 124 |
def test_validate_dns_domain(self): |
| 125 |
# Don't run tests duplicated to validate_dns_name() |
| 126 |
|
| 127 |
msg = dns.validate_dns_domain('') |
| 128 |
self.assertIsNone(msg) |
| 129 |
|
| 130 |
msg = dns.validate_dns_domain('example.com.') |
| 131 |
self.assertIsNone(msg) |
| 132 |
|
| 133 |
invalid_data = 1234 |
| 134 |
expected = f"'{invalid_data}' is not a valid string" |
| 135 |
msg = dns.validate_dns_domain(invalid_data) |
| 136 |
self.assertEqual(expected, msg) |
| 137 |
|
| 138 |
invalid_data = 'example.com' |
| 139 |
expected = f"'{invalid_data}' is not a FQDN" |
| 140 |
msg = dns.validate_dns_domain(invalid_data) |
| 141 |
self.assertEqual(expected, msg) |
| 142 |
|
| 143 |
length = 9 |
| 144 |
invalid_data = 'a' * length + '.' |
| 145 |
max_len = 11 |
| 146 |
expected = (f"'{invalid_data}' contains {length + 1} characters. " |
| 147 |
"Adding a sub-domain will cause it to exceed the maximum " |
| 148 |
f"length of a FQDN of '{max_len}'") |
| 149 |
msg = dns.validate_dns_domain(invalid_data, max_len) |
| 150 |
self.assertEqual(expected, msg) |