Esempio n. 1
0
async def test_post(core4api, worker):
    worker.start(1)
    await core4api.login()
    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "core4.queue.helper.job.example.DummyJob"},
        request_timeout=60.)
    assert resp.code == 200
    states, logs = parse_sse(resp.body)
    assert 'complete' in set(sorted(set([i["state"] for i in states])))
    assert 'running' in set(sorted(set([i["state"] for i in states])))

    assert 1 == sum(
        [1 for i in logs if i["message"].startswith("start execution")])

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.MyJob"},
        request_timeout=60.)
    assert resp.code == 200

    states, logs = parse_sse(resp.body)
    assert 'complete' in set(sorted(set([i["state"] for i in states])))
    assert 'running' in set(sorted(set([i["state"] for i in states])))
    assert 300 == sum(
        [1 for i in logs if i["message"].startswith("hello world")])
    worker.stop()
Esempio n. 2
0
async def test_job_list(core4api, worker, mongodb):
    worker.start(1)
    worker.stop()
    await core4api.login()

    resp = await core4api.get('/core4/api/v1/job/list', request_timeout=120.)
    assert resp.code == 200
    pprint(resp.json())
Esempio n. 3
0
async def test_error_job(core4api, worker, mongodb):
    worker.start(1)
    await core4api.login()

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.ErrorJob", "follow": True},
        request_timeout=120.
    )
    assert resp.code == 200
    worker.stop()
    states, logs = parse_sse(resp.body)
    assert 3 == sum(
        [1 for i in logs if i["message"].startswith("start execution")])
Esempio n. 4
0
async def test_enqueue_kill_remove(core4api, worker, mongodb):
    worker.start(1)
    await core4api.login()

    async def _kill(_id):
        await gen.sleep(6)
        resp = await core4api.put(
            '/core4/api/v1/job/kill/' + _id,
            request_timeout=220.
        )
        assert resp.code == 200
        print("*" * 80)
        print(resp.body.decode("utf-8"))

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "core4.queue.helper.job.example.DummyJob",
              "args": {"sleep": 30}, "follow": False},
        request_timeout=120.
    )
    assert resp.code == 200

    job_id = resp.json()["data"]

    loop = IOLoop.current()
    loop.spawn_callback(_kill, job_id)

    resp = await core4api.get(
        '/core4/api/v1/job/follow/' + job_id,
        request_timeout=120.
    )
    assert resp.code == 200
    states, logs = parse_sse(resp.body)
    assert 'killed' in [i["state"] for i in states]

    resp = await core4api.put(
        '/core4/api/v1/job/remove/' + job_id,
        request_timeout=120.
    )
    assert resp.code == 200

    worker.wait_queue()

    resp = await core4api.put(
        '/core4/api/v1/job/remove/' + job_id,
        request_timeout=120.
    )
    assert resp.code == 404
    worker.wait_queue()
    worker.stop()
Esempio n. 5
0
async def test_follow(core4api, worker, mongodb):
    worker.start(1)
    await core4api.login()
    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.MyJob", "follow": False})
    assert resp.code == 200
    job_id = resp.json()["data"]
    print(job_id)

    resp = await core4api.get('/core4/api/v1/job/follow/' + job_id,
                              request_timeout=60.)
    assert resp.code == 200
    states, logs = parse_sse(resp.body)
    assert 300 == sum(
        [1 for i in logs if i["message"].startswith("hello world")])
    worker.stop()
Esempio n. 6
0
async def test_syntax_error(core4api, worker):
    worker.start(1)
    await core4api.login()

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.ZeroJob", "follow": True},
        request_timeout=120.
    )
    assert resp.code == 200
    states, logs = parse_sse(resp.body)
    print(">>>")
    pprint(["{} - {}".format(i["mode"], i["message"]) for i in logs])
    print(">>>")
    pprint([(i["state"], i["trial"], i["attempts_left"]) for i in states])
    worker.stop()
    assert 'running' in set(sorted(set([i["state"] for i in states])))
    assert 'error' in set(sorted(set([i["state"] for i in states])))
Esempio n. 7
0
async def test_error(core4api, worker):
    worker.start(1)
    await core4api.login()

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.ErrorJob", "follow": True},
        request_timeout=120.
    )
    assert resp.code == 200
    states, logs = parse_sse(resp.body)
    print(">>>")
    pprint(["{} - {}".format(i["mode"], i["message"]) for i in logs])
    print(">>>")
    pprint([(i["state"], i["trial"], i["attempts_left"]) for i in states])
    worker.stop()
    assert sum([1 for i in logs if "start execution" in i["message"]]) == 3
    assert sum([1 for i in logs if "done execution with [failed]" in i["message"]]) == 2
    assert sum([1 for i in logs if "done execution with [error]" in i["message"]]) == 1
Esempio n. 8
0
async def test_enqueue_kill_restart(core4api, worker, mongodb):
    worker.start(1)
    await core4api.login()

    async def _kill(_id):
        await gen.sleep(10)
        resp = await core4api.put(
            '/core4/api/v1/job/kill/' + _id,
            request_timeout=220.
        )
        assert resp.code == 200

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.MyJob2", "follow": False},
        request_timeout=120.
    )
    assert resp.code == 200

    job_id = resp.json()["data"]

    loop = IOLoop.current()
    loop.spawn_callback(_kill, job_id)

    resp = await core4api.get(
        '/core4/api/v1/job/follow/' + job_id,
        request_timeout=220.
    )
    assert resp.code == 200

    resp = await core4api.put(
        '/core4/api/v1/job/restart/' + job_id,
        request_timeout=120.
    )
    assert resp.code == 200

    states, logs = parse_sse(resp.body)
    assert 600 == sum(
        [1 for i in logs if i["message"].startswith("hello world")])

    worker.wait_queue()
    worker.stop()
Esempio n. 9
0
async def test_job_log(core4api, worker, mongodb):
    worker.start(1)
    await core4api.login()

    resp = await core4api.post(
        '/core4/api/v1/job',
        json={"qual_name": "tests.api.test_job2.MyJob1", "follow": False},
        request_timeout=120.
    )
    assert resp.code == 200
    jid = resp.json()["data"]

    resp = await core4api.get(
        '/core4/api/v1/job/log/' + jid + "?follow=true",
        request_timeout=120.
    )
    assert resp.code == 200
    body = resp.body.decode("utf-8")
    worker.wait_queue()
    worker.stop()
    page = 0
    while True:
        resp = await core4api.get(
            '/core4/api/v1/job/log/' + jid + "?page=" + str(page),
            request_timeout=220.)
        assert resp.code == 200
        print("*" * 10, page, resp.json()["page_count"],
              resp.json()["total_count"])
        assert resp.json()["page_count"] == 31
        assert resp.json()["total_count"] == 310
        page += 1
        for doc in resp.json()["data"]:
            print(doc["message"])
        if page >= resp.json()["page_count"]:
            break
    data = [i.split("\n") for i in body.split("\n\n") if i]
    logs = [json.loads(i[1][6:]) for i in data if i[0].endswith("log")]
    assert 300 == sum([1 for i in logs if i["message"].startswith("hello")])