blob: 5f712856437faaf3e83b7e7292ecbe0f50a68689 [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
11import tempfile
12import unittest
13
Aviv Keshetb7519e12016-10-04 00:50:00 -070014from chromite.lib import constants
Nam T. Nguyenf3816362014-06-13 09:26:27 -070015from chromite.lib import cros_test_lib
Mike Nicholsa8e8f242019-09-09 14:10:29 -060016from chromite.lib import image_lib
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070017from chromite.lib import image_test_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070018from chromite.lib import osutils
19from chromite.scripts import test_image
20
21
22class TestImageTest(cros_test_lib.MockTempDirTestCase):
23 """Common class for tests ImageTest.
24
25 This sets up proper directory with test image. The image file is zero-byte.
26 """
27
28 def setUp(self):
29 # create dummy image file
30 self.image_file = os.path.join(self.tempdir,
31 constants.BASE_IMAGE_NAME + '.bin')
32 osutils.WriteFile(self.image_file, '')
LaMont Jonesea6dda62018-12-04 16:11:45 -070033 fake_partitions = (
Mike Frysingera2aeb372019-09-20 16:15:43 -040034 image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
35 image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
36 image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
LaMont Jonesea6dda62018-12-04 16:11:45 -070037 )
Mike Frysingera2aeb372019-09-20 16:15:43 -040038 self.PatchObject(image_lib, 'GetImageDiskPartitionInfo',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070039 autospec=True, return_value=fake_partitions)
Mike Nicholsa8e8f242019-09-09 14:10:29 -060040 self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
41 self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070042
43
44class FindImageTest(TestImageTest):
45 """Test FindImage() function."""
46
47 def _testFindOkay(self, image_path):
48 res = test_image.FindImage(image_path)
49 self.assertEqual(
50 res,
51 os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin')
52 )
53
54 def testFindWithDirectory(self):
55 self._testFindOkay(self.tempdir)
56
57 def testFindWithFile(self):
58 self._testFindOkay(self.image_file)
59
60 def testFindWithInvalid(self):
61 self.assertRaises(ValueError, test_image.FindImage,
62 os.path.join(self.tempdir, '404'))
63
64 def testFindWithInvalidDirectory(self):
65 os.unlink(self.image_file)
66 self.assertRaises(ValueError, test_image.FindImage,
67 os.path.join(self.tempdir))
68
69
70class MainTest(TestImageTest):
71 """Test the main invocation of the script."""
72
73 def testChdir(self):
74 """Verify the CWD is in a temp directory."""
75
Mike Frysinger3e046612017-10-18 17:19:23 -040076 class CwdTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -070077 """A dummy test class to verify current working directory."""
78
79 _expected_dir = None
80
81 def SetCwd(self, cwd):
82 self._expected_dir = cwd
83
84 def testExpectedCwd(self):
85 self.assertEqual(self._expected_dir, os.getcwd())
86
87 self.assertNotEqual('/tmp', os.getcwd())
88 os.chdir('/tmp')
89
90 test = CwdTest('testExpectedCwd')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070091 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070092 suite.addTest(test)
93 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -040094 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070095
96 # Set up the expected directory.
97 expected_dir = os.path.join(self.tempdir, 'my-subdir')
98 os.mkdir(expected_dir)
99 test.SetCwd(expected_dir)
100 self.PatchObject(tempfile, 'mkdtemp', autospec=True,
101 return_value=expected_dir)
102
103 argv = [self.tempdir]
104 self.assertEqual(0, test_image.main(argv))
105 self.assertEqual('/tmp', os.getcwd())
106
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700107 def testBoardAndDirectory(self):
108 """Verify that "--board", "--test_results_root" are passed to the tests."""
109
Mike Frysinger3e046612017-10-18 17:19:23 -0400110 class AttributeTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700111 """Dummy test class to hold board and directory."""
112
113 def testOkay(self):
114 pass
115
116 test = AttributeTest('testOkay')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700117 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700118 suite.addTest(test)
119 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400120 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700121 argv = [
122 '--board',
123 'my-board',
124 '--test_results_root',
125 'your-root',
126 self.tempdir
127 ]
128 test_image.main(argv)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400129 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700130 self.assertEqual('my-board', test._board)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400131 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700132 self.assertEqual('your-root', os.path.basename(test._result_dir))