Example #1
0
  def testRunCmdSucceeds(self):
    lockfile = utils.PathJoin(self.tmpdir, "lock")

    def fn(args, env=NotImplemented, reset_env=NotImplemented,
           postfork_fn=NotImplemented):
      self.assertEqual(args, [utils.PathJoin(self.tmpdir, "test5667")])
      self.assertEqual(env, {})
      self.assertTrue(reset_env)

      # Call back to release lock
      postfork_fn(NotImplemented)

      # Simulate a successful command
      return utils.RunResult(constants.EXIT_SUCCESS, None, "stdout14463", "",
                             utils.ShellQuoteArgs(args),
                             NotImplemented, NotImplemented)

    sleep_fn = testutils.CallCounter(_SleepForRestrictedCmd)
    prepare_fn = testutils.CallCounter(self._SuccessfulPrepare)
    runcmd_fn = testutils.CallCounter(fn)

    result = backend.RunRestrictedCmd("test5667",
                                      _lock_timeout=1.0, _lock_file=lockfile,
                                      _path=self.tmpdir, _runcmd_fn=runcmd_fn,
                                      _sleep_fn=sleep_fn,
                                      _prepare_fn=prepare_fn,
                                      _enabled=True)
    self.assertEqual(result, "stdout14463")

    self.assertEqual(sleep_fn.Count(), 0)
    self.assertEqual(prepare_fn.Count(), 1)
    self.assertEqual(runcmd_fn.Count(), 1)
Example #2
0
    def testRunCmdFails(self):
        lockfile = utils.PathJoin(self.tmpdir, "lock")

        def fn(args,
               env=NotImplemented,
               reset_env=NotImplemented,
               postfork_fn=NotImplemented):
            self.assertEqual(args, [utils.PathJoin(self.tmpdir, "test3079")])
            self.assertEqual(env, {})
            self.assertTrue(reset_env)
            self.assertTrue(callable(postfork_fn))

            trylock = utils.FileLock.Open(lockfile)
            try:
                # See if lockfile is still held
                self.assertRaises(EnvironmentError,
                                  trylock.Exclusive,
                                  blocking=False)

                # Call back to release lock
                postfork_fn(NotImplemented)

                # See if lockfile can be acquired
                trylock.Exclusive(blocking=False)
            finally:
                trylock.Close()

            # Simulate a failed command
            return utils.RunResult(constants.EXIT_FAILURE, None,
                                   "stdout", "stderr406328567",
                                   utils.ShellQuoteArgs(args), NotImplemented,
                                   NotImplemented)

        sleep_fn = testutils.CallCounter(_SleepForRestrictedCmd)
        prepare_fn = testutils.CallCounter(self._SuccessfulPrepare)
        runcmd_fn = testutils.CallCounter(fn)

        try:
            backend.RunRestrictedCmd("test3079",
                                     _lock_timeout=1.0,
                                     _lock_file=lockfile,
                                     _path=self.tmpdir,
                                     _runcmd_fn=runcmd_fn,
                                     _sleep_fn=sleep_fn,
                                     _prepare_fn=prepare_fn,
                                     _enabled=True)
        except backend.RPCFail, err:
            self.assertTrue(
                str(err).startswith("Restricted command 'test3079'"
                                    " failed:"))
            self.assertTrue("stderr406328567" in str(err),
                            msg="Error did not include output")
Example #3
0
  def testPrepareFails(self):
    lockfile = utils.PathJoin(self.tmpdir, "lock")

    sleep_fn = testutils.CallCounter(_SleepForRestrictedCmd)
    prepare_fn = testutils.CallCounter(self._PrepareFails)

    try:
      backend.RunRestrictedCmd("test29327",
                               _lock_timeout=1.0, _lock_file=lockfile,
                               _path=NotImplemented, _runcmd_fn=NotImplemented,
                               _sleep_fn=sleep_fn, _prepare_fn=prepare_fn,
                               _enabled=True)
    except backend.RPCFail, err:
      self.assertEqual(str(err), _GenericRestrictedCmdError("test29327"))
Example #4
0
 def testTimeout(self):
   fn = testutils.CallCounter(self._Fail)
   try:
     hv_xen._GetRunningInstanceList(fn, False, delays=(0.02, 1.0, 0.03),
                                    timeout=0.1)
   except errors.HypervisorError, err:
     self.assertTrue("timeout exceeded" in str(err))
