Skip to content

Commit d5b6f5a

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "tests: Move json decoding to base test class"
2 parents 31881c0 + a244bb8 commit d5b6f5a

2 files changed

Lines changed: 48 additions & 30 deletions

File tree

openstackclient/tests/functional/base.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13+
import json
1314
import logging
1415
import os
1516
import shlex
@@ -48,33 +49,52 @@ def execute(cmd, fail_ok=False, merge_stderr=False):
4849
class TestCase(testtools.TestCase):
4950

5051
@classmethod
51-
def openstack(cls, cmd, cloud=ADMIN_CLOUD, fail_ok=False):
52+
def openstack(
53+
cls,
54+
cmd,
55+
*,
56+
cloud=ADMIN_CLOUD,
57+
fail_ok=False,
58+
parse_output=False,
59+
):
5260
"""Executes openstackclient command for the given action
5361
54-
NOTE(dtroyer): There is a subtle distinction between passing
55-
cloud=None and cloud='': for compatibility reasons passing
56-
cloud=None continues to include the option '--os-auth-type none'
57-
in the command while passing cloud='' omits the '--os-auth-type'
58-
option completely to let the default handlers be invoked.
62+
:param cmd: A string representation of the command to execute.
63+
:param cloud: The cloud to execute against. This can be a string, empty
64+
string, or None. A string results in '--os-auth-type $cloud', an
65+
empty string results in the '--os-auth-type' option being
66+
omitted, and None resuts in '--os-auth-type none' for legacy
67+
reasons.
68+
:param fail_ok: If failure is permitted. If False (default), a command
69+
failure will result in `~tempest.lib.exceptions.CommandFailed`
70+
being raised.
71+
:param parse_output: If true, pass the '-f json' parameter and decode
72+
the output.
73+
:returns: The output from the command.
74+
:raises: `~tempest.lib.exceptions.CommandFailed` if the command failed
75+
and ``fail_ok`` was ``False``.
5976
"""
77+
auth_args = []
6078
if cloud is None:
6179
# Execute command with no auth
62-
return execute(
63-
'openstack --os-auth-type none ' + cmd,
64-
fail_ok=fail_ok
65-
)
66-
elif cloud == '':
67-
# Execute command with no auth options at all
68-
return execute(
69-
'openstack ' + cmd,
70-
fail_ok=fail_ok
71-
)
72-
else:
80+
auth_args.append('--os-auth-type none')
81+
elif cloud != '':
7382
# Execute command with an explicit cloud specified
74-
return execute(
75-
'openstack --os-cloud=' + cloud + ' ' + cmd,
76-
fail_ok=fail_ok
77-
)
83+
auth_args.append(f'--os-cloud {cloud}')
84+
85+
format_args = []
86+
if parse_output:
87+
format_args.append('-f json')
88+
89+
output = execute(
90+
' '.join(['openstack'] + auth_args + [cmd] + format_args),
91+
fail_ok=fail_ok,
92+
)
93+
94+
if parse_output:
95+
return json.loads(output)
96+
else:
97+
return output
7898

7999
@classmethod
80100
def is_service_enabled(cls, service, version=None):

openstackclient/tests/functional/common/test_module.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14-
#
15-
16-
import json
1714

1815
from openstackclient.tests.functional import base
1916

@@ -31,14 +28,14 @@ class ModuleTest(base.TestCase):
3128

3229
def test_module_list(self):
3330
# Test module list
34-
cmd_output = json.loads(self.openstack('module list -f json'))
31+
cmd_output = self.openstack('module list', parse_output=True)
3532
for one_module in self.CLIENTS:
3633
self.assertIn(one_module, cmd_output.keys())
3734
for one_module in self.LIBS:
3835
self.assertNotIn(one_module, cmd_output.keys())
3936

4037
# Test module list --all
41-
cmd_output = json.loads(self.openstack('module list --all -f json'))
38+
cmd_output = self.openstack('module list --all', parse_output=True)
4239
for one_module in self.CLIENTS + self.LIBS:
4340
self.assertIn(one_module, cmd_output.keys())
4441

@@ -56,7 +53,7 @@ class CommandTest(base.TestCase):
5653
]
5754

5855
def test_command_list_no_option(self):
59-
cmd_output = json.loads(self.openstack('command list -f json'))
56+
cmd_output = self.openstack('command list', parse_output=True)
6057
group_names = [each.get('Command Group') for each in cmd_output]
6158
for one_group in self.GROUPS:
6259
self.assertIn(one_group, group_names)
@@ -70,9 +67,10 @@ def test_command_list_with_group(self):
7067
'compute.v2'
7168
]
7269
for each_input in input_groups:
73-
cmd_output = json.loads(self.openstack(
74-
'command list --group %s -f json' % each_input
75-
))
70+
cmd_output = self.openstack(
71+
'command list --group %s' % each_input,
72+
parse_output=True,
73+
)
7674
group_names = [each.get('Command Group') for each in cmd_output]
7775
for each_name in group_names:
7876
self.assertIn(each_input, each_name)

0 commit comments

Comments
 (0)