blob: 31989b760b9802db5fe5246ad8aeb685438fde8c [file] [log] [blame]
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +01001# 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.kumarde24d962016-05-05 00:28:00 +090014import random
Arkady Shtemplerb8ea4ca2021-06-04 08:55:28 +030015from string import ascii_lowercase
sonu.kumarde24d962016-05-05 00:28:00 +090016
17import netaddr
Paul Glass89edc112016-04-21 17:06:33 +000018from oslo_log import log as logging
Paul Glassa3ab50c2016-05-11 16:37:18 -050019from tempest import config
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010020from tempest.lib.common.utils import data_utils
21
Paul Glass89edc112016-04-21 17:06:33 +000022LOG = logging.getLogger(__name__)
Paul Glassa3ab50c2016-05-11 16:37:18 -050023CONF = config.CONF
Paul Glass89edc112016-04-21 17:06:33 +000024
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010025
sonu.kumarde24d962016-05-05 00:28:00 +090026def rand_ip():
27 return ".".join(str(random.randrange(0, 256)) for _ in range(4))
28
29
30def 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 Shtempler6d6b27d2022-04-18 15:25:00 +030038def rand_zone_name(name='', prefix='rand', suffix=None):
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010039 """Generate a random zone name
sonu.kumaraec952a2016-04-20 10:08:46 +090040 :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 Innes25fb29e2016-04-07 08:07:04 +010043 :return: a random zone name e.g. example.org.
44 :rtype: string
45 """
Arkady Shtempler6d6b27d2022-04-18 15:25:00 +030046 if suffix is None:
47 suffix = '.{}.'.format(CONF.dns.tld_suffix)
sonu.kumaraec952a2016-04-20 10:08:46 +090048 name = data_utils.rand_name(name=name, prefix=prefix)
49 return name + suffix
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010050
51
52def 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 Shtempler4054a222022-06-23 13:16:51 +030061def rand_ttl(start=0, end=86400):
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010062 """Generate a random TTL value
63 :return: a random ttl e.g. 165
64 :rtype: string
65 """
Paul Glassa3ab50c2016-05-11 16:37:18 -050066 start = max(start, CONF.dns.min_ttl)
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010067 return data_utils.rand_int_id(start, end)
sonu.kumaraec952a2016-04-20 10:08:46 +090068
69
70def rand_zonefile_data(name=None, ttl=None):
71 """Generate random zone data, with optional overrides
sonu.kumaraec952a2016-04-20 10:08:46 +090072 :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 Glassa3ab50c2016-05-11 16:37:18 -050079 ttl = rand_ttl()
sonu.kumaraec952a2016-04-20 10:08:46 +090080
Paul Glassa3ab50c2016-05-11 16:37:18 -050081 return zone_base.replace('&', name).replace('#', str(ttl))
Paul Glass89edc112016-04-21 17:06:33 +000082
83
84def rand_quotas(zones=None, zone_records=None, zone_recordsets=None,
85 recordset_records=None, api_export_size=None):
Graham Hayesc8114982016-07-04 17:46:08 +010086 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),
Takashi Kajinamid2890712025-06-09 01:49:27 +090095 'api_export_size':
96 api_export_size or data_utils.rand_int_id(100, 999999)
Paul Glass89edc112016-04-21 17:06:33 +000097 }
sonu.kumarde24d962016-05-05 00:28:00 +090098
Graham Hayesc8114982016-07-04 17:46:08 +010099 return quotas_dict
100
sonu.kumarde24d962016-05-05 00:28:00 +0900101
102def rand_zone_data(name=None, email=None, ttl=None, description=None):
103 """Generate random zone data, with optional overrides
sonu.kumarde24d962016-05-05 00:28:00 +0900104 :return: A ZoneModel
105 """
106 if name is None:
Arkady Shtempler6d6b27d2022-04-18 15:25:00 +0300107 name = rand_zone_name(
108 prefix='testdomain', suffix='.{}.'.format(CONF.dns.tld_suffix))
sonu.kumarde24d962016-05-05 00:28:00 +0900109 if email is None:
110 email = ("admin@" + name).strip('.')
111 if description is None:
112 description = rand_zone_name(prefix='Description ', suffix='')
113 if ttl is None:
Paul Glassa3ab50c2016-05-11 16:37:18 -0500114 ttl = rand_ttl()
sonu.kumarde24d962016-05-05 00:28:00 +0900115 return {
116 'name': name,
117 'email': email,
Paul Glassa3ab50c2016-05-11 16:37:18 -0500118 'ttl': ttl,
sonu.kumarde24d962016-05-05 00:28:00 +0900119 'description': description}
120
121
122def rand_recordset_data(record_type, zone_name, name=None, records=None,
Arkady Shtempler356c5ae2022-02-05 00:16:35 +0200123 ttl=None, number_of_records=None):
sonu.kumarde24d962016-05-05 00:28:00 +0900124 """Generate random recordset data, with optional overrides
sonu.kumarde24d962016-05-05 00:28:00 +0900125 :return: A RecordsetModel
126 """
127 if name is None:
128 name = rand_zone_name(prefix=record_type, suffix='.' + zone_name)
129 if records is None:
130 records = [rand_ip()]
Arkady Shtempler356c5ae2022-02-05 00:16:35 +0200131 if number_of_records:
132 records = [rand_ip() for r in range(number_of_records)]
sonu.kumarde24d962016-05-05 00:28:00 +0900133 if ttl is None:
Paul Glassa3ab50c2016-05-11 16:37:18 -0500134 ttl = rand_ttl()
sonu.kumarde24d962016-05-05 00:28:00 +0900135 return {
136 'type': record_type,
137 'name': name,
138 'records': records,
139 'ttl': ttl}
140
141
zahlabut52602272021-08-04 17:07:29 +0300142def rand_a_recordset(zone_name, ips=None, **kwargs):
143 if ips is None:
144 return rand_recordset_data(
145 'A', zone_name, records=[rand_ip()], **kwargs)
146 else:
147 return rand_recordset_data('A', zone_name, records=ips, **kwargs)
sonu.kumarde24d962016-05-05 00:28:00 +0900148
149
150def 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
156def 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
162def 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
171def rand_spf_recordset(zone_name, data=None, **kwargs):
Dmitry Galkin9a0a3602018-11-13 19:42:29 +0000172 data = data or '"v=spf1 +all"'
sonu.kumarde24d962016-05-05 00:28:00 +0900173 return rand_recordset_data('SPF', zone_name, records=[data], **kwargs)
174
175
176def 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
183def 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
Michael Johnsoncc8f89b2021-11-30 00:31:24 +0000188 fingerprint = fingerprint or "123456789abcdef67890123456789abcdef67890"
sonu.kumarde24d962016-05-05 00:28:00 +0900189
190 data = "%s %s %s" % (algorithm_number, fingerprint_type, fingerprint)
191 return rand_recordset_data('SSHFP', zone_name, records=[data], **kwargs)
192
193
194def rand_txt_recordset(zone_name, data=None, **kwargs):
Dmitry Galkin9a0a3602018-11-13 19:42:29 +0000195 data = data or '"v=spf1 +all"'
sonu.kumarde24d962016-05-05 00:28:00 +0900196 return rand_recordset_data('TXT', zone_name, records=[data], **kwargs)
197
198
199def wildcard_ns_recordset(zone_name):
200 name = "*.{0}".format(zone_name)
Arkady Shtempler6d6b27d2022-04-18 15:25:00 +0300201 records = ["ns.example.{}.".format(CONF.dns.tld_suffix)]
sonu.kumarde24d962016-05-05 00:28:00 +0900202 return rand_recordset_data('NS', zone_name, name, records)
sonu.kumar4beb93c2016-05-05 01:12:43 +0900203
204
205def rand_ns_records():
206 ns_zone = rand_zone_name()
Michael Johnsonc3adc642022-06-21 18:47:05 +0000207 ns_records = []
208 # Make sure we don't have equal priority here which causes test failures
209 # when doing sorted comparisons
sonu.kumar4beb93c2016-05-05 01:12:43 +0900210 for i in range(0, 2):
Michael Johnsonc3adc642022-06-21 18:47:05 +0000211 ns_records.append({"hostname": "ns%s.%s" % (i, ns_zone),
212 "priority": (random.randint(1, 999) + i)})
sonu.kumar4beb93c2016-05-05 01:12:43 +0900213 return ns_records
sonu.kumar2de01be2016-05-05 10:07:28 +0900214
215
Arkady Shtemplered18eaa2022-02-15 22:09:02 +0200216def rand_soa_records(number_of_records=2):
217 return ['{} {} {} {} {} {}.'.format(
218 '{}.{}.{}'.format(rand_string(3), rand_string(7), rand_string(3)),
219 random.randint(1000000000, 2020080302), random.randint(3000, 7200),
220 random.randint(1000, 3600), random.randint(1000000, 1209600),
221 random.randint(1000, 3600)) for i in range(0, number_of_records)]
222
223
224def rand_soa_recordset(zone_name, **kwargs):
225 return rand_recordset_data(
226 'SOA', zone_name, records=rand_soa_records(), **kwargs)
227
228
sonu.kumar2de01be2016-05-05 10:07:28 +0900229def rand_tld():
230 data = {
231 "name": rand_zone_name(prefix='tld', suffix='')
232 }
233 return data
sonu.kumare9785c92016-05-17 10:56:47 +0900234
235
236def rand_transfer_request_data(description=None, target_project_id=None):
237 """Generate random transfer request data, with optional overrides
sonu.kumare9785c92016-05-17 10:56:47 +0900238 :return: A TransferRequest data
239 """
240
241 data = {}
242
243 if description is None:
244 data['description'] = data_utils.rand_name(prefix='Description ')
245
246 if target_project_id:
247 data['target_project_id'] = target_project_id
248
249 return data
sonu.kumarc3110862016-05-27 14:48:04 +0900250
251
252def rand_tsig_algorithm():
253 algorithm = ['hmac-md5', 'hmac-sha1', 'hmac-sha224', 'hmac-sha256',
254 'hmac-sha384', 'hmac-sha512']
255 return random.choice(algorithm)
256
257
258def rand_tsig_scope():
259 scope = ["ZONE", "POOL"]
260 return random.choice(scope)
Eric Larsondc715e12016-08-25 13:21:35 +0100261
262
263def make_rand_recordset(zone_name, record_type):
264 """Create a rand recordset by type
265 This essentially just dispatches to the relevant random recordset
266 creation functions.
Eric Larsondc715e12016-08-25 13:21:35 +0100267 :param str zone_name: The zone name the recordset applies to
268 :param str record_type: The type of recordset (ie A, MX, NS, etc...)
269 """
270
271 func = globals()["rand_{}_recordset".format(record_type.lower())]
272 return func(zone_name)
Arkady Shtemplerb8ea4ca2021-06-04 08:55:28 +0300273
274
275def rand_string(size):
276 """Create random string of ASCII chars by size
Arkady Shtemplerb8ea4ca2021-06-04 08:55:28 +0300277 :param int size - length os the string to be create
278 :return - random creates string of ASCII lover characters
279 """
280 return ''.join(random.choice(ascii_lowercase) for _ in range(size))
281
282
283def rand_domain_name(tld=None):
284 """Create random valid domain name
Arkady Shtemplerb8ea4ca2021-06-04 08:55:28 +0300285 :param tld (optional) - TLD that will be used to random domain name
286 :return - valid domain name, for example: paka.zbabun.iuh
287 """
288 domain_tld = tld or rand_string(3)
289 return rand_string(4) + '.' + rand_string(6) + '.' + domain_tld + '.'