Merge "tests: Improve logging for executed commands"

This commit is contained in:
Zuul 2022-05-13 11:57:56 +00:00 committed by Gerrit Code Review
commit 4b14c400ff
2 changed files with 40 additions and 14 deletions

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import logging
import os import os
import shlex import shlex
import subprocess import subprocess
@ -18,22 +19,30 @@ from tempest.lib.cli import output_parser
from tempest.lib import exceptions from tempest.lib import exceptions
import testtools import testtools
ADMIN_CLOUD = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin') ADMIN_CLOUD = os.environ.get('OS_ADMIN_CLOUD', 'devstack-admin')
LOG = logging.getLogger(__name__)
def execute(cmd, fail_ok=False, merge_stderr=False): def execute(cmd, fail_ok=False, merge_stderr=False):
"""Executes specified command for the given action.""" """Executes specified command for the given action."""
LOG.debug('Executing: %s', cmd)
cmdlist = shlex.split(cmd) cmdlist = shlex.split(cmd)
stdout = subprocess.PIPE stdout = subprocess.PIPE
stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE
proc = subprocess.Popen(cmdlist, stdout=stdout, stderr=stderr) proc = subprocess.Popen(cmdlist, stdout=stdout, stderr=stderr)
result, result_err = proc.communicate()
result = result.decode('utf-8') result_out, result_err = proc.communicate()
result_out = result_out.decode('utf-8')
LOG.debug('stdout: %s', result_out)
LOG.debug('stderr: %s', result_err)
if not fail_ok and proc.returncode != 0: if not fail_ok and proc.returncode != 0:
raise exceptions.CommandFailed(proc.returncode, cmd, result, raise exceptions.CommandFailed(
result_err) proc.returncode, cmd, result_out, result_err,
return result )
return result_out
class TestCase(testtools.TestCase): class TestCase(testtools.TestCase):

View File

@ -1195,19 +1195,19 @@ class ServerTests(common.ComputeTestCase):
def test_server_add_remove_volume(self): def test_server_add_remove_volume(self):
volume_wait_for = volume_common.BaseVolumeTests.wait_for_status volume_wait_for = volume_common.BaseVolumeTests.wait_for_status
name = uuid.uuid4().hex server_name = uuid.uuid4().hex
cmd_output = json.loads(self.openstack( cmd_output = json.loads(self.openstack(
'server create -f json ' + 'server create -f json ' +
'--network private ' + '--network private ' +
'--flavor ' + self.flavor_name + ' ' + '--flavor ' + self.flavor_name + ' ' +
'--image ' + self.image_name + ' ' + '--image ' + self.image_name + ' ' +
'--wait ' + '--wait ' +
name server_name
)) ))
self.assertIsNotNone(cmd_output['id']) self.assertIsNotNone(cmd_output['id'])
self.assertEqual(name, cmd_output['name']) self.assertEqual(server_name, cmd_output['name'])
self.addCleanup(self.openstack, 'server delete --wait ' + name) self.addCleanup(self.openstack, 'server delete --wait ' + server_name)
server_id = cmd_output['id'] server_id = cmd_output['id']
volume_name = uuid.uuid4().hex volume_name = uuid.uuid4().hex
@ -1225,7 +1225,7 @@ class ServerTests(common.ComputeTestCase):
cmd_output = json.loads(self.openstack( cmd_output = json.loads(self.openstack(
'server add volume -f json ' + 'server add volume -f json ' +
name + ' ' + server_name + ' ' +
volume_name + ' ' + volume_name + ' ' +
'--tag bar' '--tag bar'
)) ))
@ -1237,7 +1237,7 @@ class ServerTests(common.ComputeTestCase):
cmd_output = json.loads(self.openstack( cmd_output = json.loads(self.openstack(
'server volume list -f json ' + 'server volume list -f json ' +
name server_name
)) ))
self.assertEqual(volume_attachment_id, cmd_output[0]['ID']) self.assertEqual(volume_attachment_id, cmd_output[0]['ID'])
@ -1245,8 +1245,25 @@ class ServerTests(common.ComputeTestCase):
self.assertEqual(volume_id, cmd_output[0]['Volume ID']) self.assertEqual(volume_id, cmd_output[0]['Volume ID'])
volume_wait_for('volume', volume_name, 'in-use') volume_wait_for('volume', volume_name, 'in-use')
self.openstack('server remove volume ' + name + ' ' + volume_name)
cmd_output = json.loads(self.openstack(
'server event list -f json ' +
server_name
))
self.assertEqual(2, len(cmd_output))
self.assertIn('attach_volume', {x['Action'] for x in cmd_output})
self.openstack(
'server remove volume ' + server_name + ' ' + volume_name
)
volume_wait_for('volume', volume_name, 'available') volume_wait_for('volume', volume_name, 'available')
raw_output = self.openstack('server volume list ' + name) cmd_output = json.loads(self.openstack(
'server event list -f json ' +
server_name
))
self.assertEqual(3, len(cmd_output))
self.assertIn('detach_volume', {x['Action'] for x in cmd_output})
raw_output = self.openstack('server volume list ' + server_name)
self.assertEqual('\n', raw_output) self.assertEqual('\n', raw_output)