82 строки
2.0 KiB
Python
82 строки
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from mozci.errors import ArtifactNotFound, TaskNotFound
|
|
from mozci.task import Task
|
|
from mozci.util.taskcluster import get_artifact_url, get_index_url
|
|
|
|
|
|
@pytest.fixture
|
|
def create_task():
|
|
id = 0
|
|
|
|
def inner(**kwargs):
|
|
nonlocal id
|
|
task = Task.create(id=id, **kwargs)
|
|
id += 1
|
|
return task
|
|
|
|
return inner
|
|
|
|
|
|
def test_missing_artifacts(responses, create_task):
|
|
artifact = "public/artifact.txt"
|
|
task = create_task(label="foobar")
|
|
|
|
# First we'll check the new deployment.
|
|
responses.add(
|
|
responses.GET, get_artifact_url(task.id, artifact), status=404,
|
|
)
|
|
|
|
# Then we'll check the old deployment.
|
|
responses.add(
|
|
responses.GET,
|
|
get_artifact_url(task.id, artifact, old_deployment=True),
|
|
status=404,
|
|
)
|
|
|
|
with pytest.raises(ArtifactNotFound):
|
|
task.get_artifact(artifact)
|
|
|
|
|
|
def test_create(responses):
|
|
# Creating a task with just a label doesn't work.
|
|
with pytest.raises(TypeError):
|
|
Task.create(label="foobar")
|
|
|
|
# Specifying an id works with or without label.
|
|
assert Task.create(id=1, label="foobar").label == "foobar"
|
|
assert Task.create(id=1).label is None
|
|
|
|
# Can also specify an index.
|
|
index = "index.path"
|
|
responses.add(
|
|
responses.GET, get_index_url(index), json={"taskId": 1}, status=200,
|
|
)
|
|
assert Task.create(index=index, label="foobar").label == "foobar"
|
|
assert Task.create(index=index).label is None
|
|
|
|
# Specifying non-existent task index raises.
|
|
responses.replace(responses.GET, get_index_url(index), status=404)
|
|
with pytest.raises(TaskNotFound):
|
|
Task.create(index=index)
|
|
|
|
|
|
def test_to_json():
|
|
kwargs = {
|
|
"id": 1,
|
|
"label": "foobar",
|
|
"result": "pass",
|
|
"duration": 100,
|
|
}
|
|
task = Task.create(**kwargs)
|
|
result = task.to_json()
|
|
json.dumps(result) # assert doesn't raise
|
|
|
|
for k, v in kwargs.items():
|
|
assert k in result
|
|
assert result[k] == v
|