def main(options):
    """Main script.

    @param options: Options to run the script.
    """
    config = logging_config.LoggingConfig()
    if options.logfile:
        config.add_file_handler(
            file_path=os.path.abspath(options.logfile),
            level=logging.DEBUG if options.verbose else logging.INFO)

    bucket = lxc.ContainerBucket()
    logging.info('')
    logging.info('Cleaning container bucket %s', bucket.container_path)
    success_count = 0
    failure_count = 0
    for container in bucket.get_all().values():
        if is_container_orphaned(container):
            if cleanup(container, options):
                success_count += 1
            else:
                failure_count += 1
    if options.execute:
        key = 'container_cleanup.%s' % socket.gethostname().replace('.', '_')
        autotest_stats.Gauge(key).send('success', success_count)
        autotest_stats.Gauge(key).send('failure', failure_count)
    logging.info('Cleanup finished.')
def main(options):
    """main script.

    @param options: Options to run the script.
    """
    # Force to run the test as superuser.
    # TODO(dshi): crbug.com/459344 Set remove this enforcement when test
    # container can be unprivileged container.
    if utils.sudo_require_password():
        logging.warn('SSP requires root privilege to run commands, please '
                     'grant root access to this process.')
        utils.run('sudo true')

    setup_logging(
        log_level=(logging.DEBUG if options.verbose else logging.INFO))

    bucket = lxc.ContainerBucket(TEMP_DIR)

    setup_base(bucket)
    container_test_name = (lxc.TEST_CONTAINER_NAME_FMT %
                           (TEST_JOB_ID, time.time(), os.getpid()))
    container = setup_test(bucket, container_test_name, options.skip_cleanup)
    test_share(container)
    test_autoserv(container)
    if options.dut:
        test_ssh(container, options.dut)
    if options.devserver:
        test_ssh(container, options.devserver)
    # Packages are installed in TEST_SCRIPT, verify the packages are installed.
    test_package_install(container)
    logging.info('All tests passed.')
def main(options):
    """main script.

    @param options: Options to run the script.
    """
    # Verify that the test is running as the correct user.
    unittest_setup.verify_user()

    log_level = (logging.DEBUG if options.verbose else logging.INFO)
    unittest_setup.setup_logging(log_level)

    setup_base(TEMP_DIR)
    bucket = lxc.ContainerBucket(TEMP_DIR)

    container_id = lxc.ContainerId.create(TEST_JOB_ID)
    container = setup_test(bucket, container_id, options.skip_cleanup)
    test_share(container)
    test_autoserv(container)
    if options.dut:
        test_ssh(container, options.dut)
    if options.devserver:
        test_ssh(container, options.devserver)
    # Packages are installed in TEST_SCRIPT, verify the packages are installed.
    test_package_install(container)
    logging.info('All tests passed.')
예제 #4
0
    def testHostDirMissing(self):
        """Verifies that a missing host dir does not cause container bucket
        destruction to crash.
        """
        bucket = lxc.ContainerBucket(container_path, self.shared_host_path)

        # Verify that the host path does not exist.
        self.assertFalse(os.path.exists(self.shared_host_path))
        # Do not call startup, just call destroy.  This should not throw.
        bucket.destroy_all()
예제 #5
0
    def handle_sigterm(signum, frame):
        logging.debug('Received SIGTERM')
        if pid_file_manager:
            pid_file_manager.close_file(1, signal.SIGTERM)
        logging.debug('Finished writing to pid_file. Killing process.')

        # Update results folder's file permission. This needs to be done ASAP
        # before the parsing process tries to access the log.
        if use_ssp and results:
            correct_results_folder_permission(results)

        # TODO (sbasi) - remove the time.sleep when crbug.com/302815 is solved.
        # This sleep allows the pending output to be logged before the kill
        # signal is sent.
        time.sleep(.1)
        if use_ssp:
            logging.debug(
                'Destroy container %s before aborting the autoserv '
                'process.', container_name)
            metadata = {
                'drone': socket.gethostname(),
                'job_id': job_or_task_id,
                'container_name': container_name,
                'action': 'abort',
                'success': True
            }
            try:
                bucket = lxc.ContainerBucket()
                container = bucket.get(container_name)
                if container:
                    container.destroy()
                else:
                    metadata['success'] = False
                    metadata['error'] = 'container not found'
                    logging.debug('Container %s is not found.', container_name)
            except:
                metadata['success'] = False
                metadata['error'] = 'Exception: %s' % str(sys.exc_info())
                # Handle any exception so the autoserv process can be aborted.
                logging.exception('Failed to destroy container %s.',
                                  container_name)
            autotest_es.post(use_http=True,
                             type_str=lxc.CONTAINER_RUN_TEST_METADB_TYPE,
                             metadata=metadata)
            # Try to correct the result file permission again after the
            # container is destroyed, as the container might have created some
            # new files in the result folder.
            if results:
                correct_results_folder_permission(results)

        os.killpg(os.getpgrp(), signal.SIGKILL)
