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