def get_req(self,values=None): if values: if not isinstance(values,dict): raise TypeError("POST data should be a python dict") else: msg="POST: Attempting to request URL: {0}".format(self.url) data = urllib.parse.urlencode(values) data = data.encode('ascii') # data should be bytes req = urllib.request.Request(self.url, data) else: msg="GET: Attempting to request URL: {0}".format(self.url) req = urllib.request.Request(self.url) logger.info(msg) try: start_time=datetime.now() response = urllib.request.urlopen(req) #Need close the urlopen here??? or just run Burn-in except URLError as ue: if hasattr(ue,'reason'): msg='Failed to reach the server:<hl> {0}</hl>.'.format(ue.reason) logger.error(msg) elif hasattr(ue,'code'): msg='The server couldn\'t fulfill the request. Error code: <hl>{0}</hl>'\ .format(ue.code) logger.error(msg) if ue.code==401: self.Send_Auth() raise else: end_time=datetime.now() data=response.read().decode('utf-8') #To run Burn-in test, keep response.close() commented #response.close() request_time=(end_time-start_time).total_seconds() msg="Spent {0:6f}s to get response from {1:s}"\ .format(request_time,self.url) logger.debug(msg) self.response_check.request_time_check(request_time,self.url) try: self.response_dict=json.loads(data) except ValueError as ve: msg="Get invaild feedback from RESTful server when open URL:{0}, \ infor:<hl>{1}</hl>".format(self.url,data) logger.error(msg) raise self.response_check.confcompare(self.response_dict,CONF.MAIN.value_file) return self.response_dict
def main(): for i in range(CONF.MAIN.cycle): msg="SCAN ALL THE NODES IN CYCLE: {0:d}".format(i) logger.info(msg) get_node=GET_NODE(host,CONF.REST.client_app_ver,CONF.REST.bind_port) get_node.scan_node() new_url=get_node.url_list if i!=0: if last_url!=new_url: compare_url(last_url,new_url) last_url=new_url if CONF.CLI.time_to_stop: if datetime.now()>datetime.strptime(CONF.CLI.time_to_stop,\ '%Y-%m-%d %H:%M:%S'): break #input("Press Enter to continue...") msg='Stop test on {0}, cycle {1}'.format(datetime.now(),(i+1)) logger.info(msg)
def confcompare(self, url_dict, conf_file): if not os.path.isfile(conf_file): msg='Didn\'t find <hl>compare files</hl> to check the response data.' logger.warning(msg) conf = configparser.ConfigParser() conf.optionxform = str conf.read(conf_file) current_url_name=url_dict["Name"] if current_url_name in conf: for opt,value in conf[current_url_name].items(): if opt in url_dict: if str(url_dict[opt])==value.strip(): msg="{0}: Value mathced for key: {1}, got: {2}"\ .format(current_url_name,opt, value.strip()) logger.info(msg) else: msg="{0}: Value mismatched for key: <hl>{1}</hl>, expect: <hl>{2}</hl>, got: {3}"\ .format(current_url_name,opt, value.strip(),str(url_dict[opt])) logger.error(msg) else: msg="{0}: Can't find key: <hl>{1}</hl> in response data, check config"\ .format(current_url_name,opt) logger.error(msg)