[autotest] Change resultdir name inside container to be the same as outside container.

The job folder (resultdir) in drone has the format of job_id-user_name.
However, if a test runs inside the container (using server-side
packaging), its folder name is job_id only. The reason was that we
don't want to pass another piece of information to container.

This causes a problem in crash collection. During log collection, test
job creates a file uncollected_log_file to store the list of files to
collect, in the format of source and target pair, where source is the
origin path of the file, e.g., path to a log file in DUT, and target is
a path to the destination to copy to, e.g.,
/usr/local/autotest/results/226/hardware_StorageQualBase.before

If the log collection was interrupted or autoserv process died before
logs can be collected, another autoserv process will be kicked off by
scheduler to run with --collect-crashinfo. The new autoserv process will
read the content of uncollected_log_file and tries to copy file based on
the source/target pair.

Due to the difference of job folder used in container, crash collection
will fail as the target directory does not exist on the drone (as the
job folder only has job_id, but not in the format of job_id-user_name.

This change passes the full job folder name to setup_test call when
setting up the container to run the test. That way, the result path will
be exactly the same inside and outside of the container.

BUG=chromium:589173
TEST=local run ssp test, unittest
manual run crash collection:
/usr/local/autotest/server/autoserv -p --use-existing-results
--collect-crashinfo 100.96.51.226 -r
'/usr/local/autotest/results/372-debug_user/100.96.51.226'

Change-Id: I4e1abb8c566ca65e8e245e68f29a9b996fe6be32
Reviewed-on: https://chromium-review.googlesource.com/329055
Commit-Ready: Dan Shi <dshi@google.com>
Tested-by: Dan Shi <dshi@google.com>
Reviewed-by: Simran Basi <sbasi@chromium.org>
diff --git a/server/autoserv b/server/autoserv
index 889ff07..8de1f6d 100755
--- a/server/autoserv
+++ b/server/autoserv
@@ -140,7 +140,8 @@
     return script_locals['ssp_url']
 
 
-def _run_with_ssp(job, container_name, job_id, results, parser, ssp_url):
+def _run_with_ssp(job, container_name, job_id, results, parser, ssp_url,
+                  job_folder):
     """Run the server job with server-side packaging.
 
     @param job: The server job object.
@@ -153,13 +154,15 @@
                     results can be None for autoserv run requires no logging.
     @param parser: Command line parser that contains the options.
     @param ssp_url: url of the staged server-side package.
+    @param job_folder: Name of the job result folder.
     """
     bucket = lxc.ContainerBucket()
     control = (parser.args[0] if len(parser.args) > 0 and parser.args[0] != ''
                else None)
     try:
         test_container = bucket.setup_test(container_name, job_id, ssp_url,
-                                           results, control=control)
+                                           results, control=control,
+                                           job_folder=job_folder)
     except Exception as e:
         job.record('FAIL', None, None,
                    'Failed to setup container for test: %s. Check logs in '
@@ -187,12 +190,12 @@
         paths_to_replace[control] = container_control_filename
     # Update result directory with the one in container.
     if parser.options.results:
-        container_result_dir = os.path.join(lxc.RESULT_DIR_FMT % job_id)
+        container_result_dir = os.path.join(lxc.RESULT_DIR_FMT % job_folder)
         paths_to_replace[parser.options.results] = container_result_dir
     # Update parse_job directory with the one in container. The assumption is
     # that the result folder to be parsed is always the same as the results_dir.
     if parser.options.parse_job:
-        container_parse_dir = os.path.join(lxc.RESULT_DIR_FMT % job_id)
+        container_parse_dir = os.path.join(lxc.RESULT_DIR_FMT % job_folder)
         paths_to_replace[parser.options.parse_job] = container_result_dir
 
     args = [paths_to_replace.get(arg, arg) for arg in args]
@@ -340,6 +343,7 @@
             parser.options.results)
     container_name = (lxc.TEST_CONTAINER_NAME_FMT %
                       (job_or_task_id, time.time(), os.getpid()))
+    job_folder = job_directories.get_job_folder_name(parser.options.results)
 
     # Implement SIGTERM handler
     def handle_sigterm(signum, frame):
@@ -519,11 +523,22 @@
                 if use_ssp:
                     try:
                         _run_with_ssp(job, container_name, job_or_task_id,
-                                      results, parser, ssp_url)
+                                      results, parser, ssp_url, job_folder)
                     finally:
                         # Update the ownership of files in result folder.
                         correct_results_folder_permission(results)
                 else:
+                    if collect_crashinfo:
+                        # Update the ownership of files in result folder. If the
+                        # job to collect crashinfo was running inside container
+                        # (SSP) and crashed before correcting folder permission,
+                        # the result folder might have wrong permission setting.
+                        try:
+                            correct_results_folder_permission(results)
+                        except:
+                            # Ignore any error as the user may not have root
+                            # permission to run sudo command.
+                            pass
                     job.run(install_before, install_after,
                             verify_job_repo_url=verify_job_repo_url,
                             only_collect_crashinfo=collect_crashinfo,