You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aurora.apache.org by mc...@apache.org on 2014/10/07 19:39:39 UTC

git commit: Change JSON result of "job status" when job isn't found.

Repository: incubator-aurora
Updated Branches:
  refs/heads/master 3667af1e9 -> fab67b256


Change JSON result of "job status" when job isn't found.

Instead of just exiting with an error code, return a valid JSON
structure when a user calls "aurora job status", and no job matching
the jobspec is running.

Also, remove a bunch of "pants" labels from build files, which generated
warnings while building the tests.

Testing Done:
Ran all unit tests; added new test for the empty job list json case.

Bugs closed: aurora-803

Reviewed at https://reviews.apache.org/r/26417/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/fab67b25
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/fab67b25
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/fab67b25

Branch: refs/heads/master
Commit: fab67b25663aa0b60b8d71aab859ab9e6235d032
Parents: 3667af1
Author: Mark Chu-Carroll <mc...@twopensource.com>
Authored: Tue Oct 7 13:34:55 2014 -0400
Committer: Mark Chu-Carroll <mc...@twitter.com>
Committed: Tue Oct 7 13:34:55 2014 -0400

----------------------------------------------------------------------
 .../python/apache/aurora/client/cli/context.py  |  4 ++++
 .../python/apache/aurora/client/cli/jobs.py     | 21 ++++++++++++----
 src/test/python/apache/aurora/client/cli/BUILD  | 12 +++++-----
 .../apache/aurora/client/cli/test_status.py     | 25 ++++++++++++++++++--
 4 files changed, 50 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fab67b25/src/main/python/apache/aurora/client/cli/context.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/context.py b/src/main/python/apache/aurora/client/cli/context.py
index 0816ac0..4e94a4e 100644
--- a/src/main/python/apache/aurora/client/cli/context.py
+++ b/src/main/python/apache/aurora/client/cli/context.py
@@ -151,6 +151,10 @@ class AuroraCommandContext(Context):
       parts.append('*')
     return PartialJobKey(*parts)
 
