summaryrefslogtreecommitdiff
path: root/scripts/pushimage_unittest.py
blob: ae6f6c861e45c20f6a554671ee6a364bfa292cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unittests for pushimage.py"""

from __future__ import print_function

import mock
import os

from chromite.lib import cros_build_lib
from chromite.lib import cros_test_lib
from chromite.lib import gs
from chromite.lib import gs_unittest
from chromite.lib import osutils
from chromite.lib import partial_mock
from chromite.lib import signing
from chromite.scripts import pushimage


class InputInsnsTest(cros_test_lib.MockTestCase):
  """Tests for InputInsns"""

  def setUp(self):
    self.StartPatcher(gs_unittest.GSContextMock())

  def testBasic(self):
    """Simple smoke test"""
    insns = pushimage.InputInsns('test.board')
    insns.GetInsnFile('recovery')
    self.assertEqual(insns.GetChannels(), ['dev', 'canary'])
    self.assertEqual(insns.GetKeysets(), ['stumpy-mp-v3'])

  def testGetInsnFile(self):
    """Verify various inputs result in right insns path"""
    testdata = (
        ('UPPER_CAPS', 'UPPER_CAPS'),
        ('recovery', 'test.board'),
        ('firmware', 'test.board.firmware'),
        ('factory', 'test.board.factory'),
    )
    insns = pushimage.InputInsns('test.board')
    for image_type, filename in testdata:
      ret = insns.GetInsnFile(image_type)
      self.assertEqual(os.path.basename(ret), '%s.instructions' % (filename))

  def testSplitCfgField(self):
    """Verify splitting behavior behaves"""
    testdata = (
        ('', []),
        ('a b c', ['a', 'b', 'c']),
        ('a, b', ['a', 'b']),
        ('a,b', ['a', 'b']),
        ('a,\tb', ['a', 'b']),
        ('a\tb', ['a', 'b']),
    )
    for val, exp in testdata:
      ret = pushimage.InputInsns.SplitCfgField(val)
      self.assertEqual(ret, exp)

  def testOutputInsnsBasic(self):
    """Verify output instructions are sane"""
    exp_content = """[insns]
keyset = stumpy-mp-v3
channel = dev canary
chromeos_shell = false
ensure_no_password = true
firmware_update = true
security_checks = true
create_nplusone = true

[general]
"""

    insns = pushimage.InputInsns('test.board')
    m = self.PatchObject(osutils, 'WriteFile')
    insns.OutputInsns('recovery', '/bogus', {}, {})
    self.assertTrue(m.called)
    content = m.call_args_list[0][0][1]
    self.assertEqual(content.rstrip(), exp_content.rstrip())

  def testOutputInsnsReplacements(self):
    """Verify output instructions can be updated"""
    exp_content = """[insns]
keyset = batman
channel = dev
chromeos_shell = false
ensure_no_password = true
firmware_update = true
security_checks = true
create_nplusone = true

