blob: bb6b4051eb93277070e39f208adaa45e5b26ea85 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Alex Kleinfa719c92018-10-15 14:04:22 -06002# 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 Kleinfa719c92018-10-15 14:04:22 -06007import os
8
9from chromite.lib import cros_build_lib
10from chromite.lib import cros_test_lib
11from chromite.lib import image_lib
12from chromite.scripts import security_test_image
13
14
Alex Kleinfa719c92018-10-15 14:04:22 -060015class SecurityTestImageTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060016 """Security test image script tests."""
Alex Kleinfa719c92018-10-15 14:04:22 -060017
Alex Klein1699fab2022-09-08 08:46:06 -060018 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)
Alex Kleinfa719c92018-10-15 14:04:22 -060025
Alex Klein1699fab2022-09-08 08:46:06 -060026 def testParseArgs(self):
27 """Argument parsing tests."""
28 # pylint: disable=protected-access
29 # Test no arguments.
30 with self.assertRaises(SystemExit):
31 security_test_image._ParseArgs([])
Alex Kleinfa719c92018-10-15 14:04:22 -060032
Alex Klein1699fab2022-09-08 08:46:06 -060033 # Test board is set but not used when we have the full image path.
34 self.PatchObject(
35 cros_build_lib, "GetDefaultBoard", return_value="board"
36 )
37 opts = security_test_image._ParseArgs(
38 ["--image", os.path.join(self.tempdir, "other_image.bin")]
39 )
40 self.assertEqual("board", opts.board)
41 self.assertEqual(
42 opts.image, os.path.join(self.tempdir, "other_image.bin")
43 )
Alex Kleinfa719c92018-10-15 14:04:22 -060044
Trent Apted4a0812b2023-05-15 15:33:55 +100045 # Test the board is fetched and used when using the default image
46 # basename.
Alex Klein1699fab2022-09-08 08:46:06 -060047 self.PatchObject(
48 image_lib,
49 "GetLatestImageLink",
50 return_value=os.path.join(self.tempdir, "board"),
51 )
52 opts = security_test_image._ParseArgs([])
53 self.assertEqual("board", opts.board)
54 self.assertEqual(
55 opts.image, os.path.join(self.tempdir, "board/recovery_image.bin")
56 )