Пример #1
0
def test_wsgi_handler(wasc, app, client):
    @app.route("/")
    def index(was):
        return "Hello"

    @app.route("/json")
    def json(was):
        return "Hello"

    @app.route("/rpc2/add")
    def add(was, a, b):
        return a + b

    # WSGI
    vh = testutil.install_vhost_handler()
    testutil.mount("/", (app, confutil.getroot()), skitai.pref())
    request = client.get("http://www.skitai.com/")
    resp = assert_request(vh, request, 200)
    assert resp.text == "Hello"

    request = client.get("http://www.skitai.com/", version="2.0")
    resp = assert_request(vh, request, 200)
    assert resp.text == "Hello"

    request = client.get("http://www.skitai.com/a")
    resp = assert_request(vh, request, 404)

    request = client.get("http://www.skitai.com/?a=b")
    resp = assert_request(vh, request, 200)

    request = client.api().json.post({'a': 1})
    resp = assert_request(vh, request, 200)

    answer = client.rpc("http://www.skitai.com/rpc2/").add(100, 50)
    resp = assert_request(vh, request, 200)

    answer = client.jsonrpc("http://www.skitai.com/rpc2/").add(100, 50)
    resp = assert_request(vh, request, 200)

    testutil.enable_threads()
    assert wasc.numthreads == 1
    assert wasc.threads

    request = client.postjson("http://www.skitai.com/json", {'a': 1})
    resp = assert_request(vh, request, 200)

    request = client.postjson("http://www.skitai.com/json", {'a': 1},
                              version="2.0")
    resp = assert_request(vh, request, 200)

    testutil.disable_threads()
    assert wasc.numthreads == 0
    assert wasc.threads is None

    request = client.postjson("http://www.skitai.com/json", {'a': 1})
    resp = assert_request(vh, request, 200)

    request = client.postjson("http://www.skitai.com/json", {'a': 1},
                              version="2.0")
    resp = assert_request(vh, request, 200)
Пример #2
0
def test_skitai(app):
    skitai.set_worker_critical_point()

    skitai.deflu("a", "b")
    assert (skitai.dconf["models-keys"] == ["a", "b"])

    if os.name != "posix":
        return

    assert skitai.joinpath('a', 'b').endswith("/bin/a/b")
    skitai.mount("/k", app)
    assert skitai.dconf['mount']["default"][0][1][0].endswith('/bin/pytest')
    assert skitai.dconf['mount']["default"][0][1][1] == 'app'

    skitai.dconf['mount']["default"] = []
    skitai.mount("/k2", '/path/app.py', 'app')
    assert skitai.dconf['mount']["default"][0][1] == ('/path/app.py', 'app')

    skitai.dconf['mount']["default"] = []
    skitai.mount("/k2", 'path/app.py', 'app')
    assert skitai.dconf['mount']["default"][0][1][0].endswith(
        '/bin/path/app.py')
    assert skitai.dconf['mount']["default"][0][1][1] == 'app'

    if tfserver:
        pref = skitai.pref()
        pref.config.tf_models = None
        skitai.dconf['mount']["default"] = []
        skitai.mount("/k2", tfserver, pref=pref)
        assert skitai.dconf['mount']["default"][0][1][0].endswith(
            'tfserver/export/skitai/__export__.py')

        skitai.dconf['mount']["default"] = []
        skitai.mount("/k2", (tfserver, "dapp"), "dapp", pref=pref)
        assert skitai.dconf['mount']["default"][0][1][0].endswith(
            'tfserver/export/skitai/dapp')
        assert skitai.dconf['mount']["default"][0][1][1] == "dapp"

    skitai.dconf['mount']["default"] = []
    skitai.mount("/k2", "X11")
    assert skitai.dconf['mount']["default"][0][1][0].endswith('/bin/X11')

    skitai.dconf['mount']["default"] = []
    skitai.mount("/k2", "@X11")
    assert skitai.dconf['mount']["default"][0][1] == "@X11"

    if django:
        skitai.dconf['mount']["default"] = []
        t = os.path.join(os.path.dirname(__file__), "django_")
        skitai.mount("/k2", t)
        assert skitai.dconf['mount']["default"][0][1] == t

        skitai.dconf['mount']["default"] = []
        t = os.path.join(os.path.dirname(__file__), "django_", "wsgi.py")
        skitai.mount("/k2", t, "application")

        t = os.path.join(os.path.dirname(__file__), "django_", "settings.py")
        skitai.alias("@django", skitai.DJANGO, t)
        assert skitai.dconf["clusters"]["django"]
