entry_test.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Test for the Entry class
  6. import collections
  7. import os
  8. import sys
  9. import unittest
  10. import fdt
  11. import fdt_util
  12. import tools
  13. entry = None
  14. class TestEntry(unittest.TestCase):
  15. def setUp(self):
  16. tools.PrepareOutputDir(None)
  17. def tearDown(self):
  18. tools.FinaliseOutputDir()
  19. def GetNode(self):
  20. binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  21. fname = fdt_util.EnsureCompiled(
  22. os.path.join(binman_dir,('test/05_simple.dts')))
  23. dtb = fdt.FdtScan(fname)
  24. return dtb.GetNode('/binman/u-boot')
  25. def test1EntryNoImportLib(self):
  26. """Test that we can import Entry subclassess successfully"""
  27. sys.modules['importlib'] = None
  28. global entry
  29. import entry
  30. entry.Entry.Create(None, self.GetNode(), 'u-boot')
  31. def test2EntryImportLib(self):
  32. del sys.modules['importlib']
  33. global entry
  34. if entry:
  35. reload(entry)
  36. else:
  37. import entry
  38. entry.Entry.Create(None, self.GetNode(), 'u-boot-spl')
  39. del entry
  40. def testEntryContents(self):
  41. """Test the Entry bass class"""
  42. import entry
  43. base_entry = entry.Entry(None, None, None, read_node=False)
  44. self.assertEqual(True, base_entry.ObtainContents())
  45. def testUnknownEntry(self):
  46. """Test that unknown entry types are detected"""
  47. import entry
  48. Node = collections.namedtuple('Node', ['name', 'path'])
  49. node = Node('invalid-name', 'invalid-path')
  50. with self.assertRaises(ValueError) as e:
  51. entry.Entry.Create(None, node, node.name)
  52. self.assertIn("Unknown entry type 'invalid-name' in node "
  53. "'invalid-path'", str(e.exception))
  54. def testUniqueName(self):
  55. """Test Entry.GetUniqueName"""
  56. import entry
  57. Node = collections.namedtuple('Node', ['name', 'parent'])
  58. base_node = Node('root', None)
  59. base_entry = entry.Entry(None, None, base_node, read_node=False)
  60. self.assertEqual('root', base_entry.GetUniqueName())
  61. sub_node = Node('subnode', base_node)
  62. sub_entry = entry.Entry(None, None, sub_node, read_node=False)
  63. self.assertEqual('root.subnode', sub_entry.GetUniqueName())
  64. def testGetDefaultFilename(self):
  65. """Trivial test for this base class function"""
  66. import entry
  67. base_entry = entry.Entry(None, None, None, read_node=False)
  68. self.assertIsNone(base_entry.GetDefaultFilename())
  69. if __name__ == "__main__":
  70. unittest.main()