~vila/ubuntu-test-cases/retry-apt-get-update

« back to all changes in this revision

Viewing changes to selftests/test_reboot_and_wait.py

  • Committer: Leo Arias
  • Date: 2014-11-10 19:28:56 UTC
  • mfrom: (345 touch)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: leo.arias@canonical.com-20141110192856-rgpksx9n9j0b39yl
Merged with the touch branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Ubuntu Test Cases for Touch
 
2
# Copyright 2013 Canonical Ltd.
 
3
 
 
4
# This program is free software: you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License version 3, as published
 
6
# by the Free Software Foundation.
 
7
 
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
# PURPOSE.  See the GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License along
 
14
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
from __future__ import print_function
 
17
 
 
18
import imp
 
19
import mock
 
20
import os
 
21
import unittest
 
22
 
 
23
 
 
24
class TestRebootAndWait(unittest.TestCase):
 
25
 
 
26
    """Simple set of tests to make sure the reboot-and-wait command works."""
 
27
 
 
28
    def setUp(self):
 
29
        # do some trickery to load this as module
 
30
        module = 'reboot-and-wait'
 
31
        fname = os.path.join(os.path.dirname(__file__), '../scripts', module)
 
32
        m = imp.load_source(module, fname)
 
33
        self._main = m.main
 
34
        self._get_parser = m._get_arg_parser
 
35
 
 
36
    @mock.patch('phabletutils.device.AndroidBridge.reboot')
 
37
    def testRebootFail(self, reboot):
 
38
        reboot.side_effect = RuntimeError('foo')
 
39
        args = self._get_parser().parse_args([])
 
40
        with self.assertRaisesRegexp(RuntimeError, 'foo'):
 
41
            self._main(args)
 
42
 
 
43
    @mock.patch('phabletutils.device.AndroidBridge.reboot')
 
44
    @mock.patch('phabletutils.device.AndroidBridge.wait_for_device')
 
45
    def testWaitForDeviceFail(self, wait_for, reboot):
 
46
        wait_for.side_effect = RuntimeError('foo')
 
47
        args = self._get_parser().parse_args([])
 
48
        with self.assertRaisesRegexp(RuntimeError, 'foo'):
 
49
            self._main(args)
 
50
        reboot.assert_called_once_with()
 
51
 
 
52
    @mock.patch('phabletutils.device.AndroidBridge.reboot')
 
53
    @mock.patch('phabletutils.device.AndroidBridge.wait_for_device')
 
54
    @mock.patch('phabletutils.device.AndroidBridge.wait_for_network')
 
55
    def testRetries(self, wait_for_net, wait_for_dev, reboot):
 
56
        args = self._get_parser().parse_args([])
 
57
        wait_for_net.side_effect = RuntimeError('foo')
 
58
        self.assertEquals(1, self._main(args))
 
59
        self.assertEquals(args.num_tries, reboot.call_count)
 
60
 
 
61
        # now make sure it can recover after a single network failure
 
62
        reboot.reset_mock()
 
63
        wait_for_net.reset_mock()
 
64
        wait_for_net.side_effect = [RuntimeError('foo'), None]
 
65
        self.assertEquals(0, self._main(args))
 
66
        self.assertEquals(2, reboot.call_count)