blob: a8bc938f9e9c54ec66d86ddeb902f22c960faccb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass57454f42016-11-25 20:15:52 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass57454f42016-11-25 20:15:52 -07005# Test for the Entry class
6
7import collections
Simon Glass8f521362017-11-12 21:52:21 -07008import os
9import sys
Simon Glass57454f42016-11-25 20:15:52 -070010import unittest
11
Simon Glass8f521362017-11-12 21:52:21 -070012import fdt
13import fdt_util
14import tools
15
Simon Glass57454f42016-11-25 20:15:52 -070016class TestEntry(unittest.TestCase):
Simon Glass752e7552018-10-01 21:12:41 -060017 def setUp(self):
18 tools.PrepareOutputDir(None)
19
20 def tearDown(self):
21 tools.FinaliseOutputDir()
22
Simon Glass8f521362017-11-12 21:52:21 -070023 def GetNode(self):
24 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
Simon Glass8f521362017-11-12 21:52:21 -070025 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 Glass8f521362017-11-12 21:52:21 -070043 del entry
44
Simon Glass57454f42016-11-25 20:15:52 -070045 def testEntryContents(self):
46 """Test the Entry bass class"""
Simon Glassb3393262017-11-12 21:52:20 -070047 import entry
Simon Glass57454f42016-11-25 20:15:52 -070048 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 Glassb3393262017-11-12 21:52:20 -070053 import entry
Simon Glass57454f42016-11-25 20:15:52 -070054 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 Glassac599912017-11-12 21:52:22 -070060
Simon Glass639505b2018-09-14 04:57:11 -060061 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 Glass3732ec32018-09-14 04:57:18 -060072 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 Glassac599912017-11-12 21:52:22 -070077
78if __name__ == "__main__":
79 unittest.main()