Jae Hoon Kim | b3c8cab | 2021-11-10 14:57:53 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Mike Frysinger | 8b0fc37 | 2022-09-08 03:24:24 -0400 | [diff] [blame] | 3 | # Copyright 2010 The ChromiumOS Authors |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 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 +0000 | [diff] [blame] | 11 | import shutil |
| 12 | import socket |
| 13 | import tempfile |
| 14 | import unittest |
Jae Hoon Kim | b3c8cab | 2021-11-10 14:57:53 -0800 | [diff] [blame] | 15 | import unittest.mock as mock |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 16 | |
| 17 | import autoupdate |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 18 | import cherrypy # pylint: disable=import-error |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 19 | import setup_chromite # pylint: disable=unused-import |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 20 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 21 | from chromite.lib.xbuddy import common_util |
| 22 | from chromite.lib.xbuddy import xbuddy |
| 23 | |
| 24 | |
| 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>""" |
| 32 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 33 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 34 | class AutoupdateTest(unittest.TestCase): |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 35 | """Tests for the autoupdate.Autoupdate class.""" |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 36 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 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, static_dir=self.static_image_dir) |
| 57 | mock.patch.object(xbuddy.XBuddy, "_GetArtifact") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 58 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 59 | def tearDown(self): |
| 60 | shutil.rmtree(self.build_root) |
| 61 | shutil.rmtree(self.tempdir) |
| 62 | shutil.rmtree(self.static_image_dir) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 63 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 64 | def _DummyAutoupdateConstructor(self, **kwargs): |
| 65 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 66 | dummy = autoupdate.Autoupdate( |
| 67 | self._xbuddy, static_dir=self.static_image_dir, **kwargs |
| 68 | ) |
| 69 | return dummy |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 70 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 71 | def testChangeUrlPort(self): |
| 72 | # pylint: disable=protected-access |
| 73 | r = autoupdate._ChangeUrlPort("http://fuzzy:8080/static", 8085) |
| 74 | self.assertEqual(r, "http://fuzzy:8085/static") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 75 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 76 | r = autoupdate._ChangeUrlPort("http://fuzzy/static", 8085) |
| 77 | self.assertEqual(r, "http://fuzzy:8085/static") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 78 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 79 | r = autoupdate._ChangeUrlPort("ftp://fuzzy/static", 8085) |
| 80 | self.assertEqual(r, "ftp://fuzzy:8085/static") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 81 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 82 | r = autoupdate._ChangeUrlPort("ftp://fuzzy", 8085) |
| 83 | self.assertEqual(r, "ftp://fuzzy:8085") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 84 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 85 | @mock.patch.object(autoupdate.Autoupdate, "GetBuildID") |
| 86 | def testHandleUpdatePing(self, get_build_id): |
| 87 | """Tests HandleUpdatePing""" |
| 88 | au_mock = self._DummyAutoupdateConstructor() |
| 89 | get_build_id.return_value = "" |
| 90 | request = """<?xml version="1.0" encoding="UTF-8"?> |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 91 | <request protocol="3.0"> |
| 92 | <os version="Indy" platform="Chrome OS" sp="10323.52.0_x86_64"></os> |
| 93 | <app appid="platform" version="1.0.0" delta_okay="true" |
| 94 | track="stable-channel" board="eve"> |
| 95 | <ping active="1" a="1" r="1"></ping> |
| 96 | <updatecheck targetversionprefix=""></updatecheck> |
| 97 | </app> |
| 98 | </request>""" |
| 99 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 100 | self.assertIn( |
| 101 | b'<updatecheck status="noupdate"', au_mock.HandleUpdatePing(request) |
| 102 | ) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 103 | |
Jack Rosenthal | 8de609d | 2023-02-09 13:20:35 -0700 | [diff] [blame] | 104 | |
| 105 | if __name__ == "__main__": |
| 106 | unittest.main() |