Пример #3
0
def test_websocket_handler(wasc, app, client):
    @app.route("/echo")
    def echo(was, message):
        if was.wsinit():
            return was.wsconfig(skitai.WS_SIMPLE, 60)
        elif was.wsopened():
            return "Welcome Client %s" % was.wsclient()
        elif was.wshasevent():  # ignore the other events
            return
        was.websocket.send("You said," + message)
        was.websocket.send("acknowledge")

    @app.route("/chat")
    def chat(was, message, roomid):
        if was.wsinit():
            return was.wsconfig(skitai.WS_GROUPCHAT, 60)
        elif was.wsopened():
            return "Client %s has entered" % was.wsclient()
        elif was.wsclosed():
            return "Client %s has leaved" % was.wsclient()
        return "Client %s Said: %s" % (was.wsclient(), message)

    vh = testutil.install_vhost_handler()
    root = confutil.getroot()
    pref = skitai.pref()
    vh.add_route("default", ("/ws", app, root), pref)
    app.access_control_allow_origin = ["http://www.skitai.com:80"]

    # WEBSOCKET
    testutil.enable_threads()
    resp = client.ws("http://www.skitai.com/ws/echo", "Hello")
    assert resp.status_code == 101

    resp = client.ws("http://www.skitai.com/ws/chat", "Hello")
    assert resp.status_code == 500
    resp = client.ws("http://www.skitai.com/ws/chat?roomid=1", "Hello")
    assert resp.status_code == 101

    testutil.disable_threads()
    resp = client.ws("http://www.skitai.com/ws/echo", "Hello")
    assert resp.status_code == 101
    resp = client.ws("http://www.skitai.com/ws/chat?roomid=1", "Hello")
    assert resp.status_code == 101
