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 | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 12 | import fdt |
| 13 | import fdt_util |
| 14 | import tools |
| 15 | |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 16 | entry = None |
| 17 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 18 | class TestEntry(unittest.TestCase): |
Simon Glass | e0e6275 | 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 | 934cdcf | 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 | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 27 | fname = fdt_util.EnsureCompiled( |
| 28 | os.path.join(binman_dir,('test/05_simple.dts'))) |
| 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 | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 43 | if entry: |
| 44 | reload(entry) |
| 45 | else: |
| 46 | import entry |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 47 | entry.Entry.Create(None, self.GetNode(), 'u-boot-spl') |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 48 | del entry |
| 49 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 50 | def testEntryContents(self): |
| 51 | """Test the Entry bass class""" |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 52 | import entry |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 53 | base_entry = entry.Entry(None, None, None, read_node=False) |
| 54 | self.assertEqual(True, base_entry.ObtainContents()) |
| 55 | |
| 56 | def testUnknownEntry(self): |
| 57 | """Test that unknown entry types are detected""" |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 58 | import entry |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 59 | Node = collections.namedtuple('Node', ['name', 'path']) |
| 60 | node = Node('invalid-name', 'invalid-path') |
| 61 | with self.assertRaises(ValueError) as e: |
| 62 | entry.Entry.Create(None, node, node.name) |
| 63 | self.assertIn("Unknown entry type 'invalid-name' in node " |
| 64 | "'invalid-path'", str(e.exception)) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 65 | |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 66 | def testUniqueName(self): |
| 67 | """Test Entry.GetUniqueName""" |
| 68 | import entry |
| 69 | Node = collections.namedtuple('Node', ['name', 'parent']) |
| 70 | base_node = Node('root', None) |
| 71 | base_entry = entry.Entry(None, None, base_node, read_node=False) |
| 72 | self.assertEqual('root', base_entry.GetUniqueName()) |
| 73 | sub_node = Node('subnode', base_node) |
| 74 | sub_entry = entry.Entry(None, None, sub_node, read_node=False) |
| 75 | self.assertEqual('root.subnode', sub_entry.GetUniqueName()) |
| 76 | |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 77 | def testGetDefaultFilename(self): |
| 78 | """Trivial test for this base class function""" |
| 79 | import entry |
| 80 | base_entry = entry.Entry(None, None, None, read_node=False) |
| 81 | self.assertIsNone(base_entry.GetDefaultFilename()) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 82 | |
| 83 | if __name__ == "__main__": |
| 84 | unittest.main() |