/components/tools/OmeroPy/test/clitest/admin.py

https://github.com/aherbert/openmicroscopy · Python · 170 lines · 108 code · 37 blank · 25 comment · 7 complexity · 9ac5abf82d97fea3a7b2ba43e5aa7fb9 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. Test of the omero admin control.
  4. Copyright 2008 Glencoe Software, Inc. All rights reserved.
  5. Use is subject to license terms supplied in LICENSE.txt
  6. """
  7. import exceptions
  8. import unittest
  9. import os
  10. from path import path
  11. import omero
  12. import omero_ServerErrors_ice
  13. from omero.cli import CLI, NonZeroReturnCode
  14. from omero.plugins.admin import AdminControl
  15. from omero.plugins.prefs import PrefsControl
  16. from omero.util.temp_files import create_path
  17. from clitest.mocks import MockCLI
  18. omeroDir = path(os.getcwd()) / "build"
  19. class TestAdmin(unittest.TestCase):
  20. def setUp(self):
  21. # Non-temp directories
  22. build_dir = path() / "build"
  23. top_dir = path() / ".." / ".." / ".."
  24. etc_dir = top_dir / "etc"
  25. # Necessary fiels
  26. prefs_file = build_dir / "prefs.class"
  27. internal_cfg = etc_dir / "internal.cfg"
  28. master_cfg = etc_dir / "master.cfg"
  29. # Temp directories
  30. tmp_dir = create_path(folder=True)
  31. tmp_etc_dir = tmp_dir / "etc"
  32. tmp_grid_dir = tmp_etc_dir / "grid"
  33. tmp_lib_dir = tmp_dir / "lib"
  34. # Setup tmp dir
  35. [x.makedirs() for x in (tmp_grid_dir, tmp_lib_dir)]
  36. prefs_file.copy(tmp_lib_dir)
  37. master_cfg.copy(tmp_etc_dir)
  38. internal_cfg.copy(tmp_etc_dir)
  39. # Other setup
  40. self.cli = MockCLI()
  41. self.cli.dir = tmp_dir
  42. grid_dir = self.cli.dir / "etc" / "grid"
  43. self.cli.register("a", AdminControl, "TEST")
  44. self.cli.register("config", PrefsControl, "TEST")
  45. def tearDown(self):
  46. self.cli.tearDown()
  47. def invoke(self, string, fails=False):
  48. try:
  49. self.cli.invoke(string, strict=True)
  50. if fails: self.fail("Failed to fail")
  51. except:
  52. if not fails: raise
  53. def testMain(self):
  54. try:
  55. self.invoke("")
  56. except NonZeroReturnCode:
  57. # Command-loop not implemented
  58. pass
  59. #
  60. # Async first because simpler
  61. #
  62. def testStartAsync(self):
  63. self.cli.addCall(0)
  64. self.cli.checksIceVersion()
  65. self.cli.checksStatus(1) # I.e. not running
  66. self.invoke("a startasync")
  67. self.cli.assertCalled()
  68. self.cli.assertStderr(['No descriptor given. Using etc/grid/default.xml'])
  69. def testStopAsyncRunning(self):
  70. self.cli.addCall(0)
  71. self.invoke("a stopasync")
  72. self.cli.assertCalled()
  73. self.cli.assertStderr([])
  74. self.cli.assertStdout([])
  75. def testStopAsyncNotRunning(self):
  76. self.cli.addCall(1)
  77. self.invoke("a stopasync", fails=True)
  78. self.cli.assertCalled()
  79. self.cli.assertStderr([])
  80. self.cli.assertStdout(["Was the server already stopped?"])
  81. def testStop(self):
  82. self.invoke("a stop")
  83. def testComplete(self):
  84. c = AdminControl()
  85. t = ""
  86. l = "admin deploy "
  87. b = len(l)
  88. l = c._complete(t,l+"lib",b,b+3)
  89. self.assert_( "omero" in l, str(l) )
  90. l = c._complete(t,l+"lib/",b,b+4)
  91. self.assert_( "omero" in l, str(l) )
  92. def testProperMethodsUseConfigXml(self):
  93. self.fail("NYI")
  94. #
  95. # STATUS
  96. #
  97. def testStatusNodeFails(self):
  98. # Setup the call to bin/omero admin ice node
  99. popen = self.cli.createPopen()
  100. popen.wait().AndReturn(1)
  101. self.cli.mox.ReplayAll()
  102. self.assertRaises(NonZeroReturnCode, self.invoke, "a status")
  103. def testStatusSMFails(self):
  104. # Setup the call to bin/omero admin ice node
  105. popen = self.cli.createPopen()
  106. popen.wait().AndReturn(0)
  107. # Setup the call to session manager
  108. control = self.cli.controls["a"]
  109. control._intcfg = lambda: ""
  110. def sm(*args):
  111. raise exceptions.Exception("unknown")
  112. control.session_manager = sm
  113. self.cli.mox.ReplayAll()
  114. self.assertRaises(NonZeroReturnCode, self.invoke, "a status")
  115. def testStatusPasses(self):
  116. # Setup the call to bin/omero admin ice node
  117. popen = self.cli.createPopen()
  118. popen.wait().AndReturn(0)
  119. # Setup the call to session manager
  120. control = self.cli.controls["a"]
  121. control._intcfg = lambda: ""
  122. def sm(*args):
  123. class A(object):
  124. def create(self, *args): raise omero.WrappedCreateSessionException()
  125. return A()
  126. control.session_manager = sm
  127. self.cli.mox.ReplayAll()
  128. self.invoke("a status")
  129. self.assertEquals(0, self.cli.rv)
  130. if __name__ == '__main__':
  131. unittest.main()