blob: 3a38514ceaeff3e82d00a7004f1896cb041d8a33 [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)
157 content = '\n'.join(m.call_args_list[0][0][1])
158 self.assertIn('USER=', content)
159 self.assertIn('HOSTNAME=', content)
160
161 def testTbsUpload(self):
162 """Make sure we actually try to upload the file"""
163 pushimage.MarkImageToBeSigned(self.ctx, '', '', 50)
164 self.gs_mock.assertCommandContains(['cp', '--'])
165
166
167class PushImageTests(cros_test_lib.MockTestCase):
168 """Tests for PushImage()"""
169
170 def setUp(self):
171 self.gs_mock = self.StartPatcher(gs_unittest.GSContextMock())
172 self.gs_mock.SetDefaultCmdResult()
173 self.ctx = gs.GSContext()
174
175 self.mark_mock = self.PatchObject(pushimage, 'MarkImageToBeSigned')
176
177 def testBasic(self):
178 """Simple smoke test"""
179 pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0', profile='hi')
180
181 def testBasicMock(self):
182 """Simple smoke test in mock mode"""
183 pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
184 dry_run=True, mock=True)
185
186 def testBadVersion(self):
187 """Make sure we barf on bad version strings"""
188 self.assertRaises(ValueError, pushimage.PushImage, '', '', 'asdf')
189
190 def testNoInsns(self):
191 """Boards w/out insn files should get skipped"""
192 pushimage.PushImage('/src', 'a bad bad board', 'R34-5126.0.0')
193 self.assertEqual(self.gs_mock.call_count, 0)
194
195 def testSignTypesRecovery(self):
196 """Only sign the requested recovery type"""
197 pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
198 sign_types=['recovery'])
199 self.assertEqual(self.gs_mock.call_count, 18)
200 self.assertTrue(self.mark_mock.called)
201
202 def testSignTypesNone(self):
203 """Verify nothing is signed when we request an unavailable type"""
204 pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
205 sign_types=['nononononono'])
206 self.assertEqual(self.gs_mock.call_count, 16)
207 self.assertFalse(self.mark_mock.called)
208
209
210class MainTests(cros_test_lib.MockTestCase):
211 """Tests for main()"""
212
213 def setUp(self):
214 self.PatchObject(pushimage, 'PushImage')
215
216 def testBasic(self):
217 """Simple smoke test"""
218 pushimage.main(['--board', 'test.board', '/src'])
219
220
221if __name__ == '__main__':
222 # Use our local copy of insns for testing as the main one is not
223 # available in the public manifest.
224 signing.INPUT_INSN_DIR = signing.TEST_INPUT_INSN_DIR
225
226 # Run the tests.
227 cros_test_lib.main(level=logging.INFO)