Пример #4
0
def test_params(wasc, app, client):
    def z(was):
        k = list(was.request.args.keys())
        k.sort()
        return " ".join(k)

    @app.route("/do", methods=["GET", "POST"])
    def index(was):
        return z(was)

    @app.route("/do2", methods=["GET", "POST"])
    def index2(was, a):
        return z(was)

    @app.route("/do3/<u>", methods=["GET", "POST"])
    def index3(was, **args):
        return z(was)

    @app.route("/do4/<u>", methods=["GET", "POST"])
    def index4(was, u, a, b):
        return z(was)

    @app.route("/do5/<u>", methods=["GET", "POST"])
    def index5(was, u, a):
        return z(was)

    @app.route("/do6", methods=["GET", "POST"])
    def index6(was, a):
        return z(was)

    # WSGI
    vh = testutil.install_vhost_handler()
    root = confutil.getroot()
    pref = skitai.pref()
    vh.add_route("default", ("/", app, root), pref)

    request = client.get("http://www.skitai.com/do")
    resp = assert_request(vh, request, 200)
    assert resp.text == ""

    request = client.get("http://www.skitai.com/do6?a=1")
    resp = assert_request(vh, request, 200)
    assert resp.text == "a"

    request = client.post("http://www.skitai.com/do6", {'a': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a"

    request = client.postjson("http://www.skitai.com/do6", {'a': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a"

    request = client.get("http://www.skitai.com/do?a=1")
    resp = assert_request(vh, request, 200)

    request = client.post("http://www.skitai.com/do", {'a': 1})
    resp = assert_request(vh, request, 200)

    request = client.postjson("http://www.skitai.com/do", {'a': 1})
    resp = assert_request(vh, request, 200)

    #----------------------------------------

    request = client.get("http://www.skitai.com/do2")
    resp = assert_request(vh, request, 400)

    request = client.get("http://www.skitai.com/do2?a=1")
    resp = assert_request(vh, request, 200)
    assert resp.text == "a"

    request = client.get("http://www.skitai.com/do2?a=1&b=1")
    resp = assert_request(vh, request, 400)

    request = client.post("http://www.skitai.com/do2", {'a': 1, 'b': 1})
    resp = assert_request(vh, request, 400)

    request = client.post("http://www.skitai.com/do2?a=1", {'b': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a b"

    request = client.post("http://www.skitai.com/do2", {'a': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a"

    #------------------------------------------

    request = client.post("http://www.skitai.com/do3/1", {'a': 1})
    resp = assert_request(vh, request, 200)

    request = client.post("http://www.skitai.com/do4/1?a=1", {'b': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a b u"

    request = client.post("http://www.skitai.com/do4/1?b=1", {'a': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a b u"

    request = client.post("http://www.skitai.com/do4/1?b=1", {'a': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a b u"

    request = client.post("http://www.skitai.com/do5/1?b=1", {'a': 1})
    resp = assert_request(vh, request, 400)

    request = client.post("http://www.skitai.com/do5/1?a=1", {'b': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a b u"

    request = client.postjson("http://www.skitai.com/do5/1?a=1", {'b': 1})
    resp = assert_request(vh, request, 200)
    assert resp.text == "a b u"
Пример #5
0
def test_was (wasc, app, client): 
    @app.route ("/do6/<u>")
    def index6 (was, u = "hansroh"):
        return z (was)
    
    @app.route ("/index5/<u>")
    def index5 (was, u, a, b = 3):
        return z (was)
    
    @app.route ("/index7/<u>")
    @app.authorization_required ("digest")
    def index7 (was, u, a, b = 3):
        return z (was)
        
    # WSGI
    vh = testutil.install_vhost_handler ()
    root = confutil.getroot ()
    pref = skitai.pref ()
    vh.add_route ("default", ("/", app, root), pref)
    was = wasc ()
    was.app = app
    
    for each in ("index5", "index7"):
        assert was.urlfor (each, "hans", "roh") == "/{}/hans?a=roh".format (each)
        assert was.urlfor (each, "hans", "roh") == "/{}/hans?a=roh".format (each)
        assert was.baseurl (each) == "/{}/".format (each)
        
        assert was.urlfor (each, "hans", "roh", b = 3) in ("/{}/hans?b=3&a=roh".format (each), "/{}/hans?a=roh&b=3".format (each))
        assert was.urlfor (each, "hans", a = "roh", b = 3) in ("/{}/hans?b=3&a=roh".format (each), "/{}/hans?a=roh&b=3".format (each))
        with pytest.raises(AssertionError):
            assert was.ab (each, b = 3)
        
        was.request = client.get ("http://www.skitai.com/{}/hans?a=roh".format (each))
        was.request.args = {"u": "hans", "a": "roh"} 
        assert was.partial (each, a = "vans") == "/{}/hans?a=vans".format (each)
        assert was.partial (each, b = 3) in ("/{}/hans?b=3&a=roh".format (each), "/{}/hans?a=roh&b=3".format (each))
        
    assert was.urlfor ("index6") == "/do6"
    assert was.urlfor ("index6", "jenny") == "/do6/jenny"
    assert was.urlfor ("index6", u = "jenny") == "/do6/jenny"    
    was.request.args = {"u": "hans"}
    assert was.partial ("index6") == "/do6/hans"
    
    assert was.gentemp ().startswith ("/var/tmp")
    
    was.add_cluster (skitai.PROTO_HTTP, "@test", "127.0.0.1:5000")
    assert "@test" in was.clusters_for_distcall
    
    was.add_cluster (skitai.PROTO_HTTP, "@test-1", "127.0.0.1:5000 10")
    assert "@test-1" in was.clusters_for_distcall
    
    was.add_cluster (skitai.PROTO_HTTP, "@test-1", ["127.0.0.1:5000 10"])
    assert "@test-1" in was.clusters_for_distcall
        
    assert len (str (was.timestamp)) == 13
    assert len (was.uniqid) == 20
    
    with pytest.raises(AssertionError):
        was.session
    
    del was.cookie
    app.securekey = "securekey"
    assert isinstance (was.cookie, Cookie)    
    assert isinstance (was.session, NamedSession)
    assert isinstance (was.mbox, NamedSession)
        
    assert was.txnid ().endswith ("/1000")
    assert was.rebuild_header ({}, "get") ["X-Ltxn-Id"] == "1001"
    x = was.rebuild_header ({"a": "b"}, "get")
    assert "a" in x
    assert x ["X-Ltxn-Id"] == "1002"
    x = was.rebuild_header ([("a", "b")], "get")
    assert "a" in x
    assert x ["X-Ltxn-Id"] == "1003"    
    assert was.tempfile.find ("skitai-gentemp") > 0
    
    class Response:
        def __init__ (self, *args):
            self.args = args            
    was.response = Response
    r = was.redirect ("/to")
    assert r.args [0].startswith ("302 ")
    assert r.args [1].startswith ("<html>")
    assert ('Location', '/to') in r.args [2]
    was.render ("index.html").startswith ('<!DOCTYPE html>')
    
    assert isinstance (was.email ("test", "*****@*****.**", "*****@*****.**"), composer.Composer)
    
    # tokens ------------------------------------------    
    assert was.dejwt (was.mkjwt ({"a": 1, "exp": 3000000000})) == {"a": 1, "exp": 3000000000}
    assert was.dejwt (was.mkjwt ({"a": 1, "exp": 1})) == {'err': 'token expired'}
    
    t = was.mktoken ({"a": 1})
    assert was.detoken (t) == {"a": 1}
    
    t = was.mktoken ({"a": 1}, session_key = "test")
    assert was.session ["_test_token"]    
    assert was.detoken (t) == {"a": 1}
    
    was.session ["_test_token"] = 0x00
    assert was.detoken (t) is None
    assert was.session ["_test_token"] is None
    
    t = was.mktoken ({"a": 1}, session_key = "test")
    assert was.detoken (t) == {"a": 1}
    was.rmtoken (t)
    assert was.session ["_test_token"] is None
    assert was.detoken (t) is None
    
    t = was.mktoken ([1, 2])
    assert was.detoken (t) == [1, 2]
    
    t = was.csrf_token
    was.csrf_token_input.find (was.session [was.CSRF_NAME]) > 0
    
    class Request:
        args = {was.CSRF_NAME: t}
    
    was.request = Request
    
    was.request.args [was.CSRF_NAME] = 0x00
    assert not was.csrf_verify (True)    
    was.request.args [was.CSRF_NAME] = t
    assert was.csrf_verify (True)
    assert was.session [was.CSRF_NAME] == t
    
    assert was.csrf_verify ()
    assert was.session [was.CSRF_NAME] is None
    
    with pytest.raises(AttributeError):
        was.django
Пример #6
0
import skitai
import tfserver
import tensorflow as tf
import os
import sys

def load_latest_model (model_name, subdir = None, per_process_gpu_memory_fraction = 0.03):    
    loc = os.path.normpath(subdir)    
    if not os.path.isdir (loc) or not os.listdir (loc):
        return
    version = max ([int (ver) for ver in os.listdir (loc) if ver.isdigit () and os.path.isdir (os.path.join (loc, ver))])    
    model_path = os.path.join (loc, str (version))
    tfconfig = tf.ConfigProto(log_device_placement = False)
    pref.config.tf_models [model_name] = (model_path, tfconfig)    


if __name__ == "__main__":    
    pref = skitai.pref ()
    pref.max_client_body_size = 100 * 1024 * 1024 # 100 MB
    pref.debug = True
    pref.use_reloader = True
    pref.access_control_allow_origin = ["*"]
    pref.config.tf_models = {}
    load_latest_model ("test", skitai.joinpath ("models"), 0.1)
    skitai.mount ("/", tfserver, pref = pref)
    skitai.run (port = 5000, name = "tfms")