blob: 93e8b06c31bc17e56b5235ee809cfd2feef87e3e [file] [log] [blame]
Luis Hector Chaveza1518052018-06-14 08:19:34 -07001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Chris Sosa0356d3b2010-09-16 15:46:22 -07003# 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 Garrettfb15e322016-06-21 19:12:08 -07009from __future__ import print_function
10
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070011import json
Chris Sosa6a3697f2013-01-29 16:44:43 -080012import shutil
Chris Sosa7c931362010-10-11 19:49:01 -070013import socket
joychen121fc9b2013-08-02 14:30:30 -070014import tempfile
Chris Sosa0356d3b2010-09-16 15:46:22 -070015import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070016
Amin Hassani4f1e4622019-10-03 10:40:50 -070017import mock
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070018import cherrypy # pylint: disable=import-error
Gilad Arnoldabb352e2012-09-23 01:24:27 -070019
Chris Sosa0356d3b2010-09-16 15:46:22 -070020import autoupdate
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070021
22import setup_chromite # pylint: disable=unused-import
23from chromite.lib.xbuddy import common_util
24from chromite.lib.xbuddy import xbuddy
Chris Sosa0356d3b2010-09-16 15:46:22 -070025
Chris Sosa0356d3b2010-09-16 15:46:22 -070026
Amin Hassani8d718d12019-06-02 21:28:39 -070027_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>"""
Chris Sosa4b951602014-04-09 20:26:07 -070034
Amin Hassani4f1e4622019-10-03 10:40:50 -070035class AutoupdateTest(unittest.TestCase):
Chris Sosa4b951602014-04-09 20:26:07 -070036 """Tests for the autoupdate.Autoupdate class."""
37
Chris Sosa0356d3b2010-09-16 15:46:22 -070038 def setUp(self):
Chris Sosa7c931362010-10-11 19:49:01 -070039 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070040 self.test_board = 'test-board'
joychen121fc9b2013-08-02 14:30:30 -070041 self.build_root = tempfile.mkdtemp('autoupdate_build_root')
Chris Sosa0356d3b2010-09-16 15:46:22 -070042 self.latest_dir = '12345_af_12-a1'
43 self.latest_verision = '12345_af_12'
joychen121fc9b2013-08-02 14:30:30 -070044 self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir')
Chris Sosa7c931362010-10-11 19:49:01 -070045 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070046 self.test_dict = {
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070047 'version': 'ForcedUpdate',
Chris Sosa4b951602014-04-09 20:26:07 -070048 'track': 'test-channel',
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070049 'board': self.test_board,
Amin Hassani8d718d12019-06-02 21:28:39 -070050 'hwclass': 'test-hardware-class',
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070051 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070052 self.test_data = _TEST_REQUEST % self.test_dict
Chris Sosa0356d3b2010-09-16 15:46:22 -070053 self.payload = 'My payload'
Chris Sosa54555862010-10-25 17:26:17 -070054 cherrypy.request.base = 'http://%s' % self.hostname
joychen121fc9b2013-08-02 14:30:30 -070055 common_util.MkDirP(self.static_image_dir)
56 self._xbuddy = xbuddy.XBuddy(False,
joychen121fc9b2013-08-02 14:30:30 -070057 static_dir=self.static_image_dir)
Amin Hassani4f1e4622019-10-03 10:40:50 -070058 mock.patch.object(xbuddy.XBuddy, '_GetArtifact')
Chris Sosa6a3697f2013-01-29 16:44:43 -080059
60 def tearDown(self):
joychen121fc9b2013-08-02 14:30:30 -070061 shutil.rmtree(self.build_root)
Chris Sosa6a3697f2013-01-29 16:44:43 -080062 shutil.rmtree(self.static_image_dir)
Chris Sosa54555862010-10-25 17:26:17 -070063
Gilad Arnold0c9c8602012-10-02 23:58:58 -070064 def _DummyAutoupdateConstructor(self, **kwargs):
Chris Sosa0356d3b2010-09-16 15:46:22 -070065 """Creates a dummy autoupdater. Used to avoid using constructor."""
joychen121fc9b2013-08-02 14:30:30 -070066 dummy = autoupdate.Autoupdate(self._xbuddy,
Chris Sosa7c931362010-10-11 19:49:01 -070067 static_dir=self.static_image_dir,
Gilad Arnold0c9c8602012-10-02 23:58:58 -070068 **kwargs)
Chris Sosa0356d3b2010-09-16 15:46:22 -070069 return dummy
70
Don Garrett0ad09372010-12-06 16:20:30 -080071 def testChangeUrlPort(self):
Amin Hassani4f1e4622019-10-03 10:40:50 -070072 # pylint: disable=protected-access
Don Garrett0ad09372010-12-06 16:20:30 -080073 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
74 self.assertEqual(r, 'http://fuzzy:8085/static')
75
76 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
77 self.assertEqual(r, 'http://fuzzy:8085/static')
78
79 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
80 self.assertEqual(r, 'ftp://fuzzy:8085/static')
81
82 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
83 self.assertEqual(r, 'ftp://fuzzy:8085')
84
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070085 def testHandleHostInfoPing(self):
86 au_mock = self._DummyAutoupdateConstructor()
87 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
88
Gilad Arnold286a0062012-01-12 13:47:02 -080089 # Setup fake host_infos entry and ensure it comes back to us in one piece.
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070090 test_ip = '1.2.3.4'
Gilad Arnold286a0062012-01-12 13:47:02 -080091 au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070092 self.assertEqual(
93 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
94
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070095
Gilad Arnoldc65330c2012-09-20 15:17:48 -070096if __name__ == '__main__':
97 unittest.main()