blob: ae2b1a7ec68cc1cd79584fd658c1f4800d496f22 [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
15
16import netaddr
Paul Glass89edc112016-04-21 17:06:33 +000017from oslo_log import log as logging
Paul Glassa3ab50c2016-05-11 16:37:18 -050018from tempest import config
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010019from tempest.lib.common.utils import data_utils
20
Paul Glass89edc112016-04-21 17:06:33 +000021LOG = logging.getLogger(__name__)
Paul Glassa3ab50c2016-05-11 16:37:18 -050022CONF = config.CONF
Paul Glass89edc112016-04-21 17:06:33 +000023
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010024
sonu.kumarde24d962016-05-05 00:28:00 +090025def rand_ip():
26 return ".".join(str(random.randrange(0, 256)) for _ in range(4))
27
28
29def 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.kumaraec952a2016-04-20 10:08:46 +090037def rand_zone_name(name='', prefix=None, suffix='.com.'):
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010038 """Generate a random zone name
sonu.kumaraec952a2016-04-20 10:08:46 +090039 :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 Innes25fb29e2016-04-07 08:07:04 +010042 :return: a random zone name e.g. example.org.
43 :rtype: string
44 """
sonu.kumaraec952a2016-04-20 10:08:46 +090045 name = data_utils.rand_name(name=name, prefix=prefix)
46 return name + suffix
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010047
48
49def 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
58def 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 Glassa3ab50c2016-05-11 16:37:18 -050063 start = max(start, CONF.dns.min_ttl)
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010064 return data_utils.rand_int_id(start, end)
sonu.kumaraec952a2016-04-20 10:08:46 +090065
66
67def 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 Glassa3ab50c2016-05-11 16:37:18 -050077 ttl = rand_ttl()
sonu.kumaraec952a2016-04-20 10:08:46 +090078
Paul Glassa3ab50c2016-05-11 16:37:18 -050079 return zone_base.replace('&', name).replace('#', str(ttl))
Paul Glass89edc112016-04-21 17:06:33 +000080
81
82def rand_quotas(zones=None, zone_records=None, zone_recordsets=None,
83 recordset_records=None, api_export_size=None):
84 LOG.warn("Leaving `api_export_size` out of quota data due to: "
85 "https://bugs.launchpad.net/designate/+bug/1573141")
86 return {
87 'quota': {
88 'zones':
89 zones or data_utils.rand_int_id(100, 999999),
90 'zone_records':
91 zone_records or data_utils.rand_int_id(100, 999999),
92 'zone_recordsets':
93 zone_recordsets or data_utils.rand_int_id(100, 999999),
94 'recordset_records':
95 recordset_records or data_utils.rand_int_id(100, 999999),
96 # https://bugs.launchpad.net/designate/+bug/1573141
97 # 'api_export_size':
98 # api_export_size or data_utils.rand_int_id(100, 999999),
99 }
100 }
sonu.kumarde24d962016-05-05 00:28:00 +0900101
102
103def rand_zone_data(name=None, email=None, ttl=None, description=None):
104 """Generate random zone data, with optional overrides
105
106 :return: A ZoneModel
107 """
108 if name is None:
109 name = rand_zone_name(prefix='testdomain', suffix='.com.')
110 if email is None:
111 email = ("admin@" + name).strip('.')
112 if description is None:
113 description = rand_zone_name(prefix='Description ', suffix='')
114 if ttl is None:
Paul Glassa3ab50c2016-05-11 16:37:18 -0500115 ttl = rand_ttl()
sonu.kumarde24d962016-05-05 00:28:00 +0900116 return {
117 'name': name,
118 'email': email,
Paul Glassa3ab50c2016-05-11 16:37:18 -0500119 'ttl': ttl,
sonu.kumarde24d962016-05-05 00:28:00 +0900120 'description': description}
121
122
123def rand_recordset_data(record_type, zone_name, name=None, records=None,
124 ttl=None):
125 """Generate random recordset data, with optional overrides
126
127 :return: A RecordsetModel
128 """
129 if name is None:
130 name = rand_zone_name(prefix=record_type, suffix='.' + zone_name)
131 if records is None:
132 records = [rand_ip()]
133 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
142def rand_a_recordset(zone_name, ip=None, **kwargs):
143 if ip is None:
144 ip = rand_ip()
145 return rand_recordset_data('A', zone_name, records=[ip], **kwargs)
146
147
148def rand_aaaa_recordset(zone_name, ip=None, **kwargs):
149 if ip is None:
150 ip = rand_ipv6()
151 return rand_recordset_data('AAAA', zone_name, records=[ip], **kwargs)
152
153
154def rand_cname_recordset(zone_name, cname=None, **kwargs):
155 if cname is None:
156 cname = zone_name
157 return rand_recordset_data('CNAME', zone_name, records=[cname], **kwargs)
158
159
160def rand_mx_recordset(zone_name, pref=None, host=None, **kwargs):
161 if pref is None:
162 pref = str(random.randint(0, 65535))
163 if host is None:
164 host = rand_zone_name(prefix='mail', suffix='.' + zone_name)
165 data = "{0} {1}".format(pref, host)
166 return rand_recordset_data('MX', zone_name, records=[data], **kwargs)
167
168
169def rand_spf_recordset(zone_name, data=None, **kwargs):
170 data = data or "v=spf1 +all"
171 return rand_recordset_data('SPF', zone_name, records=[data], **kwargs)
172
173
174def rand_srv_recordset(zone_name, data=None):
175 data = data or "10 0 8080 %s.%s" % (rand_zone_name(suffix=''), zone_name)
176 return rand_recordset_data('SRV', zone_name,
177 name="_sip._tcp.%s" % zone_name,
178 records=[data])
179
180
181def rand_sshfp_recordset(zone_name, algorithm_number=None,
182 fingerprint_type=None, fingerprint=None,
183 **kwargs):
184 algorithm_number = algorithm_number or 2
185 fingerprint_type = fingerprint_type or 1
186 fingerprint = fingerprint or \
187 "123456789abcdef67890123456789abcdef67890"
188
189 data = "%s %s %s" % (algorithm_number, fingerprint_type, fingerprint)
190 return rand_recordset_data('SSHFP', zone_name, records=[data], **kwargs)
191
192
193def rand_txt_recordset(zone_name, data=None, **kwargs):
194 data = data or "v=spf1 +all"
195 return rand_recordset_data('TXT', zone_name, records=[data], **kwargs)
196
197
198def wildcard_ns_recordset(zone_name):
199 name = "*.{0}".format(zone_name)
200 records = ["ns.example.com."]
201 return rand_recordset_data('NS', zone_name, name, records)
sonu.kumar4beb93c2016-05-05 01:12:43 +0900202
203
204def rand_ns_records():
205 ns_zone = rand_zone_name()
206 records = []
207 for i in range(0, 2):
208 records.append("ns%s.%s" % (i, ns_zone))
209 ns_records = [{"hostname": x, "priority": random.randint(1, 999)}
210 for x in records]
211 return ns_records
sonu.kumar2de01be2016-05-05 10:07:28 +0900212
213
214def rand_tld():
215 data = {
216 "name": rand_zone_name(prefix='tld', suffix='')
217 }
218 return data
sonu.kumare9785c92016-05-17 10:56:47 +0900219
220
221def rand_transfer_request_data(description=None, target_project_id=None):
222 """Generate random transfer request data, with optional overrides
223
224 :return: A TransferRequest data
225 """
226
227 data = {}
228
229 if description is None:
230 data['description'] = data_utils.rand_name(prefix='Description ')
231
232 if target_project_id:
233 data['target_project_id'] = target_project_id
234
235 return data