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 | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 12 | import os |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 13 | import shutil |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 14 | import socket |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 15 | import tempfile |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 16 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 17 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 18 | import cherrypy |
| 19 | import mox |
| 20 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 21 | import autoupdate |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 22 | import common_util |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 23 | import devserver_constants as constants |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 24 | import xbuddy |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 25 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 26 | from nebraska import nebraska |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 27 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 28 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 29 | _TEST_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> |
| 30 | <request protocol="3.0" updater="ChromeOSUpdateEngine" updaterversion="0.1.0.0"> |
| 31 | <app appid="test-appid" version="%(version)s" track="%(track)s" |
| 32 | board="%(board)s" hardware_class="%(hwclass)s"> |
| 33 | <updatecheck /> |
| 34 | </app> |
| 35 | </request>""" |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 36 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 37 | #pylint: disable=W0212 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 38 | class AutoupdateTest(mox.MoxTestBase): |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 39 | """Tests for the autoupdate.Autoupdate class.""" |
| 40 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 41 | def setUp(self): |
| 42 | mox.MoxTestBase.setUp(self) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 43 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 44 | self.test_board = 'test-board' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 45 | self.build_root = tempfile.mkdtemp('autoupdate_build_root') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 46 | self.latest_dir = '12345_af_12-a1' |
| 47 | self.latest_verision = '12345_af_12' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 48 | self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 49 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 50 | self.test_dict = { |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 51 | 'version': 'ForcedUpdate', |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 52 | 'track': 'test-channel', |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 53 | 'board': self.test_board, |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 54 | 'hwclass': 'test-hardware-class', |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 55 | } |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 56 | self.test_data = _TEST_REQUEST % self.test_dict |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 57 | self.payload = 'My payload' |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 58 | cherrypy.request.base = 'http://%s' % self.hostname |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 59 | common_util.MkDirP(self.static_image_dir) |
| 60 | self._xbuddy = xbuddy.XBuddy(False, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 61 | static_dir=self.static_image_dir) |
| 62 | self.mox.StubOutWithMock(xbuddy.XBuddy, '_GetArtifact') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 63 | |
| 64 | def tearDown(self): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 65 | shutil.rmtree(self.build_root) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 66 | shutil.rmtree(self.static_image_dir) |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 67 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 68 | def _DummyAutoupdateConstructor(self, **kwargs): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 69 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 70 | dummy = autoupdate.Autoupdate(self._xbuddy, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 71 | static_dir=self.static_image_dir, |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 72 | **kwargs) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 73 | return dummy |
| 74 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 75 | def testChangeUrlPort(self): |
| 76 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 77 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 78 | |
| 79 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 80 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 81 | |
| 82 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 83 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 84 | |
| 85 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 86 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 87 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 88 | def testHandleHostInfoPing(self): |
| 89 | au_mock = self._DummyAutoupdateConstructor() |
| 90 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 91 | |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 92 | # 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] | 93 | test_ip = '1.2.3.4' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 94 | au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 95 | self.assertEqual( |
| 96 | json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict) |
| 97 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 98 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 99 | if __name__ == '__main__': |
| 100 | unittest.main() |