23
32
super(PushHelperTests, self).setUp()
24
33
self.tmp_dir = tempfile.mkdtemp(suffix='push-helper', prefix='tests')
25
self.helper_path = '@CMAKE_CURRENT_SOURCE_DIR@/../push-helper/' + \
26
'software-updates-helper.py'
34
self.helper_path = HELPER_DIR + 'software_updates_helper.py'
28
36
def tearDown(self):
29
37
super(PushHelperTests, self).tearDown()
30
38
shutil.rmtree(self.tmp_dir)
32
40
def run_push_helper(self, input_fname, output_fname):
33
subprocess.call([self.helper_path, input_fname, output_fname],
41
subprocess.call(["python3", self.helper_path, input_fname, output_fname],
34
42
stdout=subprocess.PIPE)
36
44
def create_input_file(self, filename, content):
68
76
def test_valid_json(self):
69
77
"""Handle a valid json input."""
70
input_f = self.create_input_file('valid_json_in', '"null"')
78
input_f = self.create_input_file('valid_json_in', '"testing"')
71
79
output_f = os.path.join(self.tmp_dir, 'valid_json_out')
72
80
self.run_push_helper(input_f, output_f)
73
81
with open(output_f, 'r') as fd:
74
82
output = json.load(fd)
75
self.assertSystemUpdateNotification(output)
83
self.assertEqual(output, {"testing": True})
85
def test_system_image_run(self):
86
"""Check that run looks sane"""
87
s = TestingSystemImage()
88
s.sysimg = mock.Mock(name="sysimg")
89
s.loop = mock.Mock(name="loop")
91
# check the main loop was run
92
s.loop.run.assert_called_once_with()
93
# check CheckForUpdate was called
94
s.sysimg.CheckForUpdate.assert_called_once_with()
95
# and connect_to_signal
96
s.sysimg.connect_to_signal.assert_any_call("UpdateDownloaded",
98
s.sysimg.connect_to_signal.assert_any_call("UpdateFailed",
100
s.sysimg.connect_to_signal.assert_any_call("UpdateAvailableStatus",
102
self.assertEqual(s.notify, False)
104
def test_available_and_downloading(self):
105
"""check that available_cb when available and d'loading just returns"""
106
s = TestingSystemImage()
107
s.quit = mock.Mock(name="quit")
109
self.assertEqual(s.notify, False)
110
# available and downloading; returns without calling quit
111
s.available_cb(True, True)
112
self.assertEqual(s.notify, False)
113
self.assertEqual(s.quit.called, False)
115
def test_available_not_downloading(self):
116
"""check that available_cb when available and not downloading
117
sets notify and quits"""
118
s = TestingSystemImage()
119
s.quit = mock.Mock(name="quit")
121
self.assertEqual(s.notify, False)
122
# available and not downloading; quits with notification
123
s.available_cb(True, False)
124
self.assertEqual(s.notify, True)
125
s.quit.assert_called_once_with()
127
def test_not_available(self):
128
"""check that available_cb quits when not available"""
129
s = TestingSystemImage()
130
s.quit = mock.Mock(name="quit")
132
self.assertEqual(s.notify, False)
133
# not available; quits without notifying
134
s.available_cb(False, False)
135
self.assertEqual(s.notify, False)
136
s.quit.assert_called_once_with()
138
def test_downloaded_cb(self):
139
"""check that on download, notify is set to True and quit is called"""
140
s = TestingSystemImage()
141
s.quit = mock.Mock(name="quit")
143
self.assertEqual(s.notify, False)
145
self.assertEqual(s.notify, True)
146
s.quit.assert_called_once_with()
148
def test_failed_cb(self):
149
"""check that on failure, notify is set to False and quit is called"""
150
s = TestingSystemImage()
151
s.quit = mock.Mock(name="quit")
153
self.assertEqual(s.notify, False)
155
self.assertEqual(s.notify, False)
156
s.quit.assert_called_once_with()
158
def test_quit_no_notify(self):
159
"""Check that quit withlooks sane"""
160
s = TestingSystemImage()
161
s.postal = mock.Mock(name="sysimg")
162
s.loop = mock.Mock(name="loop")
165
self.assertEqual(s.postal.Post.called, False)
166
self.assertEqual(s.postal.ClearPersistent.called, False)
167
s.loop.quit.assert_called_once_with()
169
def test_quit_with_notify(self):
170
"""Check that quit withlooks sane"""
171
s = TestingSystemImage()
172
s.postal = mock.Mock(name="sysimg")
173
s.loop = mock.Mock(name="loop")
176
s.postal.Post.assert_called_once_with("_ubuntu-system-settings",
177
'"system-image-update"')
178
s.postal.ClearPersistent.assert_called_once_with(
179
"_ubuntu-system-settings", "system-image-update")
180
s.loop.quit.assert_called_once_with()
78
182
if __name__ == '__main__':