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 |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 18 | from tempest.lib.common.utils import data_utils |
| 19 | |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 20 | LOG = logging.getLogger(__name__) |
| 21 | |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 22 | |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 23 | def rand_ip(): |
| 24 | return ".".join(str(random.randrange(0, 256)) for _ in range(4)) |
| 25 | |
| 26 | |
| 27 | def rand_ipv6(): |
| 28 | def hexes(n): |
| 29 | return "".join(random.choice("1234567890abcdef") for _ in range(n)) |
| 30 | result = ":".join(hexes(4) for _ in range(8)) |
| 31 | an = netaddr.IPAddress(result, version=6) |
| 32 | return an.format(netaddr.ipv6_compact) |
| 33 | |
| 34 | |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 35 | def rand_zone_name(name='', prefix=None, suffix='.com.'): |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 36 | """Generate a random zone name |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 37 | :param str name: The name that you want to include |
| 38 | :param prefix: the exact text to start the string. Defaults to "rand" |
| 39 | :param suffix: the exact text to end the string |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 40 | :return: a random zone name e.g. example.org. |
| 41 | :rtype: string |
| 42 | """ |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 43 | name = data_utils.rand_name(name=name, prefix=prefix) |
| 44 | return name + suffix |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def rand_email(domain=None): |
| 48 | """Generate a random zone name |
| 49 | :return: a random zone name e.g. example.org. |
| 50 | :rtype: string |
| 51 | """ |
| 52 | domain = domain or rand_zone_name() |
| 53 | return 'example@%s' % domain.rstrip('.') |
| 54 | |
| 55 | |
| 56 | def rand_ttl(start=1, end=86400): |
| 57 | """Generate a random TTL value |
| 58 | :return: a random ttl e.g. 165 |
| 59 | :rtype: string |
| 60 | """ |
| 61 | return data_utils.rand_int_id(start, end) |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def rand_zonefile_data(name=None, ttl=None): |
| 65 | """Generate random zone data, with optional overrides |
| 66 | |
| 67 | :return: A ZoneModel |
| 68 | """ |
| 69 | zone_base = ('$ORIGIN &\n& # IN SOA ns.& nsadmin.& # # # # #\n' |
| 70 | '& # IN NS ns.&\n& # IN MX 10 mail.&\nns.& 360 IN A 1.0.0.1') |
| 71 | if name is None: |
| 72 | name = rand_zone_name() |
| 73 | if ttl is None: |
| 74 | ttl = str(rand_ttl(1200, 8400)) |
| 75 | |
| 76 | return zone_base.replace('&', name).replace('#', ttl) |
Paul Glass | 89edc11 | 2016-04-21 17:06:33 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def rand_quotas(zones=None, zone_records=None, zone_recordsets=None, |
| 80 | recordset_records=None, api_export_size=None): |
| 81 | LOG.warn("Leaving `api_export_size` out of quota data due to: " |
| 82 | "https://bugs.launchpad.net/designate/+bug/1573141") |
| 83 | return { |
| 84 | 'quota': { |
| 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), |
| 93 | # https://bugs.launchpad.net/designate/+bug/1573141 |
| 94 | # 'api_export_size': |
| 95 | # api_export_size or data_utils.rand_int_id(100, 999999), |
| 96 | } |
| 97 | } |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def rand_zone_data(name=None, email=None, ttl=None, description=None): |
| 101 | """Generate random zone data, with optional overrides |
| 102 | |
| 103 | :return: A ZoneModel |
| 104 | """ |
| 105 | if name is None: |
| 106 | name = rand_zone_name(prefix='testdomain', suffix='.com.') |
| 107 | if email is None: |
| 108 | email = ("admin@" + name).strip('.') |
| 109 | if description is None: |
| 110 | description = rand_zone_name(prefix='Description ', suffix='') |
| 111 | if ttl is None: |
| 112 | ttl = random.randint(1200, 8400), |
| 113 | return { |
| 114 | 'name': name, |
| 115 | 'email': email, |
| 116 | 'ttl': random.randint(1200, 8400), |
| 117 | 'description': description} |
| 118 | |
| 119 | |
| 120 | def rand_recordset_data(record_type, zone_name, name=None, records=None, |
| 121 | ttl=None): |
| 122 | """Generate random recordset data, with optional overrides |
| 123 | |
| 124 | :return: A RecordsetModel |
| 125 | """ |
| 126 | if name is None: |
| 127 | name = rand_zone_name(prefix=record_type, suffix='.' + zone_name) |
| 128 | if records is None: |
| 129 | records = [rand_ip()] |
| 130 | if ttl is None: |
| 131 | ttl = random.randint(1200, 8400) |
| 132 | return { |
| 133 | 'type': record_type, |
| 134 | 'name': name, |
| 135 | 'records': records, |
| 136 | 'ttl': ttl} |
| 137 | |
| 138 | |
| 139 | def rand_a_recordset(zone_name, ip=None, **kwargs): |
| 140 | if ip is None: |
| 141 | ip = rand_ip() |
| 142 | return rand_recordset_data('A', zone_name, records=[ip], **kwargs) |
| 143 | |
| 144 | |
| 145 | def rand_aaaa_recordset(zone_name, ip=None, **kwargs): |
| 146 | if ip is None: |
| 147 | ip = rand_ipv6() |
| 148 | return rand_recordset_data('AAAA', zone_name, records=[ip], **kwargs) |
| 149 | |
| 150 | |
| 151 | def rand_cname_recordset(zone_name, cname=None, **kwargs): |
| 152 | if cname is None: |
| 153 | cname = zone_name |
| 154 | return rand_recordset_data('CNAME', zone_name, records=[cname], **kwargs) |
| 155 | |
| 156 | |
| 157 | def rand_mx_recordset(zone_name, pref=None, host=None, **kwargs): |
| 158 | if pref is None: |
| 159 | pref = str(random.randint(0, 65535)) |
| 160 | if host is None: |
| 161 | host = rand_zone_name(prefix='mail', suffix='.' + zone_name) |
| 162 | data = "{0} {1}".format(pref, host) |
| 163 | return rand_recordset_data('MX', zone_name, records=[data], **kwargs) |
| 164 | |
| 165 | |
| 166 | def rand_spf_recordset(zone_name, data=None, **kwargs): |
| 167 | data = data or "v=spf1 +all" |
| 168 | return rand_recordset_data('SPF', zone_name, records=[data], **kwargs) |
| 169 | |
| 170 | |
| 171 | def rand_srv_recordset(zone_name, data=None): |
| 172 | data = data or "10 0 8080 %s.%s" % (rand_zone_name(suffix=''), zone_name) |
| 173 | return rand_recordset_data('SRV', zone_name, |
| 174 | name="_sip._tcp.%s" % zone_name, |
| 175 | records=[data]) |
| 176 | |
| 177 | |
| 178 | def rand_sshfp_recordset(zone_name, algorithm_number=None, |
| 179 | fingerprint_type=None, fingerprint=None, |
| 180 | **kwargs): |
| 181 | algorithm_number = algorithm_number or 2 |
| 182 | fingerprint_type = fingerprint_type or 1 |
| 183 | fingerprint = fingerprint or \ |
| 184 | "123456789abcdef67890123456789abcdef67890" |
| 185 | |
| 186 | data = "%s %s %s" % (algorithm_number, fingerprint_type, fingerprint) |
| 187 | return rand_recordset_data('SSHFP', zone_name, records=[data], **kwargs) |
| 188 | |
| 189 | |
| 190 | def rand_txt_recordset(zone_name, data=None, **kwargs): |
| 191 | data = data or "v=spf1 +all" |
| 192 | return rand_recordset_data('TXT', zone_name, records=[data], **kwargs) |
| 193 | |
| 194 | |
| 195 | def wildcard_ns_recordset(zone_name): |
| 196 | name = "*.{0}".format(zone_name) |
| 197 | records = ["ns.example.com."] |
| 198 | return rand_recordset_data('NS', zone_name, name, records) |
sonu.kumar | 4beb93c | 2016-05-05 01:12:43 +0900 | [diff] [blame^] | 199 | |
| 200 | |
| 201 | def rand_ns_records(): |
| 202 | ns_zone = rand_zone_name() |
| 203 | records = [] |
| 204 | for i in range(0, 2): |
| 205 | records.append("ns%s.%s" % (i, ns_zone)) |
| 206 | ns_records = [{"hostname": x, "priority": random.randint(1, 999)} |
| 207 | for x in records] |
| 208 | return ns_records |