+  @classmethod
+  def render_partial_jobkey(cls, jobkey):
+    return "%s/%s/%s/%s" % jobkey
+
   def get_job_list(self, clusters, role=None):
     """Get a list of jobs from a group of clusters.
     :param clusters: the clusters to query for jobs

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fab67b25/src/main/python/apache/aurora/client/cli/jobs.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/jobs.py b/src/main/python/apache/aurora/client/cli/jobs.py
index 103fb53..0277cbe 100644
--- a/src/main/python/apache/aurora/client/cli/jobs.py
+++ b/src/main/python/apache/aurora/client/cli/jobs.py
@@ -606,25 +606,38 @@ class StatusCommand(Verb):
       else:
         result.append(self.render_tasks_pretty(jk, active_tasks, inactive_tasks))
     if result == []:
-      context.print_err("No matching jobs found")
       return None
     if context.options.write_json:
       return json.dumps(result, indent=2, separators=[',', ': '], sort_keys=False)
     else:
       return ''.join(result)
 
+  def _print_jobs_not_found(self, context):
+    if context.options.write_json:
+      context.print_out(json.dumps(
+        {"jobspec": context.render_partial_jobkey(context.options.jobspec),
+         "error": "No matching jobs found"},
+        separators=[",", ":"],
+        sort_keys=False))
+      # If users are scripting, they don't want an error code if they were returned
+      # a valid json answer.
+      return EXIT_OK
+    else:
+      context.print_err("Found no jobs matching %s" %
+          context.render_partial_jobkey(context.options.jobspec))
+      return EXIT_INVALID_PARAMETER
+
   def execute(self, context):
     jobs = context.get_jobs_matching_key(context.options.jobspec)
     if jobs is None or jobs == []:
-      context.print_err("Found no jobs matching %s" % context.options.jobspec)
-      return EXIT_INVALID_PARAMETER
+      return self._print_jobs_not_found()
 
     result = self.get_status_for_jobs(jobs, context)
     if result is not None:
       context.print_out(result)
       return EXIT_OK
     else:
-      return EXIT_INVALID_PARAMETER
+      return self._print_jobs_not_found(context)
 
 
 class UpdateCommand(Verb):

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fab67b25/src/test/python/apache/aurora/client/cli/BUILD
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/BUILD b/src/test/python/apache/aurora/client/cli/BUILD
index 8ce6bd5..d33e866 100644
--- a/src/test/python/apache/aurora/client/cli/BUILD
+++ b/src/test/python/apache/aurora/client/cli/BUILD
@@ -57,12 +57,12 @@ python_tests(
   name='supdate',
   sources = [ 'test_supdate.py' ],
   dependencies = [
-    pants(':util'),
-    pants('3rdparty/python:mock'),
-    pants('3rdparty/python:twitter.common.contextutil'),
-    pants('src/main/python/apache/aurora/client/cli'),
-    pants('src/main/python/apache/aurora/client/cli:client_lib'),
-    pants('src/test/python/apache/aurora/client/commands:util')
+    ':util',
+    '3rdparty/python:mock',
+    '3rdparty/python:twitter.common.contextutil',
+    'src/main/python/apache/aurora/client/cli',
+    'src/main/python/apache/aurora/client/cli:client_lib',
+    'src/test/python/apache/aurora/client/commands:util'
   ]
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fab67b25/src/test/python/apache/aurora/client/cli/test_status.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/test_status.py b/src/test/python/apache/aurora/client/cli/test_status.py
index bd30b11..38ffdb8 100644
--- a/src/test/python/apache/aurora/client/cli/test_status.py
+++ b/src/test/python/apache/aurora/client/cli/test_status.py
@@ -18,7 +18,7 @@ import textwrap
 
 from mock import Mock, patch
 
-from apache.aurora.client.cli import EXIT_INVALID_PARAMETER
+from apache.aurora.client.cli import EXIT_INVALID_PARAMETER, EXIT_OK
 from apache.aurora.client.cli.client import AuroraCommandLine
 from apache.aurora.client.cli.util import AuroraClientCommandTest, FakeAuroraCommandContext
 from apache.aurora.common.aurora_job_key import AuroraJobKey
@@ -176,6 +176,13 @@ class TestJobStatus(AuroraClientCommandTest):
   def create_failed_status_response(cls):
     return cls.create_blank_response(ResponseCode.INVALID_REQUEST, 'No tasks found for query')
 
+  @classmethod
+  def create_nojobs_status_response(cls):
+    resp = cls.create_simple_success_response()
+    resp.result.scheduleStatusResult = Mock(spec=ScheduleStatusResult)
+    resp.result.scheduleStatusResult.tasks = set()
+    return resp
+
   def test_successful_status_shallow(self):
     """Test the status command at the shallowest level: calling status should end up invoking
     the local APIs get_status method."""
@@ -352,6 +359,20 @@ class TestJobStatus(AuroraClientCommandTest):
       result = cmd.execute(['job', 'status', 'west/bozo/test/hello'])
       assert result == EXIT_INVALID_PARAMETER
 
+  def test_no_jobs_found_status_shallow(self):
+    # Calls api.check_status, which calls scheduler_proxy.getJobs
+    mock_context = FakeAuroraCommandContext()
+    mock_api = mock_context.get_api('west')
+    mock_api.check_status.return_value = self.create_nojobs_status_response()
+    with contextlib.nested(
+        patch('apache.aurora.client.cli.jobs.Job.create_context', return_value=mock_context)):
+      cmd = AuroraCommandLine()
+      result = cmd.execute(['job', 'status', '--write-json', 'west/bozo/test/hello'])
+      assert mock_context.get_out() == [
+        '{"jobspec":"west/bozo/test/hello","error":"No matching jobs found"}']
+      assert result == EXIT_OK
+
+
   def test_successful_status_json_output_no_metadata(self):
     """Test the status command more deeply: in a request with a fully specified
     job, it should end up doing a query using getTasksWithoutConfigs."""
@@ -481,4 +502,4 @@ class TestJobStatus(AuroraClientCommandTest):
       cmd = AuroraCommandLine()
       result = cmd.execute(['job', 'status', 'west/bozo/test/hello'])
       assert result == EXIT_INVALID_PARAMETER
-      assert mock_context.get_err() == ["No matching jobs found"]
+      assert mock_context.get_err() == ["Found no jobs matching west/bozo/test/hello"]