update cros_build_lib.run API usage

Switch to the new run API and away from the old RunCommand API.

BUG=chromium:1006587
TEST=CQ passes

Change-Id: I8897e4b3a90760457495845da3eb76cf9c3a3e6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crostestutils/+/1965813
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
Reviewed-by: Sean Abraham <seanabraham@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
diff --git a/au_test_harness/cros_au_test_harness_unittest.py b/au_test_harness/cros_au_test_harness_unittest.py
index 05951e6..6f09fde 100755
--- a/au_test_harness/cros_au_test_harness_unittest.py
+++ b/au_test_harness/cros_au_test_harness_unittest.py
@@ -35,7 +35,7 @@
            '--type=aws'
           ]
     with self.assertRaises(cros_build_lib.RunCommandError) as cm:
-      cros_build_lib.RunCommand(cmd)
+      cros_build_lib.run(cmd)
 
     self.assertIn(self.INVALID_TYPE_ERROR, cm.exception.result.error)
 
@@ -48,7 +48,7 @@
     # We don't have all required flags passed in so still expect an exception,
     # but it won't be complaining about invalid type.
     with self.assertRaises(cros_build_lib.RunCommandError) as cm:
-      cros_build_lib.RunCommand(cmd)
+      cros_build_lib.run(cmd)
 
     self.assertNotIn(self.INVALID_TYPE_ERROR, cm.exception.result.error)
 
diff --git a/au_test_harness/gce_au_worker.py b/au_test_harness/gce_au_worker.py
index 2e3db37..ac606c9 100644
--- a/au_test_harness/gce_au_worker.py
+++ b/au_test_harness/gce_au_worker.py
@@ -166,11 +166,10 @@
                    path_util.ToChrootPath(self.ssh_private_key))
 
       logging.info('Running test %s to verify image.', test)
-      result = cros_build_lib.RunCommand(cmd, error_code_ok=True,
-                                         enter_chroot=True,
-                                         redirect_stdout=True,
-                                         cwd=constants.CROSUTILS_DIR)
-      percent_passed = self.ParseGeneratedTestOutput(result.output)
+      result = cros_build_lib.run(cmd, check=False, enter_chroot=True,
+                                  encoding='utf-8', stdout=True,
+                                  cwd=constants.CROSUTILS_DIR)
+      percent_passed = self.ParseGeneratedTestOutput(result.stdout)
       passed = percent_passed >= percent_required_to_pass
       if not passed:
         self._HandleFail(log_directory, fail_directory)
diff --git a/au_test_harness/gce_au_worker_unittest.py b/au_test_harness/gce_au_worker_unittest.py
index a840504..2bf7761 100755
--- a/au_test_harness/gce_au_worker_unittest.py
+++ b/au_test_harness/gce_au_worker_unittest.py
@@ -98,20 +98,20 @@
     """Helper function that checks correct test is run by VerifyImage."""
 
     def _MockRunCommand(cmd, *_args, **_kwargs):
-      """A mock cros_build_lib.RunCommand that verifies passed-in arguments."""
+      """A mock cros_build_lib.run that verifies passed-in arguments."""
       self.assertIn('test_that', cmd)
       self.assertIn(expected_test_ran, cmd)
       return cros_build_lib.CommandResult()
 
     self.PatchObject(path_util, 'ToChrootPath', autospec=True)
