Fixed API test cases failed due to wrong handling of default binary path.

This commit is contained in:
Akshay Joshi 2021-06-08 13:24:17 +05:30
parent e1067ffe67
commit 5a086a9173
4 changed files with 61 additions and 44 deletions

View File

@ -11,13 +11,13 @@ import os
import sys import sys
import json import json
import config import config
import subprocess
from flask import render_template from flask import render_template
from flask_babelex import gettext as _ from flask_babelex import gettext as _
from pgadmin.utils.preferences import Preferences from pgadmin.utils.preferences import Preferences
from werkzeug.exceptions import InternalServerError from werkzeug.exceptions import InternalServerError
from pgadmin.utils.constants import BINARY_PATHS, UTILITIES_ARRAY from pgadmin.utils.constants import BINARY_PATHS
from pgadmin.utils import set_default_binary_path
class ServerType(object): class ServerType(object):
@ -67,7 +67,7 @@ class ServerType(object):
default_bin_path = config.DEFAULT_BINARY_PATHS.get(key, "") default_bin_path = config.DEFAULT_BINARY_PATHS.get(key, "")
if default_bin_path != "": if default_bin_path != "":
cls.set_default_binary_path(default_bin_path, bin_paths, key) set_default_binary_path(default_bin_path, bin_paths, key)
if key == 'pg': if key == 'pg':
st.utility_path = paths.register( st.utility_path = paths.register(
'bin_paths', 'pg_bin_dir', 'bin_paths', 'pg_bin_dir',
@ -135,6 +135,9 @@ class ServerType(object):
) )
bin_path = self.get_utility_path(sversion) bin_path = self.get_utility_path(sversion)
if bin_path is None:
return None
if "$DIR" in bin_path: if "$DIR" in bin_path:
# When running as an WSGI application, we will not find the # When running as an WSGI application, we will not find the
# '__file__' attribute for the '__main__' module. # '__file__' attribute for the '__main__' module.
@ -172,43 +175,6 @@ class ServerType(object):
return default_path return default_path
@classmethod
def set_default_binary_path(cls, binary_path, bin_paths, server_type):
"""
This function is used to iterate through the utilities and set the
default binary path.
"""
for utility in UTILITIES_ARRAY:
full_path = os.path.abspath(
os.path.join(binary_path, (utility if os.name != 'nt' else
(utility + '.exe'))))
try:
# Get the output of the '--version' command
version_string = subprocess.getoutput(full_path + ' --version')
# Get the version number by splitting the result string
version_number = \
version_string.split(") ", 1)[1].split('.', 1)[0]
# Get the paths array based on server type
if 'pg_bin_paths' in bin_paths or 'as_bin_paths' in bin_paths:
paths_array = bin_paths['pg_bin_paths']
if server_type == 'ppas':
paths_array = bin_paths['as_bin_paths']
else:
paths_array = bin_paths
for path in paths_array:
if path['version'].find(version_number) == 0 and \
path['binaryPath'] is None:
path['binaryPath'] = binary_path
path['isDefault'] = True
break
break
except Exception:
continue
# Default Server Type # Default Server Type
ServerType('pg', _("PostgreSQL"), -1) ServerType('pg', _("PostgreSQL"), -1)

View File

@ -12,7 +12,7 @@ from ipaddress import ip_address
from pgadmin.utils.crypto import encrypt, decrypt from pgadmin.utils.crypto import encrypt, decrypt
import config import config
from pgadmin.model import db, Server, SharedServer from pgadmin.model import db, Server
def is_valid_ipaddress(address): def is_valid_ipaddress(address):

View File

@ -9,6 +9,7 @@
import os import os
import sys import sys
import subprocess
from collections import defaultdict from collections import defaultdict
from operator import attrgetter from operator import attrgetter
@ -19,7 +20,8 @@ from threading import Lock
from .paths import get_storage_directory from .paths import get_storage_directory
from .preferences import Preferences from .preferences import Preferences
from pgadmin.model import Server, SharedServer from pgadmin.model import Server
from pgadmin.utils.constants import UTILITIES_ARRAY
class PgAdminModule(Blueprint): class PgAdminModule(Blueprint):
@ -294,6 +296,43 @@ def get_server(sid):
return server return server
def set_default_binary_path(binary_path, bin_paths, server_type):
"""
This function is used to iterate through the utilities and set the
default binary path.
"""
for utility in UTILITIES_ARRAY:
full_path = os.path.abspath(
os.path.join(binary_path, (utility if os.name != 'nt' else
(utility + '.exe'))))
try:
# Get the output of the '--version' command
version_string = subprocess.getoutput(full_path + ' --version')
# Get the version number by splitting the result string
version_number = \
version_string.split(") ", 1)[1].split('.', 1)[0]
# Get the paths array based on server type
if 'pg_bin_paths' in bin_paths or 'as_bin_paths' in bin_paths:
paths_array = bin_paths['pg_bin_paths']
if server_type == 'ppas':
paths_array = bin_paths['as_bin_paths']
else:
paths_array = bin_paths
for path in paths_array:
if path['version'].find(version_number) == 0 and \
path['binaryPath'] is None:
path['binaryPath'] = binary_path
path['isDefault'] = True
break
break
except Exception:
continue
# Shortcut configuration for Accesskey # Shortcut configuration for Accesskey
ACCESSKEY_FIELDS = [ ACCESSKEY_FIELDS = [
{ {

View File

@ -36,6 +36,7 @@ from regression import test_setup
from pgadmin.utils.preferences import Preferences from pgadmin.utils.preferences import Preferences
from pgadmin.utils.constants import BINARY_PATHS from pgadmin.utils.constants import BINARY_PATHS
from pgadmin.utils import set_default_binary_path
from functools import wraps from functools import wraps
@ -757,6 +758,17 @@ def configure_preferences(default_binary_path=None):
paths_pref = Preferences.module('paths') paths_pref = Preferences.module('paths')
server_types = default_binary_path.keys() server_types = default_binary_path.keys()
for server in server_types: for server in server_types:
if server != 'ppas' and server != 'pg':
continue
# get the default binary paths array
bin_paths = BINARY_PATHS
# set the default binary paths based on server version
if server in default_binary_path and \
default_binary_path[server] != "":
set_default_binary_path(
default_binary_path[server], bin_paths, server)
pref_bin_path = paths_pref.preference('{0}_bin_dir'.format(server)) pref_bin_path = paths_pref.preference('{0}_bin_dir'.format(server))
user_pref = cur.execute( user_pref = cur.execute(
'SELECT pid, uid FROM user_preferences ' 'SELECT pid, uid FROM user_preferences '
@ -772,10 +784,10 @@ def configure_preferences(default_binary_path=None):
else: else:
if server == 'ppas': if server == 'ppas':
params = (pref_bin_path.pid, 1, params = (pref_bin_path.pid, 1,
json.dumps(BINARY_PATHS['as_bin_paths'])) json.dumps(bin_paths['as_bin_paths']))
else: else:
params = (pref_bin_path.pid, 1, params = (pref_bin_path.pid, 1,
json.dumps(BINARY_PATHS['pg_bin_paths'])) json.dumps(bin_paths['pg_bin_paths']))
cur.execute( cur.execute(
insert_preferences_query, params insert_preferences_query, params
) )