~ci-train-bot/ubuntu-system-settings/ubuntu-system-settings-ubuntu-zesty-1721

1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
1
#!/usr/bin/python3
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
2
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright 2014 Canonical
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
8
9
import json
10
import os
11
import shutil
12
import subprocess
13
import sys
14
import tempfile
15
import unittest
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
16
from unittest import mock
17
1417.1.1 by Sebastien Bacher
Translate push_helper expected strings to avoid failure in non C locales
18
import gettext
19
_ = gettext.translation('ubuntu-system-settings', fallback=True).gettext
20
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
21
HELPER_DIR = '@CMAKE_CURRENT_SOURCE_DIR@/../push-helper/'
22
sys.path.append(HELPER_DIR)
23
import software_updates_helper
24
25
26
class TestingSystemImage(software_updates_helper.SystemImage):
27
    def setup(self):
28
        pass
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
29
1030.7.2 by John R. Lenton
make pep8 and pyflakes happy with the tests too
30
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
31
class PushHelperTests(unittest.TestCase):
32
    """Tests for the push-helper script."""
33
34
    def setUp(self):
35
        super(PushHelperTests, self).setUp()
36
        self.tmp_dir = tempfile.mkdtemp(suffix='push-helper', prefix='tests')
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
37
        self.helper_path = HELPER_DIR + 'software_updates_helper.py'
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
38
39
    def tearDown(self):
40
        super(PushHelperTests, self).tearDown()
41
        shutil.rmtree(self.tmp_dir)
42
43
    def run_push_helper(self, input_fname, output_fname):
1030.7.3 by John R. Lenton
tests weren't passing for me, this trivialish change fixes it
44
        subprocess.call(["python3", self.helper_path, input_fname, output_fname],
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
45
                        stdout=subprocess.PIPE)
46
47
    def create_input_file(self, filename, content):
48
        file_path = os.path.join(self.tmp_dir, filename)
49
        with open(file_path, 'w') as input_fd:
50
            input_fd.write(content)
51
        return file_path
52
53
    def assertSystemUpdateNotification(self, notif):
54
        self.assertIn('notification', notif)
55
        self.assertIn('card', notif['notification'])
56
        self.assertIn('emblem-counter', notif['notification'])
57
        self.assertIn('vibrate', notif['notification'])
58
        card = notif['notification']['card']
1417.1.1 by Sebastien Bacher
Translate push_helper expected strings to avoid failure in non C locales
59
        self.assertEqual(card['summary'], _("There's an updated system image."))
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
60
        self.assertEqual(card['actions'], ['settings:///system/system-update'])
61
        self.assertEqual(card['persist'], True)
1417.1.1 by Sebastien Bacher
Translate push_helper expected strings to avoid failure in non C locales
62
        self.assertEqual(card['body'], _('Tap to open the system updater.'))
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
63
        self.assertEqual(card.get('popup', False), False)
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
64
        emblem_counter = notif['notification']['emblem-counter']
65
        self.assertEqual(emblem_counter, {'visible': True, 'count': 1})
66
        vibrate = notif['notification']['vibrate']
67
        self.assertEqual(vibrate, {'pattern': [50, 150], 'repeat': 3})
68
69
    def test_update_broadcast(self):
70
        """Default system-update broadcast."""
71
        input_f = self.create_input_file('bcast_in',
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
72
                                         '"system-image-update"')
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
73
        output_f = os.path.join(self.tmp_dir, 'bcast_out')
74
        self.run_push_helper(input_f, output_f)
75
        with open(output_f, 'r') as fd:
76
            output = json.load(fd)
77
        self.assertSystemUpdateNotification(output)
78
79
    def test_valid_json(self):
80
        """Handle a valid json input."""
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
81
        input_f = self.create_input_file('valid_json_in', '"testing"')
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
82
        output_f = os.path.join(self.tmp_dir, 'valid_json_out')
83
        self.run_push_helper(input_f, output_f)
84
        with open(output_f, 'r') as fd:
85
            output = json.load(fd)
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
86
        self.assertEqual(output, {"testing": True})
87
88
    def test_system_image_run(self):
89
        """Check that run looks sane"""
90
        s = TestingSystemImage()
91
        s.sysimg = mock.Mock(name="sysimg")
92
        s.loop = mock.Mock(name="loop")
93
        s.run()
94
        # check the main loop was run