-    self.PatchObject(cros_build_lib, 'RunCommand', side_effect=_MockRunCommand,
+    self.PatchObject(cros_build_lib, 'run', side_effect=_MockRunCommand,
                      autospec=True)
     self.PatchObject(AUWorker, 'ParseGeneratedTestOutput', autospec=True,
                      return_value=100)
 
     worker.VerifyImage(None, test=passed_in_test)
     # Ensure that the checks in _MockRunCommand did take place.
-    self.assertEqual(1, cros_build_lib.RunCommand.call_count)
+    self.assertEqual(1, cros_build_lib.run.call_count)
 
   def testVerifyImage(self):
     """Verifies that VerifyImage runs the correct test.
@@ -130,7 +130,7 @@
     worker = GCEAUWorker(self.options, './log',
                          json_key_file=self.json_key_file)
     self.PatchObject(path_util, 'ToChrootPath', autospec=True)
-    self.PatchObject(cros_build_lib, 'RunCommand', autospec=True)
+    self.PatchObject(cros_build_lib, 'run', autospec=True)
     self.PatchObject(worker, '_HandleFail', autospec=True)
     self.PatchObject(worker, '_GetTestReport', autospec=True,
                      return_value='report')
diff --git a/au_test_harness/vm_au_worker.py b/au_test_harness/vm_au_worker.py
index e3e8956..48d5108 100644
--- a/au_test_harness/vm_au_worker.py
+++ b/au_test_harness/vm_au_worker.py
@@ -9,6 +9,7 @@
 
 import os
 import shutil
+import subprocess
 import tempfile
 
 from chromite.cbuildbot import constants as buildbot_constants
@@ -32,10 +33,10 @@
 
   def _StopVM(self):
     """Stops an existing VM."""
-    cros_build_lib.RunCommand(['./cros_vm', '--stop',
-                               '--ssh-port=%s' % self._ssh_port],
-                              print_cmd=False, error_code_ok=True,
-                              cwd=chromite_constants.CHROMITE_BIN_DIR)
+    cros_build_lib.run(['./cros_vm', '--stop',
+                        '--ssh-port=%s' % self._ssh_port],
+                       print_cmd=False, check=False,
+                       cwd=chromite_constants.CHROMITE_BIN_DIR)
 
   def CleanUp(self):
     """Stop the vm after a test."""
@@ -109,10 +110,10 @@
 
     self.TestInfo('Running %s suite to verify image.' % test)
 
-    result = cros_build_lib.RunCommand(
-        command, print_cmd=self.verbose, combine_stdout_stderr=True,
-        cwd=chromite_constants.CHROMITE_BIN_DIR, error_code_ok=True,
-        capture_output=not self.verbose)
+    result = cros_build_lib.run(
+        command, print_cmd=self.verbose, stderr=subprocess.STDOUT,
+        cwd=chromite_constants.CHROMITE_BIN_DIR, check=False,
+        capture_output=not self.verbose, encoding='utf-8')
     # If the command failed or printed warnings, print the output.
     # TODO(pwang): result.stdout should contain the output of the command even
     # though verbose is set to print to stdout.
diff --git a/devmode-test/devinstall_test.py b/devmode-test/devinstall_test.py
index 1409273..08c12db 100755
--- a/devmode-test/devinstall_test.py
+++ b/devmode-test/devinstall_test.py
@@ -80,10 +80,9 @@
         self.device.Cleanup()
       self.device = None
       if self.port:
-        cros_build_lib.RunCommand(['./cros_vm', '--stop',
-                                   '--ssh-port=%d' % self.port],
-                                  cwd=chromite_constants.CHROMITE_BIN_DIR,
-                                  error_code_ok=True)
+        cros_build_lib.run(['./cros_vm', '--stop', '--ssh-port=%d' % self.port],
+                           cwd=chromite_constants.CHROMITE_BIN_DIR,
+                           check=False)
       self.port = None
       osutils.RmDir(self.tmpdir, ignore_missing=True)
       self.tmpdir = None
@@ -117,7 +116,7 @@
     logging.info('Starting the vm on port %d.', self.port)
     vm_cmd = ['./cros_vm', '--ssh-port=%d' % self.port,
               '--image-path=%s' % self.working_image_path, '--start']
-    cros_build_lib.RunCommand(vm_cmd, cwd=chromite_constants.CHROMITE_BIN_DIR)
+    cros_build_lib.run(vm_cmd, cwd=chromite_constants.CHROMITE_BIN_DIR)
 
     self.device = remote_access.ChromiumOSDevice(
         remote_access.LOCALHOST, port=self.port, base_dir=self.tmpdir)
@@ -138,7 +137,7 @@
     """Tests that we can run dev-install and have python work afterwards."""
     try:
       logging.info('Running dev install in the vm.')
-      self.device.RunCommand(
+      self.device.run(
           ['bash', '-l', '-c',
            '"/usr/bin/dev_install --yes --binhost=%s"' % self.binhost])
 
@@ -147,9 +146,9 @@
       # while it fails from another.  Test all the prefixes (and $PATH).
       for prefix in ('/usr/bin', '/usr/local/bin', '/usr/local/usr/bin', ''):
         for prog in ('python', 'python2', 'python3'):
-          self.device.RunCommand(['sudo', '-u', 'chronos', '--',
-                                  os.path.join(prefix, prog),
-                                  '-c', '"print(\'hello world\')"'])
+          self.device.run(['sudo', '-u', 'chronos', '--',
+                           os.path.join(prefix, prog),
+                           '-c', '"print(\'hello world\')"'])
     except (cros_build_lib.RunCommandError,
             remote_access.SSHConnectionError) as e:
       self.devserver.PrintLog()
diff --git a/lib/test_helper.py b/lib/test_helper.py
index b846bb2..e47c4cb 100644
--- a/lib/test_helper.py
+++ b/lib/test_helper.py
@@ -19,10 +19,10 @@
 
 def _GetTotalMemoryGB():
   """Calculate total memory on this machine, in gigabytes."""
-  res = cros_build_lib.RunCommand(['free', '-g'], print_cmd=False,
-                                  capture_output=True)
+  res = cros_build_lib.run(['free', '-g'], print_cmd=False, capture_output=True,
+                           encoding='utf-8')
   assert res.returncode == 0
-  for line in res.output.splitlines():
+  for line in res.stdout.splitlines():
     if line.startswith('Mem:'):
       return int(line.split()[1])
   raise Exception('Could not calculate total memory')
@@ -66,7 +66,7 @@
     if board:
       cmd.extend(['--board', board])
 
-    cros_build_lib.RunCommand(cmd, enter_chroot=True, cwd=constants.SOURCE_ROOT)
+    cros_build_lib.run(cmd, enter_chroot=True, cwd=constants.SOURCE_ROOT)
 
   assert os.path.exists(vm_image_path), 'Failed to create the VM image.'
   return vm_image_path