Esempio n. 1
0
  def test_daemonize_and_stop(self, exists_mock, _exit_mock, kill_mock, sleep_mock):
    oldpid = ProcessHelper.pidfile
    pid = str(os.getpid())
    _, tmpoutfile = tempfile.mkstemp()
    ProcessHelper.pidfile = tmpoutfile

    # Test daemonization
    main.daemonize()
    saved = open(ProcessHelper.pidfile, 'r').read()
    self.assertEqual(pid, saved)

    # Reuse pid file when testing agent stop
    # Testing normal exit
    exists_mock.return_value = False
    main.stop_agent()
    kill_mock.assert_called_with(int(pid), signal.SIGTERM)
    _exit_mock.assert_called_with(0)

    # Restore
    kill_mock.reset_mock()
    _exit_mock.reset_mock()

    # Testing exit when failed to remove pid file
    exists_mock.return_value = True
    main.stop_agent()
    kill_mock.assert_any_call(int(pid), signal.SIGTERM)
    kill_mock.assert_any_call(int(pid), signal.SIGKILL)
    _exit_mock.assert_called_with(1)

    # Restore
    ProcessHelper.pidfile = oldpid
    os.remove(tmpoutfile)
Esempio n. 2
0
  def test_daemonize_and_stop(self, exists_mock, sys_exit_mock, kill_mock, sleep_mock):
    oldpid = ProcessHelper.pidfile
    pid = str(os.getpid())
    _, tmpoutfile = tempfile.mkstemp()
    ProcessHelper.pidfile = tmpoutfile

    # Test daemonization
    main.daemonize()
    saved = open(ProcessHelper.pidfile, 'r').read()
    self.assertEqual(pid, saved)

    # Reuse pid file when testing agent stop
    # Testing normal exit
    exists_mock.return_value = False
    main.stop_agent()
    kill_mock.assert_called_with(['ambari-sudo.sh', 'kill', '-15', pid])
    sys_exit_mock.assert_called_with(0)

    # Restore
    kill_mock.reset_mock()
    sys_exit_mock.reset_mock()
    kill_mock.return_value = {'exitCode': 0, 'output': 'out', 'error': 'err'}

    # Testing exit when failed to remove pid file
    exists_mock.return_value = True
    main.stop_agent()
    kill_mock.assert_any_call(['ambari-sudo.sh', 'kill', '-15', pid])
    kill_mock.assert_any_call(['ambari-sudo.sh', 'kill', '-9', pid])
    sys_exit_mock.assert_called_with(1)

    # Restore
    ProcessHelper.pidfile = oldpid
    os.remove(tmpoutfile)
Esempio n. 3
0
  def test_daemonize_and_stop(self, exists_mock, _exit_mock, kill_mock, sleep_mock):
    oldpid = ProcessHelper.pidfile
    pid = str(os.getpid())
    _, tmpoutfile = tempfile.mkstemp()
    ProcessHelper.pidfile = tmpoutfile

    # Test daemonization
    main.daemonize()
    saved = open(ProcessHelper.pidfile, 'r').read()
    self.assertEqual(pid, saved)

    # Reuse pid file when testing agent stop
    # Testing normal exit
    exists_mock.return_value = False
    main.stop_agent()
    kill_mock.assert_called_with(int(pid), signal.SIGTERM)
    _exit_mock.assert_called_with(0)

    # Restore
    kill_mock.reset_mock()
    _exit_mock.reset_mock()

    # Testing exit when failed to remove pid file
    exists_mock.return_value = True
    main.stop_agent()
    kill_mock.assert_any_call(int(pid), signal.SIGTERM)
    kill_mock.assert_any_call(int(pid), signal.SIGKILL)
    _exit_mock.assert_called_with(1)

    # Restore
    ProcessHelper.pidfile = oldpid
    os.remove(tmpoutfile)
