aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmclaugh...@apache.org
Subject incubator-aurora git commit: Extract mechanism for determining pex version so it can be used for cases other than client version.
Date Mon, 15 Dec 2014 23:24:29 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master 52efc933f -> 95ad0fa42


Extract mechanism for determining pex version so it can be used for cases other than client
version.

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


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

Branch: refs/heads/master
Commit: 95ad0fa42710a9747f3340b5744f3b325f712901
Parents: 52efc93
Author: Joshua Cohen <jcohen@twopensource.com>
Authored: Mon Dec 15 15:19:24 2014 -0800
Committer: David McLaughlin <david@dmclaughlin.com>
Committed: Mon Dec 15 15:19:24 2014 -0800

----------------------------------------------------------------------
 .../python/apache/aurora/client/cli/__init__.py | 11 +--
 .../apache/aurora/client/commands/core.py       | 13 +---
 src/main/python/apache/aurora/common/BUILD      |  9 +++
 .../python/apache/aurora/common/pex_version.py  | 33 ++++++++
 .../aurora/client/commands/test_version.py      | 38 +++------
 src/test/python/apache/aurora/common/BUILD      | 10 +++
 .../apache/aurora/common/test_pex_version.py    | 82 ++++++++++++++++++++
 7 files changed, 151 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/src/main/python/apache/aurora/client/cli/__init__.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/__init__.py b/src/main/python/apache/aurora/client/cli/__init__.py
index 0e6e40a..2717210 100644
--- a/src/main/python/apache/aurora/client/cli/__init__.py
+++ b/src/main/python/apache/aurora/client/cli/__init__.py
@@ -35,10 +35,10 @@ import logging
 import sys
 import traceback
 from abc import abstractmethod, abstractproperty
-from zipfile import BadZipfile
 
 from twitter.common.lang import AbstractClass
-from twitter.common.python.pex import PexInfo
+
+from apache.aurora.common.pex_version import pex_version, UnknownVersion
 
 from .command_hooks import GlobalCommandHookRegistry
 from .options import CommandOption
@@ -64,11 +64,8 @@ GLOBAL_HOOK_SKIP_RULES_URL = None
 
 def get_client_version():
   try:
-    pexpath = sys.argv[0]
-    pex_info = PexInfo.from_pex(pexpath)
-    return ("%s@%s" % (pex_info.build_properties.get("sha", "unknown"),
-        pex_info.build_properties.get("date", "unknown")))
-  except (BadZipfile, IOError, OSError):
+    return "%s@%s" % pex_version(sys.argv[0])
+  except UnknownVersion:
     return "VersionUnknown"
 
 

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/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 8d92862..7b970e8 100644
--- a/src/main/python/apache/aurora/client/commands/core.py
+++ b/src/main/python/apache/aurora/client/commands/core.py
@@ -25,10 +25,8 @@ import sys
 import time
 from datetime import datetime
 from tempfile import NamedTemporaryFile
-from zipfile import BadZipfile
 
 from twitter.common import app, log
-from twitter.common.python.pex import PexInfo
 
 from apache.aurora.client.api.disambiguator import LiveJobDisambiguator
 from apache.aurora.client.api.job_monitor import JobMonitor
@@ -62,6 +60,7 @@ from apache.aurora.client.options import (
     WAIT_UNTIL_OPTION
 )
 from apache.aurora.common.aurora_job_key import AuroraJobKey
+from apache.aurora.common.pex_version import pex_version, UnknownVersion
 
 from gen.apache.aurora.api.constants import ACTIVE_STATES, AURORA_EXECUTOR_NAME, CURRENT_API_VERSION
 from gen.apache.aurora.api.ttypes import ExecutorConfig, ResponseCode, ScheduleStatus
@@ -154,19 +153,15 @@ _BUILD_INFO_HEADER = "Aurora client build info:"
 _NO_BUILD_INFO_MESSAGE = "Aurora client build info not available"
 
 
-def _version(_argv=sys.argv, _print=print, _from_pex=PexInfo.from_pex):
+def _version(_argv=sys.argv, _print=print, _pex_version=pex_version):
   try:
