Giuseppe Lavagetto has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/278551

Change subject: Print out the tags any conftool result line is referring to
......................................................................

Print out the tags any conftool result line is referring to

Bug: T128199
Change-Id: Icebcfb7c06f3dce2045a6ff96578df8e358e866c
---
M conftool/__init__.py
M conftool/cli/tool.py
M conftool/node.py
M conftool/service.py
M conftool/tests/integration/test_tool.py
M conftool/tests/unit/test_node.py
M conftool/tests/unit/test_service.py
7 files changed, 56 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/software/conftool 
refs/changes/51/278551/1

diff --git a/conftool/__init__.py b/conftool/__init__.py
index cb1bda6..19e1b26 100644
--- a/conftool/__init__.py
+++ b/conftool/__init__.py
@@ -8,6 +8,14 @@
 from conftool import drivers
 
 
+def choice(*args):
+    def is_in(x):
+        if x not in args:
+            raise ValueError("{} not in '{}'".format(x, ",".join(args)))
+        return x
+    return is_in
+
+
 class KVObject(object):
     backend = None
     config = None
@@ -39,6 +47,16 @@
     def name(self):
         return os.path.basename(self.key)
 
+    @property
+    def tags(self):
+        """Returns a dict of the current tags"""
+        res = {}
+        # The current key, minus the basepath, is the list of tags + the node 
name
+        tags = self.key.replace(self.base_path(), '').split('/')[1:-1]
+        for i in range(len(self._tags)):
+            res[self._tags[i]] = tags[i]
+        return res
+
     def get_default(self, what):
         raise NotImplementedError("All kvstore objects should implement this.")
 
@@ -63,7 +81,8 @@
         self.backend.driver.delete(self.key)
 
     @classmethod
-    def get_tags(cls, taglist):
+    def parse_tags(cls, taglist):
+        """Given a taglist as a string, return an ordered list of tags"""
         tuplestrip = lambda tup: tuple(map(lambda x: x.strip(), tup))
         tagdict = dict([tuplestrip(el.split('=')) for el in taglist])
         # will raise a KeyError if not all tags are matched
@@ -126,4 +145,5 @@
 
     def __str__(self):
         d = {self.name: self._to_net()}
+        d['tags'] = self.tags
         return json.dumps(d)
diff --git a/conftool/cli/tool.py b/conftool/cli/tool.py
index 84116d7..b802955 100644
--- a/conftool/cli/tool.py
+++ b/conftool/cli/tool.py
@@ -33,7 +33,7 @@
             return []
         else:
             try:
-                return self.entity.get_tags(self._tags)
+                return self.entity.parse_tags(self._tags)
             except KeyError as e:
                 _log.critical(
                     "Invalid tag list %s - we're missing tag: %s",
@@ -92,7 +92,7 @@
                 _log.error("Failure writing to the kvstore: %s", str(e))
             except Exception as e:
                 _log.error("Error when trying to %s on %s", act, namedef)
-                _log.error("Generic action failure: %s", str(e))
+                _log.exception("Generic action failure: %s", str(e))
             else:
                 print(msg)
 
diff --git a/conftool/node.py b/conftool/node.py
index 2a6a037..2b17313 100644
--- a/conftool/node.py
+++ b/conftool/node.py
@@ -1,14 +1,6 @@
 import os
-from conftool import KVObject, _log
+from conftool import KVObject, _log, choice
 from conftool.service import Service
-
-
-def choice(*args):
-    def is_in(x):
-        if x not in args:
-            raise ValueError("{} not in '{}'".format(x, ",".join(args)))
-        return x
-    return is_in
 
 
 class ServiceCache(object):
diff --git a/conftool/service.py b/conftool/service.py
index 9dd6a7b..3a41949 100644
--- a/conftool/service.py
+++ b/conftool/service.py
@@ -3,7 +3,10 @@
 
 
 class Service(KVObject):
-    _schema = {'default_values': dict, 'datacenters': list}
+    _schema = {
+        'default_values': dict,
+        'datacenters': lambda x: x if isinstance(x, list) else []
+    }
     _tags = ['cluster']
 
     def __init__(self, cluster, name, **kwdargs):
diff --git a/conftool/tests/integration/test_tool.py 
b/conftool/tests/integration/test_tool.py
index 1831ae3..f79c526 100644
--- a/conftool/tests/integration/test_tool.py
+++ b/conftool/tests/integration/test_tool.py
@@ -38,9 +38,22 @@
             tool.main(cmdline=args)
         res = out.getvalue().strip()
         output = json.loads(res)
-        self.assertEquals(output.keys(), ['cp1008'])
+        self.assertEquals(output.keys(), ['cp1008', 'tags'])
         self.assertEquals(output['cp1008']['pooled'], 'no')
 
+    def test_find_node(self):
+        args = ['--find', '--action', 'get', 'cp1008']
+        with captured_output() as (out, err):
+            tool.main(cmdline=args)
+        res = out.getvalue().strip()
+        output = []
+        for line in res.split("\n"):
+            output.append(json.loads(line))
+        self.assertEquals(len(output), 3)
+        for serv in output:
+            self.assertIn(serv['tags']['service'],
+                          ['varnish-be', 'https', 'varnish-fe'])
+
     def test_change_node_regexp(self):
         """
         Changing values according to a regexp
diff --git a/conftool/tests/unit/test_node.py b/conftool/tests/unit/test_node.py
index c1fe76f..a40fdbe 100644
--- a/conftool/tests/unit/test_node.py
+++ b/conftool/tests/unit/test_node.py
@@ -51,5 +51,12 @@
         # Note: this fails at the moment
         # self.assertRaises(ValueError, setattr, n, "pooled", "maybe")
 
+    def test_tags(self):
+        """Test tags are correctly reported"""
+        self._mock_read({"pooled": "yes", "weight": 20})
+        n = node.Node('dc', 'cluster', 'service', 'foo')
+        for k, v in n.tags.items():
+            self.assertEquals(k, v)
+
     def test_dir(self):
         self.assertEquals(node.Node.dir('a', 'b', 'c'), 'pools/a/b/c')
diff --git a/conftool/tests/unit/test_service.py 
b/conftool/tests/unit/test_service.py
index a5cc36f..6bf64ee 100644
--- a/conftool/tests/unit/test_service.py
+++ b/conftool/tests/unit/test_service.py
@@ -53,5 +53,12 @@
         self.assertEquals(s.datacenters, [])
         self.assertEquals(s.default_values, {'pooled': "no", "weight": 0})
 
+    def test_tags(self):
+        self._mock_read({"datacenters": ['a', 'b', 'c'],
+                         "default_values": {"pooled": "no"},
+                         "something_else": "some_value"})
+        s = service.Service('cluster', 'foo')
+        self.assertEquals(s.tags, {'cluster': 'cluster'})
+
     def test_dir(self):
         self.assertEquals(service.Service.dir('a'), 'services/a')

-- 
To view, visit https://gerrit.wikimedia.org/r/278551
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icebcfb7c06f3dce2045a6ff96578df8e358e866c
Gerrit-PatchSet: 1
Gerrit-Project: operations/software/conftool
Gerrit-Branch: master
Gerrit-Owner: Giuseppe Lavagetto <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to