def test_auth_missing_body(intgr_config):
    with Session(config=intgr_config) as sess:
        request = Request(sess, 'GET', '/auth')
        with pytest.raises(BackendAPIError) as e:
            with request.fetch():
                pass
        assert e.value.status == 400
def test_kernel_execution_with_vfolder_mounts(intgr_config):
    with Session(config=intgr_config) as sess:
        vfname = 'vftest-' + token_hex(4)
        sess.VFolder.create(vfname)
        vfolder = sess.VFolder(vfname)
        try:
            with tempfile.NamedTemporaryFile('w', suffix='.py',
                                             dir=Path.cwd()) as f:
                f.write('print("hello world")\nx = 1 / 0\n')
                f.flush()
                f.seek(0)
                vfolder.upload([f.name])
            kernel = sess.Kernel.get_or_create('python:latest',
                                               mounts=[vfname])
            try:
                console, n = exec_loop(
                    kernel, 'batch', '', {
                        'build': '',
                        'exec': 'python {}/{}'.format(vfname,
                                                      Path(f.name).name),
                    })
                assert 'hello world' in console['stdout']
                assert 'ZeroDivisionError' in console['stderr']
                assert len(console['media']) == 0
            finally:
                kernel.destroy()
        finally:
            vfolder.delete()
def test_auth_malformed(intgr_config):
    with Session(config=intgr_config) as sess:
        request = Request(sess, 'GET', '/auth')
        request.set_content(
            b'<this is not json>',
            content_type='application/json',
        )
        with pytest.raises(BackendAPIError) as e:
            with request.fetch():
                pass
        assert e.value.status == 400
def test_auth(intgr_config):
    random_msg = uuid.uuid4().hex
    with Session(config=intgr_config) as sess:
        request = Request(sess, 'GET', '/auth')
        request.set_json({
            'echo': random_msg,
        })
        with request.fetch() as resp:
            assert resp.status == 200
            data = resp.json()
            assert data['authorized'] == 'yes'
            assert data['echo'] == random_msg
def test_not_found(intgr_config):
    with Session(config=intgr_config) as sess:
        request = Request(sess, 'GET', '/invalid-url-wow')
        with pytest.raises(BackendAPIError) as e:
            with request.fetch():
                pass
        assert e.value.status == 404
        request = Request(sess, 'GET', '/auth/uh-oh')
        with pytest.raises(BackendAPIError) as e:
            with request.fetch():
                pass
        assert e.value.status == 404
def test_kernel_get_or_create_reuse(intgr_config):
    with Session(config=intgr_config) as sess:
        try:
            # Sessions with same token and same language must be reused.
            t = token_hex(6)
            kernel1 = sess.Kernel.get_or_create('python:latest',
                                                client_token=t)
            kernel2 = sess.Kernel.get_or_create('python:latest',
                                                client_token=t)
            assert kernel1.kernel_id == kernel2.kernel_id
        finally:
            kernel1.destroy()
Esempio n. 7
0
 def test_auth_missing_signature(self, monkeypatch):
     random_msg = uuid.uuid4().hex
     with Session() as sess:
         rqst = Request(sess, 'GET', '/auth')
         rqst.set_json({'echo': random_msg})
         # let it bypass actual signing
         from ai.backend.client import request
         noop_sign = lambda *args, **kwargs: ({}, None)
         monkeypatch.setattr(request, 'generate_signature', noop_sign)
         with pytest.raises(BackendAPIError) as e:
             with rqst.fetch():
                 pass
         assert e.value.status == 401
def test_kernel_lifecycles(intgr_config):
    with Session(config=intgr_config) as sess:
        kernel = sess.Kernel.get_or_create('python:latest')
        kernel_id = kernel.kernel_id
        info = kernel.get_info()
        # the tag may be different depending on alias/metadata config.
        lang = info['lang']
        assert lang.startswith('python:') or lang.startswith('lablup/python:')
        assert info['numQueriesExecuted'] == 1
        info = kernel.get_info()
        assert info['numQueriesExecuted'] == 2
        kernel.destroy()
        # kernel destruction is no longer synchronous!
        time.sleep(2.0)
        with pytest.raises(BackendAPIError) as e:
            info = sess.Kernel(kernel_id).get_info()
        assert e.value.status == 404
def test_connection(intgr_config):
    with Session(config=intgr_config) as sess:
        request = Request(sess, 'GET', '/')
        with request.fetch() as resp:
            assert 'version' in resp.json()
def py3_kernel(intgr_config):
    with Session(config=intgr_config) as sess:
        kernel = sess.Kernel.get_or_create('python:latest')
        yield kernel
        kernel.destroy()