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
def test_execute_code_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):
        execute_code(kernel_id, 'hello')
def test_execute_code_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)
    execute_code(kernel_id, 'hello')

    mock_req.assert_called_once_with('POST', '/kernel/{}'.format(kernel_id), {
        'mode': 'query',
        'code': 'hello'
    })
    mock_req_obj.send.assert_called_once_with()
    mock_req_obj.send.return_value.json.assert_called_once_with()
Esempio n. 4
0
def run_execute_code(kid):
    # 2nd params is currently ignored.
    if kid is not None:
        begin = time.monotonic()
        result = execute_code(kid, sample_code)
        print(result['stdout'])
        end = time.monotonic()
        return end - begin
    return None
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