aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jco...@apache.org
Subject incubator-aurora git commit: Add the option to make a non-hooked API.
Date Thu, 12 Feb 2015 23:46:05 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master c94de9fbc -> b62ec6148


Add the option to make a non-hooked API.

Testing Done:
./pants test.pytest --no-fast src/test/python/apache/aurora/client::

Bugs closed: AURORA-1120

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


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

Branch: refs/heads/master
Commit: b62ec614853051a1c2896ad3165e3c376c2a4484
Parents: c94de9f
Author: Joshua Cohen <jcohen@twopensource.com>
Authored: Thu Feb 12 15:30:48 2015 -0800
Committer: Joshua Cohen <jcohen@twopensource.com>
Committed: Thu Feb 12 15:30:48 2015 -0800

----------------------------------------------------------------------
 .../python/apache/aurora/client/cli/context.py  | 13 +++--
 src/main/python/apache/aurora/client/factory.py | 12 ++--
 src/test/python/apache/aurora/client/BUILD      | 22 ++++++++
 src/test/python/apache/aurora/client/cli/BUILD  | 12 ++++
 .../apache/aurora/client/cli/test_context.py    | 58 ++++++++++++++++++++
 .../python/apache/aurora/client/cli/util.py     | 11 +---
 .../python/apache/aurora/client/test_factory.py | 32 +++++++++++
 src/test/python/apache/aurora/client/util.py    | 25 +++++++++
 8 files changed, 168 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/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 f8b2899..39754d5 100644
--- a/src/main/python/apache/aurora/client/cli/context.py
+++ b/src/main/python/apache/aurora/client/cli/context.py
@@ -68,16 +68,19 @@ class AuroraCommandContext(Context):
   def __init__(self):
     super(AuroraCommandContext, self).__init__()
     self.apis = {}
+    self.unhooked_apis = {}
 
-  def get_api(self, cluster):
+  def get_api(self, cluster, enable_hooks=True):
     """Gets an API object for a specified cluster
     Keeps the API handle cached, so that only one handle for each cluster will be created
in a
     session.
     """
-    if cluster not in self.apis:
-      api = make_client(cluster, AURORA_V2_USER_AGENT_NAME)
-      self.apis[cluster] = api
-    return self.apis[cluster]
+    apis = self.apis if enable_hooks else self.unhooked_apis
+
+    if cluster not in apis:
+      api = make_client(cluster, AURORA_V2_USER_AGENT_NAME, enable_hooks)
+      apis[cluster] = api
+    return apis[cluster]
 
   def get_job_config(self, jobkey, config_file):
     """Loads a job configuration from a config file."""

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/src/main/python/apache/aurora/client/factory.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/factory.py b/src/main/python/apache/aurora/client/factory.py
index 85a1398..f3ae66b 100644
--- a/src/main/python/apache/aurora/client/factory.py
+++ b/src/main/python/apache/aurora/client/factory.py
@@ -16,6 +16,7 @@ import functools
 
 from twitter.common import app
 
+from apache.aurora.client.api import AuroraClientAPI
 from apache.aurora.client.hooks.hooked_api import HookedAuroraClientAPI
 from apache.aurora.common.cluster import Cluster
 from apache.aurora.common.clusters import CLUSTERS
@@ -26,9 +27,12 @@ from .base import die
 # TODO(wickman) Kill make_client and make_client_factory as part of MESOS-3801.
 # These are currently necessary indirections for the LiveJobDisambiguator among
 # other things but can go away once those are scrubbed.
-def make_client_factory(user_agent):
+def make_client_factory(user_agent, enable_hooks=True):
   verbose = getattr(app.get_options(), 'verbosity', 'normal') == 'verbose'
-  class TwitterAuroraClientAPI(HookedAuroraClientAPI):
+
+  base_class = HookedAuroraClientAPI if enable_hooks else AuroraClientAPI
+
+  class TwitterAuroraClientAPI(base_class):
     def __init__(self, cluster, *args, **kw):
       if cluster not in CLUSTERS:
         die('Unknown cluster: %s' % cluster)
@@ -36,6 +40,6 @@ def make_client_factory(user_agent):
   return functools.partial(TwitterAuroraClientAPI, user_agent=user_agent, verbose=verbose)
 
 
