Giuseppe Lavagetto has submitted this change and it was merged.

Change subject: conftool: add --host option
......................................................................


conftool: add --host option

It allows to force the change in non-interactive way if all the objects
belongs to the same host

Bug: T149213
Change-Id: I1f6ec5d4cb60cb84c1b372382b412aeb712c7f5d
---
M conftool/cli/tool.py
M conftool/tests/integration/test_tool.py
M conftool/tests/unit/test_cli_tool.py
3 files changed, 50 insertions(+), 0 deletions(-)

Approvals:
  Giuseppe Lavagetto: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/conftool/cli/tool.py b/conftool/cli/tool.py
index 63cad8f..0241096 100644
--- a/conftool/cli/tool.py
+++ b/conftool/cli/tool.py
@@ -186,9 +186,16 @@
 
     def raise_warning(self, objects):
         tag_hosts = defaultdict(list)
+        hosts_set = set()
         for obj in objects:
             dir = os.path.dirname(obj.key).replace(self.entity.base_path(), '')
             tag_hosts[dir].append(obj.name)
+            hosts_set.add(obj.name)
+
+        if self.args.host and len(hosts_set) <= 1:
+            # The host option is set and all objects belong to the same host
+            return
+
         print "The selector you chose has selected the following objects:"
         if self.args.yaml:
             print yaml.dump(tag_hosts, default_flow_style=False)
@@ -212,6 +219,8 @@
                         choices=ToolCli.object_types.keys(), default='node')
     parser.add_argument('--yaml', action="store_true",
                         default=False, help="output values in YAML")
+    parser.add_argument('--host', action='store_true',
+                        help='Do not raise warning if all objects belong to 
the same host')
     parser.add_argument('--debug', action="store_true",
                         default=False, help="print debug info")
     parser.add_argument('--quiet', action="store_true", dest='quiet',
diff --git a/conftool/tests/integration/test_tool.py 
b/conftool/tests/integration/test_tool.py
index 90a027a..08dd81b 100644
--- a/conftool/tests/integration/test_tool.py
+++ b/conftool/tests/integration/test_tool.py
@@ -117,6 +117,7 @@
         # Check that the warning gets called upon if we select more than
         # one node, or not if we don't
         args = ['select', 'cluster=appservers', 'set/pooled=yes']
+        original_raise_warning = tool.ToolCliByLabel.raise_warning
         tool.ToolCliByLabel.raise_warning = mock.MagicMock()
         with self.assertRaises(SystemExit) as cm:
             tool.main(cmdline=args)
@@ -139,6 +140,8 @@
         out = self.output_for(['select', 'name=mw1018', 'get'])
         self.assertEquals(out[0]['mw1018']['pooled'], 'inactive')
 
+        tool.ToolCliByLabel.raise_warning = original_raise_warning
+
     def test_select_empty(self):
         # Test that regexes are anchored and a partial name will not
         # get us any result.
diff --git a/conftool/tests/unit/test_cli_tool.py 
b/conftool/tests/unit/test_cli_tool.py
index e36e009..93aedeb 100644
--- a/conftool/tests/unit/test_cli_tool.py
+++ b/conftool/tests/unit/test_cli_tool.py
@@ -1,5 +1,6 @@
 import unittest
 import mock
+
 from conftool.kvobject import KVObject
 from conftool import configuration
 from conftool import node
@@ -110,6 +111,43 @@
         with self.assertRaises(SystemExit):
             tagged(args, 're:cp10(11|20)\.example\.com', 'set')
 
+    def test_host_multiple_services(self):
+        """Set all services in a single host w/ and w/o the --host flag"""
+        # The query return a single host with multiple services
+        query_result = [
+            node.Node('dc', 'cluster', 'service_a', 'host_a'),
+            node.Node('dc', 'cluster', 'service_b', 'host_a'),
+            node.Node('dc', 'cluster', 'service_c', 'host_a')]
+
+        args = self._mock_args(selector='name=cp3009.esams.wmnet', host=False)
+        cli = tool.ToolCliByLabel(args)
+        cli._action = 'set'
+        cli.entity.query = mock.MagicMock(return_value=query_result)
+
+        # With args.host=False we expect raw_input question, answering yes
+        with mock.patch('__builtin__.raw_input', return_value='y') as _raw:
+            cli.host_list()
+            _raw.assert_called_once_with('confctl>')
+
+        # With args.host=False we expect raw_input question, answering no
+        with mock.patch('__builtin__.raw_input', return_value='n') as _raw:
+            self.assertRaises(SystemExit, cli.host_list)
+
+        # With args.host=True we do not expect raw_input questions
+        cli.args.host = True
+        with mock.patch('__builtin__.raw_input') as _raw:
+            cli.host_list()
+            self.assertEquals(_raw.call_args_list, [])
+
+        # Adding another host to the query result
+        query_result.append(node.Node('dc', 'cluster', 'service_a', 'host_b'))
+        cli.entity.query = mock.MagicMock(return_value=query_result)
+
+        # With args.host=True we expect raw_input question, answering y
+        with mock.patch('__builtin__.raw_input', return_value='y') as _raw:
+            cli.host_list()
+            _raw.assert_called_once_with('confctl>')
+
     def test_parse_args(self):
         # Taglist
         cmdline = ['tags', 'dc=a,cluster=b', '--action', 'get', 'all']

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I1f6ec5d4cb60cb84c1b372382b412aeb712c7f5d
Gerrit-PatchSet: 8
Gerrit-Project: operations/software/conftool
Gerrit-Branch: master
Gerrit-Owner: Volans <[email protected]>
Gerrit-Reviewer: BBlack <[email protected]>
Gerrit-Reviewer: Giuseppe Lavagetto <[email protected]>
Gerrit-Reviewer: Volans <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to