blob: f39334685f875690f00af2922d6dc31242538d1c [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
8import logging
9import os
10import sys
11
12sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
13 '..', '..'))
14from chromite.lib import cros_test_lib
15from chromite.lib import git
16from chromite.lib import gs
17from chromite.lib import gs_unittest
18from chromite.lib import osutils
19from chromite.lib import signing
20from chromite.scripts import pushimage
21
22
23class InputInsnsTest(cros_test_lib.MockTestCase):
24 """Tests for InputInsns"""
25
26 def testBasic(self):
27 """Simple smoke test"""
28 insns = pushimage.InputInsns('test.board')
29 insns.GetInsnFile('recovery')
30 self.assertEqual(insns.GetChannels(), ['dev', 'canary'])
31 self.assertEqual(insns.GetKeysets(), ['stumpy-mp-v3'])
32
33 def testGetInsnFile(self):
34 """Verify various inputs result in right insns path"""
35 testdata = (
36 ('UPPER_CAPS', 'UPPER_CAPS'),
37 ('recovery', 'test.board'),
38 ('firmware', 'test.board.firmware'),
39 ('factory', 'test.board.factory'),
40 )
41 insns = pushimage.InputInsns('test.board')
42 for image_type, filename in testdata:
43 ret = insns.GetInsnFile(image_type)
44 self.assertEqual(os.path.basename(ret), '%s.instructions' % (filename))
45
46 def testSplitCfgField(self):
47 """Verify splitting behavior behaves"""
48 testdata = (
49 ('', []),
50 ('a b c', ['a', 'b', 'c']),
51 ('a, b', ['a', 'b']),
52 ('a,b', ['a', 'b']),
53 ('a,\tb', ['a', 'b']),
54 ('a\tb', ['a', 'b']),
55 )
56 for val, exp in testdata:
57 ret = pushimage.InputInsns.SplitCfgField(val)
58 self.assertEqual(ret, exp)
59
60 def testOutputInsnsBasic(self):
61 """Verify output instructions are sane"""
62 exp_content = """[insns]
63keyset = stumpy-mp-v3
64channel = dev canary
65chromeos_shell = false
66ensure_no_password = true
67firmware_update = true
68security_checks = true
69create_nplusone = true
70
71[general]
72"""
73
74 insns = pushimage.InputInsns('test.board')
75 m = self.PatchObject(osutils, 'WriteFile')
76 insns.OutputInsns('recovery', '/bogus', {}, {})
77 self.assertTrue(m.called)
78 content = m.call_args_list[0][0][1]
79 self.assertEqual(content.rstrip(), exp_content.rstrip())
80
81 def testOutputInsnsReplacements(self):
82 """Verify output instructions can be updated"""
83 exp_content = """[insns]
84keyset = batman
85channel = dev
86chromeos_shell = false
87ensure_no_password = true
88firmware_update = true
89security_checks = true
90create_nplusone = true
91
92[general]
93board = board
94config_board = test.board
95"""
96 sect_insns = {
97 'channel': 'dev',
98 'keyset': 'batman',
99 }
100 sect_general = {
101 'config_board': 'test.board',
102 'board': 'board',
103 }
104
105 insns = pushimage.InputInsns('test.board')
106 m = self.PatchObject(osutils, 'WriteFile')
107 insns.OutputInsns('recovery', '/a/file', sect_insns, sect_general)
108 self.assertTrue(m.called)
109 content = m.call_args_list[0][0][1]
110 self.assertEqual(content.rstrip(), exp_content.rstrip())
111
112
113class MarkImageToBeSignedTest(cros_test_lib.MockTestCase):
114 """Tests for MarkImageToBeSigned()"""
115
116 def setUp(self):
117 self.gs_mock = self.StartPatcher(gs_unittest.GSContextMock())
118 self.gs_mock.SetDefaultCmdResult()
119 self.ctx = gs.GSContext()
120
121 # Minor optimization -- we call this for logging purposes in the main
122 # code, but don't really care about it for testing. It just slows us.
123 self.PatchObject(git, 'RunGit')
124
125 def testBasic(self):
126 """Simple smoke test"""
127 tbs_base = 'gs://some-bucket'
128 insns_path = 'chan/board/ver/file.instructions'
129 tbs_file = '%s/tobesigned/90,chan,board,ver,file.instructions' % tbs_base
130 ret = pushimage.MarkImageToBeSigned(self.ctx, tbs_base, insns_path, 90)
131 self.assertEqual(ret, tbs_file)
132
133 def testPriority(self):
134 """Verify diff priority values get used correctly"""
135 for prio, sprio in ((0, '00'), (9, '09'), (35, '35'), (99, '99')):
136 ret = pushimage.MarkImageToBeSigned(self.ctx, '', '', prio)
137 self.assertEquals(ret, '/tobesigned/%s,' % sprio)
138
139 def testBadPriority(self):
140 """Verify we reject bad priority values"""
141 for prio in (-10, -1, 100, 91239):
142 self.assertRaises(ValueError, pushimage.MarkImageToBeSigned, self.ctx,
143 '', '', prio)
144
145 def testTbsFile(self):
146 """Make sure the tbs file we write has useful data"""
147 WriteFile = osutils.WriteFile
148 def _write_check(*args, **kwargs):
149 # We can't mock every call, so do the actual write for most.
150 WriteFile(*args, **kwargs)
151
152 m = self.PatchObject(osutils, 'WriteFile')
153 m.side_effect = _write_check
154 pushimage.MarkImageToBeSigned(self.ctx, '', '', 50)
155 # We assume the first call is the one we care about.
156 self.assertTrue(m.called)
Mike Frysingere68da372014-02-04 03:10:45 -0500157 content = m.call_args_list[0][0][1]
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400158 self.assertIn('USER=', content)
159 self.assertIn('HOSTNAME=', content)
Mike Frysingere68da372014-02-04 03:10:45 -0500160 self.assertIn('\n', content)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400161
162 def testTbsUpload(self):
163 """Make sure we actually try to upload the file"""
164 pushimage.MarkImageToBeSigned(self.ctx, '', '', 50)
165 self.gs_mock.assertCommandContains(['cp', '--'])
166
167
168class PushImageTests(cros_test_lib.MockTestCase):
169 """Tests for PushImage()"""
170
171 def setUp(self):
172 self.gs_mock = self.StartPatcher(gs_unittest.GSContextMock())
173 self.gs_mock.SetDefaultCmdResult()
174 self.ctx = gs.GSContext()
175
176 self.mark_mock = self.PatchObject(pushimage, 'MarkImageToBeSigned')
177
178 def testBasic(self):
179 """Simple smoke test"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800180 EXPECTED = {
181 'canary': [
182 'gs://chromeos-releases/canary-channel/test.board-hi/5126.0.0/'
183 'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions'],
184 'dev': [
185 'gs://chromeos-releases/dev-channel/test.board-hi/5126.0.0/'
186 'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions'],
187 }
188 urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
189 profile='hi')
190
191 self.assertEqual(urls, EXPECTED)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400192
193 def testBasicMock(self):
194 """Simple smoke test in mock mode"""
195 pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
196 dry_run=True, mock=True)
197
198 def testBadVersion(self):
199 """Make sure we barf on bad version strings"""
200 self.assertRaises(ValueError, pushimage.PushImage, '', '', 'asdf')
201
202 def testNoInsns(self):
203 """Boards w/out insn files should get skipped"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800204 urls = pushimage.PushImage('/src', 'a bad bad board', 'R34-5126.0.0')
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400205 self.assertEqual(self.gs_mock.call_count, 0)
Don Garrett9459c2f2014-01-22 18:20:24 -0800206 self.assertEqual(urls, None)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400207
208 def testSignTypesRecovery(self):
209 """Only sign the requested recovery type"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800210 EXPECTED = {
211 'canary': [
212 'gs://chromeos-releases/canary-channel/test.board/5126.0.0/'
213 'ChromeOS-recovery-R34-5126.0.0-test.board.instructions'],
214 'dev': [
215 'gs://chromeos-releases/dev-channel/test.board/5126.0.0/'
216 'ChromeOS-recovery-R34-5126.0.0-test.board.instructions'],
217 }
218
219 urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
220 sign_types=['recovery'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400221 self.assertEqual(self.gs_mock.call_count, 18)
222 self.assertTrue(self.mark_mock.called)
Don Garrett9459c2f2014-01-22 18:20:24 -0800223 self.assertEqual(urls, EXPECTED)
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400224
225 def testSignTypesNone(self):
226 """Verify nothing is signed when we request an unavailable type"""
Don Garrett9459c2f2014-01-22 18:20:24 -0800227 urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
228 sign_types=['nononononono'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400229 self.assertEqual(self.gs_mock.call_count, 16)
230 self.assertFalse(self.mark_mock.called)
Don Garrett9459c2f2014-01-22 18:20:24 -0800231 self.assertEqual(urls, {})
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400232
233
234class MainTests(cros_test_lib.MockTestCase):
235 """Tests for main()"""
236
237 def setUp(self):
238 self.PatchObject(pushimage, 'PushImage')
239
240 def testBasic(self):
241 """Simple smoke test"""
Mike Frysinger09fe0122014-02-09 02:44:05 -0500242 pushimage.main(['--board', 'test.board', '/src', '--yes'])
Mike Frysingerd13faeb2013-09-05 16:00:46 -0400243
244
245if __name__ == '__main__':
246 # Use our local copy of insns for testing as the main one is not
247 # available in the public manifest.
248 signing.INPUT_INSN_DIR = signing.TEST_INPUT_INSN_DIR
249
250 # Run the tests.
251 cros_test_lib.main(level=logging.INFO)