/ion/idk/test/test_package.py

https://github.com/dstuebe/coi-services · Python · 368 lines · 207 code · 69 blank · 92 comment · 9 complexity · d47aa723cdb1bb0423ecf5a9ecce8dc6 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. @package ion.idk.test.test_package
  4. @file ion/idk/test/test_package.py
  5. @author Bill French
  6. @brief test file package process
  7. """
  8. __author__ = 'Bill French'
  9. __license__ = 'Apache 2.0'
  10. from os.path import basename, dirname
  11. from os import makedirs
  12. from os.path import exists
  13. import sys
  14. from nose.plugins.attrib import attr
  15. from mock import Mock
  16. import unittest
  17. from pyon.util.log import log
  18. from ion.idk.metadata import Metadata
  19. from ion.idk.driver_generator import DriverGenerator
  20. from ion.idk.exceptions import NotPython
  21. from ion.idk.exceptions import NoRoot
  22. from ion.idk.exceptions import FileNotFound
  23. from ion.idk.egg_generator import DriverFileList
  24. from ion.idk.egg_generator import DependencyList
  25. from ion.idk.egg_generator import EggGenerator
  26. ROOTDIR="/tmp/test_package.idk_test"
  27. # /tmp is a link on OS X
  28. if exists("/private/tmp"):
  29. ROOTDIR = "/private%s" % ROOTDIR
  30. TESTDIR="%s/mi/foo" % ROOTDIR
  31. @unittest.skip('Skip until moved to MI repo')
  32. @attr('UNIT', group='mi')
  33. class IDKPackageNose(unittest.TestCase):
  34. """
  35. Base class for IDK Package Tests
  36. """
  37. def setUp(self):
  38. """
  39. Setup the test case
  40. """
  41. # Our test path needs to be in the python path for SnakeFood to work.
  42. sys.path = ["%s/../.." % TESTDIR] + sys.path
  43. self.write_basefile()
  44. self.write_implfile()
  45. self.write_nosefile()
  46. self.write_resfile()
  47. def write_basefile(self):
  48. """
  49. Create all of the base python modules. These files live in the same root
  50. and should be reported as internal dependencies
  51. """
  52. destdir = dirname(self.basefile())
  53. if not exists(destdir):
  54. makedirs(destdir)
  55. ofile = open(self.basefile(), "w")
  56. ofile.write( "class MiFoo():\n")
  57. ofile.write( " def __init__():\n")
  58. ofile.write( " pass\n\n")
  59. ofile.close()
  60. # base2.py is a simple python module with no dependencies
  61. initfile = self.basefile().replace("base.py", 'base2.py')
  62. ofile = open(initfile, "w")
  63. ofile.write( "import mi.base4\n")
  64. ofile.close()
  65. # base3.py has an external dependency
  66. initfile = self.basefile().replace("base.py", 'base3.py')
  67. ofile = open(initfile, "w")
  68. ofile.write( "import string\n\n")
  69. ofile.close()
  70. # base4.py has an circular dependency
  71. initfile = self.basefile().replace("base.py", 'base4.py')
  72. ofile = open(initfile, "w")
  73. ofile.write( "import base2\n\n")
  74. ofile.close()
  75. # We need out init file
  76. initfile = self.basefile().replace("base.py", '__init__.py')
  77. ofile = open(initfile, "w")
  78. ofile.write("")
  79. ofile.close()
  80. def write_implfile(self):
  81. """
  82. The impl.py file is the target of our test. All tests will report the
  83. dependencies of this file.
  84. """
  85. destdir = dirname(self.implfile())
  86. if not exists(destdir):
  87. makedirs(destdir)
  88. # Write a base file
  89. ofile = open(self.implfile(), "w")
  90. # Test various forms of import. MiFoo is a class defined in base.py
  91. # The rest are py file imports.
  92. ofile.write( "from mi.base import MiFoo\n")
  93. ofile.write( "import mi.base2\n")
  94. ofile.write( "from mi import base3\n\n")
  95. ofile.close()
  96. # Add a pyc file to ignore
  97. initfile = self.implfile().replace("impl.py", 'impl.pyc')
  98. ofile = open(initfile, "w")
  99. ofile.close()
  100. # Ensure we have an import in an __init__ py file
  101. initfile = self.implfile().replace("impl.py", '__init__.py')
  102. ofile = open(initfile, "w")
  103. ofile.close()
  104. def write_nosefile(self):
  105. """
  106. The test.py file is the target of our test. All tests will report the
  107. dependencies of this file.
  108. """
  109. destdir = dirname(self.nosefile())
  110. if not exists(destdir):
  111. makedirs(destdir)
  112. # Write a base test file
  113. ofile = open(self.nosefile(), "w")
  114. ofile.close()
  115. # Ensure we have an import in an __init__ py file
  116. initfile = self.nosefile().replace("test_process.py", '__init__.py')
  117. ofile = open(initfile, "w")
  118. ofile.close()
  119. def write_resfile(self):
  120. """
  121. The impl.py file is the target of our test. All tests will report the
  122. dependencies of this file.
  123. """
  124. destdir = dirname(self.resfile())
  125. log.debug(self.resfile())
  126. if not exists(destdir):
  127. makedirs(destdir)
  128. # Write a base file
  129. ofile = open(self.resfile(), "w")
  130. # Test various forms of import. MiFoo is a class defined in base.py
  131. # The rest are py file imports.
  132. ofile.write( "hello world\n")
  133. ofile.close()
  134. def basefile(self):
  135. """
  136. The main base python file imported by the target file.
  137. """
  138. return "%s/../%s" % (TESTDIR, "base.py")
  139. def implfile(self):
  140. """
  141. The main python we will target for the tests
  142. """
  143. return "%s/%s" % (TESTDIR, "impl.py")
  144. def nosefile(self):
  145. """
  146. The main test python we will target for the tests
  147. """
  148. return "%s/%s" % (TESTDIR, "test/test_process.py")
  149. def resfile(self):
  150. """
  151. The main test resource we will target for the tests
  152. """
  153. return "%s/%s" % (TESTDIR, "res/test_file")
  154. @unittest.skip('Skip until moved to MI repo')
  155. @attr('UNIT', group='mi')
  156. class TestDependencyList(IDKPackageNose):
  157. """
  158. Test the DependencyList object that uses the snakefood module.
  159. """
  160. def test_exceptions(self):
  161. """
  162. Test all of the failure states for DependencyList
  163. """
  164. generator = None
  165. try:
  166. generator = DependencyList("this_file_does_not_exist.foo")
  167. except FileNotFound, e:
  168. self.assertTrue(e)
  169. self.assertFalse(generator)
  170. generator = None
  171. try:
  172. generator = DependencyList("/etc/hosts")
  173. except NotPython, e:
  174. self.assertTrue(e)
  175. self.assertFalse(generator)
  176. def test_internal_dependencies(self):
  177. """
  178. Test internal the dependency lists. This should include
  179. all of the files we created in setUp()
  180. """
  181. generator = DependencyList(self.implfile())
  182. root_list = generator.internal_roots()
  183. dep_list = generator.internal_dependencies()
  184. self.assertTrue(ROOTDIR in root_list)
  185. internal_deps = [
  186. "mi/base.py",
  187. "mi/base2.py",
  188. "mi/base3.py",
  189. "mi/base4.py",
  190. "mi/foo/impl.py",
  191. ]
  192. self.assertEqual(internal_deps, dep_list)
  193. def test_internal_dependencies_with_init(self):
  194. """
  195. Test internal the dependency lists. This should include
  196. all of the files we created in setUp()
  197. """
  198. generator = DependencyList(self.implfile(), include_internal_init = True)
  199. root_list = generator.internal_roots()
  200. dep_list = generator.internal_dependencies()
  201. self.assertTrue(ROOTDIR in root_list)
  202. internal_deps = [
  203. "mi/__init__.py",
  204. "mi/base.py",
  205. "mi/base2.py",
  206. "mi/base3.py",
  207. "mi/base4.py",
  208. "mi/foo/__init__.py",
  209. "mi/foo/impl.py",
  210. ]
  211. self.assertEqual(internal_deps, dep_list)
  212. def test_internal_test_dependencies_with_init(self):
  213. """
  214. Test internal the dependency lists for the unit test.
  215. """
  216. generator = DependencyList(self.nosefile(), include_internal_init = True)
  217. root_list = generator.internal_roots()
  218. dep_list = generator.internal_dependencies()
  219. self.assertTrue(ROOTDIR in root_list)
  220. internal_deps = [
  221. "mi/__init__.py",
  222. "mi/foo/__init__.py",
  223. "mi/foo/test/__init__.py",
  224. "mi/foo/test/test_process.py",
  225. ]
  226. self.assertEqual(internal_deps, dep_list)
  227. def test_external_dependencies(self):
  228. """
  229. Test external the dependency lists. This should exclude
  230. all of the files we created in setUp()
  231. """
  232. generator = DependencyList(self.implfile())
  233. root_list = generator.external_roots()
  234. dep_list = generator.external_dependencies()
  235. self.assertFalse(ROOTDIR in root_list)
  236. self.assertFalse("mi/base4.py" in dep_list)
  237. self.assertFalse("mi/base3.py" in dep_list)
  238. self.assertFalse("mi/base2.py" in dep_list)
  239. self.assertFalse("mi/foo/impl.py" in dep_list)
  240. self.assertFalse("mi/base.py" in dep_list)
  241. self.assertTrue("string.py" in dep_list)
  242. def test_all_dependencies(self):
  243. """
  244. Test the full dependency lists. This should exclude
  245. all of the files we created in setUp()
  246. """
  247. generator = DependencyList(self.implfile())
  248. root_list = generator.all_roots()
  249. dep_list = generator.all_dependencies()
  250. self.assertTrue(ROOTDIR in root_list)
  251. self.assertTrue("mi/base4.py" in dep_list)
  252. self.assertTrue("mi/base3.py" in dep_list)
  253. self.assertTrue("mi/base2.py" in dep_list)
  254. self.assertTrue("mi/foo/impl.py" in dep_list)
  255. self.assertTrue("mi/base.py" in dep_list)
  256. self.assertTrue("string.py" in dep_list)
  257. @unittest.skip('Skip until moved to MI repo')
  258. @attr('UNIT', group='mi')
  259. class TestDriverFileList(IDKPackageNose):
  260. """
  261. Test the driver file list object. The driver file list is what is
  262. stored in the driver egg
  263. """
  264. def test_extra_list(self):
  265. """
  266. Find all the files in the driver directory
  267. """
  268. rootdir = dirname(TESTDIR)
  269. filelist = DriverFileList(Metadata(), ROOTDIR, self.implfile(), self.nosefile())
  270. self.assertTrue(filelist)
  271. known_files = [
  272. '%s/res/test_file' % TESTDIR
  273. ]
  274. files = filelist._extra_files()
  275. log.debug(sorted(files))
  276. log.debug(sorted(known_files))
  277. self.assertEqual(sorted(files), sorted(known_files))
  278. def test_list(self):
  279. """
  280. Test the full file manifest
  281. """
  282. filelist = DriverFileList(Metadata(), ROOTDIR, self.implfile(), self.nosefile())
  283. self.assertTrue(filelist)
  284. known_files = [
  285. 'mi/__init__.py',
  286. 'mi/base.py',
  287. 'mi/base2.py',
  288. 'mi/base3.py',
  289. 'mi/base4.py',
  290. 'mi/foo/__init__.py',
  291. 'mi/foo/impl.py',
  292. 'mi/foo/res/test_file',
  293. 'mi/foo/test/__init__.py',
  294. 'mi/foo/test/test_process.py',
  295. ]
  296. files = filelist.files()
  297. log.debug( "F: %s" % files)
  298. self.assertEqual(sorted(files), sorted(known_files))
  299. pass