95
        s.loop.run.assert_called_once_with()
96
        # check CheckForUpdate was called
97
        s.sysimg.CheckForUpdate.assert_called_once_with()
98
        # and connect_to_signal
1030.7.2 by John R. Lenton
make pep8 and pyflakes happy with the tests too
99
        s.sysimg.connect_to_signal.assert_any_call("UpdateDownloaded",
100
                                                   s.downloaded_cb)
101
        s.sysimg.connect_to_signal.assert_any_call("UpdateFailed",
102
                                                   s.failed_cb)
103
        s.sysimg.connect_to_signal.assert_any_call("UpdateAvailableStatus",
104
                                                   s.available_cb)
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
105
        self.assertEqual(s.notify, False)
106
107
    def test_available_and_downloading(self):
1030.7.2 by John R. Lenton
make pep8 and pyflakes happy with the tests too
108
        """check that available_cb when available and d'loading just returns"""
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
109
        s = TestingSystemImage()
110
        s.quit = mock.Mock(name="quit")
111
112
        self.assertEqual(s.notify, False)
113
        # available and downloading; returns without calling quit
114
        s.available_cb(True, True)
115
        self.assertEqual(s.notify, False)
116
        self.assertEqual(s.quit.called, False)
117
118
    def test_available_not_downloading(self):
1030.7.2 by John R. Lenton
make pep8 and pyflakes happy with the tests too
119
        """check that available_cb when available and not downloading
120
        sets notify and quits"""
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
121
        s = TestingSystemImage()
122
        s.quit = mock.Mock(name="quit")
123
124
        self.assertEqual(s.notify, False)
125
        # available and not downloading; quits with notification
126
        s.available_cb(True, False)
127
        self.assertEqual(s.notify, True)
128
        s.quit.assert_called_once_with()
129
130
    def test_not_available(self):
131
        """check that available_cb quits when not available"""
132
        s = TestingSystemImage()
133
        s.quit = mock.Mock(name="quit")
134
135
        self.assertEqual(s.notify, False)
136
        # not available; quits without notifying
137
        s.available_cb(False, False)
138
        self.assertEqual(s.notify, False)
139
        s.quit.assert_called_once_with()
140
141
    def test_downloaded_cb(self):
142
        """check that on download, notify is set to True and quit is called"""
143
        s = TestingSystemImage()
144
        s.quit = mock.Mock(name="quit")
145
146
        self.assertEqual(s.notify, False)
147
        s.downloaded_cb()
148
        self.assertEqual(s.notify, True)
149
        s.quit.assert_called_once_with()
150
151
    def test_failed_cb(self):
152
        """check that on failure, notify is set to False and quit is called"""
153
        s = TestingSystemImage()
154
        s.quit = mock.Mock(name="quit")
155
156
        self.assertEqual(s.notify, False)
157
        s.failed_cb()
158
        self.assertEqual(s.notify, False)
159
        s.quit.assert_called_once_with()
160
161
    def test_quit_no_notify(self):
162
        """Check that quit withlooks sane"""
163
        s = TestingSystemImage()
164
        s.postal = mock.Mock(name="sysimg")
165
        s.loop = mock.Mock(name="loop")
166
        s.notify = False
167
        s.quit()
168
        self.assertEqual(s.postal.Post.called, False)
169
        self.assertEqual(s.postal.ClearPersistent.called, False)
170
        s.loop.quit.assert_called_once_with()
171
172
    def test_quit_with_notify(self):
173
        """Check that quit withlooks sane"""
174
        s = TestingSystemImage()
175
        s.postal = mock.Mock(name="sysimg")
176
        s.loop = mock.Mock(name="loop")
177
        s.notify = True
178
        s.quit()
1030.7.2 by John R. Lenton
make pep8 and pyflakes happy with the tests too
179
        s.postal.Post.assert_called_once_with("_ubuntu-system-settings",
180
                                              '"system-image-update"')
181
        s.postal.ClearPersistent.assert_called_once_with(
182
            "_ubuntu-system-settings", "system-image-update")
1030.7.1 by John R. Lenton
updated push helper to cope with system updates closer to spec
183
        s.loop.quit.assert_called_once_with()
979.1.1 by Guillermo Gonzalez
add basic tests for the push-helper script
184
185
if __name__ == '__main__':
186
    unittest.main(
187
        testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
188
    )