Luis Hector Chavez | a151805 | 2018-06-14 08:19:34 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 3 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Unit tests for autoupdate.py.""" |
| 8 | |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 11 | import json |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 12 | import shutil |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 13 | import socket |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 14 | import tempfile |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 15 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 16 | |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 17 | import cherrypy # pylint: disable=import-error |
| 18 | import mock |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 19 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 20 | import autoupdate |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 21 | import common_util |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 22 | import xbuddy |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 23 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 24 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 25 | _TEST_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> |
| 26 | <request protocol="3.0" updater="ChromeOSUpdateEngine" updaterversion="0.1.0.0"> |
| 27 | <app appid="test-appid" version="%(version)s" track="%(track)s" |
| 28 | board="%(board)s" hardware_class="%(hwclass)s"> |
| 29 | <updatecheck /> |
| 30 | </app> |
| 31 | </request>""" |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 32 | |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 33 | class AutoupdateTest(unittest.TestCase): |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 34 | """Tests for the autoupdate.Autoupdate class.""" |
| 35 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 36 | def setUp(self): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 37 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 38 | self.test_board = 'test-board' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 39 | self.build_root = tempfile.mkdtemp('autoupdate_build_root') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 40 | self.latest_dir = '12345_af_12-a1' |
| 41 | self.latest_verision = '12345_af_12' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 42 | self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 43 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 44 | self.test_dict = { |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 45 | 'version': 'ForcedUpdate', |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 46 | 'track': 'test-channel', |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 47 | 'board': self.test_board, |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 48 | 'hwclass': 'test-hardware-class', |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 49 | } |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 50 | self.test_data = _TEST_REQUEST % self.test_dict |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 51 | self.payload = 'My payload' |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 52 | cherrypy.request.base = 'http://%s' % self.hostname |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 53 | common_util.MkDirP(self.static_image_dir) |
| 54 | self._xbuddy = xbuddy.XBuddy(False, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 55 | static_dir=self.static_image_dir) |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 56 | mock.patch.object(xbuddy.XBuddy, '_GetArtifact') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 57 | |
| 58 | def tearDown(self): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 59 | shutil.rmtree(self.build_root) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 60 | shutil.rmtree(self.static_image_dir) |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 61 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 62 | def _DummyAutoupdateConstructor(self, **kwargs): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 63 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 64 | dummy = autoupdate.Autoupdate(self._xbuddy, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 65 | static_dir=self.static_image_dir, |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 66 | **kwargs) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 67 | return dummy |
| 68 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 69 | def testChangeUrlPort(self): |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 70 | # pylint: disable=protected-access |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 71 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 72 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 73 | |
| 74 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 75 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 76 | |
| 77 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 78 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 79 | |
| 80 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 81 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 82 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 83 | def testHandleHostInfoPing(self): |
| 84 | au_mock = self._DummyAutoupdateConstructor() |
| 85 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 86 | |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 87 | # Setup fake host_infos entry and ensure it comes back to us in one piece. |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 88 | test_ip = '1.2.3.4' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 89 | au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 90 | self.assertEqual( |
| 91 | json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict) |
| 92 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 93 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 94 | if __name__ == '__main__': |
| 95 | unittest.main() |