-def make_client(cluster, user_agent):
-  factory = make_client_factory(user_agent)
+def make_client(cluster, user_agent, enable_hooks=True):
+  factory = make_client_factory(user_agent, enable_hooks)
   return factory(cluster.name if isinstance(cluster, Cluster) else cluster)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/src/test/python/apache/aurora/client/BUILD
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/BUILD b/src/test/python/apache/aurora/client/BUILD
index c55adfe..fb706bd 100644
--- a/src/test/python/apache/aurora/client/BUILD
+++ b/src/test/python/apache/aurora/client/BUILD
@@ -17,6 +17,7 @@ python_test_suite(name = 'all',
     ':base',
     ':binding_helper',
     ':config',
+    ':factory',
     'src/test/python/apache/aurora/client/api:all',
     'src/test/python/apache/aurora/client/cli:all',
     'src/test/python/apache/aurora/client/hooks:all',
@@ -49,6 +50,18 @@ python_tests(name = 'config',
   ],
 )
 
+python_tests(name = 'factory',
+  sources = ['test_factory.py'],
+  dependencies = [
+    '3rdparty/python:mock',
+    ':util',
+    'src/main/python/apache/aurora/client/api',
+    'src/main/python/apache/aurora/client:factory',
+    'src/main/python/apache/aurora/client/hooks',
+    'src/test/python/apache/aurora/client/cli:util',
+  ],
+)
+
 python_library(
   name = 'fake_scheduler_proxy',
   sources = ['fake_scheduler_proxy.py'],
@@ -56,3 +69,12 @@ python_library(
     'src/main/python/apache/aurora/client/api:scheduler_client',
   ]
 )
+
+python_library(
+  name = 'util',
+  sources = ['util.py'],
+  dependencies = [
+    'src/main/python/apache/aurora/common:cluster',
+    'src/main/python/apache/aurora/common:clusters',
+  ]
+)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/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 7319962..c755689 100644
--- a/src/test/python/apache/aurora/client/cli/BUILD
+++ b/src/test/python/apache/aurora/client/cli/BUILD
@@ -18,6 +18,7 @@ python_test_suite(
     ':api',
     ':client',
     ':command_hooks',
+    ':context',
     ':cron',
     ':inspect',
     ':job',
@@ -38,6 +39,7 @@ python_library(
   dependencies = [
     '3rdparty/python:mock',
     'src/main/python/apache/aurora/client/cli',
+    'src/test/python/apache/aurora/client:util',
   ]
 )
 
@@ -213,3 +215,13 @@ python_tests(
     'src/main/python/apache/aurora/client/cli',
   ]
 )
+
+python_tests(
+  name = 'context',
+  sources = [ 'test_context.py' ],
+  dependencies = [
+    '3rdparty/python:mock',
+    'src/main/python/apache/aurora/client/cli',
+    'src/main/python/apache/aurora/client:factory',
+  ]
+)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/src/test/python/apache/aurora/client/cli/test_context.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/test_context.py b/src/test/python/apache/aurora/client/cli/test_context.py
new file mode 100644
index 0000000..da3c003
--- /dev/null
+++ b/src/test/python/apache/aurora/client/cli/test_context.py
@@ -0,0 +1,58 @@
+#
+# 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 mock import call, Mock, patch
+
+from apache.aurora.client.base import AURORA_V2_USER_AGENT_NAME
+from apache.aurora.client.cli.context import AuroraCommandContext
+
+
+def test_get_api_defaults_to_hooks_enabled():
+  with patch('apache.aurora.client.cli.context.make_client') as mock_make_client:
+    cluster = 'some-cluster'
+
+    AuroraCommandContext().get_api(cluster)
+    assert mock_make_client.mock_calls == [call(cluster, AURORA_V2_USER_AGENT_NAME, True)]
+
+
+def test_get_api_caches_hook_enabled_apis_separately():
+  with patch('apache.aurora.client.cli.context.make_client') as mock_make_client:
+    # return a new Mock instance for each call of the two calls we're expecting.
+    mock_make_client.side_effect = [Mock(), Mock()]
+
+    cluster = 'some-cluster'
+
+    context = AuroraCommandContext()
+    hooked_api = context.get_api(cluster)
+    unhooked_api = context.get_api(cluster, False)
+
+    assert mock_make_client.mock_calls == [
+      call(cluster, AURORA_V2_USER_AGENT_NAME, True),
+      call(cluster, AURORA_V2_USER_AGENT_NAME, False)]
+
+    assert hooked_api != unhooked_api
+
+    assert hooked_api in context.apis.values()
+    assert hooked_api not in context.unhooked_apis.values()
+
+    assert unhooked_api in context.unhooked_apis.values()
+    assert unhooked_api not in context.apis.values()
+
+
+def test_get_api_forwards_hooks_disabled():
+  with patch('apache.aurora.client.cli.context.make_client') as mock_make_client:
+    cluster = 'some-cluster'
+
+    AuroraCommandContext().get_api(cluster, enable_hooks=False)
+    assert mock_make_client.mock_calls == [call(cluster, AURORA_V2_USER_AGENT_NAME, False)]

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/src/test/python/apache/aurora/client/cli/util.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/util.py b/src/test/python/apache/aurora/client/cli/util.py
index 12360c6..d7096a7 100644
--- a/src/test/python/apache/aurora/client/cli/util.py
+++ b/src/test/python/apache/aurora/client/cli/util.py
@@ -21,10 +21,9 @@ from apache.aurora.client.cli.client import AuroraCommandLine
 from apache.aurora.client.cli.context import AuroraCommandContext
 from apache.aurora.client.hooks.hooked_api import HookedAuroraClientAPI
 from apache.aurora.common.aurora_job_key import AuroraJobKey
