aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kevi...@apache.org
Subject incubator-aurora git commit: Fix test_version to use explicit injection points.
Date Thu, 20 Nov 2014 01:05:54 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master 1d78ac564 -> 065a3c5c8


Fix test_version to use explicit injection points.

Testing Done:
./pants src/test/python:all
./pants src/main/python/apache/aurora/client/bin:aurora_client
dist/aurora_client.pex version

Bugs closed: AURORA-938

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


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

Branch: refs/heads/master
Commit: 065a3c5c82b50b703662b4eb99c1529c951c5899
Parents: 1d78ac5
Author: Kevin Sweeney <kevints@apache.org>
Authored: Wed Nov 19 14:59:16 2014 -0800
Committer: Kevin Sweeney <kevints@apache.org>
Committed: Wed Nov 19 17:05:45 2014 -0800

----------------------------------------------------------------------
 .../apache/aurora/client/commands/core.py       | 34 +++++----
 .../aurora/client/commands/test_version.py      | 73 +++++++++++++++-----
 2 files changed, 76 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/065a3c5c/src/main/python/apache/aurora/client/commands/core.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/commands/core.py b/src/main/python/apache/aurora/client/commands/core.py
index 852b6b1..ee22716 100644
--- a/src/main/python/apache/aurora/client/commands/core.py
+++ b/src/main/python/apache/aurora/client/commands/core.py
@@ -148,27 +148,35 @@ def wait_kill_tasks(scheduler, job_key, instances=None):
     die('Tasks were not killed in time.')
 
 
-@app.command
-def version(args):
-  """usage: version
+_API_VERSION_MESSAGE = "Aurora API version: %s" % CURRENT_API_VERSION
+_BUILD_INFO_HEADER = "Aurora client build info:"
+_NO_BUILD_INFO_MESSAGE = "Aurora client build info not available"
 
-  Prints information about the version of the aurora client being run.
-  """
+
+def _version(_argv=sys.argv, _print=print, _from_pex=PexInfo.from_pex):
   try:
-    pex_info = PexInfo.from_pex(sys.argv[0])
-    properties = pex_info.build_properties
+    properties = _from_pex(_argv[0]).build_properties
     # Different versions of pants/pex set different keys in the PEX-INFO file. This approach
     # attempts to work regardless of the pants/pex version used.
     build_sha = properties.get('sha', properties.get('revision'))
     build_date = properties.get('date', properties.get('datetime'))
 
-    print("Aurora client build info:")
-    print("\tsha: %s" % build_sha)
-    print("\tdate: %s" % build_date)
+    _print(_BUILD_INFO_HEADER)
+    _print("\tsha: %s" % build_sha)
+    _print("\tdate: %s" % build_date)
+
+  except (AttributeError, IOError, OSError, BadZipfile):
+    _print(_NO_BUILD_INFO_MESSAGE)
+  _print(_API_VERSION_MESSAGE)
 
-  except (IOError, OSError, BadZipfile):
-    print("Aurora client build info not available")
-  print("Aurora API version: %s" % CURRENT_API_VERSION)
+
+@app.command
+def version():
+  """usage: version
+
+  Prints information about the version of the aurora client being run.
+  """
+  _version()
 
 
 def maybe_disable_hooks(options):

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/065a3c5c/src/test/python/apache/aurora/client/commands/test_version.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/commands/test_version.py b/src/test/python/apache/aurora/client/commands/test_version.py
index 5c3c45a..d3b315e 100644
--- a/src/test/python/apache/aurora/client/commands/test_version.py
+++ b/src/test/python/apache/aurora/client/commands/test_version.py
@@ -13,33 +13,70 @@
 #
 from __future__ import print_function
 
-from mock import patch, PropertyMock
+from unittest import TestCase
+from zipfile import BadZipfile
 
-from apache.aurora.client.commands.core import version
+from mock import call, create_autospec
+from twitter.common.python.pex import PexInfo
 
-from .util import AuroraClientCommandTest
+from apache.aurora.client.commands.core import (
+    _API_VERSION_MESSAGE,
+    _BUILD_INFO_HEADER,
+    _NO_BUILD_INFO_MESSAGE,
+    _version
+)
 
 
-class TestVersionCommand(AuroraClientCommandTest):
+class TestVersionCommand(TestCase):
 
-  @patch('__builtin__.print')
-  @patch('twitter.common.python.pex.PexInfo.build_properties', new_callable=PropertyMock)
-  def test_version_with_old_pants(self, mock_buildinfo, mock_print):
+  def setUp(self):
+    self.mock_print = create_autospec(print, spec_set=True)
+    self.mock_from_pex = create_autospec(PexInfo.from_pex, spec_set=True)
+    self.mock_argv = ['test-aurora.pex']
+
+    self.mock_pex_info = create_autospec(PexInfo, instance=True, spec_set=True)
+
+  def _invoke_version(self):
+    _version(_argv=self.mock_argv, _print=self.mock_print, _from_pex=self.mock_from_pex)
+
+  def test_version_with_old_pants(self):
     # Old versions of pants wrote out sha and date keys
-    mock_buildinfo.return_value = {'sha': 'foo', 'date': 'somedate'}
-    version([])
-    assert mock_print.call_count == 4
-    calls = mock_print.mock_calls
+    self.mock_pex_info.build_properties = {'sha': 'foo', 'date': 'somedate'}
+    self.mock_from_pex.return_value = self.mock_pex_info
+
+    self._invoke_version()
+
+    self.mock_from_pex.assert_called_once_with(self.mock_argv[0])
+    assert self.mock_print.call_count == 4
+    calls = self.mock_print.mock_calls
+    assert calls[0] == call(_BUILD_INFO_HEADER)
     assert "foo" in calls[1][1][0]
     assert "somedate" in calls[2][1][0]
+    assert calls[3] == call(_API_VERSION_MESSAGE)
 
-  @patch('__builtin__.print')
-  @patch('twitter.common.python.pex.PexInfo.build_properties', new_callable=PropertyMock)
-  def test_version_with_new_pants(self, mock_buildinfo, mock_print):
+  def test_version_with_new_pants(self):
     # New versions of pants write out revision and datetime
-    mock_buildinfo.return_value = {'revision': 'bar', 'datetime': 'somedatetime'}
-    version([])
-    assert mock_print.call_count == 4
-    calls = mock_print.mock_calls
+    self.mock_pex_info.build_properties = {'revision': 'bar', 'datetime': 'somedatetime'}
+    self.mock_from_pex.return_value = self.mock_pex_info
+
+    self._invoke_version()
+
+    self.mock_from_pex.assert_called_once_with(self.mock_argv[0])
+    assert self.mock_print.call_count == 4
+    calls = self.mock_print.mock_calls
+    assert calls[0] == call(_BUILD_INFO_HEADER)
     assert "bar" in calls[1][1][0]
     assert "somedatetime" in calls[2][1][0]
+    assert calls[3] == call(_API_VERSION_MESSAGE)
+
+  def test_version_with_no_pants(self):
+    # If we aren't a PEX we'll be a bad zip file.
+    self.mock_from_pex.side_effect = BadZipfile
+
+    self._invoke_version()
+
+    self.mock_from_pex.assert_called_once_with(self.mock_argv[0])
+    self.mock_print.assert_has_calls([
+      call(_NO_BUILD_INFO_MESSAGE),
+      call(_API_VERSION_MESSAGE),
+    ])


Mime
View raw message