예제 #6
0
def main():
    """Clean up the remnants from any old aborted unit tests."""
    # Manually clean out the host dir.
    if lxc_utils.path_exists(TEST_HOST_PATH):
        for host_dir in os.listdir(TEST_HOST_PATH):
            host_dir = os.path.realpath(os.path.join(TEST_HOST_PATH, host_dir))
            try:
                utils.run('sudo umount %s' % host_dir)
            except error.CmdError:
                pass
            utils.run('sudo rm -r %s' % host_dir)

    # Utilize the container_bucket to clear out old test containers.
    bucket = lxc.ContainerBucket(TEST_CONTAINER_PATH, TEST_HOST_PATH)
    bucket.destroy_all()
예제 #7
0
def main():
    """main script."""
    # Force to run the setup as superuser.
    # TODO(dshi): crbug.com/459344 Set remove this enforcement when test
    # container can be unprivileged container.
    if utils.sudo_require_password():
        logging.warn('SSP requires root privilege to run commands, please '
                     'grant root access to this process.')
        utils.run('sudo true')

    options = parse_options()
    bucket = lxc.ContainerBucket(container_path=options.path)
    if options.setup:
        bucket.setup_base(name=options.name, force_delete=options.force_delete)
    elif options.force_delete:
        bucket.destroy_all()
예제 #8
0
    def testHostDirCreationAndCleanup(self):
        """Verifies that the host dir is properly created and cleaned up when
        the container bucket is set up and destroyed.
        """
        bucket = lxc.ContainerBucket(container_path, self.shared_host_path)

        # Verify the host path in the container bucket.
        self.assertEqual(os.path.realpath(bucket.shared_host_path),
                         self.shared_host_path)

        # Set up, verify that the path is created.
        bucket.setup_base()
        self.assertTrue(os.path.isdir(self.shared_host_path))

        # Clean up, verify that the path is removed.
        bucket.destroy_all()
        self.assertFalse(os.path.isdir(self.shared_host_path))
예제 #9
0
def main(options):
    """Main script.

    @param options: Options to run the script.
    """
    config = logging_config.LoggingConfig()
    if options.logfile:
        config.add_file_handler(
            file_path=os.path.abspath(options.logfile),
            level=logging.DEBUG if options.verbose else logging.INFO)

    bucket = lxc.ContainerBucket()
    logging.info('')
    logging.info('Cleaning container bucket %s', bucket.container_path)
    success_count = 0
    failure_count = 0
    for container in bucket.get_all().values():
        if is_container_orphaned(container):
            if cleanup(container, options):
                success_count += 1
            else:
                failure_count += 1
    logging.info('Cleanup finished.')
예제 #10
0
    def handle_sigterm(signum, frame):
        logging.debug('Received SIGTERM')
        if pid_file_manager:
            pid_file_manager.close_file(1, signal.SIGTERM)
        logging.debug('Finished writing to pid_file. Killing process.')

        # Update results folder's file permission. This needs to be done ASAP
        # before the parsing process tries to access the log.
        if use_ssp and results:
            correct_results_folder_permission(results)

        # TODO (sbasi) - remove the time.sleep when crbug.com/302815 is solved.
        # This sleep allows the pending output to be logged before the kill
        # signal is sent.
        time.sleep(.1)
        if use_ssp:
            logging.debug(
                'Destroy container %s before aborting the autoserv '
                'process.', container_id)
            try:
                bucket = lxc.ContainerBucket()
                container = bucket.get_container(container_id)
                if container:
                    container.destroy()
                else:
                    logging.debug('Container %s is not found.', container_id)
            except:
                # Handle any exception so the autoserv process can be aborted.
                logging.exception('Failed to destroy container %s.',
                                  container_id)
            # Try to correct the result file permission again after the
            # container is destroyed, as the container might have created some
            # new files in the result folder.
            if results:
                correct_results_folder_permission(results)

        os.killpg(os.getpgrp(), signal.SIGKILL)
        log_level=(logging.DEBUG if options.verbose else logging.INFO))

    bucket = lxc.ContainerBucket(TEMP_DIR)

    setup_base(bucket)
    container_test_name = (lxc.TEST_CONTAINER_NAME_FMT %
                           (TEST_JOB_ID, time.time(), os.getpid()))
    container = setup_test(bucket, container_test_name, options.skip_cleanup)
    test_share(container)
    test_autoserv(container)
    if options.dut:
        test_ssh(container, options.dut)
    if options.devserver:
        test_ssh(container, options.devserver)
    # Packages are installed in TEST_SCRIPT, verify the packages are installed.
    test_package_install(container)
    logging.info('All tests passed.')


