blob: a27427492227049e8cbb8a47a33423901de49113 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070017import pexpect,os
18import time
19import json
20import collections
21import sys
22import robot
23import os.path
24from os.path import expanduser
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070025import uuid
Kailash Khalasi2adbad82017-05-15 14:53:40 -070026import random
27import re
Kailash Khalasi86e231e2017-06-06 13:13:43 -070028import yaml
You Wangaabb2832017-11-16 17:24:09 -080029import glob
Kailash Khalasi8416bfe2017-12-20 13:06:37 -080030import string
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070031
32class utils(object):
33
34 @staticmethod
35 def listToDict(alist, intListIndex):
36 dictInfo = alist[int(intListIndex)]
37 return dictInfo
38
39 @staticmethod
40 def jsonToList(strFile, strListName):
41 data = json.loads(open(strFile).read())
42 #print "data...",data
43 dataList = data[strListName]
44 return dataList
45
You Wangaabb2832017-11-16 17:24:09 -080046 def readFile(self, path, single=True):
47 dataDict = {}
48 for fileName in glob.glob(path):
49 print "Reading ", fileName
50 data = open(fileName).read()
51 dataDict[fileName] = data
52 if bool(single):
53 return data
54 return dataDict
55
56 def readFiles(self, path):
57 return self.readFile(path, single=False)
58
Suchitra.Vemuri85220062016-10-25 10:44:11 -070059 '''
60 @method compare_dict
61 @Description: validates if contents of dict1 exists in dict2
62 @params: dict1 = input_data entered through api
63 dict2 = retrieved data from GET method
64 returns True if contents of dict1 exists in dict2
65 '''
You Wang507c4562016-11-23 13:36:09 -080066 def compare_dict(self, dict1, dict2):
67 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070068 print "get data", dict2
69 if dict1 == None or dict2 == None:
70 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070071 if type(dict1) is not dict or type(dict2) is not dict:
72 return False
You Wang507c4562016-11-23 13:36:09 -080073 if dict1 == {}:
74 return True
75 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070076
You Wang507c4562016-11-23 13:36:09 -080077 '''
78 @method compare_dict_recursive
79 @Description: recursive function to validate if dict1 is a subset of dict2
80 returns True if contents of dict1 exists in dict2
81 '''
82 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070083 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080084 if key1 not in dict2.keys():
85 print "Missing key", key1, "in dict2"
86 return False
87 value2 = dict2[key1]
88 if type(value1) is dict and type(value2) is dict:
89 if not self.compare_dict_recursive(value1, value2):
90 return False
91 else:
92 if value2 != value1:
93 print "Values of key", key1, "in two dicts are not equal"
94 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070095 return True
96
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070097 '''
You Wang3964e842016-12-09 12:04:32 -080098 @method compare_list_of_dicts
99 @Description: validates if contents of dicts in list1 exists in dicts of list2
100 returns True if for each dict in list1, there's a dict in list2 that contains its content
101 '''
102 def compare_list_of_dicts(self, list1, list2):
103 for dict1 in list1:
104 if dict1 == {}:
105 continue
106 key = dict1.keys()[0]
107 value = dict1[key]
108 dict2 = self.getDictFromListOfDict(list2, key, value)
109 if dict2 == {}:
110 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
111 return False
112 if self.compare_dict(dict1, dict2) == False:
113 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
114 return False
115 return True
116
117 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700118 @method search_dictionary
119 @Description: Searches for a key in the provided nested dictionary
120 @params: input_dict = dictionary to be searched
121 search_key = name of the key to be searched for
122 returns two values: search_key value and status of the search.
123 True if found (False when not found)
124
125 '''
126 def search_dictionary(self,input_dict, search_key):
127 input_keys = input_dict.keys()
128 key_value = ''
129 found = False
130 for key in input_keys:
131 if key == search_key:
132 key_value = input_dict[key]
133 found = True
134 break
135 elif type(input_dict[key]) == dict:
136 key_value, found = self.search_dictionary(input_dict[key],search_key)
137 if found == True:
138 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800139 elif type(input_dict[key]) == list:
140 if not input_dict[key]:
141 found = False
142 break
143 for item in input_dict[key]:
144 if isinstance(item, dict):
145 key_value, found = self.search_dictionary(item, search_key)
146 if found == True:
147 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700148 return key_value,found
149 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800150 @method getDictFromListOfDict
151 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700152 @Description: Searches for the dictionary in the provided list of dictionaries
153 that matches the value of the key provided
154 @params : List of dictionaries(getResponse Data from the URL),
155 SearchKey - Key that needs to be searched for (ex: account_num)
156 searchKeyValue - Value of the searchKey (ex: 21)
157 @Returns: Dictionary returned when match found for searchKey with the corresponding
158 searchKeyValue provided
159 '''
160
161 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
162 return_dict = {}
163 result = ''
164 for data in getJsonDataList:
Suchitra Vemurif58de922017-06-14 12:53:42 -0700165 print "data", data
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700166 return_dict = {}
167 found = False
168 input_keys = data.keys()
169 for key in input_keys:
170 if key == searchKey and str(data[key]) == str(searchKeyValue):
171 found = True
172 return_dict = data
Suchitra Vemurif58de922017-06-14 12:53:42 -0700173 print "return_dict",return_dict
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700174 break
175 elif type(data[key]) == dict:
176 result, found = self.search_dictionary(data[key],searchKey)
177 if found == True and str(result) == str(searchKeyValue):
178 return_dict = data
179 break
180 elif type(data[key]) == list:
181 for item in data[key]:
182 if isinstance(item, dict):
183 result, found = self.search_dictionary(data[key], searchKey)
184 if found == True and str(result) == str(searchKeyValue):
185 return_dict = data
186 break
187 if return_dict:
188 break
189 return return_dict
190
191
192 '''
193 @method getFieldValueFromDict
194 @params : search_dict - Dictionary to be searched
195 field - Key to be searched for (ex: account_num)
196 @Returns: Returns the value of the Key that was provided
197 '''
198 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700199 results = ''
200 found = False
201 input_keys = search_dict.keys()
202 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800203 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700204 if key == field:
205 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800206 if not results:
207 found = True
208 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700209 elif type(search_dict[key]) == dict:
210 results, found = self.search_dictionary(search_dict[key],field)
211 if found == True:
212 break
213 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800214 if not search_dict[key]:
215 found = False
You Wangf462ee92016-12-16 13:11:43 -0800216 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700217 for item in search_dict[key]:
218 if isinstance(item, dict):
219 results, found = self.search_dictionary(item, field)
220 if found == True:
221 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800222 if results:
223 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700224
225 return results
226
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800227 def setFieldValueInDict(self,input_dict,field,field_value):
228 input_dict[field]=field_value
229 return input_dict
230
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800231 '''
232 @method getAllFieldValues
233 @params : getJsonDataDictList - List of dictionaries to be searched
234 fieldName - Key to be searched for (ex: instance_id)
235 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800236 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800237
238 def getAllFieldValues(self, getJsonDataDictList, fieldName):
239 value_list = []
240 uniqValue = ''
241 uniq_list = []
242 for data in getJsonDataDictList:
243 fieldValue = ''
244 fieldValue = self.getFieldValueFromDict(data, fieldName)
245 value_list.append(fieldValue)
246 uniq_list = sorted(set(value_list))
247 if len(uniq_list) == 1:
248 uniqValue = uniq_list[0]
249 else:
250 print "list of values found for ", fieldName, ";", uniq_list
251 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800252
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700253 def generate_uuid(self):
254 return uuid.uuid4()
255
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700256 def generate_random_number_from_blacklist(self, blacklist, min=100, max=500, typeTag=False):
257 num = None
258 while num in blacklist or num is None:
259 num = random.randrange(int(min), int(max))
260 if typeTag:
261 return num
262 else:
263 return str(num)
264
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700265 def get_dynamic_resources(self, inputfile, resource):
266 resourceNames = []
267 names = {}
268 dnames = []
269 with open(inputfile, 'r') as f:
270 contents = yaml.load(f)
271 resources = contents[resource]
272 for i in resources:
273 resourceNames.append(i["name"])
274 for i in resourceNames:
275 names['name']=i
276 dnames.append(names.copy())
277 return dnames
Kailash Khalasi8416bfe2017-12-20 13:06:37 -0800278
279 def generate_random_value(self, value):
280 if value == 'string':
281 return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
282 if value == 'bool':
283 return random.choice([True, False])
284 if value == 'int32' or value == 'uint32':
285 return random.randint(1,10000)
286 if value == 'float':
287 return random.uniform(1,10)
288 else:
289 return None
290
291 def generate_random_slice_name(self):
292 random_name = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
293 return 'testloginbase' + random_name