1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.   1
2# Use of this source code is governed by a BSD-style license that can be   2
3# found in the LICENSE file.
4
5"Module containing common utilities for server-side autoupdate tests."
6
7import logging
8
9import common
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import autoupdater, dev_server
12from autotest_lib.server.cros.dynamic_suite import tools
13
14
15def get_updater_from_repo_url(host, job_repo_url=None):
16    """Returns the autoupdater instance to use for a given autoupdate test.
17
18    All server-side tests that run in the lab have an associated job_repo_url
19    assigned to their host that is associated with the version of the build that
20    is currently installed on them. Given most autoupdate tests need to
21    update to some build as part of the test, we conveniently re-update to the
22    same version installed. This method serves as a helper to get the
23    instantiated autoupdater instance for that build.
24
25    This method guarantees that the devserver associated with the autoupdater
26    has already staged the necessary files for autoupdate.
27
28    @param host: The host for the DUT of the server-side test.
29    @param job_repo_url: If set, the job_repo_url to use.
30
31    @raise error.TestError: If we fail to get a job_repo_url.
32    """
33    # Get the job_repo_url -- if not present, attempt to use the one
34    # specified in the host attributes for the host.
35    if not job_repo_url:
36        try:
37            job_repo_url = host.lookup_job_repo_url()
38        except KeyError:
39            logging.fatal('Could not lookup job_repo_url from afe.')
40
41        if not job_repo_url:
42            raise error.TestError(
43                    'Could not find a job_repo_url for the given host.')
44
45    # Get the devserver url and build (image) from the repo url e.g.
46    # 'http://mydevserver:8080', 'x86-alex-release/R27-123.0.0'
47    ds_url, build = tools.get_devserver_build_from_package_url(job_repo_url)
48    devserver = dev_server.ImageServer(ds_url)
49    devserver.stage_artifacts(build, ['full_payload', 'stateful'])
50
51    # We only need to update stateful to do this test.
52    return autoupdater.ChromiumOSUpdater(devserver.get_update_url(build),
53                                         host=host)
54