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 |
| 15 | |
| 16 | import netaddr |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 17 | from oslo_log import log as logging |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 18 | from tempest import config |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 19 | from tempest.lib.common.utils import data_utils |
| 20 | |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 21 | LOG = logging.getLogger(__name__) |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 22 | CONF = config.CONF |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 23 | |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 24 | |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 25 | def rand_ip(): |
| 26 | return ".".join(str(random.randrange(0, 256)) for _ in range(4)) |
| 27 | |
| 28 | |
| 29 | def rand_ipv6(): |
| 30 | def hexes(n): |
| 31 | return "".join(random.choice("1234567890abcdef") for _ in range(n)) |
| 32 | result = ":".join(hexes(4) for _ in range(8)) |
| 33 | an = netaddr.IPAddress(result, version=6) |
| 34 | return an.format(netaddr.ipv6_compact) |
| 35 | |
| 36 | |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 37 | def rand_zone_name(name='', prefix=None, suffix='.com.'): |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 38 | """Generate a random zone name |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 39 | :param str name: The name that you want to include |
| 40 | :param prefix: the exact text to start the string. Defaults to "rand" |
| 41 | :param suffix: the exact text to end the string |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 42 | :return: a random zone name e.g. example.org. |
| 43 | :rtype: string |
| 44 | """ |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 45 | name = data_utils.rand_name(name=name, prefix=prefix) |
| 46 | return name + suffix |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def rand_email(domain=None): |
| 50 | """Generate a random zone name |
| 51 | :return: a random zone name e.g. example.org. |
| 52 | :rtype: string |
| 53 | """ |
| 54 | domain = domain or rand_zone_name() |
| 55 | return 'example@%s' % domain.rstrip('.') |
| 56 | |
| 57 | |
| 58 | def rand_ttl(start=1, end=86400): |
| 59 | """Generate a random TTL value |
| 60 | :return: a random ttl e.g. 165 |
| 61 | :rtype: string |
| 62 | """ |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 63 | start = max(start, CONF.dns.min_ttl) |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 64 | return data_utils.rand_int_id(start, end) |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def rand_zonefile_data(name=None, ttl=None): |
| 68 | """Generate random zone data, with optional overrides |
| 69 | |
| 70 | :return: A ZoneModel |
| 71 | """ |
| 72 | zone_base = ('$ORIGIN &\n& # IN SOA ns.& nsadmin.& # # # # #\n' |
| 73 | '& # IN NS ns.&\n& # IN MX 10 mail.&\nns.& 360 IN A 1.0.0.1') |
| 74 | if name is None: |
| 75 | name = rand_zone_name() |
| 76 | if ttl is None: |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 77 | ttl = rand_ttl() |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 78 | |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 79 | return zone_base.replace('&', name).replace('#', str(ttl)) |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def rand_quotas(zones=None, zone_records=None, zone_recordsets=None, |
| 83 | recordset_records=None, api_export_size=None): |
Graham Hayes | c811498 | 2016-07-04 17:46:08 +0100 | [diff] [blame] | 84 | quotas_dict = { |
| 85 | 'zones': |
| 86 | zones or data_utils.rand_int_id(100, 999999), |
| 87 | 'zone_records': |
| 88 | zone_records or data_utils.rand_int_id(100, 999999), |
| 89 | 'zone_recordsets': |
| 90 | zone_recordsets or data_utils.rand_int_id(100, 999999), |
| 91 | 'recordset_records': |
| 92 | recordset_records or data_utils.rand_int_id(100, 999999), |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 93 | } |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 94 | |
Graham Hayes | c811498 | 2016-07-04 17:46:08 +0100 | [diff] [blame] | 95 | if CONF.dns_feature_enabled.bug_1573141_fixed: |
| 96 | quotas_dict['api_export_size'] = \ |
| 97 | api_export_size or data_utils.rand_int_id(100, 999999) |
| 98 | else: |
| 99 | LOG.warn("Leaving `api_export_size` out of quota data due to: " |
| 100 | "https://bugs.launchpad.net/designate/+bug/1573141") |
| 101 | |
| 102 | return quotas_dict |
| 103 | |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 104 | |
| 105 | def rand_zone_data(name=None, email=None, ttl=None, description=None): |
| 106 | """Generate random zone data, with optional overrides |
| 107 | |
| 108 | :return: A ZoneModel |
| 109 | """ |
| 110 | if name is None: |
| 111 | name = rand_zone_name(prefix='testdomain', suffix='.com.') |
| 112 | if email is None: |
| 113 | email = ("admin@" + name).strip('.') |
| 114 | if description is None: |
| 115 | description = rand_zone_name(prefix='Description ', suffix='') |
| 116 | if ttl is None: |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 117 | ttl = rand_ttl() |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 118 | return { |
| 119 | 'name': name, |
| 120 | 'email': email, |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 121 | 'ttl': ttl, |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 122 | 'description': description} |
| 123 | |
| 124 | |
| 125 | def rand_recordset_data(record_type, zone_name, name=None, records=None, |
| 126 | ttl=None): |
| 127 | """Generate random recordset data, with optional overrides |
| 128 | |
| 129 | :return: A RecordsetModel |
| 130 | """ |
| 131 | if name is None: |
| 132 | name = rand_zone_name(prefix=record_type, suffix='.' + zone_name) |
| 133 | if records is None: |
| 134 | records = [rand_ip()] |
| 135 | if ttl is None: |
Paul Glass | a3ab50c | 2016-05-11 16:37:18 -0500 | [diff] [blame] | 136 | ttl = rand_ttl() |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 137 | return { |
| 138 | 'type': record_type, |
| 139 | 'name': name, |
| 140 | 'records': records, |
| 141 | 'ttl': ttl} |
| 142 | |
| 143 | |
| 144 | def rand_a_recordset(zone_name, ip=None, **kwargs): |
| 145 | if ip is None: |
| 146 | ip = rand_ip() |
| 147 | return rand_recordset_data('A', zone_name, records=[ip], **kwargs) |
| 148 | |
| 149 | |
| 150 | def rand_aaaa_recordset(zone_name, ip=None, **kwargs): |
| 151 | if ip is None: |
| 152 | ip = rand_ipv6() |
| 153 | return rand_recordset_data('AAAA', zone_name, records=[ip], **kwargs) |
| 154 | |
| 155 | |
| 156 | def rand_cname_recordset(zone_name, cname=None, **kwargs): |
| 157 | if cname is None: |
| 158 | cname = zone_name |
| 159 | return rand_recordset_data('CNAME', zone_name, records=[cname], **kwargs) |
| 160 | |
| 161 | |
| 162 | def rand_mx_recordset(zone_name, pref=None, host=None, **kwargs): |
| 163 | if pref is None: |
| 164 | pref = str(random.randint(0, 65535)) |
| 165 | if host is None: |
| 166 | host = rand_zone_name(prefix='mail', suffix='.' + zone_name) |
| 167 | data = "{0} {1}".format(pref, host) |
| 168 | return rand_recordset_data('MX', zone_name, records=[data], **kwargs) |
| 169 | |
| 170 | |
| 171 | def rand_spf_recordset(zone_name, data=None, **kwargs): |
| 172 | data = data or "v=spf1 +all" |
| 173 | return rand_recordset_data('SPF', zone_name, records=[data], **kwargs) |
| 174 | |
| 175 | |
| 176 | def rand_srv_recordset(zone_name, data=None): |
| 177 | data = data or "10 0 8080 %s.%s" % (rand_zone_name(suffix=''), zone_name) |
| 178 | return rand_recordset_data('SRV', zone_name, |
| 179 | name="_sip._tcp.%s" % zone_name, |
| 180 | records=[data]) |
| 181 | |
| 182 | |
| 183 | def rand_sshfp_recordset(zone_name, algorithm_number=None, |
| 184 | fingerprint_type=None, fingerprint=None, |
| 185 | **kwargs): |
| 186 | algorithm_number = algorithm_number or 2 |
| 187 | fingerprint_type = fingerprint_type or 1 |
| 188 | fingerprint = fingerprint or \ |
| 189 | "123456789abcdef67890123456789abcdef67890" |
| 190 | |
| 191 | data = "%s %s %s" % (algorithm_number, fingerprint_type, fingerprint) |
| 192 | return rand_recordset_data('SSHFP', zone_name, records=[data], **kwargs) |
| 193 | |
| 194 | |
| 195 | def rand_txt_recordset(zone_name, data=None, **kwargs): |
| 196 | data = data or "v=spf1 +all" |
| 197 | return rand_recordset_data('TXT', zone_name, records=[data], **kwargs) |
| 198 | |
| 199 | |
| 200 | def wildcard_ns_recordset(zone_name): |
| 201 | name = "*.{0}".format(zone_name) |
| 202 | records = ["ns.example.com."] |
| 203 | return rand_recordset_data('NS', zone_name, name, records) |
sonu.kumar | 4beb93c | 2016-05-05 01:12:43 +0900 | [diff] [blame] | 204 | |
| 205 | |
| 206 | def rand_ns_records(): |
| 207 | ns_zone = rand_zone_name() |
| 208 | records = [] |
| 209 | for i in range(0, 2): |
| 210 | records.append("ns%s.%s" % (i, ns_zone)) |
| 211 | ns_records = [{"hostname": x, "priority": random.randint(1, 999)} |
| 212 | for x in records] |
| 213 | return ns_records |
sonu.kumar | 2de01be | 2016-05-05 10:07:28 +0900 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def rand_tld(): |
| 217 | data = { |
| 218 | "name": rand_zone_name(prefix='tld', suffix='') |
| 219 | } |
| 220 | return data |
sonu.kumar | e9785c9 | 2016-05-17 10:56:47 +0900 | [diff] [blame] | 221 | |
| 222 | |
| 223 | def rand_transfer_request_data(description=None, target_project_id=None): |
| 224 | """Generate random transfer request data, with optional overrides |
| 225 | |
| 226 | :return: A TransferRequest data |
| 227 | """ |
| 228 | |
| 229 | data = {} |
| 230 | |
| 231 | if description is None: |
| 232 | data['description'] = data_utils.rand_name(prefix='Description ') |
| 233 | |
| 234 | if target_project_id: |
| 235 | data['target_project_id'] = target_project_id |
| 236 | |
| 237 | return data |
sonu.kumar | c311086 | 2016-05-27 14:48:04 +0900 | [diff] [blame] | 238 | |
| 239 | |
| 240 | def rand_tsig_algorithm(): |
| 241 | algorithm = ['hmac-md5', 'hmac-sha1', 'hmac-sha224', 'hmac-sha256', |
| 242 | 'hmac-sha384', 'hmac-sha512'] |
| 243 | return random.choice(algorithm) |
| 244 | |
| 245 | |
| 246 | def rand_tsig_scope(): |
| 247 | scope = ["ZONE", "POOL"] |
| 248 | return random.choice(scope) |
Eric Larson | dc715e1 | 2016-08-25 13:21:35 +0100 | [diff] [blame^] | 249 | |
| 250 | |
| 251 | def make_rand_recordset(zone_name, record_type): |
| 252 | """Create a rand recordset by type |
| 253 | This essentially just dispatches to the relevant random recordset |
| 254 | creation functions. |
| 255 | |
| 256 | :param str zone_name: The zone name the recordset applies to |
| 257 | :param str record_type: The type of recordset (ie A, MX, NS, etc...) |
| 258 | """ |
| 259 | |
| 260 | func = globals()["rand_{}_recordset".format(record_type.lower())] |
| 261 | return func(zone_name) |