blob: 2379adefc894782ad773f45b4088d6e419f47fca [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
Nam T. Nguyenf3816362014-06-13 09:26:27 -070016from chromite.lib import cros_test_lib
Mike Nicholsa8e8f242019-09-09 14:10:29 -060017from chromite.lib import image_lib
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070018from chromite.lib import image_test_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070019from chromite.lib import osutils
20from chromite.scripts import test_image
21
22
Mike Frysinger6165cdc2020-02-21 02:38:07 -050023assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
24
25
Nam T. Nguyenf3816362014-06-13 09:26:27 -070026class TestImageTest(cros_test_lib.MockTempDirTestCase):
27 """Common class for tests ImageTest.
28
29 This sets up proper directory with test image. The image file is zero-byte.
30 """
31
32 def setUp(self):
33 # create dummy image file
34 self.image_file = os.path.join(self.tempdir,
35 constants.BASE_IMAGE_NAME + '.bin')
36 osutils.WriteFile(self.image_file, '')
LaMont Jonesea6dda62018-12-04 16:11:45 -070037 fake_partitions = (
Mike Frysingera2aeb372019-09-20 16:15:43 -040038 image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
39 image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
40 image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
LaMont Jonesea6dda62018-12-04 16:11:45 -070041 )
Mike Frysingera2aeb372019-09-20 16:15:43 -040042 self.PatchObject(image_lib, 'GetImageDiskPartitionInfo',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070043 autospec=True, return_value=fake_partitions)
Mike Nicholsa8e8f242019-09-09 14:10:29 -060044 self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
45 self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070046
47
48class FindImageTest(TestImageTest):
49 """Test FindImage() function."""
50
51 def _testFindOkay(self, image_path):
52 res = test_image.FindImage(image_path)
53 self.assertEqual(
54 res,
55 os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin')
56 )
57
58 def testFindWithDirectory(self):
59 self._testFindOkay(self.tempdir)
60
61 def testFindWithFile(self):
62 self._testFindOkay(self.image_file)
63
64 def testFindWithInvalid(self):
65 self.assertRaises(ValueError, test_image.FindImage,
66 os.path.join(self.tempdir, '404'))
67
68 def testFindWithInvalidDirectory(self):
69 os.unlink(self.image_file)
70 self.assertRaises(ValueError, test_image.FindImage,
71 os.path.join(self.tempdir))
72
73
74class MainTest(TestImageTest):
75 """Test the main invocation of the script."""
76
77 def testChdir(self):
78 """Verify the CWD is in a temp directory."""
79
Mike Frysinger3e046612017-10-18 17:19:23 -040080 class CwdTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -070081 """A dummy test class to verify current working directory."""
82
83 _expected_dir = None
84
85 def SetCwd(self, cwd):
86 self._expected_dir = cwd
87
88 def testExpectedCwd(self):
89 self.assertEqual(self._expected_dir, os.getcwd())
90
91 self.assertNotEqual('/tmp', os.getcwd())
92 os.chdir('/tmp')
93
94 test = CwdTest('testExpectedCwd')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070095 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070096 suite.addTest(test)
97 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -040098 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070099
100 # Set up the expected directory.
101 expected_dir = os.path.join(self.tempdir, 'my-subdir')
102 os.mkdir(expected_dir)
103 test.SetCwd(expected_dir)
104 self.PatchObject(tempfile, 'mkdtemp', autospec=True,
105 return_value=expected_dir)
106
107 argv = [self.tempdir]
108 self.assertEqual(0, test_image.main(argv))
109 self.assertEqual('/tmp', os.getcwd())
110
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700111 def testBoardAndDirectory(self):
112 """Verify that "--board", "--test_results_root" are passed to the tests."""
113
Mike Frysinger3e046612017-10-18 17:19:23 -0400114 class AttributeTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700115 """Dummy test class to hold board and directory."""
116
117 def testOkay(self):
118 pass
119
120 test = AttributeTest('testOkay')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700121 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700122 suite.addTest(test)
123 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400124 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700125 argv = [
126 '--board',
127 'my-board',
128 '--test_results_root',
129 'your-root',
130 self.tempdir
131 ]
132 test_image.main(argv)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400133 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700134 self.assertEqual('my-board', test._board)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400135 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700136 self.assertEqual('your-root', os.path.basename(test._result_dir))