فهرست منبع

HUE-1534 [zookeeper] host_ports conf parsing breaking

Romain Rigaux 12 سال پیش
والد
کامیت
a091b41
3فایلهای تغییر یافته به همراه84 افزوده شده و 9 حذف شده
  1. 10 1
      apps/zookeeper/src/zookeeper/conf.py
  2. 66 0
      apps/zookeeper/src/zookeeper/tests.py
  3. 8 8
      apps/zookeeper/src/zookeeper/views.py

+ 10 - 1
apps/zookeeper/src/zookeeper/conf.py

@@ -15,7 +15,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from desktop.lib.conf import Config, UnspecifiedConfigSection, ConfigSection, validate_path, coerce_bool
+from desktop.lib.conf import Config, UnspecifiedConfigSection, ConfigSection
+
+
+def coerce_string(value):
+  if type(value) == list:
+    return ','.join(value)
+  else:
+    return value
 
 
 CLUSTERS = UnspecifiedConfigSection(
@@ -28,11 +35,13 @@ CLUSTERS = UnspecifiedConfigSection(
           "host_ports",
           help="Zookeeper ensemble. Comma separated list of Host/Port, e.g. localhost:2181,localhost:2182,localhost:2183",
           default="localhost:2181",
+          type=coerce_string,
       ),
       REST_URL=Config(
           "rest_url",
           help="The URL of the REST contrib service.",
           default="http://localhost:9998",
+          type=str,
       ),
     )
   )

+ 66 - 0
apps/zookeeper/src/zookeeper/tests.py

@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you 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 nose.tools import assert_true, assert_equal
+
+
+from zookeeper import stats
+from zookeeper.conf import CLUSTERS
+from zookeeper.views import _get_global_overview
+
+
+class MockZooKeeperStats(object):
+
+  def __init__(self, host, port):
+    pass
+
+  def get_stats(self):
+    return {}
+
+
+
+class ZooKeeperMockBase(object):
+
+  def setUp(self):
+    # Beware: Monkey patch ZooKeeper with Mock API
+    if not hasattr(stats, 'OriginalZooKeeperApi'):
+      stats.OriginalZooKeeperApi = stats.ZooKeeperStats
+
+    stats.ZooKeeperStats = MockZooKeeperStats
+
+  def tearDown(self):
+    stats.ZooKeeperStats = stats.OriginalZooKeeperApi
+
+
+class TestMockedZooKeeper(ZooKeeperMockBase):
+
+  def test_get_global_overview(self):
+    """Beware: this test is not testing, need to better mock the config."""
+    finish = CLUSTERS.set_for_testing({'default': {'localhost:2181': {}}})
+
+    try:
+      _get_global_overview()
+
+    finally:
+      finish()
+
+    finish = CLUSTERS.set_for_testing({'default': {'localhost:2181,localhost:2182': {}}})
+    try:
+      _get_global_overview()
+    finally:
+      finish()

+ 8 - 8
apps/zookeeper/src/zookeeper/views.py

@@ -30,9 +30,9 @@ from desktop.lib.django_util import render
 from desktop.lib.exceptions_renderable import PopupException
 
 from zookeeper import settings
+from zookeeper import stats
 from zookeeper.conf import CLUSTERS
 from zookeeper.forms import CreateZNodeForm, EditZNodeForm
-from zookeeper.stats import ZooKeeperStats
 from zookeeper.rest import ZooKeeper
 from zookeeper.utils import get_cluster_or_404
 
@@ -43,15 +43,15 @@ def _get_global_overview():
 
 
 def _get_overview(host_ports):
-  stats = {}
+  zstats = {}
 
-  for s in host_ports.split(','):
-    host, port = map(str.strip, s.split(':'))
+  for host_port in host_ports.split(','):
+    host, port = map(str.strip, host_port.split(':'))
 
-    zks = ZooKeeperStats(host, port)
-    stats[s] = zks.get_stats() or {}
+    zks = stats.ZooKeeperStats(host, port)
+    zstats[host_port] = zks.get_stats() or {}
 
-  return stats
+  return zstats
 
 
 def _group_stats_by_role(stats):
@@ -100,7 +100,7 @@ def clients(request, id, host):
     raise Http404
 
   host, port = parts
-  zks = ZooKeeperStats(host, port)
+  zks = stats.ZooKeeperStats(host, port)
   clients = zks.get_clients()
 
   return render('clients.mako', request, {