Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 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 | |
| 9 | from __future__ import print_function |
| 10 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 11 | import shutil |
| 12 | import socket |
| 13 | import tempfile |
| 14 | import unittest |
| 15 | |
| 16 | import mock |
| 17 | import cherrypy # pylint: disable=import-error |
| 18 | |
| 19 | import autoupdate |
| 20 | |
| 21 | import setup_chromite # pylint: disable=unused-import |
| 22 | from chromite.lib.xbuddy import common_util |
| 23 | from chromite.lib.xbuddy import xbuddy |
| 24 | |
| 25 | |
| 26 | _TEST_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> |
| 27 | <request protocol="3.0" updater="ChromeOSUpdateEngine" updaterversion="0.1.0.0"> |
| 28 | <app appid="test-appid" version="%(version)s" track="%(track)s" |
| 29 | board="%(board)s" hardware_class="%(hwclass)s"> |
| 30 | <updatecheck /> |
| 31 | </app> |
| 32 | </request>""" |
| 33 | |
| 34 | class AutoupdateTest(unittest.TestCase): |
| 35 | """Tests for the autoupdate.Autoupdate class.""" |
| 36 | |
| 37 | def setUp(self): |
| 38 | self.port = 8080 |
| 39 | self.test_board = 'test-board' |
| 40 | self.build_root = tempfile.mkdtemp('autoupdate_build_root') |
| 41 | self.tempdir = tempfile.mkdtemp('tempdir') |
| 42 | self.latest_dir = '12345_af_12-a1' |
| 43 | self.latest_verision = '12345_af_12' |
| 44 | self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir') |
| 45 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
| 46 | self.test_dict = { |
| 47 | 'version': 'ForcedUpdate', |
| 48 | 'track': 'test-channel', |
| 49 | 'board': self.test_board, |
| 50 | 'hwclass': 'test-hardware-class', |
| 51 | } |
| 52 | self.test_data = _TEST_REQUEST % self.test_dict |
| 53 | self.payload = 'My payload' |
| 54 | cherrypy.request.base = 'http://%s' % self.hostname |
| 55 | common_util.MkDirP(self.static_image_dir) |
| 56 | self._xbuddy = xbuddy.XBuddy(False, |
| 57 | static_dir=self.static_image_dir) |
| 58 | mock.patch.object(xbuddy.XBuddy, '_GetArtifact') |
| 59 | |
| 60 | def tearDown(self): |
| 61 | shutil.rmtree(self.build_root) |
| 62 | shutil.rmtree(self.tempdir) |
| 63 | shutil.rmtree(self.static_image_dir) |
| 64 | |
| 65 | def _DummyAutoupdateConstructor(self, **kwargs): |
| 66 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 67 | dummy = autoupdate.Autoupdate(self._xbuddy, |
| 68 | static_dir=self.static_image_dir, |
| 69 | **kwargs) |
| 70 | return dummy |
| 71 | |
| 72 | def testChangeUrlPort(self): |
| 73 | # pylint: disable=protected-access |
| 74 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 75 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 76 | |
| 77 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 78 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 79 | |
| 80 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 81 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 82 | |
| 83 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 84 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 85 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 86 | @mock.patch.object(autoupdate.Autoupdate, 'GetPathToPayload') |
| 87 | def testHandleUpdatePing(self, path_to_payload_mock): |
| 88 | """Tests HandleUpdatePing""" |
| 89 | au_mock = self._DummyAutoupdateConstructor() |
| 90 | path_to_payload_mock.return_value = self.tempdir |
| 91 | request = """<?xml version="1.0" encoding="UTF-8"?> |
| 92 | <request protocol="3.0"> |
| 93 | <os version="Indy" platform="Chrome OS" sp="10323.52.0_x86_64"></os> |
| 94 | <app appid="platform" version="1.0.0" delta_okay="true" |
| 95 | track="stable-channel" board="eve"> |
| 96 | <ping active="1" a="1" r="1"></ping> |
| 97 | <updatecheck targetversionprefix=""></updatecheck> |
| 98 | </app> |
| 99 | </request>""" |
| 100 | |
Amin Hassani | 8b50358 | 2020-12-03 04:47:01 | [diff] [blame] | 101 | self.assertIn('<updatecheck status="noupdate"', |
| 102 | au_mock.HandleUpdatePing(request)) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 103 | |
| 104 | if __name__ == '__main__': |
| 105 | unittest.main() |