Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unittests for the security_test_image script.""" |
| 6 | |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 7 | import os |
| 8 | |
| 9 | from chromite.lib import cros_build_lib |
| 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.lib import image_lib |
| 12 | from chromite.scripts import security_test_image |
| 13 | |
| 14 | |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 15 | class SecurityTestImageTest(cros_test_lib.MockTempDirTestCase): |
| 16 | """Security test image script tests.""" |
| 17 | |
| 18 | def setUp(self): |
| 19 | D = cros_test_lib.Directory |
| 20 | filesystem = ( |
| 21 | D('board', ('recovery_image.bin',)), |
| 22 | 'other_image.bin', |
| 23 | ) |
| 24 | cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem) |
| 25 | |
| 26 | def testParseArgs(self): |
| 27 | """Argument parsing tests.""" |
| 28 | # pylint: disable=protected-access |
| 29 | # Test no arguments. |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 30 | with self.assertRaises(SystemExit): |
| 31 | security_test_image._ParseArgs([]) |
| 32 | |
| 33 | # Test board is set but not used when we have the full image path. |
| 34 | self.PatchObject(cros_build_lib, 'GetDefaultBoard', return_value='board') |
| 35 | opts = security_test_image._ParseArgs( |
| 36 | ['--image', os.path.join(self.tempdir, 'other_image.bin')]) |
| 37 | self.assertEqual('board', opts.board) |
| 38 | self.assertEqual(opts.image, |
| 39 | os.path.join(self.tempdir, 'other_image.bin')) |
| 40 | |
| 41 | # Test the board is fetched and used when using the default image basename. |
| 42 | self.PatchObject(image_lib, 'GetLatestImageLink', |
| 43 | return_value=os.path.join(self.tempdir, 'board')) |
| 44 | opts = security_test_image._ParseArgs([]) |
| 45 | self.assertEqual('board', opts.board) |
| 46 | self.assertEqual(opts.image, |
| 47 | os.path.join(self.tempdir, |
| 48 | 'board/recovery_image.bin')) |