-    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'))
+    build_sha, build_date = _pex_version(_argv[0])
 
     _print(_BUILD_INFO_HEADER)
     _print("\tsha: %s" % build_sha)
     _print("\tdate: %s" % build_date)
 
-  except (AttributeError, IOError, OSError, BadZipfile):
+  except UnknownVersion:
     _print(_NO_BUILD_INFO_MESSAGE)
   _print(_API_VERSION_MESSAGE)
 

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/src/main/python/apache/aurora/common/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/common/BUILD b/src/main/python/apache/aurora/common/BUILD
index 2149688..1c6464d 100644
--- a/src/main/python/apache/aurora/common/BUILD
+++ b/src/main/python/apache/aurora/common/BUILD
@@ -59,6 +59,14 @@ python_library(
 )
 
 python_library(
+  name = 'pex_version',
+  sources = ['pex_version.py'],
+  dependencies = [
+    '3rdparty/python:twitter.common.python',
+  ]
+)
+
+python_library(
   name = 'shellify',
   sources = ['shellify.py'],
   dependencies = [
@@ -82,6 +90,7 @@ python_library(
     ':cluster',
     ':cluster_option',
     ':clusters',
+    ':pex_version',
     ':http_signaler',
     ':shellify',
     ':transport',

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/src/main/python/apache/aurora/common/pex_version.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/common/pex_version.py b/src/main/python/apache/aurora/common/pex_version.py
new file mode 100644
index 0000000..6aecd8a
--- /dev/null
+++ b/src/main/python/apache/aurora/common/pex_version.py
@@ -0,0 +1,33 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from zipfile import BadZipfile
+
+from twitter.common.python.pex import PexInfo
+
+
+class UnknownVersion(Exception):
+  pass
+
+
+def pex_version(executable_path, _from_pex=PexInfo.from_pex):
+  try:
+    properties = _from_pex(executable_path).build_properties
+  except (AttributeError, BadZipfile, IOError, OSError):
+    raise UnknownVersion
+
+  # 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.
+  return (properties.get('sha', properties.get('revision')),
+      properties.get('date', properties.get('datetime')))

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/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 d3b315e..cb0f410 100644
--- a/src/test/python/apache/aurora/client/commands/test_version.py
+++ b/src/test/python/apache/aurora/client/commands/test_version.py
@@ -14,10 +14,8 @@
 from __future__ import print_function
 
 from unittest import TestCase
-from zipfile import BadZipfile
 
 from mock import call, create_autospec
-from twitter.common.python.pex import PexInfo
 
 from apache.aurora.client.commands.core import (
     _API_VERSION_MESSAGE,
@@ -25,28 +23,25 @@ from apache.aurora.client.commands.core import (
     _NO_BUILD_INFO_MESSAGE,
     _version
 )
+from apache.aurora.common.pex_version import pex_version, UnknownVersion
 
 
 class TestVersionCommand(TestCase):
 
   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_pex_version = create_autospec(pex_version, 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)
+    _version(_argv=self.mock_argv, _print=self.mock_print, _pex_version=self.mock_pex_version)
 
-  def test_version_with_old_pants(self):
-    # Old versions of pants wrote out sha and date keys
-    self.mock_pex_info.build_properties = {'sha': 'foo', 'date': 'somedate'}
-    self.mock_from_pex.return_value = self.mock_pex_info
+  def test_version(self):
+    self.mock_pex_version.return_value = ("foo", "somedate")
 
     self._invoke_version()
 
-    self.mock_from_pex.assert_called_once_with(self.mock_argv[0])
+    self.mock_pex_version.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)
@@ -54,28 +49,13 @@ class TestVersionCommand(TestCase):
     assert "somedate" in calls[2][1][0]
     assert calls[3] == call(_API_VERSION_MESSAGE)
 
-  def test_version_with_new_pants(self):
-    # New versions of pants write out revision and datetime
-    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):
+  def test_unknown_version(self):
     # If we aren't a PEX we'll be a bad zip file.
-    self.mock_from_pex.side_effect = BadZipfile
+    self.mock_pex_version.side_effect = UnknownVersion
 
     self._invoke_version()
 
-    self.mock_from_pex.assert_called_once_with(self.mock_argv[0])
+    self.mock_pex_version.assert_called_once_with(self.mock_argv[0])
     self.mock_print.assert_has_calls([
       call(_NO_BUILD_INFO_MESSAGE),
       call(_API_VERSION_MESSAGE),

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/src/test/python/apache/aurora/common/BUILD
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/common/BUILD b/src/test/python/apache/aurora/common/BUILD
index afc0659..14165b9 100644
--- a/src/test/python/apache/aurora/common/BUILD
+++ b/src/test/python/apache/aurora/common/BUILD
@@ -20,6 +20,7 @@ python_test_suite(
     ':test_clusters',
     ':test_cluster_option',
     ':test_http_signaler',
+    ':test_pex_version',
     ':test_shellify',
     ':test_transport',
   ]
@@ -33,6 +34,15 @@ python_tests(name = 'test_aurora_job_key',
 )
 
 python_tests(
+  name = 'test_pex_version',
+  sources = ['test_pex_version.py'],
+  dependencies = [
+    '3rdparty/python:mock',
+    'src/main/python/apache/aurora/common:pex_version',
+  ]
+)
+
+python_tests(
   name = 'test_cluster',
   sources = ['test_cluster.py'],
   dependencies = [

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/95ad0fa4/src/test/python/apache/aurora/common/test_pex_version.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/common/test_pex_version.py b/src/test/python/apache/aurora/common/test_pex_version.py
new file mode 100644
index 0000000..7280f70
--- /dev/null
+++ b/src/test/python/apache/aurora/common/test_pex_version.py
@@ -0,0 +1,82 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from zipfile import BadZipfile
+
+import mock
+import pytest
+from twitter.common.python.pex import PexInfo
+
+from apache.aurora.common.pex_version import pex_version, UnknownVersion
+
+SHA = 'foo'
+DATE = 'some-date'
+
+
+def test_old_pants_output():
+  mock_pex_info = mock.create_autospec(PexInfo, spec_set=True)
+  mock_pex_info.build_properties = {'sha': SHA, 'date': DATE}
+
+  mock_from_pex = mock.create_autospec(PexInfo.from_pex, spec_set=True)
+  mock_from_pex.return_value = mock_pex_info
+
+  sha, date = pex_version('path/to/some.pex', _from_pex=mock_from_pex)
+
+  assert sha == SHA
+  assert date == DATE
+
+
+def test_new_pants_output():
+  mock_pex_info = mock.create_autospec(PexInfo, spec_set=True)
+  mock_pex_info.build_properties = {'revision': SHA, 'datetime': DATE}
+
+  mock_from_pex = mock.create_autospec(PexInfo.from_pex, spec_set=True)
+  mock_from_pex.return_value = mock_pex_info
+
+  sha, date = pex_version('path/to/some.pex', _from_pex=mock_from_pex)
+
+  assert sha == SHA
+  assert date == DATE
+
+
+def test_attribute_error():
+  mock_from_pex = mock.create_autospec(PexInfo.from_pex, spec_set=True)
+  mock_from_pex.side_effect = AttributeError
+
+  with pytest.raises(UnknownVersion):
+    pex_version('path/to/some.pex', _from_pex=mock_from_pex)
+
+
+def test_no_pants():
+  mock_from_pex = mock.create_autospec(PexInfo.from_pex, spec_set=True)
+  mock_from_pex.side_effect = BadZipfile
+
+  with pytest.raises(UnknownVersion):
+    pex_version('path/to/some.pex', _from_pex=mock_from_pex)
+
+
+def test_io_error():
+  mock_from_pex = mock.create_autospec(PexInfo.from_pex, spec_set=True)
+  mock_from_pex.side_effect = IOError
+
+  with pytest.raises(UnknownVersion):
+    pex_version('path/to/some.pex', _from_pex=mock_from_pex)
+
+
+def test_os_error():
+  mock_from_pex = mock.create_autospec(PexInfo.from_pex, spec_set=True)
+  mock_from_pex.side_effect = OSError
+
+  with pytest.raises(UnknownVersion):
+    pex_version('path/to/some.pex', _from_pex=mock_from_pex)


Mime
View raw message