Improve unit-tests (#1479)

* tests were failing on windows and macos
* improve coverage
This commit is contained in:
Alexander Kukushkin
2020-04-09 10:34:35 +02:00
committed by GitHub
parent 369a93ce2a
commit 27cda08ece
5 changed files with 32 additions and 15 deletions
+2 -2
View File
@@ -110,8 +110,8 @@ def validate_data_dir(data_dir):
bin_dir = schema.data.get("postgresql", {}).get("bin_dir", None) bin_dir = schema.data.get("postgresql", {}).get("bin_dir", None)
major_version = get_major_version(bin_dir) major_version = get_major_version(bin_dir)
if pgversion != major_version: if pgversion != major_version:
raise ConfigParseError("data_dir directory postgresql version ({}) doesn't match" raise ConfigParseError("data_dir directory postgresql version ({}) doesn't match with "
"with 'postgres --version' output ({})".format(pgversion, major_version)) "'postgres --version' output ({})".format(pgversion, major_version))
return True return True
+11 -9
View File
@@ -7,7 +7,7 @@ from datetime import datetime, timedelta
from mock import patch, Mock from mock import patch, Mock
from patroni.ctl import ctl, store_config, load_config, output_members, get_dcs, parse_dcs, \ from patroni.ctl import ctl, store_config, load_config, output_members, get_dcs, parse_dcs, \
get_all_members, get_any_member, get_cursor, query_member, configure, PatroniCtlException, apply_config_changes, \ get_all_members, get_any_member, get_cursor, query_member, configure, PatroniCtlException, apply_config_changes, \
format_config_for_editing, show_diff, invoke_editor, format_pg_version, find_executable format_config_for_editing, show_diff, invoke_editor, format_pg_version, find_executable, CONFIG_FILE_PATH
from patroni.dcs.etcd import Client, Failover from patroni.dcs.etcd import Client, Failover
from patroni.utils import tzutc from patroni.utils import tzutc
from psycopg2 import OperationalError from psycopg2 import OperationalError
@@ -18,16 +18,18 @@ from .test_etcd import etcd_read, socket_getaddrinfo
from .test_ha import get_cluster_initialized_without_leader, get_cluster_initialized_with_leader, \ from .test_ha import get_cluster_initialized_without_leader, get_cluster_initialized_with_leader, \
get_cluster_initialized_with_only_leader, get_cluster_not_initialized_without_leader, get_cluster, Member get_cluster_initialized_with_only_leader, get_cluster_not_initialized_without_leader, get_cluster, Member
CONFIG_FILE_PATH = './test-ctl.yaml'
def test_rw_config(): def test_rw_config():
global CONFIG_FILE_PATH
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
store_config({'etcd': {'host': 'localhost:2379'}}, CONFIG_FILE_PATH + '/dummy') load_config(CONFIG_FILE_PATH, None)
load_config(CONFIG_FILE_PATH + '/dummy', '0.0.0.0') CONFIG_PATH = './test-ctl.yaml'
os.remove(CONFIG_FILE_PATH + '/dummy') store_config({'etcd': {'host': 'localhost:2379'}}, CONFIG_PATH + '/dummy')
os.rmdir(CONFIG_FILE_PATH) load_config(CONFIG_PATH + '/dummy', '0.0.0.0')
os.remove(CONFIG_PATH + '/dummy')
os.rmdir(CONFIG_PATH)
@patch('patroni.ctl.load_config', @patch('patroni.ctl.load_config',
@@ -42,11 +44,11 @@ class TestCtl(unittest.TestCase):
self.runner = CliRunner() self.runner = CliRunner()
self.e = get_dcs({'etcd': {'ttl': 30, 'host': 'ok:2379', 'retry_timeout': 10}}, 'foo') self.e = get_dcs({'etcd': {'ttl': 30, 'host': 'ok:2379', 'retry_timeout': 10}}, 'foo')
def test_abort_on_missing_or_unaccessible_config(self): def test_load_config(self):
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
with self.assertRaises(PatroniCtlException): self.assertRaises(PatroniCtlException, load_config, './non-existing-config-file', None)
load_config('./non-existing-config-file', None) self.assertRaises(PatroniCtlException, load_config, './non-existing-config-file', None)
@patch('psycopg2.connect', psycopg2_connect) @patch('psycopg2.connect', psycopg2_connect)
def test_get_cursor(self): def test_get_cursor(self):
+9 -1
View File
@@ -33,13 +33,18 @@ def mock_config_map(*args, **kwargs):
mock.metadata.resource_version = '2' mock.metadata.resource_version = '2'
return mock return mock
@patch('socket.TCP_KEEPIDLE', 4, create=True)
@patch('socket.TCP_KEEPINTVL', 5, create=True)
@patch('socket.TCP_KEEPCNT', 6, create=True)
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_config_map', mock_config_map) @patch.object(k8s_client.CoreV1Api, 'patch_namespaced_config_map', mock_config_map)
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_config_map', mock_config_map) @patch.object(k8s_client.CoreV1Api, 'create_namespaced_config_map', mock_config_map)
@patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True) @patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True)
@patch.object(Thread, 'start', Mock()) @patch.object(Thread, 'start', Mock())
class TestKubernetes(unittest.TestCase): class TestKubernetes(unittest.TestCase):
@patch('socket.TCP_KEEPIDLE', 4, create=True)
@patch('socket.TCP_KEEPINTVL', 5, create=True)
@patch('socket.TCP_KEEPCNT', 6, create=True)
@patch('kubernetes.config.load_kube_config', Mock()) @patch('kubernetes.config.load_kube_config', Mock())
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map) @patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map)
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_pod', mock_list_namespaced_pod) @patch.object(k8s_client.CoreV1Api, 'list_namespaced_pod', mock_list_namespaced_pod)
@@ -149,6 +154,9 @@ class TestKubernetes(unittest.TestCase):
class TestCacheBuilder(unittest.TestCase): class TestCacheBuilder(unittest.TestCase):
@patch('socket.TCP_KEEPIDLE', 4, create=True)
@patch('socket.TCP_KEEPINTVL', 5, create=True)
@patch('socket.TCP_KEEPCNT', 6, create=True)
@patch('kubernetes.config.load_kube_config', Mock()) @patch('kubernetes.config.load_kube_config', Mock())
@patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True) @patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True)
@patch.object(Thread, 'start', Mock()) @patch.object(Thread, 'start', Mock())
+5 -1
View File
@@ -44,7 +44,11 @@ class TestPatroni(unittest.TestCase):
def test_no_config(self): def test_no_config(self):
self.assertRaises(SystemExit, patroni_main) self.assertRaises(SystemExit, patroni_main)
@patch('pkgutil.get_importer', Mock(return_value=MockFrozenImporter())) @patch('sys.argv', ['patroni.py', '--validate-config', 'postgres0.yml'])
def test_validate_config(self):
self.assertRaises(SystemExit, patroni_main)
@patch('pkgutil.iter_importers', Mock(return_value=[MockFrozenImporter()]))
@patch('sys.frozen', Mock(return_value=True), create=True) @patch('sys.frozen', Mock(return_value=True), create=True)
@patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock()) @patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock())
@patch.object(etcd.Client, 'read', etcd_read) @patch.object(etcd.Client, 'read', etcd_read)
+5 -2
View File
@@ -1,6 +1,7 @@
import copy import copy
import os import os
import socket import socket
import tempfile
import unittest import unittest
from mock import Mock, patch, mock_open from mock import Mock, patch, mock_open
@@ -57,8 +58,8 @@ config = {
"superuser": {"username": "user"}, "superuser": {"username": "user"},
"rewind": {"username": "user"}, "rewind": {"username": "user"},
}, },
"data_dir": "/tmp/data_dir", "data_dir": os.path.join(tempfile.gettempdir(), "data_dir"),
"bin_dir": "/tmp/bin_dir", "bin_dir": os.path.join(tempfile.gettempdir(), "bin_dir"),
"parameters": { "parameters": {
"unix_socket_directories": "." "unix_socket_directories": "."
}, },
@@ -84,6 +85,8 @@ files = []
def isfile_side_effect(arg): def isfile_side_effect(arg):
if arg.endswith('.exe'):
arg = arg[:-4]
return arg in files return arg in files