blob: 77fdace2195d4d930e46cbb173b191a32727ac57 [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
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070022pytestmark = cros_test_lib.pytestmark_inside_only
23
Nam T. Nguyenf3816362014-06-13 09:26:27 -070024
Mike Frysinger6165cdc2020-02-21 02:38:07 -050025assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
26
27
Nam T. Nguyenf3816362014-06-13 09:26:27 -070028class TestImageTest(cros_test_lib.MockTempDirTestCase):
29 """Common class for tests ImageTest.
30
31 This sets up proper directory with test image. The image file is zero-byte.
32 """
33
34 def setUp(self):
35 # create dummy image file
36 self.image_file = os.path.join(self.tempdir,
37 constants.BASE_IMAGE_NAME + '.bin')
38 osutils.WriteFile(self.image_file, '')
LaMont Jonesea6dda62018-12-04 16:11:45 -070039 fake_partitions = (
Mike Frysingera2aeb372019-09-20 16:15:43 -040040 image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
41 image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
42 image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
LaMont Jonesea6dda62018-12-04 16:11:45 -070043 )
Mike Frysingera2aeb372019-09-20 16:15:43 -040044 self.PatchObject(image_lib, 'GetImageDiskPartitionInfo',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070045 autospec=True, return_value=fake_partitions)
Mike Nicholsa8e8f242019-09-09 14:10:29 -060046 self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
47 self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070048
49
50class FindImageTest(TestImageTest):
51 """Test FindImage() function."""
52
53 def _testFindOkay(self, image_path):
54 res = test_image.FindImage(image_path)
55 self.assertEqual(
56 res,
57 os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin')
58 )
59
60 def testFindWithDirectory(self):
61 self._testFindOkay(self.tempdir)
62
63 def testFindWithFile(self):
64 self._testFindOkay(self.image_file)
65
66 def testFindWithInvalid(self):
67 self.assertRaises(ValueError, test_image.FindImage,
68 os.path.join(self.tempdir, '404'))
69
70 def testFindWithInvalidDirectory(self):
71 os.unlink(self.image_file)
72 self.assertRaises(ValueError, test_image.FindImage,
73 os.path.join(self.tempdir))
74
75
76class MainTest(TestImageTest):
77 """Test the main invocation of the script."""
78
79 def testChdir(self):
80 """Verify the CWD is in a temp directory."""
81
Mike Frysinger3e046612017-10-18 17:19:23 -040082 class CwdTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -070083 """A dummy test class to verify current working directory."""
84
85 _expected_dir = None
86
87 def SetCwd(self, cwd):
88 self._expected_dir = cwd
89
90 def testExpectedCwd(self):
91 self.assertEqual(self._expected_dir, os.getcwd())
92
93 self.assertNotEqual('/tmp', os.getcwd())
94 os.chdir('/tmp')
95
96 test = CwdTest('testExpectedCwd')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070097 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070098 suite.addTest(test)
99 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400100 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700101
102 # Set up the expected directory.
103 expected_dir = os.path.join(self.tempdir, 'my-subdir')
104 os.mkdir(expected_dir)
105 test.SetCwd(expected_dir)
106 self.PatchObject(tempfile, 'mkdtemp', autospec=True,
107 return_value=expected_dir)
108
109 argv = [self.tempdir]
110 self.assertEqual(0, test_image.main(argv))
111 self.assertEqual('/tmp', os.getcwd())
112
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700113 def testBoardAndDirectory(self):
114 """Verify that "--board", "--test_results_root" are passed to the tests."""
115
Mike Frysinger3e046612017-10-18 17:19:23 -0400116 class AttributeTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700117 """Dummy test class to hold board and directory."""
118
119 def testOkay(self):
120 pass
121
122 test = AttributeTest('testOkay')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700123 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700124 suite.addTest(test)
125 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400126 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700127 argv = [
128 '--board',
129 'my-board',
130 '--test_results_root',
131 'your-root',
132 self.tempdir
133 ]
134 test_image.main(argv)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400135 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700136 self.assertEqual('my-board', test._board)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400137 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700138 self.assertEqual('your-root', os.path.basename(test._result_dir))