def test_task_get_by_task_id3(client): """Test getting a task by task id fails if task id is empty""" msg = "Task ID is required and can't be None or empty." with expect.error_to_happen(RuntimeError, message=msg): Task.get_by_task_id(None) with expect.error_to_happen(RuntimeError, message=msg): Task.get_by_task_id("")
def test_enqueue2(client): """Test enqueue a job with the same task does not create a new task""" task_id = str(uuid4()) data = {"image": "ubuntu", "command": "ls"} options = dict( data=dumps(data), headers={"Content-Type": "application/json"}, follow_redirects=True, ) rv = client.post(f"/tasks/{task_id}", **options) expect(rv.status_code).to_equal(200) obj = loads(rv.data) job_id = obj["jobId"] expect(job_id).not_to_be_null() expect(obj["queueJobId"]).not_to_be_null() rv = client.post(f"/tasks/{task_id}", **options) expect(rv.status_code).to_equal(200) obj = loads(rv.data) job_id = obj["jobId"] expect(job_id).not_to_be_null() expect(obj["queueJobId"]).not_to_be_null() task = Task.get_by_task_id(obj["taskId"]) expect(task).not_to_be_null() expect(task.jobs).not_to_be_empty() with client.application.app_context(): count = Task.objects.count() expect(count).to_equal(1)
def test_task_get_by_task_id(client): """Test getting a task by task id""" task_id = str(uuid4()) t = Task.create_task(task_id) topic = Task.get_by_task_id(t.task_id) expect(topic.id).to_equal(t.id)
def get_task(task_id): logger = g.logger.bind(operation="get_task", task_id=task_id) logger.debug("Getting job...") task = Task.get_by_task_id(task_id) if task is None: logger.error("Task not found.") abort(404) return logger.debug("Task retrieved successfully...") jobs = [] for job_id in task.jobs: url = url_for("task.get_job", task_id=task_id, job_id=str(job_id.id), _external=True) job = {"id": str(job_id.id), "url": url} jobs.append(job) return jsonify({"taskId": task_id, "jobs": jobs})
def test_enqueue1(client): """Test enqueue a job works""" task_id = str(uuid4()) data = {"image": "ubuntu", "command": "ls"} rv = client.post(f"/tasks/{task_id}", data=dumps(data), follow_redirects=True) expect(rv.status_code).to_equal(200) obj = loads(rv.data) job_id = obj["jobId"] expect(job_id).not_to_be_null() expect(obj["queueJobId"]).not_to_be_null() app = client.application task = Task.get_by_task_id(obj["taskId"]) with app.app_context(): expect(obj["taskUrl"]).to_equal(task.get_url()) queue_job_id = obj["queueJobId"] hash_key = f"rq:job:{queue_job_id}" res = app.redis.exists(hash_key) expect(res).to_be_true() res = app.redis.hget(hash_key, "status") expect(res).to_equal("queued") res = app.redis.hexists(hash_key, "created_at") expect(res).to_be_true() res = app.redis.hexists(hash_key, "enqueued_at") expect(res).to_be_true() res = app.redis.hexists(hash_key, "data") expect(res).to_be_true() res = app.redis.hget(hash_key, "origin") expect(res).to_equal("jobs") res = app.redis.hget(hash_key, "description") expect(res).to_equal( f"fastlane.worker.job.run_job('{obj['taskId']}', '{job_id}', 'ubuntu', 'ls')" ) res = app.redis.hget(hash_key, "timeout") expect(res).to_equal("-1") expect(task).not_to_be_null() expect(task.jobs).not_to_be_empty() j = task.jobs[0] expect(str(j.id)).to_equal(job_id) q = "rq:queue:jobs" res = app.redis.llen(q) expect(res).to_equal(1) res = app.redis.lpop(q) expect(res).to_equal(queue_job_id) with app.app_context(): count = Task.objects.count() expect(count).to_equal(1)
def test_task_get_by_task_id2(client): """Test getting a task by task id returns None if no task exists""" task_id = str(uuid4()) topic = Task.get_by_task_id(task_id) expect(topic).to_be_null()
def test_enqueue1(client): """Test enqueue a job works""" task_id = str(uuid4()) data = { "image": "ubuntu", "command": "ls", } rv = client.post(f'/tasks/{task_id}', data=dumps(data), follow_redirects=True) expect(rv.status_code).to_equal(200) obj = loads(rv.data) job_id = obj['jobId'] expect(job_id).not_to_be_null() expect(obj['queueJobId']).not_to_be_null() queue_job_id = obj["queueJobId"] hash_key = f'rq:job:{queue_job_id}' app = client.application res = app.redis.exists(hash_key) expect(res).to_be_true() res = app.redis.hget(hash_key, 'status') expect(res).to_equal('queued') res = app.redis.hexists(hash_key, 'created_at') expect(res).to_be_true() res = app.redis.hexists(hash_key, 'enqueued_at') expect(res).to_be_true() res = app.redis.hexists(hash_key, 'data') expect(res).to_be_true() res = app.redis.hget(hash_key, 'origin') expect(res).to_equal('jobs') res = app.redis.hget(hash_key, 'description') expect(res).to_equal( f"fastlane.worker.job.run_job('{obj['taskId']}', '{job_id}', 'ubuntu', 'ls')" ) res = app.redis.hget(hash_key, 'timeout') expect(res).to_equal('-1') task = Task.get_by_task_id(obj['taskId']) expect(task).not_to_be_null() expect(task.jobs).not_to_be_empty() j = task.jobs[0] expect(str(j.id)).to_equal(job_id) q = 'rq:queue:jobs' res = app.redis.llen(q) expect(res).to_equal(1) res = app.redis.lpop(q) expect(res).to_equal(queue_job_id) with client.application.app_context(): count = Task.objects.count() expect(count).to_equal(1)