summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkihiro Motoki <motoki@da.jp.nec.com>2016-02-24 20:47:32 +0900
committerIhar Hrachyshka <ihrachys@redhat.com>2016-03-23 16:11:01 +0000
commit0411c5a8d410b39a63df600a08de16df5c82c86d (patch)
tree269730c230d8b46b8aca9efa2723ff5e22ef8f9f
parentd88cccb45703818b4d50df4ea525e4c6ba4f5adb (diff)
downloadpython-neutronclient-0411c5a8d410b39a63df600a08de16df5c82c86d.tar.gz
Do not print 'Created' message when using non-table formatter
When using non-table formatter like JSON or YAML formatter, users want to pass output result to some other program. Printing 'Created' message breaks the data format and non-table formatter makes useless. Functional tests for non-table formatters is added to avoid the same thing again. Change-Id: Ieb38bd26d26d134d4d274ad2d9f4d1e79fa4e977 Closes-Bug: #1548897 (cherry picked from commit 150cc4ce56fdf9f80a11fd5a7ca9cab4e7bc2c62)
-rw-r--r--neutronclient/neutron/v2_0/__init__.py5
-rw-r--r--neutronclient/tests/functional/core/test_cli_formatter.py59
2 files changed, 62 insertions, 2 deletions
diff --git a/neutronclient/neutron/v2_0/__init__.py b/neutronclient/neutron/v2_0/__init__.py
index cdf694c..05402f1 100644
--- a/neutronclient/neutron/v2_0/__init__.py
+++ b/neutronclient/neutron/v2_0/__init__.py
@@ -487,8 +487,9 @@ class CreateCommand(NeutronCommand, show.ShowOne):
self.format_output_data(data)
info = self.resource in data and data[self.resource] or None
if info:
- print(_('Created a new %s:') % self.resource,
- file=self.app.stdout)
+ if parsed_args.formatter == 'table':
+ print(_('Created a new %s:') % self.resource,
+ file=self.app.stdout)
else:
info = {'': ''}
return zip(*sorted(six.iteritems(info)))
diff --git a/neutronclient/tests/functional/core/test_cli_formatter.py b/neutronclient/tests/functional/core/test_cli_formatter.py
new file mode 100644
index 0000000..cd133de
--- /dev/null
+++ b/neutronclient/tests/functional/core/test_cli_formatter.py
@@ -0,0 +1,59 @@
+# Copyright 2016 NEC Corporation
+#
+# 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 oslo_serialization import jsonutils
+from oslo_utils import uuidutils
+import yaml
+
+from neutronclient.tests.functional import base
+
+
+class TestCLIFormatter(base.ClientTestBase):
+
+ def setUp(self):
+ super(TestCLIFormatter, self).setUp()
+ self.net_name = 'net-%s' % uuidutils.generate_uuid()
+ self.addCleanup(self.neutron, 'net-delete %s' % self.net_name)
+
+ def _create_net(self, fmt, col_attrs):
+ params = ['-c %s' % attr for attr in col_attrs]
+ params.append('-f %s' % fmt)
+ params.append(self.net_name)
+ param_string = ' '.join(params)
+ return self.neutron('net-create', params=param_string)
+
+ def test_net_create_with_json_formatter(self):
+ result = self._create_net('json', ['name', 'admin_state_up'])
+ self.assertDictEqual({'name': self.net_name,
+ 'admin_state_up': True},
+ jsonutils.loads(result))
+
+ def test_net_create_with_yaml_formatter(self):
+ result = self._create_net('yaml', ['name', 'admin_state_up'])
+ self.assertDictEqual({'name': self.net_name,
+ 'admin_state_up': True},
+ yaml.load(result))
+
+ def test_net_create_with_value_formatter(self):
+ # NOTE(amotoki): In 'value' formatter, there is no guarantee
+ # in the order of attribute, so we use one attribute in this test.
+ result = self._create_net('value', ['name'])
+ self.assertEqual(self.net_name, result.strip())
+
+ def test_net_create_with_shell_formatter(self):
+ result = self._create_net('shell', ['name', 'admin_state_up'])
+ result_lines = set(result.strip().split('\n'))
+ self.assertSetEqual(set(['name="%s"' % self.net_name,
+ 'admin_state_up="True"']),
+ result_lines)