-from apache.aurora.common.cluster import Cluster
-from apache.aurora.common.clusters import Clusters
 
 from ...api_util import SchedulerProxyApiSpec, SchedulerThriftApiSpec
+from ..util import TEST_CLUSTER, TEST_CLUSTERS
 
 from gen.apache.aurora.api.constants import ACTIVE_STATES
 from gen.apache.aurora.api.ttypes import (
@@ -309,17 +308,13 @@ jobs = [HELLO_WORLD]
 
   TEST_JOB = 'hello'
 
-  TEST_CLUSTER = 'west'
+  TEST_CLUSTER = TEST_CLUSTER
 
   TEST_JOBSPEC = 'west/bozo/test/hello'
 
   TEST_JOBKEY = AuroraJobKey('west', 'bozo', 'test', 'hello')
 
-  TEST_CLUSTERS = Clusters([Cluster(
-      name='west',
-      zk='zookeeper.example.com',
-      scheduler_zk_path='/foo/bar',
-      auth_mechanism='UNAUTHENTICATED')])
+  TEST_CLUSTERS = TEST_CLUSTERS
 
   @classmethod
   def get_instance_spec(cls, instances_spec):

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/src/test/python/apache/aurora/client/test_factory.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/test_factory.py b/src/test/python/apache/aurora/client/test_factory.py
new file mode 100644
index 0000000..5598567
--- /dev/null
+++ b/src/test/python/apache/aurora/client/test_factory.py
@@ -0,0 +1,32 @@
+#
+# 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 mock import patch
+
+from apache.aurora.client.api import AuroraClientAPI
+from apache.aurora.client.factory import make_client
+from apache.aurora.client.hooks.hooked_api import HookedAuroraClientAPI
+from apache.aurora.client.util import TEST_CLUSTER, TEST_CLUSTERS
+
+
+def test_make_client_defaults_to_hooks_enabled():
+  with patch('apache.aurora.client.factory.CLUSTERS', new=TEST_CLUSTERS):
+    assert isinstance(make_client(TEST_CLUSTER, 'some-user-agent'), HookedAuroraClientAPI)
+
+
+def test_make_client_hooks_disabled():
+  with patch('apache.aurora.client.factory.CLUSTERS', new=TEST_CLUSTERS):
+    client = make_client(TEST_CLUSTER, 'some-user-agent', enable_hooks=False)
+    assert not isinstance(client, HookedAuroraClientAPI)
+    assert isinstance(client, AuroraClientAPI)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/b62ec614/src/test/python/apache/aurora/client/util.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/util.py b/src/test/python/apache/aurora/client/util.py
new file mode 100644
index 0000000..0598697
--- /dev/null
+++ b/src/test/python/apache/aurora/client/util.py
@@ -0,0 +1,25 @@
+#
+# 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 apache.aurora.common.cluster import Cluster
+from apache.aurora.common.clusters import Clusters
+
+TEST_CLUSTER = 'west'
+
+
+TEST_CLUSTERS = Clusters([Cluster(
+  name=TEST_CLUSTER,
+  zk='zookeeper.example.com',
+  scheduler_zk_path='/foo/bar',
+  auth_mechanism='UNAUTHENTICATED')])


Mime
View raw message