update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / ra / pytest / ns / restapitest / utils / traversal_engine.py
1
2 from .imports import * # noqa
3
4
5 def populate_data(data_type, original=True, test_value={}, keys={}):
6 """Generate data from schema depends its Data-type
7 Args:
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
11 values
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
15 key
16 Returns:
17 string_ (string): string value
18 """
19
20 if original:
21 if (isinstance(data_type, dict)):
22 if 'enumeration' in data_type:
23 string_ = list(data_type['enumeration']['enum'])[0]
24 return string_
25 if 'leafref' in data_type:
26 data_type = 'leafref'
27 if 'union' in data_type:
28 data_type = 'union'
29
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':
56 string_ = True
57 else:
58 string_ = data_type
59
60 return string_
61 else:
62 if (isinstance(data_type, dict)):
63 if 'enumeration' in data_type:
64 string_ = list(data_type['enumeration']['enum'])[0]
65 return string_
66 if 'leafref' in data_type:
67 data_type = 'leafref'
68 if 'union' in data_type:
69 data_type = 'union'
70
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]
77 else:
78 if data_type in test_value:
79 return test_value[data_type]
80
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':
107 string_ = True
108 else:
109 string_ = data_type
110
111 return string_
112
113
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
118
119 Args:
120 it (json): schema
121 data_json (dict): used to generate the data for particular key which is
122 present in this dict
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
128 created
129 depth (int): depth of the JSON
130 max_depth (int: will be the max depth of the JSON)
131
132 Returns:
133 Json data
134 """
135
136 if (isinstance(it, list)):
137 temp = {}
138 depth += 1
139 if depth == max_depth:
140 return []
141 for item in it:
142 # print(path)
143
144 x = traverse_it(item, path=path, data_json=data_json,
145 original=original,
146 test_value=test_value, test_key=test_key,
147 avoid=avoid,
148 depth=depth,
149 max_depth=max_depth)
150 temp.update(x)
151 return temp
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():
159
160 if it['type'] == 'container':
161 depth += 1
162 if depth == max_depth:
163 return {}
164 data_json = {
165 it['name']: traverse_it(it['properties'],
166 path=path, data_json=data_json,
167 original=original,
168 test_value=test_value,
169 test_key=test_key,
170 avoid=avoid,
171 depth=depth,
172 max_depth=max_depth)
173 }
174 return data_json
175 elif it['type'] == 'list':
176 for item_check in it['properties']:
177
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']: []}
182 return temp
183 depth += 1
184
185 if depth == max_depth:
186 return {}
187
188 temp = {
189 it['name']:
190 [traverse_it(it['properties'], path=path,
191 data_json=data_json,
192 original=original,
193 test_value=test_value, test_key=test_key,
194 avoid=avoid,
195 depth=depth,
196 max_depth=max_depth)]
197 }
198 return temp
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']:
204 return {}
205 depth += 1
206 if depth == max_depth:
207 return {}
208
209 return traverse_it(it['properties'][0], path=path,
210 data_json=data_json,
211 original=original,
212 test_value=test_value, test_key=test_key,
213 avoid=avoid,
214 depth=depth,
215 max_depth=max_depth)
216 elif it['type'] == 'choice':
217 depth += 1
218
219 if depth == max_depth:
220 return {}
221
222 return traverse_it(it['properties'][0], path=path,
223 data_json=data_json,
224 original=original,
225 test_value=test_value, test_key=test_key,
226 avoid=avoid,
227 depth=depth,
228 max_depth=max_depth)
229 elif it['type'] == 'leaf':
230 # print(data_json)
231 if it['name'] in avoid:
232 return {}
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']:
237 data = ''
238 elif 'ephemeral' == it['name']:
239 data = ''
240 else:
241 data = populate_data(it['data-type'],
242 original=original,
243 test_value=test_value,
244 keys={it['name']: test_key})
245 return {it['name']: data}
246 else:
247 if 'subnet-address' == it['name']:
248 data = '255.255.255.0/24'
249 elif 'numa-unaware' == it['name']:
250 data = ''
251 elif 'ephemeral' == it['name']:
252 data = ''
253 else:
254 data = populate_data(it['data-type'],
255 original=original,
256 test_value=test_value,
257 keys={it['name']: test_key})
258 return {it['name']: data}
259
260 else:
261 print('Error in the JSON!')
262 exit(1)