def _run(self, command="get_param", data=None, **kwargs ): self.sendCmds['cmd'] = command if data is not None: self.sendCmds['data'] = data else: self.sendCmds['data'] = [] localIpAddr = localIp.get_lan_ip_address("enp0s3") self.sendCmds['targ'] = kwargs.get("target", "FF:FF:FF:FF:FF:FF") self.sendCmds['811Ip'] = kwargs.get("811Ip", localIpAddr) self.sendCmds['811Port'] = kwargs.get("811Port", 3840 ) #json_string = json.dumps(self.sendCmds) self.start_time = time.time() # time() is float msg = self.cmdSocket.send(cmd=self.sendCmds) result = self.cmdSocket.receive() self.timeused = (time.time() - self.start_time) * 1000 # millsecond ColorMsg.debug_msg('After %s ms,JSON result type %s: "%s"\n' %(self.timeused, type(result), result), self.debug ) for res in result: if res['login-ack'] != "OK": ColorMsg.error_msg("\tIP Command '%s' failed %s" %(self.sendCmds["cmd"], res['pwd-msg'])) else: None #ColorMsg.success_msg("\tIP Command '%s' success after %s ms" %(self.sendCmds["cmd"], self.timeused)) return result
def tearDown(self): """ called in every test case, eg. every test method in this class :return: """ super().tearDown() ColorMsg.debug_msg("tearDown", debug=True)
def accessPage(*args, **kwargs): uri = kwargs.get("uri", None) if uri is None: ColorMsg.error_msg("No URI is defined") return webClient = WebClient(*args, **kwargs) webClient.accessUri(*args, **kwargs)
def __init__(self, connections=conns, timeout=time, **kwargs): # 'grep -v message': ignore the message # cmd = 'iperf -c %s %s | grep -v SUM' % ('192.168.166.2', opts) self.ip = kwargs.get("ip", "192.168.168.120") cmd = 'iperf -c %s %s' % (self.ip, cmdOpts) ColorMsg.debug_msg('%s connection:%d time:%d' % (self.ip, connections, timeout), True) super().__init__(cmd)
def receiveRaw(self): """ : default for Unix socket and TCP socket """ datas = [] while 1: try: # return self.sock.recv(4092) data, node = self.sock.recvfrom(4096) usedTime = (time.time() - self.start_time) * 1000 #ColorMsg.debug_msg("After %s ms, receive %d bytes data from %s data:%s " % (usedTime, len(data), node, data), self.debug) ColorMsg.success_msg( "\tAfter %s ms, receive %d bytes data from %s" % (usedTime, len(data), node)) datas.append(data) if self.server != "<broadcast>": return datas except socket.timeout as e: usedTime = (time.time() - self.start_time) * 1000 ColorMsg.error_msg("\tAfter %s ms, read timeout on socket " % (usedTime)) self.sock.close() if self.server != "<broadcast>": return None return datas
def sendPacket(self, data): """ """ ColorMsg.debug_msg('send packet: %s to %s\n' % (data, self.peer), self.debug) self.start_time = time.time() sent = self.sock.sendto(data, self.peer) return sent
def parseResponse(self, resp, *args, **kwargs): if self.contentLength == 0: ColorMsg.error_msg("\n\tURI %s: status: failed in request\n" % (kwargs.get("uri"))) print("headers:" + str(resp.headers)) # print("******************") print("\nNo.%d Test: \tURI %s; Code: %s Type: %s; Res Size: %s; Update Size:%s; Time: %f ms\n" \ %(self.totalTests, kwargs.get("uri"),str(resp.status_code), resp.headers['Content-Type'], self.contentLength, self.number, self.timeused) ) return resp
def secureGetStatus(self, *args, **kwargs): secureGetStatusCmd = [{"get_status": ""}] node = self.findOneNode(*args, **kwargs) if node is None: ColorMsg.error_msg("Node with IP %s is not found" % self.ip) for node in self.nodes: return self.runOneNode(command=settings.IP_CMD_SECURE, data=secureGetStatusCmd, target=node["targ"])
def findOneNode(self, *args, **kwargs): self.ip = kwargs.get("ip", None) if self.ip is None: ColorMsg.error_msg("Node IP address is not defined") return None self.find() for node in self.nodes: if self.ip == node["data"][0]["ip"]: return node return None
def sysConfig(self, *args, **kwargs): data = kwargs.get('data', None) if data is None: ColorMsg.error_msg("No data is defined for \"%s\""%(settings.IP_CMD_SET_PARAM) ) return node = self.findOneNode(*args, **kwargs) if node is None: ColorMsg.error_msg("Node with IP %s is not found"%self.ip) self.runOneNode(command=settings.IP_CMD_SET_PARAM, data=data, target=node["targ"])
def rs232Data(self, *args, **kwargs): data = kwargs.get('data', "1122334455") rs232Cmd = [{"hexdata": data, "isFeedback": 1, "waitTime": 1000}] node = self.findOneNode(*args, **kwargs) if node is None: ColorMsg.error_msg("Node with IP %s is not found" % self.ip) ColorMsg.debug_msg("Rs232 on:" + node["targ"], True) self.runOneNode(command=settings.IP_CMD_RS232_DATA, data=rs232Cmd, target=node["targ"])
def __init__(self, *args, **kwargs): super().__init__(self, *args, **kwargs) self.cmdSocket = IpCmdIf.IpCmdSocket(self, *args, **kwargs ) self.sendCmds = { "targ": kwargs.get("target", "FF:FF:FF:FF:FF:FF"), "cmd": kwargs.get("command", "get_param"), "login-ack": kwargs.get("user", "admin"), "pwd-msg": kwargs.get("password", "admin"), "data": kwargs.get("data", None) } ColorMsg.debug_msg("set sendCmds", debug=True)
def secureSetKey(self, *args, **kwargs): data = kwargs.get('data', "1122334455") secureSetKeyCmd = [{"set_key": data}] node = self.findOneNode(*args, **kwargs) if node is None: ColorMsg.error_msg("Node with IP %s is not found" % self.ip) for node in self.nodes: return self.runOneNode(command=settings.IP_CMD_SECURE, data=secureSetKeyCmd, target=node["targ"])
def decode(data, debug=False): """ return JSON object """ # ColorMsg.debug_msg("Received data:'%s'" % (data), debug) try: # 1: unpack json string # print("receive from:", binascii.hexlify(data)) tag, packedLen = struct.unpack("<HH", data[0:4]) packedLen = socket.ntohs(packedLen) # ColorMsg.debug_msg("\nUnpacked TAG 0x%x; length:%d" % (tag, packedLen), debug) #msg = data[4:len].decode('utf-8') # # format = b'HH%ds' % (length - 4) #print('received %d IP CMD, format:"%s"' % (len, msg)) (header, _header, jsonBytes) = struct.unpack(b'<HH%ds'%(packedLen-4), data[:packedLen]) ColorMsg.debug_msg('Received JSON string: "%s":' %(jsonBytes), debug ) # ascii try: replyJson = json.loads(jsonBytes.decode("ascii")) ColorMsg.debug_msg('Received JSON object: "%s":' % (replyJson)) return replyJson except (TypeError, ValueError): #raise Exception('Data received was not in JSON format %s'%(jsonBytes)) ColorMsg.error_msg('Data received was not in JSON format %s'%(jsonBytes)) return None #return json.loads(msg) # print(self.replyMsg) except (ValueError, TypeError) as e: #KeyError, ColorMsg.error_msg("Struct Unpack error: ".format(e))
def setUp(self, *args, **kwargs): """ called per test case, eg. every test method in this class :param args: :param kwargs: :return: """ super().setUp() self.ip = "192.168.166.2" self.debug = True self.gateway = "192.168.166.1" self.agent = DeviceIpCmd(simGateway=self.gateway, debug=self.debug) #self.agent = DeviceIpCmd( debug=self.debug) ColorMsg.debug_msg("setup", debug=True)
def sendPacket(self, data): """ """ ColorMsg.debug_msg('send packet: %s to %s\n' % (data, self.server), self.debug) self.start_time = time.time() try: sent = self.sock.sendall(data) return sent except socket.error as e: usedTime = (time.time() - self.start_time) * 1000 ColorMsg.error_msg("\tAfter %s ms, send timeout on socket "% (usedTime)) self.sock.close() #ColorMsg.error_msg('closing UNIX socket when sendall data: %s' %(msg)) #self.sock.close() return 0
def _request(self, method, *args, **kwargs): #kwargs.setdefault('content_type', 'application/json') ColorMsg.debug_msg( "in request, kwargs ID:%d %s" % (id(kwargs), kwargs), self.debug) self.createUrl(self, *args, **kwargs) ColorMsg.debug_msg( "METHOD \"%s\" on URL %s" % (method.__name__, self.url), self.debug) #print("kwargs %s"%(kwargs) ) # kwargs.setdefault('follow_redirects', True) kwargs2 = copy.copy(kwargs) response = None done = False while not done: try: start_time = time.time() # time() is float response = method(self.url, **kwargs2) self.timeused = (time.time() - start_time) * 1000 # millsecond ColorMsg.debug_msg("Response after %s ms" % (self.timeused), self.debug) self.totalTests = self.totalTests + 1 # print("passed kwargs %s" % (kwargs2)) done = True except TypeError as err: # for: TypeError: request() got an unexpected keyword argument 'uri' #ColorMsg.debug_msg("TypeError output:{0}".format(err), self.debug) log.debug("TypeError output:{0}".format(err), self.debug) # re.findall() return a list, so get first item in list key = re.findall("\'(\w+)\'", format(err))[0] #ColorMsg.debug_msg("Method \"%s\" removing \"%s\"" % (method.__name__, str(key)), self.debug ) log.debug( "Method \"%s\" removing \"%s\"" % (method.__name__, str(key)), self.debug) del kwargs2[key] # Remove offending key except requests.exceptions.ConnectionError as err: ColorMsg.error_msg("ConnectionError output:{0}".format(err)) ColorMsg.error_msg("Can't connect to %s" % (self.url)) return #response = method(self.url, *args, **kwargs) if response is None: self.contentLength = 0 return self.contentLength = response.headers['Content-Length'] return self.parseResponse(response, *args, **kwargs)
def receive(self): """ receive from socket :return: node dict list """ nodes = [] lenPacket = MSGLEN while 1:#bytes_recd < MSGLEN: jsonBytes = self.receiveRaw() # json data in byte format if jsonBytes is None: break; # return datas # ColorMsg.error_msg("chunk %d: %s" % (len(datas), self.cmd)) # for chunk in datas: # bytes_recd = 0 # chunks = b'' # bytearray()#[] # if chunk == b'': # raise RuntimeError("socket connection broken") # if bytes_recd == 0 and ( (chunk is not None) and (len(chunk) > 4) ): # unpacker = struct.Struct("<HH") # tag, lenPacket = unpacker.unpack(chunk[0:4]) # lenPacket = socket.ntohs(lenPacket) # chunks = b"".join([chunks, chunk]) # (chunk)#.decode('utf-8')) # bytes_recd = bytes_recd + len(chunk) # ColorMsg.debug_msg('read length :%d, IP Command length :%d' % (bytes_recd, lenPacket), self.debug) # if bytes_recd > lenPacket + 4: # ColorMsg.error_msg("bytes_read %d larger than %s" % (bytes_recd, lenPacket+4)) # break # ColorMsg.debug_msg("receive data:%s" % (chunks), self.debug) # replyJson = CommandCodec.decode(chunks, debug=self.debug) # if replyJson is not None: # nodes.append(replyJson) #self.sock.close() try: replyJson = json.loads(jsonBytes[0].decode("ascii")) ColorMsg.debug_msg('Received JSON object: "%s":' % (replyJson)) return replyJson except (TypeError, ValueError): #raise Exception('Data received was not in JSON format %s'%(jsonBytes)) ColorMsg.error_msg('Data received was not in JSON format %s'%(jsonBytes)) return None
def _run(self, *argd, **kwargs): self.fmType = kwargs.get("fmType", None) if self.fmType is None or self.fmType == settings.FIRMWARE_TYPE_TFTP_OS: self.fmType = settings.FIRMWARE_TYPE_TFTP_OS self.filename = kwargs.get("file", settings.FIRMWARE_BIN_OS) elif self.fmType == settings.FIRMWARE_TYPE_TFTP_FPGA: self.fmType = settings.FIRMWARE_TYPE_TFTP_FPGA self.filename = kwargs.get("file", settings.FIRMWARE_BIN_FPGA_RX) else: ColorMsg.error_msg("FirmType is error %s" % self.fmType) return ColorMsg.debug_msg( "TFTP %s, upload %s : %s" % (self.ip, self.fmType, self.filename), self.debug) self.client = tftp.TftpClient(self.ip) self.client.upload(self.fmType, self.filename, timeout=5)
def getRate(self): self.expect('Client connecting to') # child.expect("") rate = 0.0 for i in range(0, conns): m = self.expect(['Bytes([^M]*)Mbits', 'Bytes([^K]*)Kbits'], timeout=time + 30) if m == 0: rate += float(self.match.group(1)) elif m == 1: rate += float(self.match.group(1)) / 1000.0 else: ColorMsg.error_msg("Unknown units for iPerf results!\n") assert False # child.expect(prompt) print("bandwidth %d Mbps" % (rate)) return self.ip, rate
def parseResponse(self, response, *args, **kwargs): if self.assertContentType(response, "application/json"): ColorMsg.error_msg( "Content type '%s' is not validate for REST API" % (response.headers['Content-Type'])) return response try: jData = json.loads(response.content.decode("utf-8")) print("The response contains {0} properties".format(len(jData))) if (response.ok): print('raw "%s"\n' % (json.dumps(jData, indent=4, sort_keys=True))) else: print("\n") for key in jData: print(key + " : " + str(jData[key])) except (TypeError, ValueError): raise Exception("parse JSON string failed \"%s\"" % (resStr))
def testSysCfg(*arg, **kwargs): agent = DeviceIpCmd(*arg, **kwargs) nodes = agent.find() if len(nodes) == 0: ColorMsg.error_msg("No node is found in LAN") return setupSystemCmd = [ { "cName": "SimTx", "mac": "00:04:25:1c:10:03", "ip": "192.168.168.120", "isDhcp": 0, "isDipOn": 0 } ] for node in nodes: data = node["data"][0] agent.sysConfig(data=setupSystemCmd, ip=data["ip"], target=node["targ"])
def receive(self): """ receive from socket :return: node dict list """ nodes = [] lenPacket = MSGLEN while 1: #bytes_recd < MSGLEN: datas = self.receiveRaw() # min(MSGLEN - bytes_recd, 2048)) if datas is None: return nodes # ColorMsg.error_msg("chunk %d: %s" % (len(datas), self.cmd)) for chunk in datas: bytes_recd = 0 chunks = b'' # bytearray()#[] if chunk == b'': raise RuntimeError("socket connection broken") if bytes_recd == 0 and ((chunk is not None) and (len(chunk) > 4)): unpacker = struct.Struct("<HH") tag, lenPacket = unpacker.unpack(chunk[0:4]) lenPacket = socket.ntohs(lenPacket) chunks = b"".join([chunks, chunk]) # (chunk)#.decode('utf-8')) bytes_recd = bytes_recd + len(chunk) # ColorMsg.debug_msg('read length :%d, IP Command length :%d' % (bytes_recd, lenPacket), self.debug) if bytes_recd > lenPacket + 4: ColorMsg.error_msg("bytes_read %d larger than %s" % (bytes_recd, lenPacket + 4)) break # ColorMsg.debug_msg("receive data:%s" % (chunks), self.debug) replyJson = CommandCodec.decode(chunks, debug=self.debug) if replyJson is not None: nodes.append(replyJson) #self.sock.close() return nodes
def uploadFile(self, *args, **kwargs): uri = kwargs.get("type", None) if uri is None or uri == settings.FIRMWARE_URL_OS: uri = settings.FIRMWARE_URL_OS fmfile = settings.FIRMWARE_BIN_OS elif uri == settings.FIRMWARE_URL_FPGA: fmfile = settings.FIRMWARE_BIN_FPGA_RX else: ColorMsg.error_msg("\"%\" is not validate URI" % (uri)) return try: _filename = kwargs.get("file", None) # client assign it to None if _filename is None: _filename = fmfile fileObj = open(_filename, 'rb') except Exception as e: ColorMsg.error_msg("Open firmware file \"%s\" failed: %s:%s" % (_filename, str(e), repr(e))) return ColorMsg.debug_msg('"%s" is uploading to %s.....' % (_filename, uri), self.debug) files = {'file': fileObj} res = self.post(*args, uri=uri, files=files, **kwargs) return res
def __init__(self, *args, **kwargs): super().__init__(self, *args, **kwargs) self.cmdSocket = CmdIf.IpCmdSocket(self, *args, **kwargs) self.replyCmds = { "targ": kwargs.get("target", "FF:FF:FF:FF:FF:FF"), "cmd": kwargs.get("command", "get_param"), "login-ack": kwargs.get("user", "admin"), "pwd-msg": kwargs.get("password", "admin"), "data": kwargs.get("data", None) } ColorMsg.debug_msg("set sendCmds", debug=True) self.server = kwargs.get("ip", "<broadcast>") self.port = kwargs.get("port", 3840) self.debug = kwargs.get("debug", False) #self.simGateway = kwargs.get("simGateway", None) self.peer = (self.server, self.port) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Set a timeout so the socket does not block indefinitely when trying to receive data. #self.sock.settimeout(3.0) ColorMsg.debug_msg( '%s, %s:%s' % (self.__class__.__name__, self.server, self.port), self.debug) try: self.sock.bind((self.server, self.port)) #self.sock.listen(10) except socket.error as msg: ColorMsg.error_msg("BIND failed: ".format(msg))
def __init__(self, devCtrl, *args, **kwargs): self.dev = devCtrl self.server = kwargs.get("ip", "<broadcast>") self.port = kwargs.get("port", 3600) self.debug = kwargs.get("debug", False) #self.simGateway = kwargs.get("simGateway", None) self.peer = (self.server, self.port) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Set a timeout so the socket does not block indefinitely when trying to receive data. self.sock.settimeout(settings.IP_CMD_FIND_TIMEOUT) ColorMsg.debug_msg( '%s, %s:%s, gateway:%s' % (self.__class__.__name__, self.server, self.port, self.dev.simGateway), self.debug) if self.server == "<broadcast>": self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) if self.dev.simGateway is not None: self.sock.bind((self.dev.simGateway, 3600))
def _run(self, command="get_param", data=None, **kwargs): self.replyCmds['cmd'] = command self.count = 0 if data is not None: self.replyCmds['data'] = data else: self.replyCmds['data'] = [] #self.replyCmds['targ'] = kwargs.get("target", "FF:FF:FF:FF:FF:FF") #self.replyCmds['811Ip'] = kwargs.get("811Ip", "192.168.168.102") #self.replyCmds['811Port'] = kwargs.get("811Port", 3840 ) #json_string = json.dumps(self.sendCmds) start_time = time.time() # time() is float while True: data, addr = self.sock.recvfrom(2018) self.count = self.count + 1 cmdJson = CommandCodec.decode(data, self.debug) #self.timeused = (time.time() - start_time) * 1000 # millsecond ColorMsg.debug_msg( 'recv #.%d: %s from %s\n' % (self.count, cmdJson, addr), self.debug) self.replyCmds['targ'] = cmdJson['targ'] self.replyCmds['cmd'] = cmdJson['cmd'] self.replyCmds['login-ack'] = "OK" self.replyCmds['pwd-msg'] = "OK" reply = CommandCodec.encode(self.replyCmds, debug=self.debug) ColorMsg.debug_msg( 'reply #.%d: %s to %s\n' % (self.count, self.replyCmds, addr), self.debug) self.sock.sendto(reply, addr) return None
def connect(self, *args, **kwargs): #self.status = self.load_data() self.cmd = kwargs.get("cmd", "get") ColorMsg.debug_msg('send "%s" to %s\n' % (self.cmd, self.server), self.debug) ColorMsg.debug_msg('connect to %s\n' % (self.server), self.debug) try: self.sock.connect(self.server) except socket.error as msg: ColorMsg.error_msg("connect failed") ColorMsg.error_msg(msg) return json.loads('{"status": 500, "detail": "Can not connect to backend server"}' ) # sys.exit(1) # raise SystemExit exception len = self.send() if len == 0: return json.loads('{"status": 500, "detail": "Command can not be sent to backend server"}' ) data = self.receive() return data
def setParams(self, *args, **kwargs): data = kwargs.get('data', None) if data is None: ColorMsg.error_msg("No data is defined for \"%s\""%(settings.IP_CMD_SET_PARAM) ) return node = self.findOneNode(*args, **kwargs) if node is None: ColorMsg.error_msg("Node with IP %s is not found"%self.ip) return ColorMsg.debug_msg("data is \"%s\" to node %s"%(data, node), self.debug) self.runOneNode(command=settings.IP_CMD_SET_PARAM, data=data, target=node["targ"])
def parseResponse(self, resp, *args, **kwargs): self.number = "0" #if self.assertContentType(resp, "text/html"): if "text/" in resp.headers['Content-Type']: ColorMsg.debug_msg(resp.content.decode("utf-8"), self.debug) ns = re.findall("Result:(\w+)", resp.content.decode("utf-8")) if len(ns) > 0: # list self.number = ns[0] #elif self.assertContentType(resp, "image/jpeg"): elif 'image/' in resp.headers['Content-Type']: ColorMsg.debug_msg(resp.content, self.debug) elif self.assertContentType(resp, "application/javascript"): ColorMsg.debug_msg(resp.content, self.debug) #elif self.assertContentType(resp, "text/css"): # ColorMsg.debug_msg(resp.content, self.debug ) # return resp else: print("ELSE ") ColorMsg.error_msg("Unknown content type \"%s\"" % (resp.headers['Content-Type'])) return super().parseResponse(resp, *args, **kwargs)