]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
qa/workunits/mon/test_mon_config_key: use subprocess.run() instead of proc.communicate() 42498/head
authorKefu Chai <kchai@redhat.com>
Fri, 16 Apr 2021 04:09:14 +0000 (12:09 +0800)
committerDeepika Upadhyay <dupadhya@redhat.com>
Tue, 27 Jul 2021 04:06:24 +0000 (04:06 +0000)
the loop of proc.communicate() on python3.6, where we always are able to
get something out of stdout and/or stderr PIPEs. and the `stdout` and
`stderr` keep growing until out of memory. and teuthology considers
the command crashed after a while.

Fixes: https://tracker.ceph.com/issues/50393
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit f2e0770c276e6fffa01a0bcb2d969f02ee1aafef)

qa/workunits/mon/test_mon_config_key.py

index 285d1f4cea6ad28ce8e534a3f97907aeecf44faa..5e0760af3dab7bb2af0361a6862a56a5f4351dd6 100755 (executable)
@@ -78,39 +78,19 @@ def run_cmd(cmd, expects=0):
     cmdlog = LOG.getChild('run_cmd')
     cmdlog.debug('{fc}'.format(fc=' '.join(full_cmd)))
 
-    proc = subprocess.Popen(full_cmd,
-                            stdout=subprocess.PIPE,
-                            stderr=subprocess.PIPE)
-
-    stdout = []
-    stderr = []
-    while True:
-        try:
-            out, err = proc.communicate()
-            if out is not None:
-                stdout += out.decode().split('\n')
-                cmdlog.debug('stdout: {s}'.format(s=out))
-            if err is not None:
-                stdout += err.decode().split('\n')
-                cmdlog.debug('stderr: {s}'.format(s=err))
-        except ValueError:
-            ret = proc.wait()
-            break
-
-    if ret != expects:
-        cmdlog.error('cmd > {cmd}'.format(cmd=full_cmd))
-        cmdlog.error("expected return '{expected}' got '{got}'".format(
-            expected=expects, got=ret))
+    proc = subprocess.run(full_cmd,
+                          stdout=subprocess.PIPE,
+                          stderr=subprocess.PIPE,
+                          universal_newlines=True)
+    if proc.returncode != expects:
+        cmdlog.error(f'cmd > {proc.args}')
+        cmdlog.error(f'expected return "{expects}" got "{proc.returncode}"')
         cmdlog.error('stdout')
-        for i in stdout:
-            cmdlog.error('{x}'.format(x=i))
+        cmdlog.error(proc.stdout)
         cmdlog.error('stderr')
-        for i in stderr:
-            cmdlog.error('{x}'.format(x=i))
+        cmdlog.error(proc.stderr)
 
 
-# end run_cmd
-
 def gen_data(size, rnd):
     chars = string.ascii_letters + string.digits
     return ''.join(rnd.choice(chars) for _ in range(size))