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