Esempio n. 4
0
  def test_daemonize_and_stop(self, exists_mock, sleep_mock):
    from ambari_commons.shell import shellRunnerLinux

    oldpid = ProcessHelper.pidfile
    pid = str(os.getpid())
    _, tmpoutfile = tempfile.mkstemp()
    ProcessHelper.pidfile = tmpoutfile

    # Test daemonization
    main.daemonize()
    saved = open(ProcessHelper.pidfile, 'r').read()
    self.assertEqual(pid, saved)

    main.GRACEFUL_STOP_TRIES = 1
    with patch("ambari_commons.shell.shellRunnerLinux.run") as kill_mock:
      # Reuse pid file when testing agent stop
      # Testing normal exit
      exists_mock.return_value = False
      kill_mock.side_effect = [{'exitCode': 0, 'output': '', 'error': ''},
                               {'exitCode': 1, 'output': '', 'error': ''}]
      try:
        main.stop_agent()
        raise Exception("main.stop_agent() should raise sys.exit(0).")
      except SystemExit as e:
        self.assertEquals(0, e.code);

      kill_mock.assert_has_calls([call(['ambari-sudo.sh', 'kill', '-15', pid]),
                                 call(['ambari-sudo.sh', 'kill', '-0', pid])])

      # Restore
      kill_mock.reset_mock()
      kill_mock.side_effect = [{'exitCode': 0, 'output': '', 'error': ''},
                               {'exitCode': 0, 'output': '', 'error': ''},
                               {'exitCode': 0, 'output': '', 'error': ''}]

      # Testing exit when failed to remove pid file
      exists_mock.return_value = True
      try:
        main.stop_agent()
        raise Exception("main.stop_agent() should raise sys.exit(0).")
      except SystemExit as e:
        self.assertEquals(0, e.code);

      kill_mock.assert_has_calls([call(['ambari-sudo.sh', 'kill', '-15', pid]),
                                  call(['ambari-sudo.sh', 'kill', '-0', pid]),
                                  call(['ambari-sudo.sh', 'kill', '-9', pid])])

    # Restore
    ProcessHelper.pidfile = oldpid
    os.remove(tmpoutfile)
Esempio n. 5
0
    def test_daemonize_and_stop(self, exists_mock, sys_exit_mock, sleep_mock):
        from ambari_commons.shell import shellRunnerLinux

        oldpid = ProcessHelper.pidfile
        pid = str(os.getpid())
        _, tmpoutfile = tempfile.mkstemp()
        ProcessHelper.pidfile = tmpoutfile

        # Test daemonization
        main.daemonize()
        saved = open(ProcessHelper.pidfile, 'r').read()
        self.assertEqual(pid, saved)

        with patch("ambari_commons.shell.shellRunnerLinux.run") as kill_mock:
            # Reuse pid file when testing agent stop
            # Testing normal exit
            exists_mock.return_value = False
            main.stop_agent()
            kill_mock.assert_called_with(
                ['ambari-sudo.sh', 'kill', '-15', pid])
            sys_exit_mock.assert_called_with(0)

            # Restore
            kill_mock.reset_mock()
            sys_exit_mock.reset_mock()
            kill_mock.return_value = {
                'exitCode': 0,
                'output': 'out',
                'error': 'err'
            }

            # Testing exit when failed to remove pid file
            exists_mock.return_value = True
            main.stop_agent()
            kill_mock.assert_any_call(['ambari-sudo.sh', 'kill', '-15', pid])
            kill_mock.assert_any_call(['ambari-sudo.sh', 'kill', '-9', pid])
            sys_exit_mock.assert_called_with(1)

        # Restore
        ProcessHelper.pidfile = oldpid
        os.remove(tmpoutfile)
Esempio n. 6
0
    def test_daemonize_and_stop(self, exists_mock, _exit_mock, kill_mock,
                                sleep_mock):
        oldpid = ProcessHelper.pidfile
        pid = str(os.getpid())
        _, tmpoutfile = tempfile.mkstemp()
        ProcessHelper.pidfile = tmpoutfile

        # Test daemonization
        main.daemonize()
        saved = open(ProcessHelper.pidfile, 'r').read()
        self.assertEqual(pid, saved)

        # Reuse pid file when testing agent stop
        # Testing normal exit
        exists_mock.return_value = False
        main.stop_agent()
        kill_mock.assert_called_with(['sudo', 'kill', '-15', pid])
        _exit_mock.assert_called_with(0)

        # Restore
        kill_mock.reset_mock()
        _exit_mock.reset_mock()
        kill_mock.return_value = {
            'exitCode': 0,
            'output': 'out',
            'error': 'err'
        }

        # Testing exit when failed to remove pid file
        exists_mock.return_value = True
        main.stop_agent()
        kill_mock.assert_any_call(['sudo', 'kill', '-15', pid])
        kill_mock.assert_any_call(['sudo', 'kill', '-9', pid])
        _exit_mock.assert_called_with(1)

        # Restore
        ProcessHelper.pidfile = oldpid
        os.remove(tmpoutfile)