def __startProcess(self, timeout=20):
        """threaded function to start the process"""
        try:
            if sys.platform == "win32":
                __cmd__ = BIN_WIN
                __cmd__ += r' -Dwebdriver.chrome.driver="%s\plugins\selenium3\bin\chromedriver.exe" ' % (
                    Settings.getDirExec())
                __cmd__ += r' -Dwebdriver.gecko.driver="%s\plugins\selenium3\bin\geckodriver.exe" ' % (
                    Settings.getDirExec())
                __cmd__ += r' -Dwebdriver.edge.driver="%s\plugins\selenium3\bin\msedgedriver.exe" ' % (
                    Settings.getDirExec())

                __cmd__ += r' "%s\plugins\selenium3\bin\selenium-server-standalone.jar"' % (
                    Settings.getDirExec())
                #  -debug true
                __cmd__ += r' -log "%s\selenium3_%s.log"  -debug' % (
                    "%s\%s" %
                    (Settings.getDirExec(), Settings.get('Paths', 'logs')),
                    self.toolName)
            else:
                __cmd__ = r'%s -log "%s/selenium_%s.log" -debug true' % (
                    BIN_LINUX, "%s\%s" %
                    (Settings.getDirExec(), Settings.get('Paths', 'logs')),
                    self.toolName)
            logging.debug("external program called: %s" % __cmd__)

            self.seleniumProcess = subprocess.Popen(__cmd__,
                                                    stdin=subprocess.PIPE,
                                                    stdout=subprocess.DEVNULL,
                                                    stderr=subprocess.STDOUT,
                                                    shell=True)
            logging.debug("selenium server thread started pid=%s" %
                          self.seleniumProcess.pid)

            # checking if the server is properly started
            currentTime = startedTime = time.time()
            started = False
            while ((currentTime - startedTime) < timeout):
                try:
                    requestlib.urlopen(self.urlHost).read()
                except Exception as err:
                    currentTime = time.time()
                    time.sleep(2.0)
                    continue
                started = True
                break
            if not started:
                raise RuntimeError('start selenium java process failed!')
            else:
                time.sleep(2.0)
                logging.info("selenium server is started")

        except Exception as e:
            logging.error("unable to start selenium server: %s" % str(e))
 def onConnectionSuccessful(self):
     """
     On connection successful
     """
     logging.debug("Connection successful")
     if self.wsSupport:
         logging.debug("Do ws handshake")
         wspath = Settings.get('Server', 'websocket-path')
         if self.sslSupport:
             wspath = Settings.get('Server', 'websocket-secure-path')
         self.handshakeWebSocket(resource=wspath,
                                 hostport=self.controllerIp)
     else:
         self.doRegistration()
Ejemplo n.º 3
0
    def __init__(self,
                 controllerIp,
                 controllerPort,
                 toolName,
                 toolDesc,
                 defaultTool,
                 supportProxy=0,
                 proxyIp=None,
                 proxyPort=None,
                 sslSupport=True):
        """class ssh agent"""
        GenericTool.Tool.__init__(self,
                                  controllerIp,
                                  controllerPort,
                                  toolName,
                                  toolDesc,
                                  defaultTool,
                                  supportProxy=supportProxy,
                                  proxyIp=proxyIp,
                                  proxyPort=proxyPort,
                                  sslSupport=sslSupport)

        paramiko.util.log_to_file(
            "%s/%s/sshlog_%s.txt" %
            (Settings.getDirExec(), Settings.get('Paths', 'logs'), toolName))
Ejemplo n.º 4
0
 def __init__(self):
     """class constructor"""
     self.maxReconnect = int(Settings.get('Server', 'max-reconnect'))
     self.currentReconnect = 0
     self.timersList = []
     self.tool = None
     self.running = False
     self.disconnected = False
