s3:test: make sids2xids test compatible with py2 and py3
authorJoe Guo <joeg@catalyst.net.nz>
Thu, 13 Dec 2018 03:33:40 +0000 (16:33 +1300)
committerNoel Power <npower@samba.org>
Fri, 14 Dec 2018 13:40:20 +0000 (14:40 +0100)
define a wrapper function for subprocess.check_output,
to return bytes for py2 and unicode for py3, and replace Popen with it.

Signed-off-by: Joe Guo <joeg@catalyst.net.nz>
Reviewed-by: Noel Power <npower@samba.org>
source3/script/tests/test_wbinfo_sids2xids_int.py

index a3cc3901d747c9bfc1e40ef333f70637663519f6..08a0091e028ed5259f4ce616e98f382b75934dc0 100755 (executable)
@@ -4,6 +4,7 @@ from __future__ import print_function
 import sys
 import os
 import subprocess
+from samba.compat import PY3
 
 
 if len(sys.argv) != 3:
@@ -14,6 +15,16 @@ wbinfo = sys.argv[1]
 netcmd = sys.argv[2]
 
 
+def run(cmd):
+    """
+    Run a cmd, return bytes str for py2 and unicode str for py3.
+
+    NOTE: subprocess api always return bytes, in both py2 and py3.
+    """
+    output = subprocess.check_output(cmd).strip()
+    return output.decode('utf-8') if PY3 else output
+
+
 def flush_cache(sids=[], uids=[], gids=[]):
     for sid in sids:
         os.system(netcmd + (" cache del IDMAP/SID2XID/%s" % (sid)))
@@ -27,14 +38,11 @@ def fill_cache(inids, idtype='gid'):
     for inid in inids:
         if inid is None:
             continue
-        subprocess.Popen([wbinfo, '--%s-to-sid=%s' % (idtype, inid)],
-                         stdout=subprocess.PIPE).communicate()
+        run([wbinfo, '--%s-to-sid=%s' % (idtype, inid)])
 
 
-domain = subprocess.Popen([wbinfo, "--own-domain"],
-                          stdout=subprocess.PIPE).communicate()[0].strip()
-domsid = subprocess.Popen([wbinfo, "-n", domain + "/"],
-                          stdout=subprocess.PIPE).communicate()[0]
+domain = run([wbinfo, "--own-domain"])
+domsid = run([wbinfo, "-n", domain + "/"])
 domsid = domsid.split(' ')[0]
 
 # print domain
@@ -44,8 +52,7 @@ sids = [domsid + '-512', 'S-1-5-32-545', domsid + '-513', 'S-1-1-0', 'S-1-3-1',
 
 flush_cache(sids=sids)
 
-sids2xids = subprocess.Popen([wbinfo, '--sids-to-unix-ids=' + ','.join(sids)],
-                             stdout=subprocess.PIPE).communicate()[0].strip()
+sids2xids = run([wbinfo, '--sids-to-unix-ids=' + ','.join(sids)])
 
 gids = []
 uids = []
@@ -83,8 +90,7 @@ def check_singular(sids, ids, idtype='gid'):
         if ids[i] is None:
             continue
 
-        outid = subprocess.Popen([wbinfo, '--sid-to-%s' % idtype, sid],
-                                 stdout=subprocess.PIPE).communicate()[0].strip()
+        outid = run([wbinfo, '--sid-to-%s' % idtype, sid])
         if outid != ids[i]:
             print("Expected %s, got %s\n" % (outid, ids[i]))
             flush_cache(sids=sids, uids=uids, gids=gids)
@@ -96,8 +102,7 @@ def check_singular(sids, ids, idtype='gid'):
 
 
 def check_multiple(sids, idtypes):
-    sids2xids = subprocess.Popen([wbinfo, '--sids-to-unix-ids=' + ','.join(sids)],
-                                 stdout=subprocess.PIPE).communicate()[0].strip()
+    sids2xids = run([wbinfo, '--sids-to-unix-ids=' + ','.join(sids)])
     # print sids2xids
     i = 0
     for line in sids2xids.split('\n'):