コード例 #1
0
def test_get_kernel_info_raises_err_with_abnormal_status(mocker):
    mock_req_obj = mock.Mock()
    mock_req_obj.send.return_value = mock.MagicMock(status=400)
    mocker.patch('ai.backend.client.kernel.Request', return_value=mock_req_obj)

    kernel_id = token_hex(12)
    with pytest.raises(BackendAPIError):
        get_kernel_info(kernel_id)
コード例 #2
0
def test_get_kernel_info_url(mocker):
    mock_req_obj = mock.Mock()
    mock_req_obj.send.return_value = mock.MagicMock(status=200)
    mock_req = mocker.patch('ai.backend.client.kernel.Request',
                            return_value=mock_req_obj)

    kernel_id = token_hex(12)
    get_kernel_info(kernel_id)

    mock_req.assert_called_once_with('GET', '/kernel/{}'.format(kernel_id))
    mock_req_obj.send.assert_called_once_with()
    mock_req_obj.send.return_value.json.assert_called_once_with()
コード例 #3
0
def test_kernel_lifecycles(defconfig):
    kernel_id = create_kernel('python3')
    info = get_kernel_info(kernel_id)
    assert info['lang'] == 'python3'
    assert info['age'] > 0
    assert info['numQueriesExecuted'] == 0
    info = get_kernel_info(kernel_id)
    assert info['numQueriesExecuted'] == 1
    destroy_kernel(kernel_id)
    # kernel destruction is no longer synchronous!
    time.sleep(2.0)
    with pytest.raises(BackendAPIError) as e:
        info = get_kernel_info(kernel_id)
    assert e.value.args[0] == 404
コード例 #4
0
def test_kernel_restart(defconfig, py3_kernel):
    kernel_id = py3_kernel
    first_code = textwrap.dedent('''
    a = "first"
    with open("test.txt", "w") as f:
        f.write("helloo?")
    print(a)
    ''').strip()
    second_code_name_error = textwrap.dedent('''
    print(a)
    ''').strip()
    second_code_file_check = textwrap.dedent('''
    with open("test.txt", "r") as f:
        print(f.read())
    ''').strip()
    result = execute_code(kernel_id, first_code)
    console = aggregate_console(result['console'])
    assert 'first' in console['stdout']
    assert console['stderr'] == ''
    assert len(console['media']) == 0
    restart_kernel(kernel_id)
    result = execute_code(kernel_id, second_code_name_error)
    console = aggregate_console(result['console'])
    assert 'NameError' in console['stderr']
    assert len(console['media']) == 0
    result = execute_code(kernel_id, second_code_file_check)
    console = aggregate_console(result['console'])
    assert 'helloo?' in console['stdout']
    assert console['stderr'] == ''
    assert len(console['media']) == 0
    info = get_kernel_info(kernel_id)
    # FIXME: this varies between 2~4
    assert 2 <= info['numQueriesExecuted'] <= 4
コード例 #5
0
def test_kernel_execution(defconfig, py3_kernel):
    kernel_id = py3_kernel
    result = execute_code(kernel_id, 'print("hello world")')
    console = aggregate_console(result['console'])
    assert 'hello world' in console['stdout']
    assert console['stderr'] == ''
    assert len(console['media']) == 0
    info = get_kernel_info(kernel_id)
    assert info['numQueriesExecuted'] == 1