2 from .imports
import * # noqa
5 def populate_data(data_type
, original
=True, test_value
={}, keys
={}):
6 """Generate data from schema depends its Data-type
8 data_type (string): data_type from the test IP json
9 original (boolean): if it is True,
10 will generate normal JSON with randon
12 test_value (dict): will be like this {'string': '-1'}, means, if
13 string data typr comes, data will be -1
14 keys (dict): if this is present, value testing for this particular
17 string_ (string): string value
21 if (isinstance(data_type
, dict)):
22 if 'enumeration' in data_type
:
23 string_
= list(data_type
['enumeration']['enum'])[0]
25 if 'leafref' in data_type
:
27 if 'union' in data_type
:
30 if data_type
== 'string':
31 string_
= ''.join(choice(ascii_lowercase
) for i
in range(12))
32 elif data_type
== 'uint64':
33 string_
= uuid
.uuid4().int & (1 << 64) - 1
34 elif data_type
== 'uint8':
35 string_
= uuid
.uuid4().int & (1 << 8) - 1
36 elif data_type
== 'uint32':
37 string_
= uuid
.uuid4().int & (1 << 32) - 1
38 elif data_type
== 'uint16':
39 string_
= uuid
.uuid4().int & (1 << 16) - 1
40 elif data_type
== 'decimal64':
41 string_
= float(decimal
.Decimal('%d.%d'
42 % (random
.randint(0, 2134342),
43 random
.randint(0, 999))))
44 elif data_type
== 'int64':
45 string_
= random
.randint(0, 1000000000000)
46 elif data_type
== 'int32':
47 string_
= random
.randint(0, 1000000000)
48 elif data_type
== 'int16':
49 string_
= random
.randint(0, 10000)
50 elif data_type
== 'leafref':
51 string_
= 'leafref_data-type'
52 elif data_type
== 'union':
53 string_
= socket
.inet_ntoa(
54 struct
.pack('>I', random
.randint(1, 0xffffffff)))
55 elif data_type
== 'boolean':
62 if (isinstance(data_type
, dict)):
63 if 'enumeration' in data_type
:
64 string_
= list(data_type
['enumeration']['enum'])[0]
66 if 'leafref' in data_type
:
68 if 'union' in data_type
:
71 # print(data_type, test_value)
72 if not (isinstance(data_type
, dict)):
73 if keys
and keys
[list(keys
)[0]]:
74 if list(keys
.values())[0] in keys
:
75 if data_type
in test_value
:
76 return test_value
[data_type
]
78 if data_type
in test_value
:
79 return test_value
[data_type
]
81 if data_type
== 'string':
82 string_
= ''.join(choice(ascii_lowercase
) for i
in range(12))
83 elif data_type
== 'uint64':
84 string_
= uuid
.uuid4().int & (1 << 64) - 1
85 elif data_type
== 'uint8':
86 string_
= uuid
.uuid4().int & (1 << 8) - 1
87 elif data_type
== 'uint32':
88 string_
= uuid
.uuid4().int & (1 << 32) - 1
89 elif data_type
== 'uint16':
90 string_
= uuid
.uuid4().int & (1 << 16) - 1
91 elif data_type
== 'decimal64':
92 string_
= float(decimal
.Decimal('%d.%d'
93 % (random
.randint(0, 99999999),
94 random
.randint(0, 999))))
95 elif data_type
== 'int64':
96 string_
= random
.randint(0, 99999999)
97 elif data_type
== 'int32':
98 string_
= random
.randint(0, 999999)
99 elif data_type
== 'int16':
100 string_
= random
.randint(0, 999999)
101 elif data_type
== 'leafref':
102 string_
= 'leafref_data-type'
103 elif data_type
== 'union':
104 string_
= socket
.inet_ntoa(
105 struct
.pack('>I', random
.randint(1, 0xffffffff)))
106 elif data_type
== 'boolean':
114 def traverse_it(it
, path
='', data_json
={}, original
=True, test_value
={},
115 test_key
=None, avoid
=[], depth
=0, max_depth
=0):
116 """Main recursicve traversel method, which will go through the schema
117 and generate data JSON
121 data_json (dict): used to generate the data for particular key which is
123 original (boolean): used to generate original(complete) data JSON
124 test_value (dict): data type and the corresponding value which is
125 getting replaced generated
126 test_key (string): the key which is gonna get tested
127 avoid (list): these keys will get avoided while JSON is getting
129 depth (int): depth of the JSON
130 max_depth (int: will be the max depth of the JSON)
136 if (isinstance(it
, list)):
139 if depth
== max_depth
:
144 x
= traverse_it(item
, path
=path
, data_json
=data_json
,
146 test_value
=test_value
, test_key
=test_key
,
152 elif (isinstance(it
, dict)):
153 if 'name' in it
.keys():
154 if it
['name'] == 'disabled':
155 temp
= [{it
['name']: ''}, {}]
156 return random
.choice(temp
)
157 path
= path
+ '/' + it
['name']
158 if 'type' in it
.keys():
160 if it
['type'] == 'container':
162 if depth
== max_depth
:
165 it
['name']: traverse_it(it
['properties'],
166 path
=path
, data_json
=data_json
,
168 test_value
=test_value
,
175 elif it
['type'] == 'list':
176 for item_check
in it
['properties']:
178 if 'data-type' in item_check
:
179 if (isinstance(item_check
['data-type'], dict)):
180 if 'leafref' in item_check
['data-type']:
181 temp
= {it
['name']: []}
185 if depth
== max_depth
:
190 [traverse_it(it
['properties'], path
=path
,
193 test_value
=test_value
, test_key
=test_key
,
196 max_depth
=max_depth
)]
199 elif it
['type'] == 'case':
200 for item_check
in it
['properties']:
201 if 'data-type' in item_check
:
202 if (isinstance(item_check
['data-type'], dict)):
203 if 'leafref' in item_check
['data-type']:
206 if depth
== max_depth
:
209 return traverse_it(it
['properties'][0], path
=path
,
212 test_value
=test_value
, test_key
=test_key
,
216 elif it
['type'] == 'choice':
219 if depth
== max_depth
:
222 return traverse_it(it
['properties'][0], path
=path
,
225 test_value
=test_value
, test_key
=test_key
,
229 elif it
['type'] == 'leaf':
231 if it
['name'] in avoid
:
233 if 'data-type' in it
:
234 if 'subnet-address' == it
['name']:
235 data
= '255.255.255.0/24'
236 elif 'numa-unaware' == it
['name']:
238 elif 'ephemeral' == it
['name']:
241 data
= populate_data(it
['data-type'],
243 test_value
=test_value
,
244 keys
={it
['name']: test_key
})
245 return {it
['name']: data
}
247 if 'subnet-address' == it
['name']:
248 data
= '255.255.255.0/24'
249 elif 'numa-unaware' == it
['name']:
251 elif 'ephemeral' == it
['name']:
254 data
= populate_data(it
['data-type'],
256 test_value
=test_value
,
257 keys
={it
['name']: test_key
})
258 return {it
['name']: data
}
261 print('Error in the JSON!')