Example #5
0
    def testMigrateInstance(self):
        clustername = "cluster.example.com"
        instname = "server01.example.com"
        target = constants.IP4_ADDRESS_LOCALHOST
        port = 22364

        hvparams = {constants.HV_XEN_CMD: self.CMD}

        for live in [False, True]:
            for fail in [False, True]:
                ping_fn = \
                  testutils.CallCounter(compat.partial(self._FakeTcpPing,
                                                       (target, port), True))

                run_cmd = \
                  compat.partial(self._MigrateInstanceCmd,
                                 clustername, instname, target, port, live,
                                 fail)

                hv = self._GetHv(run_cmd=run_cmd)

                if fail:
                    try:
                        hv._MigrateInstance(clustername,
                                            instname,
                                            target,
                                            port,
                                            live,
                                            hvparams,
                                            _ping_fn=ping_fn)
                    except errors.HypervisorError, err:
                        self.assertTrue(
                            str(err).startswith("Failed to migrate instance"))
                    else:
                        self.fail("Exception was not raised")
                else:
                    hv._MigrateInstance(clustername,
                                        instname,
                                        target,
                                        port,
                                        live,
                                        hvparams,
                                        _ping_fn=ping_fn)

                if self.CMD == constants.XEN_CMD_XM:
                    expected_pings = 1
                else:
                    expected_pings = 0

                self.assertEqual(ping_fn.Count(), expected_pings)
Example #6
0
 def testNonExistantLockDirectory(self):
   lockfile = utils.PathJoin(self.tmpdir, "does", "not", "exist")
   sleep_fn = testutils.CallCounter(_SleepForRestrictedCmd)
   self.assertFalse(os.path.exists(lockfile))
   self.assertRaises(backend.RPCFail,
                     backend.RunRestrictedCmd, "test",
                     _lock_timeout=NotImplemented,
                     _lock_file=lockfile,
                     _path=NotImplemented,
                     _sleep_fn=sleep_fn,
                     _prepare_fn=NotImplemented,
                     _runcmd_fn=NotImplemented,
                     _enabled=True)
   self.assertEqual(sleep_fn.Count(), 1)
Example #7
0
    def testTimeout(self):
        fn = testutils.CallCounter(self._Fail)
        try:
            hv_xen._GetRunningInstanceList(fn,
                                           False,
                                           delays=(0.02, 1.0, 0.03),
                                           timeout=0.1)
        except errors.HypervisorError as err:
            self.assertTrue("timeout exceeded" in str(err))
        else:
            self.fail("Exception was not raised")

        self.assertTrue(fn.Count() < 10,
                        msg="'xm list' was called too many times")
Example #8
0
  def testOmitCrashed(self):
    data = testutils.ReadTestData("xen-xl-list-4.4-crashed-instances.txt")

    fn = testutils.CallCounter(compat.partial(self._Success, data))

    result = hv_xen._GetAllInstanceList(fn, True, delays=(0.02, 1.0, 0.03),
                                        timeout=0.1)

    self.assertEqual(len(result), 2)

    self.assertEqual(map(compat.fst, result), [
      "Domain-0",
      "server01.example.com",
      ])

    self.assertEqual(fn.Count(), 1)
Example #9
0
    def testSuccess(self):
        data = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")

        fn = testutils.CallCounter(compat.partial(self._Success, data))

        result = hv_xen._GetRunningInstanceList(fn, True, _timeout=0.1)

        self.assertEqual(len(result), 4)

        self.assertEqual(map(compat.fst, result), [
            "Domain-0",
            "server01.example.com",
            "web3106215069.example.com",
            "testinstance.example.com",
        ])

        self.assertEqual(fn.Count(), 1)
Example #10
0
  def _TryLock(lockfile):
    sleep_fn = testutils.CallCounter(_SleepForRestrictedCmd)

    result = False
    try:
      backend.RunRestrictedCmd("test22717",
                               _lock_timeout=0.1,
                               _lock_file=lockfile,
                               _path=NotImplemented,
                               _sleep_fn=sleep_fn,
                               _prepare_fn=NotImplemented,
                               _runcmd_fn=NotImplemented,
                               _enabled=True)
    except backend.RPCFail, err:
      assert str(err) == _GenericRestrictedCmdError("test22717"), \
             "Did not fail with generic error message"
      result = True