[autotest] Disable install autotest from job_repo_url for test_that

use_packing option for autotest._install method is True by default. When using
test_that to run test, DUT will try to install autotest package from
job_repo_url with server configured in global_config (cautotest). For DUT not
located in lab, there is no job_repo_url existed for such DUT in cautotest, and
therefore autotest will try to copy over the local files. However, when a user
try to use a DUT in the lab to run test_that, autotest package is downloaded
and installed from job_repo_url. That leads to a problem that developers can't
use test_that to verify autotest code.

The fix is to add a new option no_use_packaging to autoserv, and thread the
option to autotest._install. The option is set to False by default, and set
to True in test_that. Also, when this argument is set to True, any package
installation will use AutoservFetcher.

BUG=chromium:265673
TEST=run following command and confirm local autotest code is copied over to
     the DUT: test_that -b lumpy 172.22.75.225 dummy_Pass

Change-Id: I365f5f7dc79c8e65cddaa0af77f1f1a1e4b0012a
Reviewed-on: https://chromium-review.googlesource.com/169343
Reviewed-by: Mungyung Ryu <mkryu@google.com>
Commit-Queue: Mungyung Ryu <mkryu@google.com>
Tested-by: Mungyung Ryu <mkryu@google.com>
diff --git a/server/autoserv b/server/autoserv
index d9b967d..597fa92 100755
--- a/server/autoserv
+++ b/server/autoserv
@@ -293,6 +293,7 @@
     skip_crash_collection = parser.options.skip_crash_collection
     ssh_verbosity = int(parser.options.ssh_verbosity)
     ssh_options = parser.options.ssh_options
+    no_use_packaging = parser.options.no_use_packaging
 
     # can't be both a client and a server side test
     if client and server:
@@ -372,7 +373,8 @@
                             verify_job_repo_url=verify_job_repo_url,
                             only_collect_crashinfo=collect_crashinfo,
                             skip_crash_collection=skip_crash_collection,
-                            job_labels=job_labels)
+                            job_labels=job_labels,
+                            use_packaging=(not no_use_packaging))
         finally:
             while job.hosts:
                 host = job.hosts.pop()
diff --git a/server/autoserv_parser.py b/server/autoserv_parser.py
index 3d706a4..d64e147 100644
--- a/server/autoserv_parser.py
+++ b/server/autoserv_parser.py
@@ -178,6 +178,10 @@
                                      "process runs in a drone without server-"
                                      "side packaging support, even though the "
                                      "job requires server-side packaging"))
+        self.parser.add_option("--no_use_packaging", action="store_true",
+                               dest="no_use_packaging", default=False,
+                               help=("Disable install modes that use the "
+                                     "packaging system."))
 
 
     def parse_args(self):
diff --git a/server/autoserv_utils.py b/server/autoserv_utils.py
index 2db6052..3275fb4 100644
--- a/server/autoserv_utils.py
+++ b/server/autoserv_utils.py
@@ -27,7 +27,8 @@
                              write_pidfile=True, fast_mode=False,
                              ssh_verbosity=0,
                              no_console_prefix=False,
-                             ssh_options=None,):
+                             ssh_options=None,
+                             use_packaging=True):
     """
     Construct an autoserv command from a job or host queue entry.
 
@@ -54,7 +55,10 @@
                               in autoserv console logs.
     @param ssh_options: A string giving extra arguments to be tacked on to
                         ssh commands.
+    @param use_packaging Enable install modes that use the packaging system.
+
     @returns The autoserv command line as a list of executable + parameters.
+
     """
     command = [os.path.join(autoserv_directory, 'autoserv')]
 
@@ -107,6 +111,9 @@
         command.append('--disable_sysinfo')
         command.append('--no_collect_crashinfo')
 
+    if not use_packaging:
+        command.append('--no_use_packaging')
+
     return command + extra_args
 
 
diff --git a/server/autotest.py b/server/autotest.py
index 10f8376..b17d455 100644
--- a/server/autotest.py
+++ b/server/autotest.py
@@ -1,4 +1,5 @@
 # Copyright 2007 Google Inc. Released under the GPL v2