Ejemplo n.º 5
0
    def restartTool(self):
        """restart agent"""
        if self.running:
            # stop the plugin before to restart
            self.tool.onPreCleanup()

            # init the interval, exponential restart
            # 0       1         2           3           4
            # 0 ----> 5s -----> 15s ------> 30s ------> 50s
            # max: 5s x 10 = 50s
            interval = int(Settings.get('Server', 'initial-retry'))
            interval = interval * self.currentReconnect

            logging.info("sleeping %s sec before to restart" % interval)

            # start timer
            t = threading.Timer(interval, self.tool.startConnection)
            self.timersList.append(t)
            t.start()
    def __callRest(self, resultPath, fileName, fileContent, callId=None):
        """
        Rest call
        """
        # set proxy is activated
        proxyDict = {}
        if eval(Settings.get('Server', 'proxy-active')):
            proxyAddr = Settings.get('Server', 'addr-proxy-http')
            proxyPort = Settings.get('Server', 'port-proxy-http')
            logging.debug("Proxy activated for rest Ip=%s Port=%s" %
                          (proxyAddr, proxyPort))

            https_proxy = "https://%s:%s" % (proxyAddr, proxyPort)
            proxyDict = {"https": https_proxy}

        # for support server on python3, not nice ...
        resultPath = resultPath.replace('\\', '\\\\')

        req = '{"result-path": "%s", "file-name": "%s", "file-content": "%s"}' % (
            resultPath, fileName, fileContent.decode("utf8"))
        api_scheme = "https"
        if not eval(Settings.get('Server', 'rest-api-ssl')):
            api_scheme = "http"
        api_url = "%s://%s:%s%sresults/upload/file" % (
            api_scheme, self.controllerIp,
            Settings.get('Server', 'rest-api-port'),
            Settings.get('Server', 'rest-api-path'))
        logging.debug("API URL=%s" % (api_url))
        r = requests.post(
            api_url,
            headers={'Content-Type': 'application/json;charset=utf-8'},
            data=req.encode("utf8"),
            proxies=proxyDict,
            verify=False)
        if r.status_code != 200:
            logging.error('Unable to reach the rest api: %s - %s' %
                          (r.status_code, r.text))
            self.onUploadError(callId=callId)
    def execAction(self, request):
        """
        on execute action
        """
        logging.debug( "<< Begin curl command"  )

        if sys.platform == "win32" :
            infile = "%s\\req_%s" % (self.getTemp(), uuid.uuid4() )
            outfile = "%s\\rsp_%s" % (self.getTemp(), uuid.uuid4() )
        if sys.platform == "linux2":
            infile = "%s/req_%s" % (self.getTemp(), uuid.uuid4() )
            outfile = "%s/rsp_%s" % (self.getTemp(), uuid.uuid4() )
            
        __options = ' ' 
        try:
            if request["data"]["cmd"] == "send-http":
                if "host" not in request["data"] and "timeout-connect" not in request["data"] \
                    and "timeout-max" not in request["data"] :
                    logging.error("options missing in request")
                else:
                    logging.debug( "<< Curl Host=%s" % request["data"]["host"]  )

                    __options += ' --user-agent %s' % Settings.get('Common', 'acronym-server')
                    
                    if "method" in request["data"]:
                        __options += " -X %s" % request["data"]["method"]
                    if "headers" in request["data"]:
                        for hdr in request["data"]["headers"].splitlines():
                            __options += ' -H "%s"' % hdr
                    if "proxy-host" in request["data"]:
                        __options += ' -x %s' % request["data"]["proxy-host"]
                    if "more" in request["data"]:
                        __options += " %s" % request["data"]["more"]
                        
                    __options += ' -w '
                    __options += '"\\n%{time_connect},%{time_total},%{speed_download},'
                    __options += '%{time_appconnect}, %{time_namelookup},'
                    __options += '%{http_code},%{size_download},'
                    __options += '%{url_effective},%{remote_ip}\\n"'
                    
                    __options += ' --connect-timeout %s' % request["data"]["timeout-connect"]
                    __options += ' --max-time %s ' % int(request["data"]["timeout-max"])
                    
                    __options += ' -v %s -s ' % request["data"]["host"]
                    if "body" in request["data"]:
                        with open(infile, "w") as f:
                            f.write(request["data"]["body"])
                        __options += ' --data-binary "@%s"'  % infile
                    
                    __options += ' -o "%s"' % outfile
                    logging.debug("curl options: %s" % __options )
        except Exception as e:
            logging.error("Unable to init curl command - %s" % e)
     
        # windows support
        if sys.platform == "win32" :
            __curlexe = r'"%s\Plugins\curl\bin\curl.exe"' % (Settings.getDirExec() )
            __curlexe += __options
            logging.debug("curl command: %s" % __curlexe )
            try:
                command_process = subprocess.Popen(__curlexe, shell=True, 
                                                    stdin=subprocess.PIPE, 
                                                    stdout=subprocess.PIPE,
                                                    stderr=subprocess.STDOUT, 
                                                    bufsize=0 )
            except Exception as e:
                logging.error("Unable to start curl on windows- %s" % e)
                
        # linux support
        if sys.platform == "linux2":
            __curlexe = 'curl '
            __curlexe += __options
            logging.debug("curl command: %s" % __curlexe )
            try:
                command_process = subprocess.Popen(__curlexe, shell=True, 
                                                    stdin=subprocess.PIPE, 
                                                    stdout=subprocess.PIPE,
                                                    stderr=subprocess.STDOUT, 
                                                    bufsize=0 )
            except Exception as e:
                logging.error("Unable to start curl on linux - %s" % e)

        # read the response
        conn_info = []
        req_out = []
        rsp_in = []
        
        try:
        
            while True:
                line = command_process.stdout.readline()
                if sys.version_info < (3,):
                    line = line.decode('latin-1').encode("utf-8")
                if line != b'':
                    if line.startswith(b"*"):
                        conn_info.append(line[1:].strip())
                    elif line.startswith(b"> "):
                        req_out.append(line[2:].strip())
                    elif line.startswith(b"< "):
                        if not len(rsp_in):
                            # notify the server with the request
                            event = { "response-type": "http-request", 
                                       "headers": b"\n".join(req_out),
                                      "cmd": request["data"]["cmd"] }
                            if "body" in request["data"]:
                                event["body"] = request["data"]["body"]  
                            self.sendNotify( request=request, data=event )
                            
                        rsp_in.append(line[2:].strip())	
                    elif line.startswith(b"{"):
                        continue
                    elif line.startswith(b"}"):
                        continue
                    else:
                        conn_info.append(line.strip())
                else:
                    break

            
            
            rsp_body=None
            if len(rsp_in):
                with open(outfile, "rb") as f:
                    rsp_body = f.read()
                    
                # notify the server with the response
                event = { "response-type": "http-response", 
                          "headers": b"\n".join(rsp_in),
                          "cmd": request["data"]["cmd"] }
                if rsp_body is not None:
                    event["body"] = rsp_body 
                self.sendNotify( request=request, data=event )
            
            event = { "response-type": "http-info", 
                      "debug": b"\n".join(conn_info),
                      "cmd": request["data"]["cmd"] }
            self.sendNotify( request=request, data=event )
        except Exception as e:
            logging.error("Unable to parse curl output - %s" % e)
 
        # remove temps files
        try:
            os.remove(infile)
        except:
            pass
        try:
            os.remove(outfile)
        except:
            pass
            
        logging.debug( "<< Curl terminated")
    def execAction(self, request):
        """
        Execute the action received from the server
        """
        # read the request
        waitUntil = False
        waitUntil_Timeout = 10.0
        waitUntil_Not = False
        waitUntil_Pool = 0.5
        waitUntil_Value = None

        # extract selenium data
        try:
            logging.debug('starting extract data for selenium')
            driver_command = request['data']['command-name']
            driver_params = request['data']['command-params']
            driver_capabilities = request['data']['command-capabilities']
            if 'sessionId' in driver_params:
                sessionId = driver_params['sessionId']
            else:
                sessionId = None

            if "wait-until" in request['data']:
                waitUntil = request['data']["wait-until"]
            if "wait-until-timeout" in request['data']:
                waitUntil_Timeout = request['data']["wait-until-timeout"]
            if "wait-until-pool" in request['data']:
                waitUntil_Pool = request['data']["wait-until-pool"]
            if "wait-until-value" in request['data']:
                waitUntil_Value = request['data']["wait-until-value"]
        except Exception as e:
            logging.error('unable to extract request from server: %s' % e)
            return

        # prepare id
        try:
            globalId = "%s_%s_%s" % (request['script_id'],
                                     request['source-adapter'],
                                     request['data']['command-id'])
            logging.debug("<< %s #%s [%s %s %s]" %
                          (request['data']['command-name'], globalId,
                           waitUntil, waitUntil_Timeout, waitUntil_Value))
        except Exception as e:
            logging.error('unable to read request: %s' % e)
            return

        # prepare driver for selenium
        try:
            logging.debug('preparing selenium driver')

            command_executor = 'http://%s:%s/wd/hub' % (self.seleniumIp,
                                                        self.seleniumPort)
            seleniumDriver = WebdriverRemote(command_executor=command_executor)
            seleniumDriver.session_id = sessionId
            seleniumDriver.capabilities = driver_capabilities
        except Exception as e:
            logging.error('unable to prepare driver: %s' % e)
            self.sendError(request=request,
                           data='unable to run selenium: %s' % e)
            return

        # execute the selenium command
        try:
            if waitUntil:
                end_time = time.time() + waitUntil_Timeout
                timeout_raised = False
                while True:
                    try:
                        response = seleniumDriver.execute(
                            driver_command=driver_command,
                            params=driver_params)
                        send_notify = False
                        if waitUntil_Value != None:
                            if response['status'] == 0 and response[
                                    'value'] == waitUntil_Value:
                                send_notify = True
                        else:
                            if response['status'] == 0:
                                send_notify = True

                        if send_notify:
                            data_notify = {
                                'command-name':
                                request['data']['command-name'],
                                'command-id': request['data']['command-id'],
                                'command-value': response
                            }
                            self.sendNotify(request=request, data=data_notify)
                            break
                    except Exception as e:
                        pass
                    time.sleep(waitUntil_Pool)
                    if time.time() > end_time:
                        timeout_raised = True
                        break

                # timeout raised
                if timeout_raised:
                    data_notify = {
                        'command-name': request['data']['command-name'],
                        'command-id': request['data']['command-id'],
                        'command-value': {
                            "status": 1000,
                            'value': None
                        }
                    }
                    self.sendNotify(request=request, data=data_notify)

            else:

                logging.debug(
                    'executing the selenium command %s with params %s' %
                    (driver_command, driver_params))
                response = seleniumDriver.execute(
                    driver_command=driver_command, params=driver_params)
                logging.debug(">> Action #%s terminated" % globalId)

                # remove image on error response
                if response['status'] != 0:
                    logging.error("error on selenium response - %s" % response)
                    if isinstance(response['value'], str):
                        if ',"screen":"' in response['value']:
                            begin, left = response['value'].split(
                                ',"screen":"')
                            junk, left = left.split('"', 1)
                            del junk
                            response['value'] = "%s%s" % (begin, left)

                # notify user
                data_notify = {
                    'command-name': request['data']['command-name'],
                    'command-id': request['data']['command-id'],
                    'command-value': response
                }
                self.sendNotify(request=request, data=data_notify)

                # manually take screenshot
                if driver_command == "screenshot":
                    logging.debug("<< Uploading screenshot...")
                    extension = Settings.get('Screenshot', 'extension')
                    fileName = "%s_%s_ADP%s_step%s_%s.%s" % (
                        request['testcase-name'], request['test-replay-id'],
                        request['source-adapter'],
                        request['data']['command-id'],
                        request['data']['command-name'], extension.lower())
                    screenshot = base64.b64decode(
                        response['value'].encode('ascii'))
                    self.uploadData(fileName=fileName,
                                    resultPath=request['result-path'],
                                    data=screenshot)

                logging.debug(
                    'executing the selenium command - terminated - notify sent'
                )
        except Exception as e:
            logging.error('unable to execute action: %s' % e)
            self.sendError(request=request,
                           data='unable to execute action: %s' % e)
 def getTemp(self):
     """
     Return the path of the temp area
     """
     return "%s/%s/" % (Settings.getDirExec(), Settings.get('Paths', 'tmp'))
    def __init__(self,
                 controllerIp,
                 controllerPort,
                 toolName,
                 toolDesc,
                 defaultTool,
                 supportProxy=False,
                 proxyIp=None,
                 proxyPort=None,
                 sslSupport=True,
                 toolType=NetLayerLib.TYPE_AGENT_AGENT,
                 fromCmd=False,
                 name=None):
        """Constructor"""
        self.login = "******"
        self.password = "******"
        self.toolType = toolType

        # init ssl
        self.sslSupportFinal = sslSupport
        if not Settings.getBool('Server', 'ssl-support'):
            self.sslSupportFinal = False

        # init websocket
        wsSupport = False
        if Settings.getBool('Server', 'websocket-support'): wsSupport = True

        NetLayerLib.ClientAgent.__init__(
            self,
            typeAgent=toolType,
            startAuto=True,
            keepAliveInterval=Settings.getInt('Network', 'keepalive-interval'),
            inactivityTimeout=Settings.getInt('Network', 'inactivity-timeout'),
            timeoutTcpConnect=Settings.getInt('Network',
                                              'tcp-connect-timeout'),
            responseTimeout=Settings.getInt('Network', 'response-timeout'),
            selectTimeout=Settings.get('Network', 'select-timeout'),
            sslSupport=self.sslSupportFinal,
            wsSupport=wsSupport,
            pickleVer=Settings.getInt('Network', 'pickle-version'),
            tcpKeepAlive=Settings.getBool('Network', 'tcp-keepalive'),
            tcpKeepIdle=Settings.getInt('Network', 'tcp-keepidle'),
            tcpKeepCnt=Settings.getInt('Network', 'tcp-keepcnt'),
            tcpKeepIntvl=Settings.getInt('Network', 'tcp-keepintvl'))
        serverPort = Settings.getInt('Server', 'port')
        if controllerPort is not None:
            serverPort = int(controllerPort)

        self.setServerAddress(ip=controllerIp, port=serverPort)
        if supportProxy:
            self.setProxyAddress(ip=proxyIp, port=int(proxyPort))
        self.setAgentName(name=toolName)
        toolMore = {'details': toolDesc, 'default': False}
        if name is not None:
            self.setDetails(name=name,
                            desc=toolMore,
                            ver=Settings.getVersion())
        else:
            self.setDetails(name=self.__class__.__name__,
                            desc=toolMore,
                            ver=Settings.getVersion())

        self.controllerIp = controllerIp
        self.controllerPort = serverPort

        self.__mutex__ = threading.RLock()
        self.regThread = None
        self.regRequest = None

        self.testsContext = {}