restapi.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright (C) 2017-2018 Wind River Systems, Inc.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License version 2 as
  5. # published by the Free Software Foundation.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. # See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. import unittest
  16. import tempfile
  17. import os
  18. import bb
  19. import layerindexlib
  20. from layerindexlib.tests.common import LayersTest
  21. import logging
  22. class LayerIndexWebRestApiTest(LayersTest):
  23. if os.environ.get("BB_SKIP_NETTESTS") == "yes":
  24. print("Unset BB_SKIP_NETTESTS to run network tests")
  25. else:
  26. def setUp(self):
  27. LayersTest.setUp(self)
  28. self.layerindex = layerindexlib.LayerIndex(self.d)
  29. self.layerindex.load_layerindex('http://layers.openembedded.org/layerindex/api/;branch=sumo', load=['layerDependencies'])
  30. def test_layerindex_is_empty(self):
  31. self.assertFalse(self.layerindex.is_empty(), msg="Layerindex is empty")
  32. def test_layerindex_store_file(self):
  33. self.layerindex.store_layerindex('file://%s/file.json' % self.tempdir, self.layerindex.indexes[0])
  34. self.assertTrue(os.path.isfile('%s/file.json' % self.tempdir), msg="Temporary file was not created by store_layerindex")
  35. reload = layerindexlib.LayerIndex(self.d)
  36. reload.load_layerindex('file://%s/file.json' % self.tempdir)
  37. self.assertFalse(reload.is_empty(), msg="Layerindex is empty")
  38. # Calculate layerItems in original index that should NOT be in reload
  39. layerItemNames = []
  40. for itemId in self.layerindex.indexes[0].layerItems:
  41. layerItemNames.append(self.layerindex.indexes[0].layerItems[itemId].name)
  42. for layerBranchId in self.layerindex.indexes[0].layerBranches:
  43. layerItemNames.remove(self.layerindex.indexes[0].layerBranches[layerBranchId].layer.name)
  44. for itemId in reload.indexes[0].layerItems:
  45. self.assertFalse(reload.indexes[0].layerItems[itemId].name in layerItemNames, msg="Item reloaded when it shouldn't have been")
  46. # Compare the original to what we wrote...
  47. for type in self.layerindex.indexes[0]._index:
  48. if type == 'apilinks' or \
  49. type == 'layerItems' or \
  50. type in self.layerindex.indexes[0].config['local']:
  51. continue
  52. for id in getattr(self.layerindex.indexes[0], type):
  53. self.logger.debug(1, "type %s" % (type))
  54. self.assertTrue(id in getattr(reload.indexes[0], type), msg="Id number not in reloaded index")
  55. self.logger.debug(1, "%s ? %s" % (getattr(self.layerindex.indexes[0], type)[id], getattr(reload.indexes[0], type)[id]))
  56. self.assertEqual(getattr(self.layerindex.indexes[0], type)[id], getattr(reload.indexes[0], type)[id], msg="Reloaded contents different")
  57. def test_layerindex_store_split(self):
  58. self.layerindex.store_layerindex('file://%s' % self.tempdir, self.layerindex.indexes[0])
  59. reload = layerindexlib.LayerIndex(self.d)
  60. reload.load_layerindex('file://%s' % self.tempdir)
  61. self.assertFalse(reload.is_empty(), msg="Layer index is empty")
  62. for type in self.layerindex.indexes[0]._index:
  63. if type == 'apilinks' or \
  64. type == 'layerItems' or \
  65. type in self.layerindex.indexes[0].config['local']:
  66. continue
  67. for id in getattr(self.layerindex.indexes[0] ,type):
  68. self.logger.debug(1, "type %s" % (type))
  69. self.assertTrue(id in getattr(reload.indexes[0], type), msg="Id number missing from reloaded data")
  70. self.logger.debug(1, "%s ? %s" % (getattr(self.layerindex.indexes[0] ,type)[id], getattr(reload.indexes[0], type)[id]))
  71. self.assertEqual(getattr(self.layerindex.indexes[0] ,type)[id], getattr(reload.indexes[0], type)[id], msg="reloaded data does not match original")
  72. def test_dependency_resolution(self):
  73. # Verify depth first searching...
  74. (dependencies, invalidnames) = self.layerindex.find_dependencies(names=['meta-python'])
  75. first = True
  76. for deplayerbranch in dependencies:
  77. layerBranch = dependencies[deplayerbranch][0]
  78. layerDeps = dependencies[deplayerbranch][1:]
  79. if not first:
  80. continue
  81. first = False
  82. # Top of the deps should be openembedded-core, since everything depends on it.
  83. self.assertEqual(layerBranch.layer.name, "openembedded-core", msg='OpenEmbedded-Core is no the first dependency')
  84. # meta-python should cause an openembedded-core dependency, if not assert!
  85. for dep in layerDeps:
  86. if dep.layer.name == 'meta-python':
  87. break
  88. else:
  89. self.logger.debug(1, "meta-python was not found")
  90. self.assetTrue(False)
  91. # Only check the first element...
  92. break
  93. else:
  94. # Empty list, this is bad.
  95. self.logger.debug(1, "Empty list of dependencies")
  96. self.assertIsNotNone(first, msg="Empty list of dependencies")
  97. # Last dep should be the requested item
  98. layerBranch = dependencies[deplayerbranch][0]
  99. self.assertEqual(layerBranch.layer.name, "meta-python", msg="Last dependency not meta-python")
  100. def test_find_collection(self):
  101. def _check(collection, expected):
  102. self.logger.debug(1, "Looking for collection %s..." % collection)
  103. result = self.layerindex.find_collection(collection)
  104. if expected:
  105. self.assertIsNotNone(result, msg="Did not find %s when it should be there" % collection)
  106. else:
  107. self.assertIsNone(result, msg="Found %s when it shouldn't be there" % collection)
  108. tests = [ ('core', True),
  109. ('openembedded-core', False),
  110. ('networking-layer', True),
  111. ('meta-python', True),
  112. ('openembedded-layer', True),
  113. ('notpresent', False) ]
  114. for collection,result in tests:
  115. _check(collection, result)
  116. def test_find_layerbranch(self):
  117. def _check(name, expected):
  118. self.logger.debug(1, "Looking for layerbranch %s..." % name)
  119. for index in self.layerindex.indexes:
  120. for layerbranchid in index.layerBranches:
  121. self.logger.debug(1, "Present: %s" % index.layerBranches[layerbranchid].layer.name)
  122. result = self.layerindex.find_layerbranch(name)
  123. if expected:
  124. self.assertIsNotNone(result, msg="Did not find %s when it should be there" % collection)
  125. else:
  126. self.assertIsNone(result, msg="Found %s when it shouldn't be there" % collection)
  127. tests = [ ('openembedded-core', True),
  128. ('core', False),
  129. ('meta-networking', True),
  130. ('meta-python', True),
  131. ('meta-oe', True),
  132. ('notpresent', False) ]
  133. for collection,result in tests:
  134. _check(collection, result)