+#pylint: disable-msg=C0111
 
 import re, os, sys, traceback, time, glob, tempfile
 import logging
@@ -157,8 +158,8 @@
         return repos
 
 
-    def install(self, host=None, autodir=None):
-        self._install(host=host, autodir=autodir)
+    def install(self, host=None, autodir=None, use_packaging=True):
+        self._install(host=host, autodir=autodir, use_packaging=use_packaging)
 
 
     def install_full_client(self, host=None, autodir=None):
@@ -250,6 +251,8 @@
         if use_packaging:
             try:
                 self._install_using_packaging(host, autodir)
+                logging.info("Installation of autotest completed using the "
+                             "packaging system.")
                 return
             except (error.PackageInstallError, error.AutoservRunError,
                     global_config.ConfigError), e:
@@ -266,7 +269,8 @@
                 self._install_using_send_file(host, autodir)
             else:
                 host.send_file(self.source_material, autodir, delete_dest=True)
-            logging.info("Installation of autotest completed")
+            logging.info("Installation of autotest completed from %s",
+                         self.source_material)
             self.installed = True
             return
 
@@ -278,7 +282,7 @@
             host.run('svn checkout %s %s' % (AUTOTEST_SVN, autodir))
         except error.AutoservRunError, e:
             host.run('svn checkout %s %s' % (AUTOTEST_HTTP, autodir))
-        logging.info("Installation of autotest completed")
+        logging.info("Installation of autotest completed using SVN.")
         self.installed = True
 
 
@@ -321,7 +325,7 @@
 
     def run(self, control_file, results_dir='.', host=None, timeout=None,
             tag=None, parallel_flag=False, background=False,
-            client_disconnect_timeout=None):
+            client_disconnect_timeout=None, use_packaging=True):
         """
         Run an autotest job on the remote machine.
 
@@ -344,7 +348,7 @@
         @raises AutotestRunError: If there is a problem executing
                 the control file.
         """
-        host = self._get_host_and_setup(host)
+        host = self._get_host_and_setup(host, use_packaging=use_packaging)
         results_dir = os.path.abspath(results_dir)
 
         if client_disconnect_timeout is None:
@@ -355,21 +359,21 @@
 
         atrun = _Run(host, results_dir, tag, parallel_flag, background)
         self._do_run(control_file, results_dir, host, atrun, timeout,
-                     client_disconnect_timeout)
+                     client_disconnect_timeout, use_packaging=use_packaging)
 
 
-    def _get_host_and_setup(self, host):
+    def _get_host_and_setup(self, host, use_packaging=True):
         if not host:
             host = self.host
         if not self.installed:
-            self.install(host)
+            self.install(host, use_packaging=use_packaging)
 
         host.wait_up(timeout=30)
         return host
 
 
     def _do_run(self, control_file, results_dir, host, atrun, timeout,
-                client_disconnect_timeout):
+                client_disconnect_timeout, use_packaging=True):
         try:
             atrun.verify_machine()
         except:
@@ -401,10 +405,12 @@
         # If the packaging system is being used, add the repository list.
         repos = None
         try:
-            repos = self.get_fetch_location()
-            pkgmgr = packages.PackageManager('autotest', hostname=host.hostname,
-                                             repo_urls=repos)
-            prologue_lines.append('job.add_repository(%s)\n' % repos)
+            if use_packaging:
+                repos = self.get_fetch_location()
+                prologue_lines.append('job.add_repository(%s)\n' % repos)
+            else:
+                logging.debug('use_packaging is set to False, do not add any '
+                              'repository.')
         except global_config.ConfigError, e:
             # If repos is defined packaging is enabled so log the error
             if repos:
diff --git a/server/autotest_unittest.py b/server/autotest_unittest.py
index da8e151..7d57429 100755
--- a/server/autotest_unittest.py
+++ b/server/autotest_unittest.py
@@ -175,10 +175,9 @@
 
         # stub out install
         self.god.stub_function(self.base_autotest, "install")
