Exemple #1
0
class HostTests(unittest.TestCase):
    def setUp(self):
        self.hostName = 'Fake host'
        self.vmName = 'Marc'
        self.host = Host(self.hostName,
                         [Vm(self.vmName, powerState='poweredOff')])

    def test_hostname(self):
        self.failIf(self.host.hostname != self.hostName)

    def test_Connect(self):
        self.host.Connect()

    def test_GetVm_valid(self):
        vm = self.host.GetVm(self.vmName)

        self.failIf(not isinstance(vm, Vm))
        self.failIf(vm.host != self.host)

    def test_GetVm_nonexistent(self):
        self.failUnlessRaises(pyVmomi.vim.fault.NotFound, self.host.GetVm,
                              'unknownVm')

    def test_GetVm_None(self):
        self.failUnlessRaises(pyVmomi.vim.fault.NotFound, self.host.GetVm,
                              None)

    def test_GetVms(self):
        vms = self.host.GetVms()

        self.failIf(not isinstance(vms, list))
        self.failIf(vms[0] != self.host.GetVm(self.vmName))
Exemple #2
0
class CommandProcessorTests(unittest.TestCase):

   redirect_stdout = True
   redirect_stderr = True

   def setUp(self):
      self.host = Host('fakehost',
                       vmList=[Vm(name='FakeVm',
                                  powerState='poweredOn')])

      ## Capture what the SUT is writing to stdout and stderr so that
      ## we can verify it later.
      if self.redirect_stdout:
         sys.stdout = FileWriteCapture(sys.stdout).StartCapture()
      if self.redirect_stderr:
         sys.stderr = FileWriteCapture(sys.stderr).StartCapture()

   def tearDown(self):
      ## Revert to the normal sys.stdout and sys.stderr
      if self.redirect_stderr:
         sys.stderr = sys.stderr.EndCapture()
      if self.redirect_stdout:
         sys.stdout = sys.stdout.EndCapture()

   def test_004_Process_noOptionsAndNoArgs(self):
      """
      Call Process with no options and no args and verify that an
      exception is raised.
      """

      self.assertRaises(
         CommandProcessor.InvalidOperation,
         CommandProcessor.Process, self.host, [])

   def test_004_Process_vmoperation(self):
      """
      Test invoking an operation that returns vm powerState.
      """

      result = CommandProcessor.Process(
         host=self.host,
         args=['vmoperation', 'FakeVm'])

      self.assertEqual(result[0], 'vmoperation')
      self.assertEqual(result[1], self.host.GetVm('FakeVm').powerState)

   def test_004_Process_returnarg(self):
      """
      Test invoking an operation that returns the arg passed to it.
      """

      result = CommandProcessor.Process(
         host=self.host,
         args=['returnarg', 'foo'])

      self.assertEqual(result, ('returnarg', 'foo'))

   def test_004_Process_raiseexception(self):
      """
      Invoke an operation that raises an exception and verify that an
      error message with the exception message gets written to
      sys.stderr
      """

      ## Raise the exception that we expect CommandProcessor.Process
      ## to raise in a bit, so that we know what the exception message
      ## looks like and don't have to duplicate it here in the test
      ## and make the test fragile.
      try:
         RaiseException().DoIt(None)
      except vim.fault.InvalidState, e:
         exceptionMsg = e.msg

      CommandProcessor.Process(host=self.host, args=['raiseexception'])

      if hasattr(sys.stderr, 'getvalue'):
         self.assertEqual(sys.stderr.getvalue(),
                          'vim.fault.InvalidState: %s\n' % exceptionMsg)