[general]
board = board
config_board = test.board
"""
    sect_insns = {
        'channel': 'dev',
        'keyset': 'batman',
    }
    sect_general = {
        'config_board': 'test.board',
        'board': 'board',
    }

    insns = pushimage.InputInsns('test.board')
    m = self.PatchObject(osutils, 'WriteFile')
    insns.OutputInsns('recovery', '/a/file', sect_insns, sect_general)
    self.assertTrue(m.called)
    content = m.call_args_list[0][0][1]
    self.assertEqual(content.rstrip(), exp_content.rstrip())


class MarkImageToBeSignedTest(gs_unittest.AbstractGSContextTest):
  """Tests for MarkImageToBeSigned()"""

  def setUp(self):
    # Minor optimization -- we call this for logging purposes in the main
    # code, but don't really care about it for testing.  It just slows us.
    self.PatchObject(cros_build_lib, 'MachineDetails', return_value='1234\n')

  def testBasic(self):
    """Simple smoke test"""
    tbs_base = 'gs://some-bucket'
    insns_path = 'chan/board/ver/file.instructions'
    tbs_file = '%s/tobesigned/90,chan,board,ver,file.instructions' % tbs_base
    ret = pushimage.MarkImageToBeSigned(self.ctx, tbs_base, insns_path, 90)
    self.assertEqual(ret, tbs_file)

  def testPriority(self):
    """Verify diff priority values get used correctly"""
    for prio, sprio in ((0, '00'), (9, '09'), (35, '35'), (99, '99')):
      ret = pushimage.MarkImageToBeSigned(self.ctx, '', '', prio)
      self.assertEquals(ret, '/tobesigned/%s,' % sprio)

  def testBadPriority(self):
    """Verify we reject bad priority values"""
    for prio in (-10, -1, 100, 91239):
      self.assertRaises(ValueError, pushimage.MarkImageToBeSigned, self.ctx,
                        '', '', prio)

  def testTbsUpload(self):
    """Make sure we actually try to upload the file"""
    pushimage.MarkImageToBeSigned(self.ctx, '', '', 50)
    self.gs_mock.assertCommandContains(['cp', '--'])


class PushImageTests(gs_unittest.AbstractGSContextTest):
  """Tests for PushImage()"""

  def setUp(self):
    self.mark_mock = self.PatchObject(pushimage, 'MarkImageToBeSigned')

  def testBasic(self):
    """Simple smoke test"""
    EXPECTED = {
        'canary': [
            ('gs://chromeos-releases/canary-channel/test.board-hi/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions')],
        'dev': [
            ('gs://chromeos-releases/dev-channel/test.board-hi/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions')],
    }
    with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
      urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
                                 profile='hi')

    self.assertEqual(urls, EXPECTED)

  def testBasic_SignTypesEmptyList(self):
    """Tests PushImage behavior when |sign_types| is empty instead of None.

    As part of the buildbots, PushImage function always receives a tuple for
    |sign_types| argument.  This test checks the behavior for empty tuple.
    """
    EXPECTED = {
        'canary': [
            ('gs://chromeos-releases/canary-channel/test.board-hi/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions')],
        'dev': [
            ('gs://chromeos-releases/dev-channel/test.board-hi/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-test.board-hi.instructions')],
    }
    with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
      urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
                                 profile='hi', sign_types=())

    self.assertEqual(urls, EXPECTED)

  def testBasic_RealBoardName(self):
    """Runs a simple smoke test using a real board name."""
    EXPECTED = {
        'canary': [
            ('gs://chromeos-releases/canary-channel/x86-alex/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-x86-alex.instructions')],
        'dev': [
            ('gs://chromeos-releases/dev-channel/x86-alex/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-x86-alex.instructions')],
    }
    with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
      urls = pushimage.PushImage('/src', 'x86-alex', 'R34-5126.0.0')

    self.assertEqual(urls, EXPECTED)

  def testBasicMock(self):
    """Simple smoke test in mock mode"""
    with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
      pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
                          dry_run=True, mock=True)

  def testBadVersion(self):
    """Make sure we barf on bad version strings"""
    self.assertRaises(ValueError, pushimage.PushImage, '', '', 'asdf')

  def testNoInsns(self):
    """Boards w/out insn files should get skipped"""
    urls = pushimage.PushImage('/src', 'a bad bad board', 'R34-5126.0.0')
    self.assertEqual(self.gs_mock.call_count, 0)
    self.assertEqual(urls, None)

  def testSignTypesRecovery(self):
    """Only sign the requested recovery type"""
    EXPECTED = {
        'canary': [
            ('gs://chromeos-releases/canary-channel/test.board/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-test.board.instructions')],
        'dev': [
            ('gs://chromeos-releases/dev-channel/test.board/5126.0.0/'
             'ChromeOS-recovery-R34-5126.0.0-test.board.instructions')],
    }

    with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
      urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
                                 sign_types=['recovery'])
    self.assertEqual(self.gs_mock.call_count, 22)
    self.assertTrue(self.mark_mock.called)
    self.assertEqual(urls, EXPECTED)

  def testSignTypesBase(self):
    """Only sign the requested recovery type"""
    EXPECTED = {
        'canary': [
            ('gs://chromeos-releases/canary-channel/test.board/5126.0.0/'
             'ChromeOS-base-R34-5126.0.0-test.board.instructions')],
        'dev': [
            ('gs://chromeos-releases/dev-channel/test.board/5126.0.0/'
             'ChromeOS-base-R34-5126.0.0-test.board.instructions')],
    }

    with mock.patch.object(gs.GSContext, 'Exists', return_value=True):
      urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
                                 sign_types=['base'])
    self.assertEqual(self.gs_mock.call_count, 24)
    self.assertTrue(self.mark_mock.called)
    self.assertEqual(urls, EXPECTED)

  def testSignTypesNone(self):
    """Verify nothing is signed when we request an unavailable type"""
    urls = pushimage.PushImage('/src', 'test.board', 'R34-5126.0.0',
                               sign_types=['nononononono'])
    self.assertEqual(self.gs_mock.call_count, 20)
    self.assertFalse(self.mark_mock.called)
    self.assertEqual(urls, {})

  def testGsError(self):
    """Verify random GS errors don't make us blow up entirely"""
    self.gs_mock.AddCmdResult(partial_mock.In('stat'), returncode=1,
                              output='gobblety gook\n')
    with cros_test_lib.LoggingCapturer('chromite'):
      self.assertRaises(pushimage.PushError, pushimage.PushImage, '/src',
                        'test.board', 'R34-5126.0.0')


class MainTests(cros_test_lib.MockTestCase):
  """Tests for main()"""

  def setUp(self):
    self.PatchObject(pushimage, 'PushImage')

  def testBasic(self):
    """Simple smoke test"""
    pushimage.main(['--board', 'test.board', '/src', '--yes'])


def main(_argv):
  # Use our local copy of insns for testing as the main one is not
  # available in the public manifest.
  signing.INPUT_INSN_DIR = signing.TEST_INPUT_INSN_DIR

  # Run the tests.
  cros_test_lib.main(level='info', module=__name__)