-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVEMENT] Add
promgen register-job
to register job from command…
… line #192
- Loading branch information
Showing
4 changed files
with
82 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (c) 2019 LINE Corporation | ||
# These sources are released under the terms of the MIT license: see LICENSE | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from promgen import models, util | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
help_text = util.HelpFor(models.Exporter) | ||
|
||
parser.add_argument("project", help="Existing Project") | ||
parser.add_argument("job", help=help_text.job) | ||
parser.add_argument("port", type=int, help=help_text.port) | ||
parser.add_argument("path", default="", nargs="?", help=help_text.path) | ||
parser.add_argument("--enabled", default=False, action="store_true", help=help_text.enabled) | ||
|
||
def handle(self, **kwargs): | ||
try: | ||
project = models.Project.objects.get(name=kwargs["project"]) | ||
except models.Project.DoesNotExist: | ||
raise CommandError("Unable to find Project :%s" % kwargs["project"]) | ||
|
||
job, created = models.Exporter.objects.get_or_create( | ||
project=project, | ||
job=kwargs["job"], | ||
port=kwargs["port"], | ||
defaults={"path": kwargs["path"], "enabled": kwargs["enabled"]}, | ||
) | ||
if created: | ||
print("Registered job", job) | ||
else: | ||
print("Found existing job", job) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) 2019 LINE Corporation | ||
# These sources are released under the terms of the MIT license: see LICENSE | ||
|
||
from promgen import models | ||
from promgen.tests import PromgenTest | ||
|
||
from django.core import management | ||
from django.core.management.base import CommandError | ||
|
||
|
||
class CLITests(PromgenTest): | ||
def test_register_job(self): | ||
# Assert when project doesn't exist | ||
with self.assertRaises(CommandError): | ||
management.call_command("register-job", "missing-project", "example", 1234) | ||
|
||
# Create a Service and Project and then try adding our job | ||
shard = models.Shard.objects.create(name="TestShard") | ||
service = models.Service.objects.create(name="TestService") | ||
_ = models.Project.objects.create(name="TestProject", service=service, shard=shard) | ||
management.call_command("register-job", "TestProject", "example", 1234) | ||
|
||
# Ensure the jobs we expect exist | ||
self.assertCount(models.Exporter, 1) | ||
|
||
# Registering the same job again shouldn't change our count | ||
management.call_command("register-job", "TestProject", "example", 1234) | ||
self.assertCount(models.Exporter, 1) | ||
|
||
# But registering a new one will | ||
management.call_command("register-job", "TestProject", "example", 4321) | ||
self.assertCount(models.Exporter, 2) |
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