blob: 1f61a788a765dc99a1a96219a87a3ae6da9b7214 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Nam T. Nguyenf3816362014-06-13 09:26:27 -07002# Copyright 2014 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"""Unit tests for the functions in test_image."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Nam T. Nguyenf3816362014-06-13 09:26:27 -070010import os
Mike Frysinger6165cdc2020-02-21 02:38:07 -050011import sys
Nam T. Nguyenf3816362014-06-13 09:26:27 -070012import tempfile
13import unittest
14
Aviv Keshetb7519e12016-10-04 00:50:00 -070015from chromite.lib import constants
Mike Frysinger417a85c2021-04-23 00:58:17 -040016from chromite.lib import cros_build_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070017from chromite.lib import cros_test_lib
Mike Nicholsa8e8f242019-09-09 14:10:29 -060018from chromite.lib import image_lib
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070019from chromite.lib import image_test_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070020from chromite.lib import osutils
21from chromite.scripts import test_image
22
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070023pytestmark = cros_test_lib.pytestmark_inside_only
24
Nam T. Nguyenf3816362014-06-13 09:26:27 -070025
Mike Frysinger6165cdc2020-02-21 02:38:07 -050026assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
27
28
Nam T. Nguyenf3816362014-06-13 09:26:27 -070029class TestImageTest(cros_test_lib.MockTempDirTestCase):
30 """Common class for tests ImageTest.
31
32 This sets up proper directory with test image. The image file is zero-byte.
33 """
34
35 def setUp(self):
Mike Frysinger417a85c2021-04-23 00:58:17 -040036 # Create empty image file.
Nam T. Nguyenf3816362014-06-13 09:26:27 -070037 self.image_file = os.path.join(self.tempdir,
38 constants.BASE_IMAGE_NAME + '.bin')
Mike Frysinger417a85c2021-04-23 00:58:17 -040039 osutils.AllocateFile(self.image_file, 1024 * 1024)
40 # In case sfdisk is in /sbin, search that too.
41 cros_build_lib.run(
42 ['sfdisk', self.image_file],
43 extra_env={'PATH': '/sbin:/usr/sbin:%s' % os.environ['PATH']},
44 input='label: dos')
LaMont Jonesea6dda62018-12-04 16:11:45 -070045 fake_partitions = (
Mike Frysingera2aeb372019-09-20 16:15:43 -040046 image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
47 image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
48 image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
LaMont Jonesea6dda62018-12-04 16:11:45 -070049 )
Mike Frysingera2aeb372019-09-20 16:15:43 -040050 self.PatchObject(image_lib, 'GetImageDiskPartitionInfo',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070051 autospec=True, return_value=fake_partitions)
Mike Nicholsa8e8f242019-09-09 14:10:29 -060052 self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
53 self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070054
55
56class FindImageTest(TestImageTest):
57 """Test FindImage() function."""
58
59 def _testFindOkay(self, image_path):
60 res = test_image.FindImage(image_path)
61 self.assertEqual(
62 res,
63 os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin')
64 )
65
66 def testFindWithDirectory(self):
67 self._testFindOkay(self.tempdir)
68
69 def testFindWithFile(self):
70 self._testFindOkay(self.image_file)
71
72 def testFindWithInvalid(self):
73 self.assertRaises(ValueError, test_image.FindImage,
74 os.path.join(self.tempdir, '404'))
75
76 def testFindWithInvalidDirectory(self):
77 os.unlink(self.image_file)
78 self.assertRaises(ValueError, test_image.FindImage,
79 os.path.join(self.tempdir))
80
81
82class MainTest(TestImageTest):
83 """Test the main invocation of the script."""
84
85 def testChdir(self):
86 """Verify the CWD is in a temp directory."""
87
Mike Frysinger3e046612017-10-18 17:19:23 -040088 class CwdTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -070089 """A dummy test class to verify current working directory."""
90
91 _expected_dir = None
92
93 def SetCwd(self, cwd):
94 self._expected_dir = cwd
95
96 def testExpectedCwd(self):
97 self.assertEqual(self._expected_dir, os.getcwd())
98
99 self.assertNotEqual('/tmp', os.getcwd())
100 os.chdir('/tmp')
101
102 test = CwdTest('testExpectedCwd')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700103 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700104 suite.addTest(test)
105 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400106 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700107
108 # Set up the expected directory.
109 expected_dir = os.path.join(self.tempdir, 'my-subdir')
110 os.mkdir(expected_dir)
111 test.SetCwd(expected_dir)
112 self.PatchObject(tempfile, 'mkdtemp', autospec=True,
113 return_value=expected_dir)
114
115 argv = [self.tempdir]
116 self.assertEqual(0, test_image.main(argv))
117 self.assertEqual('/tmp', os.getcwd())
118
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700119 def testBoardAndDirectory(self):
120 """Verify that "--board", "--test_results_root" are passed to the tests."""
121
Mike Frysinger3e046612017-10-18 17:19:23 -0400122 class AttributeTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700123 """Dummy test class to hold board and directory."""
124
125 def testOkay(self):
126 pass
127
128 test = AttributeTest('testOkay')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700129 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700130 suite.addTest(test)
131 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400132 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700133 argv = [
134 '--board',
135 'my-board',
136 '--test_results_root',
137 'your-root',
138 self.tempdir
139 ]
140 test_image.main(argv)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400141 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700142 self.assertEqual('my-board', test._board)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400143 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700144 self.assertEqual('your-root', os.path.basename(test._result_dir))