-        self.god.stub_class(packages, "PackageManager")
 
         # record
-        self.base_autotest.install.expect_call(self.host)
+        self.base_autotest.install.expect_call(self.host, use_packaging=True)
         self.host.wait_up.expect_call(timeout=30)
         os.path.abspath.expect_call('.').and_return('.')
         run_obj = autotest._Run.expect_new(self.host, '.', None, False, False)
@@ -205,9 +204,6 @@
         c = autotest.global_config.global_config
         c.get_config_value.expect_call("PACKAGES",
             'fetch_location', type=list, default=[]).and_return(['repo'])
-        pkgmgr = packages.PackageManager.expect_new('autotest',
-                                                     repo_urls=['repo'],
-                                                     hostname='hostname')
 
         cfile = self.god.create_mock_class(file, "file")
         cfile_orig = "original control file"
diff --git a/server/control_segments/client_wrapper b/server/control_segments/client_wrapper
index 0d29c7e..13b8359 100644
--- a/server/control_segments/client_wrapper
+++ b/server/control_segments/client_wrapper
@@ -4,7 +4,7 @@
 def run_client(machine):
     host = hosts.create_host(machine)
     host.log_kernel()
-    at.run(control, host=host)
+    at.run(control, host=host, use_packaging=use_packaging)
 
 
 job.parallel_simple(run_client, machines)
diff --git a/server/server_job.py b/server/server_job.py
index 38b2d0c..eb81db0 100644
--- a/server/server_job.py
+++ b/server/server_job.py
@@ -558,7 +558,7 @@
             collect_crashdumps=True, namespace={}, control=None,
             control_file_dir=None, verify_job_repo_url=False,
             only_collect_crashinfo=False, skip_crash_collection=False,
-            job_labels=''):
+            job_labels='', use_packaging=True):
         # for a normal job, make sure the uncollected logs file exists
         # for a crashinfo-only run it should already exist, bail out otherwise
         created_uncollected_logs = False
@@ -641,6 +641,7 @@
                     utils.open_write_close(server_control_file, control)
 
                 logging.info("Processing control file")
+                namespace['use_packaging'] = use_packaging
                 self._execute_code(server_control_file, namespace)
                 logging.info("Finished processing control file")
 
diff --git a/server/site_autotest.py b/server/site_autotest.py
index 0f9f2a5..fe00835 100755
--- a/server/site_autotest.py
+++ b/server/site_autotest.py
@@ -19,6 +19,7 @@
 
 
 class SiteAutotest(installable_object.InstallableObject):
+    """Site implementation of Autotest."""
 
     def get(self, location=None):
         if not location:
@@ -98,16 +99,19 @@
         return repos
 
 
-    def install(self, host=None, autodir=None):
+    def install(self, host=None, autodir=None, use_packaging=True):
         """Install autotest.  If |host| is not None, stores it in |self.host|.
 
         @param host A Host instance on which autotest will be installed
         @param autodir Location on the remote host to install to
+        @param use_packaging Enable install modes that use the packaging system.
+
         """
         if host:
             self.host = host
 
-        super(SiteAutotest, self).install(host=host, autodir=autodir)
+        super(SiteAutotest, self).install(host=host, autodir=autodir,
+                                          use_packaging=use_packaging)
 
 
     def _install(self, host=None, autodir=None, use_autoserv=True,
diff --git a/site_utils/test_that.py b/site_utils/test_that.py
index d7b076e..5ea8583 100755
--- a/site_utils/test_that.py
+++ b/site_utils/test_that.py
@@ -253,7 +253,8 @@
                 fast_mode=fast_mode, ssh_verbosity=ssh_verbosity,
                 ssh_options=ssh_options,
                 extra_args=extra_args,
-                no_console_prefix=True)
+                no_console_prefix=True,
+                use_packaging=False)
 
         _run_autoserv(command, pretend)
         return results_directory