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