Beispiel #1
0
	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)
Beispiel #2
0
	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
Beispiel #3
0
	def request_time_check(self, request_time,url):
		msg="RESTful Server takes <hl>'{0}'</hl> to respond URL:{1}".format(request_time,url)
		if request_time>CONF.REQUEST.http_time_warn and request_time<CONF.REQUEST.http_time_error:
			logger.warning(msg)
		elif request_time>CONF.REQUEST.http_time_error:
			logger.error(msg)