blob: 8389cfdada50ffc9a8e907eefe3e45946318898d [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 Shtemplerb8ea4ca2021-06-04 08:55:28 +030038def rand_zone_name(name='', prefix='rand', suffix='.com.'):
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 """
sonu.kumaraec952a2016-04-20 10:08:46 +090046 name = data_utils.rand_name(name=name, prefix=prefix)
47 return name + suffix
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010048
49
Arkady Shtempler5ed36ff2022-04-10 16:21:39 +030050def rand_dns_name_by_size(name_size, label_size=63):
51 """Generates label based DNS name, by given characters size
52 :param name_size: size in characters
53 :param label_size: the max number of characters to be used
54 for label. Max value according the RFC is 63
55 https://datatracker.ietf.org/doc/html/rfc1035#
56 section-2.3.4in
57 :return: DNS name
58 """
59 template = ''
60 while len(template) < name_size:
61 remaining_length = name_size - len(template)
62 template += '{}.'.format(rand_string(
63 min(remaining_length - 1, label_size)))
64 if template.endswith('..'):
65 raise Exception("There is no way to generate a valid DNS name "
66 "using provided set of values:{},{}, consider "
67 "changing those values".format(name_size, label_size))
68 return template
69
70
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010071def rand_email(domain=None):
72 """Generate a random zone name
73 :return: a random zone name e.g. example.org.
74 :rtype: string
75 """
76 domain = domain or rand_zone_name()
77 return 'example@%s' % domain.rstrip('.')
78
79
80def rand_ttl(start=1, end=86400):
81 """Generate a random TTL value
82 :return: a random ttl e.g. 165
83 :rtype: string
84 """
Paul Glassa3ab50c2016-05-11 16:37:18 -050085 start = max(start, CONF.dns.min_ttl)
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010086 return data_utils.rand_int_id(start, end)
sonu.kumaraec952a2016-04-20 10:08:46 +090087
88
89def rand_zonefile_data(name=None, ttl=None):
90 """Generate random zone data, with optional overrides
91
92 :return: A ZoneModel
93 """
94 zone_base = ('$ORIGIN &\n& # IN SOA ns.& nsadmin.& # # # # #\n'
95 '& # IN NS ns.&\n& # IN MX 10 mail.&\nns.& 360 IN A 1.0.0.1')
96 if name is None:
97 name = rand_zone_name()
98 if ttl is None:
Paul Glassa3ab50c2016-05-11 16:37:18 -050099 ttl = rand_ttl()
sonu.kumaraec952a2016-04-20 10:08:46 +0900100
Paul Glassa3ab50c2016-05-11 16:37:18 -0500101 return zone_base.replace('&', name).replace('#', str(ttl))
Paul Glass89edc112016-04-21 17:06:33 +0000102
103
104def rand_quotas(zones=None, zone_records=None, zone_recordsets=None,
105 recordset_records=None, api_export_size=None):
Graham Hayesc8114982016-07-04 17:46:08 +0100106 quotas_dict = {
107 'zones':
108 zones or data_utils.rand_int_id(100, 999999),
109 'zone_records':
110 zone_records or data_utils.rand_int_id(100, 999999),
111 'zone_recordsets':
112 zone_recordsets or data_utils.rand_int_id(100, 999999),
113 'recordset_records':
114 recordset_records or data_utils.rand_int_id(100, 999999),
Paul Glass89edc112016-04-21 17:06:33 +0000115 }
sonu.kumarde24d962016-05-05 00:28:00 +0900116
Graham Hayesc8114982016-07-04 17:46:08 +0100117 if CONF.dns_feature_enabled.bug_1573141_fixed:
118 quotas_dict['api_export_size'] = \
119 api_export_size or data_utils.rand_int_id(100, 999999)
120 else:
Takashi Kajinami299344d2021-11-29 15:56:14 +0900121 LOG.warning("Leaving `api_export_size` out of quota data due to: "
122 "https://bugs.launchpad.net/designate/+bug/1573141")
Graham Hayesc8114982016-07-04 17:46:08 +0100123
124 return quotas_dict
125
sonu.kumarde24d962016-05-05 00:28:00 +0900126
127def rand_zone_data(name=None, email=None, ttl=None, description=None):
128 """Generate random zone data, with optional overrides
129
130 :return: A ZoneModel
131 """
132 if name is None:
133 name = rand_zone_name(prefix='testdomain', suffix='.com.')
134 if email is None:
135 email = ("admin@" + name).strip('.')
136 if description is None:
137 description = rand_zone_name(prefix='Description ', suffix='')
138 if ttl is None:
Paul Glassa3ab50c2016-05-11 16:37:18 -0500139 ttl = rand_ttl()
sonu.kumarde24d962016-05-05 00:28:00 +0900140 return {
141 'name': name,
142 'email': email,
Paul Glassa3ab50c2016-05-11 16:37:18 -0500143 'ttl': ttl,
sonu.kumarde24d962016-05-05 00:28:00 +0900144 'description': description}
145
146
147def rand_recordset_data(record_type, zone_name, name=None, records=None,
148 ttl=None):
149 """Generate random recordset data, with optional overrides
150
151 :return: A RecordsetModel
152 """
153 if name is None:
154 name = rand_zone_name(prefix=record_type, suffix='.' + zone_name)
155 if records is None:
156 records = [rand_ip()]
157 if ttl is None:
Paul Glassa3ab50c2016-05-11 16:37:18 -0500158 ttl = rand_ttl()
sonu.kumarde24d962016-05-05 00:28:00 +0900159 return {
160 'type': record_type,
161 'name': name,
162 'records': records,
163 'ttl': ttl}
164
165
zahlabut52602272021-08-04 17:07:29 +0300166def rand_a_recordset(zone_name, ips=None, **kwargs):
167 if ips is None:
168 return rand_recordset_data(
169 'A', zone_name, records=[rand_ip()], **kwargs)
170 else:
171 return rand_recordset_data('A', zone_name, records=ips, **kwargs)
sonu.kumarde24d962016-05-05 00:28:00 +0900172
173
174def rand_aaaa_recordset(zone_name, ip=None, **kwargs):
175 if ip is None:
176 ip = rand_ipv6()
177 return rand_recordset_data('AAAA', zone_name, records=[ip], **kwargs)
178
179
180def rand_cname_recordset(zone_name, cname=None, **kwargs):
181 if cname is None:
182 cname = zone_name
183 return rand_recordset_data('CNAME', zone_name, records=[cname], **kwargs)
184
185
186def rand_mx_recordset(zone_name, pref=None, host=None, **kwargs):
187 if pref is None:
188 pref = str(random.randint(0, 65535))
189 if host is None:
190 host = rand_zone_name(prefix='mail', suffix='.' + zone_name)
191 data = "{0} {1}".format(pref, host)
192 return rand_recordset_data('MX', zone_name, records=[data], **kwargs)
193
194
195def rand_spf_recordset(zone_name, data=None, **kwargs):
Dmitry Galkin9a0a3602018-11-13 19:42:29 +0000196 data = data or '"v=spf1 +all"'
sonu.kumarde24d962016-05-05 00:28:00 +0900197 return rand_recordset_data('SPF', zone_name, records=[data], **kwargs)
198
199
200def rand_srv_recordset(zone_name, data=None):
201 data = data or "10 0 8080 %s.%s" % (rand_zone_name(suffix=''), zone_name)
202 return rand_recordset_data('SRV', zone_name,
203 name="_sip._tcp.%s" % zone_name,
204 records=[data])
205
206
207def rand_sshfp_recordset(zone_name, algorithm_number=None,
208 fingerprint_type=None, fingerprint=None,
209 **kwargs):
210 algorithm_number = algorithm_number or 2
211 fingerprint_type = fingerprint_type or 1
212 fingerprint = fingerprint or \
213 "123456789abcdef67890123456789abcdef67890"
214
215 data = "%s %s %s" % (algorithm_number, fingerprint_type, fingerprint)
216 return rand_recordset_data('SSHFP', zone_name, records=[data], **kwargs)
217
218
219def rand_txt_recordset(zone_name, data=None, **kwargs):
Dmitry Galkin9a0a3602018-11-13 19:42:29 +0000220 data = data or '"v=spf1 +all"'
sonu.kumarde24d962016-05-05 00:28:00 +0900221 return rand_recordset_data('TXT', zone_name, records=[data], **kwargs)
222
223
224def wildcard_ns_recordset(zone_name):
225 name = "*.{0}".format(zone_name)
226 records = ["ns.example.com."]
227 return rand_recordset_data('NS', zone_name, name, records)
sonu.kumar4beb93c2016-05-05 01:12:43 +0900228
229
230def rand_ns_records():
231 ns_zone = rand_zone_name()
232 records = []
233 for i in range(0, 2):
234 records.append("ns%s.%s" % (i, ns_zone))
235 ns_records = [{"hostname": x, "priority": random.randint(1, 999)}
236 for x in records]
237 return ns_records
sonu.kumar2de01be2016-05-05 10:07:28 +0900238
239
240def rand_tld():
241 data = {
242 "name": rand_zone_name(prefix='tld', suffix='')
243 }
244 return data
sonu.kumare9785c92016-05-17 10:56:47 +0900245
246
247def rand_transfer_request_data(description=None, target_project_id=None):
248 """Generate random transfer request data, with optional overrides
249
250 :return: A TransferRequest data
251 """
252
253 data = {}
254
255 if description is None:
256 data['description'] = data_utils.rand_name(prefix='Description ')
257
258 if target_project_id:
259 data['target_project_id'] = target_project_id
260
261 return data
sonu.kumarc3110862016-05-27 14:48:04 +0900262
263
264def rand_tsig_algorithm():
265 algorithm = ['hmac-md5', 'hmac-sha1', 'hmac-sha224', 'hmac-sha256',
266 'hmac-sha384', 'hmac-sha512']
267 return random.choice(algorithm)
268
269
270def rand_tsig_scope():
271 scope = ["ZONE", "POOL"]
272 return random.choice(scope)
Eric Larsondc715e12016-08-25 13:21:35 +0100273
274
275def make_rand_recordset(zone_name, record_type):
276 """Create a rand recordset by type
277 This essentially just dispatches to the relevant random recordset
278 creation functions.
279
280 :param str zone_name: The zone name the recordset applies to
281 :param str record_type: The type of recordset (ie A, MX, NS, etc...)
282 """
283
284 func = globals()["rand_{}_recordset".format(record_type.lower())]
285 return func(zone_name)
Arkady Shtemplerb8ea4ca2021-06-04 08:55:28 +0300286
287
288def rand_string(size):
289 """Create random string of ASCII chars by size
290
291 :param int size - length os the string to be create
292 :return - random creates string of ASCII lover characters
293 """
294 return ''.join(random.choice(ascii_lowercase) for _ in range(size))
295
296
297def rand_domain_name(tld=None):
298 """Create random valid domain name
299
300 :param tld (optional) - TLD that will be used to random domain name
301 :return - valid domain name, for example: paka.zbabun.iuh
302 """
303 domain_tld = tld or rand_string(3)
304 return rand_string(4) + '.' + rand_string(6) + '.' + domain_tld + '.'