-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rachel Chen
authored and
Rachel Chen
committed
Sep 18, 2024
1 parent
9b2b0ab
commit 5253427
Showing
6 changed files
with
149 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
from typing import Any, cast | ||
from typing import cast | ||
|
||
from snuba.manual_jobs import Job | ||
from snuba.manual_jobs import Job, JobSpec | ||
|
||
|
||
class JobLoader: | ||
@staticmethod | ||
def get_job_instance(class_name: str, dry_run: bool, **kwargs: Any) -> "Job": | ||
job_type_class = Job.class_from_name(class_name) | ||
def get_job_instance(job_spec: JobSpec, dry_run: bool) -> "Job": | ||
job_type_class = Job.class_from_name(job_spec.job_type) | ||
if job_type_class is None: | ||
raise Exception( | ||
f"Job does not exist. Did you make a file {class_name}.py yet?" | ||
f"Job does not exist. Did you make a file {job_spec.job_type}.py yet?" | ||
) | ||
|
||
return cast("Job", job_type_class(dry_run=dry_run, **kwargs)) | ||
return cast("Job", job_type_class(job_spec, dry_run=dry_run)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
import os | ||
from typing import Any, Sequence | ||
from typing import Mapping | ||
|
||
import simplejson | ||
|
||
from snuba.manual_jobs import JobSpec | ||
|
||
|
||
class _ManifestReader: | ||
@staticmethod | ||
def read(filename: str) -> Sequence[Any]: | ||
def read(filename: str) -> Mapping[str, JobSpec]: | ||
local_root = os.path.dirname(__file__) | ||
|
||
with open(os.path.join(local_root, filename)) as stream: | ||
contents = simplejson.loads(stream.read()) | ||
assert isinstance(contents, Sequence) | ||
return contents | ||
|
||
job_specs = {} | ||
for content in contents: | ||
job_id = content["id"] | ||
assert isinstance(job_id, str) | ||
job_type = content["job_type"] | ||
assert isinstance(job_type, str) | ||
|
||
job_spec = JobSpec( | ||
job_id=job_id, | ||
job_type=job_type, | ||
params=content.get("params"), | ||
) | ||
job_specs[job_id] = job_spec | ||
return job_specs | ||
|
||
|
||
def read_jobs_manifest() -> Sequence[Any]: | ||
def read_jobs_manifest() -> Mapping[str, JobSpec]: | ||
return _ManifestReader.read("job_manifest.json") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,71 @@ | ||
from click.testing import CliRunner | ||
|
||
from snuba.cli.jobs import run | ||
from snuba.cli.jobs import JOB_SPECIFICATION_ERROR_MSG, run, run_from_manifest | ||
|
||
|
||
def test_valid_job() -> None: | ||
def test_cmd_line_valid() -> None: | ||
runner = CliRunner() | ||
result = runner.invoke(run, ["ToyJob", "--dry_run", "True", "k1=v1", "k2=v2"]) | ||
result = runner.invoke( | ||
run, | ||
["--job_type", "ToyJob", "--job_id", "0001"], | ||
) | ||
|
||
assert result.exit_code == 0 | ||
|
||
|
||
def test_invalid_job() -> None: | ||
def test_invalid_job_errors() -> None: | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
run, | ||
[ | ||
"--job_type", | ||
"NonexistentJob", | ||
"--job_id", | ||
"0001", | ||
"--dry_run", | ||
"True", | ||
"k1=v1", | ||
"k2=v2", | ||
], | ||
) | ||
|
||
assert result.exit_code == 1 | ||
|
||
|
||
def test_cmd_line_no_job_specification_errors() -> None: | ||
runner = CliRunner() | ||
result = runner.invoke(run, ["--dry_run", "True", "k1=v1", "k2=v2"]) | ||
assert result.exit_code == 1 | ||
assert result.output == "Error: " + JOB_SPECIFICATION_ERROR_MSG + "\n" | ||
|
||
|
||
def test_cmd_line_no_job_id_errors() -> None: | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
run, ["--job_type", "ToyJob", "--dry_run", "True", "k1=v1", "k2=v2"] | ||
) | ||
assert result.exit_code == 1 | ||
assert result.output == "Error: " + JOB_SPECIFICATION_ERROR_MSG + "\n" | ||
|
||
|
||
def test_cmd_line_no_job_type_errors() -> None: | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
run, ["SomeJobThatDoesntExist", "--dry_run", "True", "k1=v1", "k2=v2"] | ||
run, ["--job_id", "0001", "--dry_run", "True", "k1=v1", "k2=v2"] | ||
) | ||
assert result.exit_code == 1 | ||
assert result.output == "Error: " + JOB_SPECIFICATION_ERROR_MSG + "\n" | ||
|
||
|
||
def test_json_valid() -> None: | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
run_from_manifest, | ||
[ | ||
"--json_manifest", | ||
"run_manifest.json", | ||
"--job_id", | ||
"abc1234", | ||
], | ||
) | ||
assert result.exit_code == 0 |