blob: 79e73aa6fa3dd700afdf65102a828532ee974195 [file] [log] [blame]
Alex Kleinfa719c92018-10-15 14:04:22 -06001# 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 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
Alex Klein1699fab2022-09-08 08:46:06 -060045 # Test the board is fetched and used when using the default image basename.
46 self.PatchObject(
47 image_lib,
48 "GetLatestImageLink",
49 return_value=os.path.join(self.tempdir, "board"),
50 )
51 opts = security_test_image._ParseArgs([])
52 self.assertEqual("board", opts.board)
53 self.assertEqual(
54 opts.image, os.path.join(self.tempdir, "board/recovery_image.bin")
55 )