Exemplo n.º 1
0
 def do_post(self, url, locator, request, settings={}, 
             headers={}, https=True):
     '''
         can be used for non multi-part posts
    '''
     print("*** POST *** (thr: %s, t: %s) %s" %
          (_thread.get_ident(), time.time(), url + locator))
     params = pytaf_utils.anystring_as_utf8(request)
     if DEBUG:
         print("params:", params)
     api_qa_cert = os.getenv("PYTAF_HOME") + "/resources/cert.pem"
     certificate_file = settings.get("cert_file", api_qa_cert)
     host = settings.get('host', url)
     cookie = settings.get('cookie', None)
     content_type = settings.get('content_type', "text/xml")
     headers["Content-type"] = content_type
     if host != None:
         headers['host'] = host
     if cookie != None:
         headers['Set-Cookie'] = cookie
     if DEBUG:
         print(headers)
     if https == True:
         conn = httplib.HTTPSConnection(url, cert_file=certificate_file)
     else:
         conn = httplib.HTTPConnection(url)
     start_time = time.time()
     conn.request("POST", locator, params, headers)
     response = conn.getresponse()
     end_time = time.time()
     if DEBUG:
         print("http response time: (thr: %s, t: %s)" %
              (_thread.get_ident(), round(float(end_time - start_time), 2)))
     if DEBUG:
         print(response.status, response.reason)  # response.getheaders()
     data = response.read().decode()  # read() returns a bytes object
     if DEBUG:
         print (data)
     return {"data": data, "status": response.status,
             "reason": response.reason}
Exemplo n.º 2
0
    def do_test(self, modules, settings, test, params):
        result = (False, "error")
        start_time = end_time = elapsed_time = 0
        found_module = None
        test_was_found = False
        for module in modules:
            try:
                if DEBUG:
                    print("do test %s from module %s" % (test, module))
                method_to_call = getattr(module, test)
                found_module = str(module)
                test_was_found = True
                start_time = int(time.time())
                print("------------\n starting test: %s" % test)
                print(" start time:    %s" % datetime.datetime.now())
                print("------------")
                args = {"settings": settings, "params": params}
                result = method_to_call(args)
                end_time = int(time.time())
                elapsed_time = end_time - start_time
            except Exception as inst:
                if DEBUG:
                    print("exception from methodToCall: %s" % sys.exc_info()[0])
                continue

        if test_was_found == False:
            print(
                "error: pytaf did not find the test case (%s) \
            in the modules defined in the config file (%s)"
                % (test, str(modules))
            )
            return

        # tests return (True|False, String)
        error_message = str(result[1])  # could be Exception, hence the cast
        status = result[0]

        try:
            if status == True:  # any return value except False is PASSED
                status_string = "PASSED"
            else:
                status_string = "FAILED"

            module_string = ""
            if found_module != None:
                idx1 = found_module.rfind(os.sep) + 1
                idx2 = found_module.find(".py")
                module_string = found_module[idx1:idx2]

            self.results.append(
                {"test_method": test, "status": status_string, "message": error_message[:1024], "module": module_string}
            )

            if status != False:
                result_str = "RESULT ===> PASSED: %s" % test
            else:
                result_str = "RESULT ===> FAILED: %s, %s" % (test, pytaf_utils.anystring_as_utf8(error_message))
            if elapsed_time > 0:
                result_str = "%s, elapsed time: %s seconds" % (result_str, str(elapsed_time))
            print("%s\n---------------" % result_str)
        except:
            print(pytaf_utils.formatExceptionInfo())

        return status