Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 5 | # Test for the Entry class |
| 6 | |
| 7 | import collections |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 8 | import os |
| 9 | import sys |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 10 | import unittest |
| 11 | |
Simon Glass | e90fe1b | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 12 | import entry |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 13 | import fdt |
| 14 | import fdt_util |
| 15 | import tools |
| 16 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 17 | class TestEntry(unittest.TestCase): |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 18 | def setUp(self): |
| 19 | tools.PrepareOutputDir(None) |
| 20 | |
| 21 | def tearDown(self): |
| 22 | tools.FinaliseOutputDir() |
| 23 | |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 24 | def GetNode(self): |
| 25 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 26 | fname = fdt_util.EnsureCompiled( |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 27 | os.path.join(binman_dir,('test/005_simple.dts'))) |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 28 | dtb = fdt.FdtScan(fname) |
| 29 | return dtb.GetNode('/binman/u-boot') |
| 30 | |
Simon Glass | e90fe1b | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 31 | def _ReloadEntry(self): |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 32 | global entry |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 33 | if entry: |
Simon Glass | 0199d19 | 2019-05-17 22:00:47 -0600 | [diff] [blame] | 34 | if sys.version_info[0] >= 3: |
| 35 | import importlib |
| 36 | importlib.reload(entry) |
| 37 | else: |
| 38 | reload(entry) |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 39 | else: |
| 40 | import entry |
Simon Glass | e90fe1b | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 41 | |
| 42 | def test1EntryNoImportLib(self): |
| 43 | """Test that we can import Entry subclassess successfully""" |
| 44 | sys.modules['importlib'] = None |
| 45 | global entry |
| 46 | self._ReloadEntry() |
| 47 | entry.Entry.Create(None, self.GetNode(), 'u-boot') |
| 48 | self.assertFalse(entry.have_importlib) |
| 49 | |
| 50 | def test2EntryImportLib(self): |
| 51 | del sys.modules['importlib'] |
| 52 | global entry |
| 53 | self._ReloadEntry() |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 54 | entry.Entry.Create(None, self.GetNode(), 'u-boot-spl') |
Simon Glass | e90fe1b | 2019-07-08 14:25:23 -0600 | [diff] [blame] | 55 | self.assertTrue(entry.have_importlib) |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 56 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 57 | def testEntryContents(self): |
| 58 | """Test the Entry bass class""" |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 59 | import entry |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 60 | base_entry = entry.Entry(None, None, None, read_node=False) |
| 61 | self.assertEqual(True, base_entry.ObtainContents()) |
| 62 | |
| 63 | def testUnknownEntry(self): |
| 64 | """Test that unknown entry types are detected""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 65 | Node = collections.namedtuple('Node', ['name', 'path']) |
| 66 | node = Node('invalid-name', 'invalid-path') |
| 67 | with self.assertRaises(ValueError) as e: |
| 68 | entry.Entry.Create(None, node, node.name) |
| 69 | self.assertIn("Unknown entry type 'invalid-name' in node " |
| 70 | "'invalid-path'", str(e.exception)) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 71 | |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 72 | def testUniqueName(self): |
| 73 | """Test Entry.GetUniqueName""" |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 74 | Node = collections.namedtuple('Node', ['name', 'parent']) |
| 75 | base_node = Node('root', None) |
| 76 | base_entry = entry.Entry(None, None, base_node, read_node=False) |
| 77 | self.assertEqual('root', base_entry.GetUniqueName()) |
| 78 | sub_node = Node('subnode', base_node) |
| 79 | sub_entry = entry.Entry(None, None, sub_node, read_node=False) |
| 80 | self.assertEqual('root.subnode', sub_entry.GetUniqueName()) |
| 81 | |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 82 | def testGetDefaultFilename(self): |
| 83 | """Trivial test for this base class function""" |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 84 | base_entry = entry.Entry(None, None, None, read_node=False) |
| 85 | self.assertIsNone(base_entry.GetDefaultFilename()) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 86 | |
| 87 | if __name__ == "__main__": |
| 88 | unittest.main() |