blob: 67b861c93f17123a412bdf1dab8574829a6f02b0 [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_build_lib
16from 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
23class TestImageTest(cros_test_lib.MockTempDirTestCase):
24 """Common class for tests ImageTest.
25
26 This sets up proper directory with test image. The image file is zero-byte.
27 """
28
29 def setUp(self):
30 # create dummy image file
31 self.image_file = os.path.join(self.tempdir,
32 constants.BASE_IMAGE_NAME + '.bin')
33 osutils.WriteFile(self.image_file, '')
LaMont Jonesea6dda62018-12-04 16:11:45 -070034 fake_partitions = (
35 cros_build_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
36 cros_build_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
37 cros_build_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
38 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070039 self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
40 autospec=True, return_value=fake_partitions)
Mike Nicholsa8e8f242019-09-09 14:10:29 -060041 self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
42 self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070043
44
45class FindImageTest(TestImageTest):
46 """Test FindImage() function."""
47
48 def _testFindOkay(self, image_path):
49 res = test_image.FindImage(image_path)
50 self.assertEqual(
51 res,
52 os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin')
53 )
54
55 def testFindWithDirectory(self):
56 self._testFindOkay(self.tempdir)
57
58 def testFindWithFile(self):
59 self._testFindOkay(self.image_file)
60
61 def testFindWithInvalid(self):
62 self.assertRaises(ValueError, test_image.FindImage,
63 os.path.join(self.tempdir, '404'))
64
65 def testFindWithInvalidDirectory(self):
66 os.unlink(self.image_file)
67 self.assertRaises(ValueError, test_image.FindImage,
68 os.path.join(self.tempdir))
69
70
71class MainTest(TestImageTest):
72 """Test the main invocation of the script."""
73
74 def testChdir(self):
75 """Verify the CWD is in a temp directory."""
76
Mike Frysinger3e046612017-10-18 17:19:23 -040077 class CwdTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -070078 """A dummy test class to verify current working directory."""
79
80 _expected_dir = None
81
82 def SetCwd(self, cwd):
83 self._expected_dir = cwd
84
85 def testExpectedCwd(self):
86 self.assertEqual(self._expected_dir, os.getcwd())
87
88 self.assertNotEqual('/tmp', os.getcwd())
89 os.chdir('/tmp')
90
91 test = CwdTest('testExpectedCwd')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070092 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070093 suite.addTest(test)
94 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -040095 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070096
97 # Set up the expected directory.
98 expected_dir = os.path.join(self.tempdir, 'my-subdir')
99 os.mkdir(expected_dir)
100 test.SetCwd(expected_dir)
101 self.PatchObject(tempfile, 'mkdtemp', autospec=True,
102 return_value=expected_dir)
103
104 argv = [self.tempdir]
105 self.assertEqual(0, test_image.main(argv))
106 self.assertEqual('/tmp', os.getcwd())
107
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700108 def testBoardAndDirectory(self):
109 """Verify that "--board", "--test_results_root" are passed to the tests."""
110
Mike Frysinger3e046612017-10-18 17:19:23 -0400111 class AttributeTest(image_test_lib.ImageTestCase):
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700112 """Dummy test class to hold board and directory."""
113
114 def testOkay(self):
115 pass
116
117 test = AttributeTest('testOkay')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700118 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700119 suite.addTest(test)
120 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400121 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700122 argv = [
123 '--board',
124 'my-board',
125 '--test_results_root',
126 'your-root',
127 self.tempdir
128 ]
129 test_image.main(argv)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400130 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700131 self.assertEqual('my-board', test._board)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400132 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700133 self.assertEqual('your-root', os.path.basename(test._result_dir))