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 | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 16 | class TestEntry(unittest.TestCase): |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 17 | def GetNode(self): |
| 18 | binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 19 | tools.PrepareOutputDir(None) |
| 20 | fname = fdt_util.EnsureCompiled( |
| 21 | os.path.join(binman_dir,('test/05_simple.dts'))) |
| 22 | dtb = fdt.FdtScan(fname) |
| 23 | return dtb.GetNode('/binman/u-boot') |
| 24 | |
| 25 | def test1EntryNoImportLib(self): |
| 26 | """Test that we can import Entry subclassess successfully""" |
| 27 | |
| 28 | sys.modules['importlib'] = None |
| 29 | global entry |
| 30 | import entry |
| 31 | entry.Entry.Create(None, self.GetNode(), 'u-boot') |
| 32 | |
| 33 | def test2EntryImportLib(self): |
| 34 | del sys.modules['importlib'] |
| 35 | global entry |
| 36 | reload(entry) |
| 37 | entry.Entry.Create(None, self.GetNode(), 'u-boot-spl') |
| 38 | tools._RemoveOutputDir() |
| 39 | del entry |
| 40 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 41 | def testEntryContents(self): |
| 42 | """Test the Entry bass class""" |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 43 | import entry |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 44 | base_entry = entry.Entry(None, None, None, read_node=False) |
| 45 | self.assertEqual(True, base_entry.ObtainContents()) |
| 46 | |
| 47 | def testUnknownEntry(self): |
| 48 | """Test that unknown entry types are detected""" |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 49 | import entry |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 50 | Node = collections.namedtuple('Node', ['name', 'path']) |
| 51 | node = Node('invalid-name', 'invalid-path') |
| 52 | with self.assertRaises(ValueError) as e: |
| 53 | entry.Entry.Create(None, node, node.name) |
| 54 | self.assertIn("Unknown entry type 'invalid-name' in node " |
| 55 | "'invalid-path'", str(e.exception)) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 56 | |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 57 | def testUniqueName(self): |
| 58 | """Test Entry.GetUniqueName""" |
| 59 | import entry |
| 60 | Node = collections.namedtuple('Node', ['name', 'parent']) |
| 61 | base_node = Node('root', None) |
| 62 | base_entry = entry.Entry(None, None, base_node, read_node=False) |
| 63 | self.assertEqual('root', base_entry.GetUniqueName()) |
| 64 | sub_node = Node('subnode', base_node) |
| 65 | sub_entry = entry.Entry(None, None, sub_node, read_node=False) |
| 66 | self.assertEqual('root.subnode', sub_entry.GetUniqueName()) |
| 67 | |
Simon Glass | 6c234bf | 2018-09-14 04:57:18 -0600 | [diff] [blame] | 68 | def testGetDefaultFilename(self): |
| 69 | """Trivial test for this base class function""" |
| 70 | import entry |
| 71 | base_entry = entry.Entry(None, None, None, read_node=False) |
| 72 | self.assertIsNone(base_entry.GetDefaultFilename()) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 73 | |
| 74 | if __name__ == "__main__": |
| 75 | unittest.main() |