blob: 6e0a37628667983ee8909a3f1a8fb696b5a7093a [file] [log] [blame]
Chris Sosa0356d3b2010-09-16 15:46:22 -07001#!/usr/bin/python
2
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
9import mox
10import os
Chris Sosa7c931362010-10-11 19:49:01 -070011import socket
Chris Sosa0356d3b2010-09-16 15:46:22 -070012import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070013
14import autoupdate
15
16_TEST_REQUEST = """
17<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" >
18 <o:app version="%(version)s" track="%(track)s" board="%(board)s" />
Chris Sosaa387a872010-09-29 11:51:36 -070019 <o:updatecheck />
Chris Sosa0356d3b2010-09-16 15:46:22 -070020</client_test>"""
21
22
23class AutoupdateTest(mox.MoxTestBase):
24 def setUp(self):
25 mox.MoxTestBase.setUp(self)
26 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize')
27 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash')
Chris Sosaa387a872010-09-29 11:51:36 -070028 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256')
Chris Sosa0356d3b2010-09-16 15:46:22 -070029 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
30 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Chris Sosa7c931362010-10-11 19:49:01 -070031 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070032 self.test_board = 'test-board'
33 self.build_root = '/src_path/build/images'
34 self.latest_dir = '12345_af_12-a1'
35 self.latest_verision = '12345_af_12'
36 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070037 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070038 self.test_dict = { 'client': 'ChromeOSUpdateEngine-1.0',
39 'version': 'ForcedUpdate',
40 'track': 'unused_var',
41 'board': self.test_board
42 }
43 self.test_data = _TEST_REQUEST % self.test_dict
44 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
45 self.hash = 12345
46 self.size = 54321
47 self.url = 'http://%s/static/update.gz' % self.hostname
48 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070049 self.sha256 = 'SHA LA LA'
Chris Sosa0356d3b2010-09-16 15:46:22 -070050
51 def _DummyAutoupdateConstructor(self):
52 """Creates a dummy autoupdater. Used to avoid using constructor."""
53 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070054 static_dir=self.static_image_dir,
55 port=self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070056 dummy.client_prefix = 'ChromeOSUpdateEngine'
Chris Sosa0356d3b2010-09-16 15:46:22 -070057 return dummy
58
59 def testGenerateLatestUpdateImageWithForced(self):
60 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage')
61 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
62 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
63 autoupdate.Autoupdate.GenerateUpdateImage(
64 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
65 self.latest_dir),
66 move_to_static_dir=True,
67 static_image_dir=self.static_image_dir).AndReturn(True)
68
69 self.mox.ReplayAll()
70 au_mock = self._DummyAutoupdateConstructor()
71 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
72 'ForcedUpdate',
73 self.static_image_dir))
74 self.mox.VerifyAll()
75
76 def testHandleUpdatePingForForcedImage(self):
77 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage')
78
79 test_data = _TEST_REQUEST % self.test_dict
80
81 autoupdate.Autoupdate.GenerateUpdateImage(
Chris Sosaa387a872010-09-29 11:51:36 -070082 self.forced_image_path,
83 move_to_static_dir=True,
84 static_image_dir=self.static_image_dir).AndReturn(True)
Chris Sosa0356d3b2010-09-16 15:46:22 -070085 autoupdate.Autoupdate._GetHash(os.path.join(
86 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -070087 autoupdate.Autoupdate._GetSHA256(os.path.join(
88 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -070089 autoupdate.Autoupdate._GetSize(os.path.join(
90 self.static_image_dir, 'update.gz')).AndReturn(self.size)
91 autoupdate.Autoupdate.GetUpdatePayload(
Chris Sosaa387a872010-09-29 11:51:36 -070092 self.hash, self.sha256, self.size, self.url).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -070093
94 self.mox.ReplayAll()
95 au_mock = self._DummyAutoupdateConstructor()
96 au_mock.forced_image = self.forced_image_path
97 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
98 self.mox.VerifyAll()
99
100 def testHandleUpdatePingForLatestImage(self):
101 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
102
103 test_data = _TEST_REQUEST % self.test_dict
104
105 autoupdate.Autoupdate.GenerateLatestUpdateImage(
106 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(True)
107 autoupdate.Autoupdate._GetHash(os.path.join(
108 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700109 autoupdate.Autoupdate._GetSHA256(os.path.join(
110 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700111 autoupdate.Autoupdate._GetSize(os.path.join(
112 self.static_image_dir, 'update.gz')).AndReturn(self.size)
113 autoupdate.Autoupdate.GetUpdatePayload(
Chris Sosaa387a872010-09-29 11:51:36 -0700114 self.hash, self.sha256, self.size, self.url).AndReturn(self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700115
116 self.mox.ReplayAll()
117 au_mock = self._DummyAutoupdateConstructor()
118 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
119 self.mox.VerifyAll()
120
121 def testHandleUpdatePingForArchivedBuild(self):
122 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateImageFromZip')
123
124 test_data = _TEST_REQUEST % self.test_dict
125
126 autoupdate.Autoupdate.GenerateImageFromZip(
127 self.static_image_dir).AndReturn(True)
128 autoupdate.Autoupdate._GetHash(os.path.join(
129 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700130 autoupdate.Autoupdate._GetSHA256(os.path.join(
131 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700132 autoupdate.Autoupdate._GetSize(os.path.join(
133 self.static_image_dir, 'update.gz')).AndReturn(self.size)
134 autoupdate.Autoupdate.GetUpdatePayload(
Chris Sosaa387a872010-09-29 11:51:36 -0700135 self.hash, self.sha256, self.size,
Chris Sosa0356d3b2010-09-16 15:46:22 -0700136 'http://%s/static/archive/update.gz' % self.hostname).AndReturn(
137 self.payload)
138
139 self.mox.ReplayAll()
140 au_mock = self._DummyAutoupdateConstructor()
141 au_mock.serve_only = True
Chris Sosaa387a872010-09-29 11:51:36 -0700142 au_mock.static_urlbase = 'http://%s/static/archive' % self.hostname
Chris Sosa0356d3b2010-09-16 15:46:22 -0700143 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
144 self.mox.VerifyAll()
145
146
147if __name__ == '__main__':
148 unittest.main()