if __name__ == '__main__':
    options = parse_options()
    try:
        main(options)
    finally:
        if not options.skip_cleanup:
            logging.info('Cleaning up temporary directory %s.', TEMP_DIR)
            try:
                lxc.ContainerBucket(TEMP_DIR).destroy_all()
            finally:
                utils.run('sudo rm -rf "%s"' % TEMP_DIR)
예제 #12
0
def _run_with_ssp(job, container_id, job_id, results, parser, ssp_url,
                  machines):
    """Run the server job with server-side packaging.

    @param job: The server job object.
    @param container_id: ID of the container to run the test.
    @param job_id: ID of the test job.
    @param results: Folder to store results. This could be different from
                    parser.options.results:
                    parser.options.results  can be set to None for results to be
                    stored in a temp folder.
                    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 machines: A list of machines to run the test.
    """
    if not ssp_url:
        job.record('FAIL', None, None, 'Failed to stage server-side package')
        raise error.AutoservError('Failed to stage server-side package')

    bucket = lxc.ContainerBucket()
    control = (parser.args[0]
               if len(parser.args) > 0 and parser.args[0] != '' else None)
    try:
        dut_name = machines[0] if len(machines) >= 1 else None
        test_container = bucket.setup_test(container_id,
                                           job_id,
                                           ssp_url,
                                           results,
                                           control=control,
                                           job_folder=_LXC_JOB_FOLDER,
                                           dut_name=dut_name,
                                           isolate_hash=parser.options.isolate)
    except Exception as e:
        job.record(
            'FAIL', None, None,
            'Failed to setup container for test: %s. Check logs in '
            'ssp_logs folder for more details.' % e)
        raise

    args = sys.argv[:]
    args.remove('--require-ssp')
    # --parent_job_id is only useful in autoserv running in host, not in
    # container. Include this argument will cause test to fail for builds before
    # CL 286265 was merged.
    if '--parent_job_id' in args:
        index = args.index('--parent_job_id')
        args.remove('--parent_job_id')
        # Remove the actual parent job id in command line arg.
        del args[index]

    # A dictionary of paths to replace in the command line. Key is the path to
    # be replaced with the one in value.
    paths_to_replace = {}
    # Replace the control file path with the one in container.
    if control:
        container_control_filename = os.path.join(lxc.CONTROL_TEMP_PATH,
                                                  os.path.basename(control))
        paths_to_replace[control] = container_control_filename
    # Update result directory with the one in container.
    container_result_dir = os.path.join(lxc.RESULT_DIR_FMT % _LXC_JOB_FOLDER)
    if parser.options.results:
        paths_to_replace[parser.options.results] = container_result_dir
    args = [paths_to_replace.get(arg, arg) for arg in args]

    # Apply --use-existing-results, results directory is aready created and
    # mounted in container. Apply this arg to avoid exception being raised.
    if not '--use-existing-results' in args:
        args.append('--use-existing-results')

    # Make sure autoserv running in container using a different pid file.
    if not '--pidfile-label' in args:
        args.extend(['--pidfile-label', 'container_autoserv'])

    cmd_line = ' '.join(["'%s'" % arg if ' ' in arg else arg for arg in args])
    logging.info('Run command in container: %s', cmd_line)
    success = False
    try:
        test_container.attach_run(cmd_line)
        success = True
    except Exception as e:
        # If the test run inside container fails without generating any log,
        # write a message to status.log to help troubleshooting.
        debug_files = os.listdir(os.path.join(results, 'debug'))
        if not debug_files:
            job.record(
                'FAIL', None, None,
                'Failed to run test inside the container: %s. Check '
                'logs in ssp_logs folder for more details.' % e)
        raise
    finally:
        metrics.Counter('chromeos/autotest/experimental/execute_job_in_ssp'
                        ).increment(fields={'success': success})
        test_container.destroy()
예제 #13
0
 def setUp(self):
     self.tmpdir = tempfile.mkdtemp()
     self.shared_host_path = os.path.realpath(
         os.path.join(self.tmpdir, 'host'))
     self.bucket = lxc.ContainerBucket(container_path,
                                       self.shared_host_path)