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