1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.cellular import test_environment
9from autotest_lib.client.cros.update_engine import nebraska_wrapper
10from autotest_lib.client.cros.update_engine import update_engine_test
11
12class autoupdate_CannedOmahaUpdate(update_engine_test.UpdateEngineTest):
13    """ Updates a DUT with a given image using a Nebraska instance."""
14
15    version = 1
16
17
18    def run_canned_update(self, allow_failure, update_url):
19        """
20        Performs the update.
21
22        @param allow_failure: True if we dont raise an error on failure.
23        @param update_url: The URL to get an update.
24
25        """
26
27        try:
28            self._check_for_update(update_url, critical_update=True,
29                                   wait_for_completion=True)
30        except error.CmdError as e:
31            if not allow_failure:
32                raise error.TestFail('Update attempt failed: %s' %
33                                     self._get_last_error_string())
34            else:
35                logging.info('Ignoring failed update. Failure reason: %s', e)
36
37
38    def run_once(self, payload_url, allow_failure=False, public_key=None,
39                 use_cellular=False):
40        """
41        Runs an update with a canned response using Nebraska.
42
43        @param payload_url: Path to a payload on Google storage.
44        @param allow_failure: If true, failing the update is expected.
45        @param public_key: The public key to serve to the update client.
46        @param use_cellular: True if this test uses cellular.
47
48        """
49
50        with nebraska_wrapper.NebraskaWrapper(
51            log_dir=self.resultsdir, payload_url=payload_url,
52            public_key=public_key) as nebraska:
53
54            if not use_cellular:
55                self.run_canned_update(allow_failure, nebraska.get_update_url())
56                return
57
58            # Setup DUT so that we have ssh over ethernet but DUT uses
59            # cellular as main connection.
60            try:
61                with test_environment.CellularOTATestEnvironment() as test_env:
62                    service = test_env.shill.wait_for_cellular_service_object()
63                    if not service:
64                        raise error.TestError('No cellular service found.')
65
66                    CONNECT_TIMEOUT = 120
67                    test_env.shill.connect_service_synchronous(
68                            service, CONNECT_TIMEOUT)
69                    self.run_canned_update(allow_failure,
70                                           nebraska.get_update_url())
71            except error.TestError as e:
72                # Raise as test failure instead of test error so it is
73                # propagated to the server test's failure message.
74                logging.error('Failed setting up cellular connection.')
75                raise error.TestFail(e)
76