File: test_raft_controller.py

package info (click to toggle)
patroni 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,704 kB
  • sloc: python: 29,743; sh: 573; makefile: 29
file content (46 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import logging
import os
import unittest

from unittest.mock import Mock, patch

from pysyncobj import SyncObj

from patroni.config import Config
from patroni.raft_controller import main as _main, RaftController

from . import SleepException
from .test_raft import remove_files


class TestPatroniRaftController(unittest.TestCase):

    SELF_ADDR = '127.0.0.1:5360'

    def remove_files(self):
        remove_files(self.SELF_ADDR + '.')

    @patch('pysyncobj.tcp_server.TcpServer.bind', Mock())
    def setUp(self):
        self._handlers = logging.getLogger().handlers[:]
        self.remove_files()
        os.environ['PATRONI_RAFT_SELF_ADDR'] = self.SELF_ADDR
        config = Config('postgres0.yml', validator=None)
        self.rc = RaftController(config)

    def tearDown(self):
        logging.getLogger().handlers[:] = self._handlers
        self.remove_files()

    def test_reload_config(self):
        self.rc.reload_config()

    @patch('logging.Logger.error', Mock(side_effect=SleepException))
    @patch.object(SyncObj, 'doTick', Mock(side_effect=Exception))
    def test_run(self):
        self.assertRaises(SleepException, self.rc.run)
        self.rc.shutdown()

    @patch('sys.argv', ['patroni'])
    def test_patroni_raft_controller_main(self):
        self.assertRaises(SystemExit, _main)