blob: 6b1278bc537a921529c713541ea2e47e0d60915d [file] [log] [blame]
Mike Frysingerd13faeb2013-09-05 16:00:46 -04001#!/usr/bin/python
2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unittests for pushimage.py"""
7
Mike Frysinger1d4752b2014-11-08 04:00:18 -05008# pylint: disable=bad-continuation
9
Mike Frysinger6791b1d2014-03-04 20:52:22 -050010from __future__ import print_function
11
Mike Frysingerd13faeb2013-09-05 16:00:46 -040012import logging
Don Garrett9fd20a82014-09-04 11:37:22 -070013import mock
Mike Frysingerd13faeb2013-09-05 16:00:46 -040014import os
15import sys
16
17sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
18 '..', '..'))
Mike Frysinger6791b1d2014-03-04 20:52:22 -050019from chromite.lib import cros_build_lib
Mike Frysingerd13faeb2013-09-05 16:00:46 -040020from chromite.lib import cros_test_lib
Don Garrett9fd20a82014-09-04 11:37:22 -070021from chromite.lib import gs
Mike Frysingerd13faeb2013-09-05 16:00:46 -040022from chromite.lib import gs_unittest
23from chromite.lib import osutils
Mike Frysinger4495b032014-03-05 17:24:03 -050024from chromite.lib import partial_mock
Mike Frysingerd13faeb2013-09-05 16:00:46 -040025from chromite.lib import signing
26from chromite.scripts import pushimage
27
28
29class InputInsnsTest(cros_test_lib.MockTestCase):
30 """Tests for InputInsns"""
31
32 def testBasic(self):
33 """Simple smoke test"""
Don Garrett9fd20a82014-09-04 11:37:22 -070034 with mock.patch.object(gs.GSContext, 'Exists', return_value=False):
35 insns = pushimage.InputInsns('test.board')
36 insns.GetInsnFile('recovery')
37 self.assertEqual(insns.GetChannels(), ['dev', 'canary'])
38 self.assertEqual(insns.GetKeysets(), ['stumpy-mp-v3'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -040039
40 def testGetInsnFile(self):
41 """Verify various inputs result in right insns path"""
42 testdata = (
43 ('UPPER_CAPS', 'UPPER_CAPS'),
44 ('recovery', 'test.board'),
45 ('firmware', 'test.board.firmware'),
46 ('factory', 'test.board.factory'),
47 )
48 insns = pushimage.InputInsns('test.board')
49 for image_type, filename in testdata:
50 ret = insns.GetInsnFile(image_type)
51 self.assertEqual(os.path.basename(ret), '%s.instructions' % (filename))
52
53 def testSplitCfgField(self):
54 """Verify splitting behavior behaves"""
55 testdata = (
56 ('', []),
57 ('a b c', ['a', 'b', 'c']),
58 ('a, b', ['a', 'b']),
59 ('a,b', ['a', 'b']),
60 ('a,\tb', ['a', 'b']),
61 ('a\tb', ['a', 'b']),
62 )
63 for val, exp in testdata:
64 ret = pushimage.InputInsns.SplitCfgField(val)
65 self.assertEqual(ret, exp)
66
67 def testOutputInsnsBasic(self):
68 """Verify output instructions are sane"""
69 exp_content = """[insns]
70keyset = stumpy-mp-v3
71channel = dev canary
72chromeos_shell = false
73ensure_no_password = true
74firmware_update = true
75security_checks = true
76create_nplusone = true
77
78[general]
79"""
80
81 insns = pushimage.InputInsns('test.board')
82 m = self.PatchObject(osutils, 'WriteFile')
83 insns.OutputInsns('recovery', '/bogus', {}, {})
84 self.assertTrue(m.called)
85 content = m.call_args_list[0][0][1]
86 self.assertEqual(content.rstrip(), exp_content.rstrip())
87
88 def testOutputInsnsReplacements(self):
89 """Verify output instructions can be updated"""
90 exp_content = """[insns]
91keyset = batman
92channel = dev
93chromeos_shell = false
94ensure_no_password = true
95firmware_update = true
96security_checks = true
97create_nplusone = true
98
99[general]
100board = board
101config_board = test.board
102"""
103 sect_insns = {
104 'channel': 'dev',
105 'keyset': 'batman',
106 }
107 sect_general = {
108 'config_board': 'test.board',
109 'board': 'board',
110 }
111
112 insns = pushimage.InputInsns('test.board')
113 m = self.PatchObject(osutils, 'WriteFile')
114 insns.OutputInsns('recovery', '/a/file', sect_insns, sect_general)
115 self.assertTrue(m.called)
116 content = m.call_args_list[0][0][1]
117 self.assertEqual(content.rstrip(), exp_content.rstrip())
118
119
Mike Frysinger4495b032014-03-05 17:24:03 -0500120class MarkImageToBeSignedTest(gs_unittest.AbstractGSContextTest):
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400121 """Tests for MarkImageToBeSigned()"""
122
123 def setUp(self):
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400124 # Minor optimization -- we call this for logging purposes in the main
125 # code, but don't really care about it for testing. It just slows us.
Mike Frysinger6430d132014-10-27 23:43:30 -0400126 self.PatchObject(cros_build_lib, 'MachineDetails', return_value='1234\n')
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400127
128 def testBasic(self):
129 """Simple smoke test"""
130 tbs_base = 'gs://some-bucket'
131 insns_path = 'chan/board/ver/file.instructions'
132 tbs_file = '%s/tobesigned/90,chan,board,ver,file.instructions' % tbs_base
133 ret = pushimage.MarkImageToBeSigned(self.ctx, tbs_base, insns_path, 90)
134 self.assertEqual(ret, tbs_file)
135
136 def testPriority(self):
137 """Verify diff priority values get used correctly"""
138 for prio, sprio in ((0, '00'), (9, '09'), (35, '35'), (99, '99')):
139 ret = pushimage.MarkImageToBeSigned(self.ctx, '', '', prio)
140 self.assertEquals(ret, '/tobesigned/%s,' % sprio)
141
142 def testBadPriority(self):
143 """Verify we reject bad priority values"""
144 for prio in (-10, -1, 100, 91239):
145 self.assertRaises(ValueError, pushimage.MarkImageToBeSigned, self.ctx,
146 '', '', prio)
147
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400148 def testTbsUpload(self):
149 """Make sure we actually try to upload the file"""
150 pushimage.MarkImageToBeSigned(self.ctx, '', '', 50)
151 self.gs_mock.assertCommandContains(['cp', '--'])
152
153
Mike Frysinger4495b032014-03-05 17:24:03 -0500154class PushImageTests(gs_unittest.AbstractGSContextTest):
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400155 """Tests for PushImage()"""
156
157 def setUp(self):
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400158 self.mark_mock = self.PatchObject(pushimage, 'MarkImageToBeSigned')
159
160 def testBasic(self):
161 """Simple smoke test"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800162 EXPECTED = {
163 'canary': [
164 'gs://chromeos-releases/canary-channel/test.board-hi/5126.0.0/'
165 'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions'],
166 'dev': [
167 'gs://chromeos-releases/dev-channel/test.board-hi/5126.0.0/'
168 'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions'],
169 }
Don Garrett9fd20a82014-09-04 11:37:22 -0700170 with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
171 urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
172 profile='hi')
Don Garrett9459c2f2014-01-22 18:20:24 -0800173
174 self.assertEqual(urls, EXPECTED)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400175
176 def testBasicMock(self):
177 """Simple smoke test in mock mode"""
Don Garrett9fd20a82014-09-04 11:37:22 -0700178 with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
179 pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
180 dry_run=True, mock=True)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400181
182 def testBadVersion(self):
183 """Make sure we barf on bad version strings"""
184 self.assertRaises(ValueError, pushimage.PushImage, '', '', 'asdf')
185
186 def testNoInsns(self):
187 """Boards w/out insn files should get skipped"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800188 urls = pushimage.PushImage('/src', 'a bad bad board', 'R34-5126.0.0')
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400189 self.assertEqual(self.gs_mock.call_count, 0)
Don Garrett9459c2f2014-01-22 18:20:24 -0800190 self.assertEqual(urls, None)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400191
192 def testSignTypesRecovery(self):
193 """Only sign the requested recovery type"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800194 EXPECTED = {
195 'canary': [
196 'gs://chromeos-releases/canary-channel/test.board/5126.0.0/'
197 'ChromeOS-recovery-R34-5126.0.0-test.board.instructions'],
198 'dev': [
199 'gs://chromeos-releases/dev-channel/test.board/5126.0.0/'
200 'ChromeOS-recovery-R34-5126.0.0-test.board.instructions'],
201 }
202
203 urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
204 sign_types=['recovery'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400205 self.assertEqual(self.gs_mock.call_count, 18)
206 self.assertTrue(self.mark_mock.called)
Don Garrett9459c2f2014-01-22 18:20:24 -0800207 self.assertEqual(urls, EXPECTED)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400208
209 def testSignTypesNone(self):
210 """Verify nothing is signed when we request an unavailable type"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800211 urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
212 sign_types=['nononononono'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400213 self.assertEqual(self.gs_mock.call_count, 16)
214 self.assertFalse(self.mark_mock.called)
Don Garrett9459c2f2014-01-22 18:20:24 -0800215 self.assertEqual(urls, {})
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400216
Mike Frysinger4495b032014-03-05 17:24:03 -0500217 def testGsError(self):
218 """Verify random GS errors don't make us blow up entirely"""
219 self.gs_mock.AddCmdResult(partial_mock.In('stat'), returncode=1,
220 output='gobblety gook\n')
221 with cros_test_lib.LoggingCapturer('chromite'):
222 self.assertRaises(pushimage.PushError, pushimage.PushImage, '/src',
223 'test.board', 'R34-5126.0.0')
224
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400225
226class MainTests(cros_test_lib.MockTestCase):
227 """Tests for main()"""
228
229 def setUp(self):
230 self.PatchObject(pushimage, 'PushImage')
231
232 def testBasic(self):
233 """Simple smoke test"""
Mike Frysinger09fe0122014-02-09 02:44:05 -0500234 pushimage.main(['--board', 'test.board', '/src', '--yes'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400235
236
237if __name__ == '__main__':
238 # Use our local copy of insns for testing as the main one is not
239 # available in the public manifest.
240 signing.INPUT_INSN_DIR = signing.TEST_INPUT_INSN_DIR
241
242 # Run the tests.
243 cros_test_lib.main(level=logging.INFO)