Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 1 | # Copyright 2016 Hewlett Packard Enterprise Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 14 | import random |
Arkady Shtempler | b8ea4ca | 2021-06-04 08:55:28 +0300 | [diff] [blame] | 15 | from string import ascii_lowercase |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 16 | |
| 17 | import netaddr |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 18 | from oslo_log import log as logging |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 19 | from tempest import config |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 20 | from tempest.lib.common.utils import data_utils |
| 21 | |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 22 | LOG = logging.getLogger(__name__) |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 23 | CONF = config.CONF |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 24 | |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 25 | |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 26 | def rand_ip(): |
| 27 | return ".".join(str(random.randrange(0, 256)) for _ in range(4)) |
| 28 | |
| 29 | |
| 30 | def rand_ipv6(): |
| 31 | def hexes(n): |
| 32 | return "".join(random.choice("1234567890abcdef") for _ in range(n)) |
| 33 | result = ":".join(hexes(4) for _ in range(8)) |
| 34 | an = netaddr.IPAddress(result, version=6) |
| 35 | return an.format(netaddr.ipv6_compact) |
| 36 | |
| 37 | |
Arkady Shtempler | 6d6b27d | 2022-04-18 15:25:00 +0300 | [diff] [blame] | 38 | def rand_zone_name(name='', prefix='rand', suffix=None): |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 39 | """Generate a random zone name |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 40 | :param str name: The name that you want to include |
| 41 | :param prefix: the exact text to start the string. Defaults to "rand" |
| 42 | :param suffix: the exact text to end the string |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 43 | :return: a random zone name e.g. example.org. |
| 44 | :rtype: string |
| 45 | """ |
Arkady Shtempler | 6d6b27d | 2022-04-18 15:25:00 +0300 | [diff] [blame] | 46 | if suffix is None: |
| 47 | suffix = '.{}.'.format(CONF.dns.tld_suffix) |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 48 | name = data_utils.rand_name(name=name, prefix=prefix) |
| 49 | return name + suffix |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def rand_email(domain=None): |
| 53 | """Generate a random zone name |
| 54 | :return: a random zone name e.g. example.org. |
| 55 | :rtype: string |
| 56 | """ |
| 57 | domain = domain or rand_zone_name() |
| 58 | return 'example@%s' % domain.rstrip('.') |
| 59 | |
| 60 | |
Arkady Shtempler | 4054a22 | 2022-06-23 13:16:51 +0300 | [diff] [blame^] | 61 | def rand_ttl(start=0, end=86400): |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 62 | """Generate a random TTL value |
| 63 | :return: a random ttl e.g. 165 |
| 64 | :rtype: string |
| 65 | """ |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 66 | start = max(start, CONF.dns.min_ttl) |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 67 | return data_utils.rand_int_id(start, end) |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def rand_zonefile_data(name=None, ttl=None): |
| 71 | """Generate random zone data, with optional overrides |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 72 | :return: A ZoneModel |
| 73 | """ |
| 74 | zone_base = ('$ORIGIN &\n& # IN SOA ns.& nsadmin.& # # # # #\n' |
| 75 | '& # IN NS ns.&\n& # IN MX 10 mail.&\nns.& 360 IN A 1.0.0.1') |
| 76 | if name is None: |
| 77 | name = rand_zone_name() |
| 78 | if ttl is None: |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 79 | ttl = rand_ttl() |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 80 | |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 81 | return zone_base.replace('&', name).replace('#', str(ttl)) |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | def rand_quotas(zones=None, zone_records=None, zone_recordsets=None, |
| 85 | recordset_records=None, api_export_size=None): |
Graham Hayes | c811498 | 2016-07-04 17:46:08 +0100 | [diff] [blame] | 86 | quotas_dict = { |
| 87 | 'zones': |
| 88 | zones or data_utils.rand_int_id(100, 999999), |
| 89 | 'zone_records': |
| 90 | zone_records or data_utils.rand_int_id(100, 999999), |
| 91 | 'zone_recordsets': |
| 92 | zone_recordsets or data_utils.rand_int_id(100, 999999), |
| 93 | 'recordset_records': |
| 94 | recordset_records or data_utils.rand_int_id(100, 999999), |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 95 | } |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 96 | |
Graham Hayes | c811498 | 2016-07-04 17:46:08 +0100 | [diff] [blame] | 97 | if CONF.dns_feature_enabled.bug_1573141_fixed: |
| 98 | quotas_dict['api_export_size'] = \ |
| 99 | api_export_size or data_utils.rand_int_id(100, 999999) |
| 100 | else: |
Takashi Kajinami | 299344d | 2021-11-29 15:56:14 +0900 | [diff] [blame] | 101 | LOG.warning("Leaving `api_export_size` out of quota data due to: " |
| 102 | "https://bugs.launchpad.net/designate/+bug/1573141") |
Graham Hayes | c811498 | 2016-07-04 17:46:08 +0100 | [diff] [blame] | 103 | |
| 104 | return quotas_dict |
| 105 | |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 106 | |
| 107 | def rand_zone_data(name=None, email=None, ttl=None, description=None): |
| 108 | """Generate random zone data, with optional overrides |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 109 | :return: A ZoneModel |
| 110 | """ |
| 111 | if name is None: |
Arkady Shtempler | 6d6b27d | 2022-04-18 15:25:00 +0300 | [diff] [blame] | 112 | name = rand_zone_name( |
| 113 | prefix='testdomain', suffix='.{}.'.format(CONF.dns.tld_suffix)) |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 114 | if email is None: |
| 115 | email = ("admin@" + name).strip('.') |
| 116 | if description is None: |
| 117 | description = rand_zone_name(prefix='Description ', suffix='') |
| 118 | if ttl is None: |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 119 | ttl = rand_ttl() |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 120 | return { |
| 121 | 'name': name, |
| 122 | 'email': email, |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 123 | 'ttl': ttl, |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 124 | 'description': description} |
| 125 | |
| 126 | |
| 127 | def rand_recordset_data(record_type, zone_name, name=None, records=None, |
| 128 | ttl=None): |
| 129 | """Generate random recordset data, with optional overrides |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 130 | :return: A RecordsetModel |
| 131 | """ |
| 132 | if name is None: |
| 133 | name = rand_zone_name(prefix=record_type, suffix='.' + zone_name) |
| 134 | if records is None: |
| 135 | records = [rand_ip()] |
| 136 | if ttl is None: |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 137 | ttl = rand_ttl() |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 138 | return { |
| 139 | 'type': record_type, |
| 140 | 'name': name, |
| 141 | 'records': records, |
| 142 | 'ttl': ttl} |
| 143 | |
| 144 | |
zahlabut | 5260227 | 2021-08-04 17:07:29 +0300 | [diff] [blame] | 145 | def rand_a_recordset(zone_name, ips=None, **kwargs): |
| 146 | if ips is None: |
| 147 | return rand_recordset_data( |
| 148 | 'A', zone_name, records=[rand_ip()], **kwargs) |
| 149 | else: |
| 150 | return rand_recordset_data('A', zone_name, records=ips, **kwargs) |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def rand_aaaa_recordset(zone_name, ip=None, **kwargs): |
| 154 | if ip is None: |
| 155 | ip = rand_ipv6() |
| 156 | return rand_recordset_data('AAAA', zone_name, records=[ip], **kwargs) |
| 157 | |
| 158 | |
| 159 | def rand_cname_recordset(zone_name, cname=None, **kwargs): |
| 160 | if cname is None: |
| 161 | cname = zone_name |
| 162 | return rand_recordset_data('CNAME', zone_name, records=[cname], **kwargs) |
| 163 | |
| 164 | |
| 165 | def rand_mx_recordset(zone_name, pref=None, host=None, **kwargs): |
| 166 | if pref is None: |
| 167 | pref = str(random.randint(0, 65535)) |
| 168 | if host is None: |
| 169 | host = rand_zone_name(prefix='mail', suffix='.' + zone_name) |
| 170 | data = "{0} {1}".format(pref, host) |
| 171 | return rand_recordset_data('MX', zone_name, records=[data], **kwargs) |
| 172 | |
| 173 | |
| 174 | def rand_spf_recordset(zone_name, data=None, **kwargs): |
Dmitry Galkin | 9a0a360 | 2018-11-13 19:42:29 +0000 | [diff] [blame] | 175 | data = data or '"v=spf1 +all"' |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 176 | return rand_recordset_data('SPF', zone_name, records=[data], **kwargs) |
| 177 | |
| 178 | |
| 179 | def rand_srv_recordset(zone_name, data=None): |
| 180 | data = data or "10 0 8080 %s.%s" % (rand_zone_name(suffix=''), zone_name) |
| 181 | return rand_recordset_data('SRV', zone_name, |
| 182 | name="_sip._tcp.%s" % zone_name, |
| 183 | records=[data]) |
| 184 | |
| 185 | |
| 186 | def rand_sshfp_recordset(zone_name, algorithm_number=None, |
| 187 | fingerprint_type=None, fingerprint=None, |
| 188 | **kwargs): |
| 189 | algorithm_number = algorithm_number or 2 |
| 190 | fingerprint_type = fingerprint_type or 1 |
| 191 | fingerprint = fingerprint or \ |
| 192 | "123456789abcdef67890123456789abcdef67890" |
| 193 | |
| 194 | data = "%s %s %s" % (algorithm_number, fingerprint_type, fingerprint) |
| 195 | return rand_recordset_data('SSHFP', zone_name, records=[data], **kwargs) |
| 196 | |
| 197 | |
| 198 | def rand_txt_recordset(zone_name, data=None, **kwargs): |
Dmitry Galkin | 9a0a360 | 2018-11-13 19:42:29 +0000 | [diff] [blame] | 199 | data = data or '"v=spf1 +all"' |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 200 | return rand_recordset_data('TXT', zone_name, records=[data], **kwargs) |
| 201 | |
| 202 | |
| 203 | def wildcard_ns_recordset(zone_name): |
| 204 | name = "*.{0}".format(zone_name) |
Arkady Shtempler | 6d6b27d | 2022-04-18 15:25:00 +0300 | [diff] [blame] | 205 | records = ["ns.example.{}.".format(CONF.dns.tld_suffix)] |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 206 | return rand_recordset_data('NS', zone_name, name, records) |
sonu.kumar | 4beb93c | 2016-05-05 01:12:43 +0900 | [diff] [blame] | 207 | |
| 208 | |
| 209 | def rand_ns_records(): |
| 210 | ns_zone = rand_zone_name() |
| 211 | records = [] |
| 212 | for i in range(0, 2): |
| 213 | records.append("ns%s.%s" % (i, ns_zone)) |
| 214 | ns_records = [{"hostname": x, "priority": random.randint(1, 999)} |
| 215 | for x in records] |
| 216 | return ns_records |
sonu.kumar | 2de01be | 2016-05-05 10:07:28 +0900 | [diff] [blame] | 217 | |
| 218 | |
Arkady Shtempler | ed18eaa | 2022-02-15 22:09:02 +0200 | [diff] [blame] | 219 | def rand_soa_records(number_of_records=2): |
| 220 | return ['{} {} {} {} {} {}.'.format( |
| 221 | '{}.{}.{}'.format(rand_string(3), rand_string(7), rand_string(3)), |
| 222 | random.randint(1000000000, 2020080302), random.randint(3000, 7200), |
| 223 | random.randint(1000, 3600), random.randint(1000000, 1209600), |
| 224 | random.randint(1000, 3600)) for i in range(0, number_of_records)] |
| 225 | |
| 226 | |
| 227 | def rand_soa_recordset(zone_name, **kwargs): |
| 228 | return rand_recordset_data( |
| 229 | 'SOA', zone_name, records=rand_soa_records(), **kwargs) |
| 230 | |
| 231 | |
sonu.kumar | 2de01be | 2016-05-05 10:07:28 +0900 | [diff] [blame] | 232 | def rand_tld(): |
| 233 | data = { |
| 234 | "name": rand_zone_name(prefix='tld', suffix='') |
| 235 | } |
| 236 | return data |
sonu.kumar | e9785c9 | 2016-05-17 10:56:47 +0900 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def rand_transfer_request_data(description=None, target_project_id=None): |
| 240 | """Generate random transfer request data, with optional overrides |
sonu.kumar | e9785c9 | 2016-05-17 10:56:47 +0900 | [diff] [blame] | 241 | :return: A TransferRequest data |
| 242 | """ |
| 243 | |
| 244 | data = {} |
| 245 | |
| 246 | if description is None: |
| 247 | data['description'] = data_utils.rand_name(prefix='Description ') |
| 248 | |
| 249 | if target_project_id: |
| 250 | data['target_project_id'] = target_project_id |
| 251 | |
| 252 | return data |
sonu.kumar | c311086 | 2016-05-27 14:48:04 +0900 | [diff] [blame] | 253 | |
| 254 | |
| 255 | def rand_tsig_algorithm(): |
| 256 | algorithm = ['hmac-md5', 'hmac-sha1', 'hmac-sha224', 'hmac-sha256', |
| 257 | 'hmac-sha384', 'hmac-sha512'] |
| 258 | return random.choice(algorithm) |
| 259 | |
| 260 | |
| 261 | def rand_tsig_scope(): |
| 262 | scope = ["ZONE", "POOL"] |
| 263 | return random.choice(scope) |
Eric Larson | dc715e1 | 2016-08-25 13:21:35 +0100 | [diff] [blame] | 264 | |
| 265 | |
| 266 | def make_rand_recordset(zone_name, record_type): |
| 267 | """Create a rand recordset by type |
| 268 | This essentially just dispatches to the relevant random recordset |
| 269 | creation functions. |
Eric Larson | dc715e1 | 2016-08-25 13:21:35 +0100 | [diff] [blame] | 270 | :param str zone_name: The zone name the recordset applies to |
| 271 | :param str record_type: The type of recordset (ie A, MX, NS, etc...) |
| 272 | """ |
| 273 | |
| 274 | func = globals()["rand_{}_recordset".format(record_type.lower())] |
| 275 | return func(zone_name) |
Arkady Shtempler | b8ea4ca | 2021-06-04 08:55:28 +0300 | [diff] [blame] | 276 | |
| 277 | |
| 278 | def rand_string(size): |
| 279 | """Create random string of ASCII chars by size |
Arkady Shtempler | b8ea4ca | 2021-06-04 08:55:28 +0300 | [diff] [blame] | 280 | :param int size - length os the string to be create |
| 281 | :return - random creates string of ASCII lover characters |
| 282 | """ |
| 283 | return ''.join(random.choice(ascii_lowercase) for _ in range(size)) |
| 284 | |
| 285 | |
| 286 | def rand_domain_name(tld=None): |
| 287 | """Create random valid domain name |
Arkady Shtempler | b8ea4ca | 2021-06-04 08:55:28 +0300 | [diff] [blame] | 288 | :param tld (optional) - TLD that will be used to random domain name |
| 289 | :return - valid domain name, for example: paka.zbabun.iuh |
| 290 | """ |
| 291 | domain_tld = tld or rand_string(3) |
| 292 | return rand_string(4) + '.' + rand_string(6) + '.' + domain_tld + '.' |