Skip to content

Commit

Permalink
Python3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rsiera committed Dec 5, 2016
1 parent 1afd06c commit b83d197
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 30 deletions.
6 changes: 5 additions & 1 deletion pg_view/models/collector_partition.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import os
import time
from multiprocessing import Process

import os
import psutil
import sys

from pg_view.consts import RD, TICK_LENGTH, SECTOR_SIZE
from pg_view.formatters import StatusFormatter, FnFormatter
from pg_view.models.collector_base import BaseStatCollector, logger
from pg_view.models.displayers import COLALIGN

if sys.hexversion >= 0x03000000:
long = int


class PartitionStatCollector(BaseStatCollector):
"""Collect statistics about PostgreSQL partitions """
Expand Down
4 changes: 2 additions & 2 deletions pg_view/models/collector_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

def dbversion_as_float(server_version):
version_num = server_version
version_num /= 100
return float('{0}.{1}'.format(version_num / 100, version_num % 100))
version_num //= 100
return float('{0}.{1}'.format(version_num // 100, version_num % 100))


def process_sort_key(process):
Expand Down
4 changes: 2 additions & 2 deletions pg_view/models/collector_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ def get_missing_cpu_stat_from_file(self):
missing_data = {}
with open_binary('%s/stat' % get_procfs_path()) as f:
for line in f:
name, args, value = line.strip().partition(' ')
if name.startswith('procs_'):
name, args, value = line.strip().partition(b' ')
if name.startswith(b'procs_'):
missing_data[name] = int(value)
return missing_data

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def read_module(path):
data = {}
with open(path, 'r') as fd:
exec (fd.read(), data)
exec(fd.read(), data)
return data


Expand Down
5 changes: 4 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from StringIO import StringIO
try:
from io import StringIO
except ImportError:
from StringIO import StringIO

import os

Expand Down
25 changes: 11 additions & 14 deletions tests/test_db_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DbClientUtilsTest(TestCase):
@mock.patch('pg_view.models.clients.logger')
@mock.patch('__builtin__.open')
@mock.patch('pg_view.models.clients.open', create=True)
def test_read_postmaster_pid_should_return_none_when_error(self, mocked_open, mocked_logger):
mocked_open.side_effect = Exception
data = read_postmaster_pid('/var/lib/postgresql/9.3/main', 'default')
Expand All @@ -20,7 +20,7 @@ def test_read_postmaster_pid_should_return_none_when_error(self, mocked_open, mo
expected_msg.format(name='default', wd='/var/lib/postgresql/9.3/main'))

@mock.patch('pg_view.models.clients.logger')
@mock.patch('__builtin__.open')
@mock.patch('pg_view.models.clients.open', create=True)
def test_read_postmaster_pid_should_return_none_when_error_strip(self, mocked_open, mocked_logger):
mocked_open.return_value.readline.return_value = []
data = read_postmaster_pid('/var/lib/postgresql/9.3/main', 'default')
Expand All @@ -29,7 +29,7 @@ def test_read_postmaster_pid_should_return_none_when_error_strip(self, mocked_op
mocked_logger.error.assert_called_with(
expected_msg.format(name='default', wd='/var/lib/postgresql/9.3/main'))

@mock.patch('__builtin__.open')
@mock.patch('pg_view.models.clients.open', create=True)
def test_read_postmaster_pid_should_return_pid_when_read_file(self, mocked_open):
mocked_open.return_value.readline.return_value = '123 '
data = read_postmaster_pid('/var/lib/postgresql/9.3/main', 'default')
Expand Down Expand Up @@ -79,14 +79,12 @@ def test_detect_db_connection_arguments_should_return_none_when_not_pickable_con
mocked_detect_with_proc_net,
mocked_logger):
finder = DBConnectionFinder('workdir', 1049, '9.3', 'username', 'atlas')
mocked_proc_worker.return_value.detect_with_postmaster_pid.return_value = {
'unix_wrong': [('/var/run/postgresql', '5432')],
'tcp_wrong': [('localhost', '5432')]
}
conn_params = {'unix_wrong': [('/var/run/postgresql', '5432')], 'tcp_wrong': [('localhost', '5432')]}
mocked_proc_worker.return_value.detect_with_postmaster_pid.return_value = conn_params
conn_args = finder.detect_db_connection_arguments()
self.assertIsNone(conn_args)
expected_msg = "unable to connect to PostgreSQL cluster at workdir using any of the detected connection " \
"options: {'unix_wrong': [('/var/run/postgresql', '5432')], 'tcp_wrong': [('localhost', '5432')]}"
"options: {0}".format(conn_params)
mocked_logger.error.assert_called_with(expected_msg)

@mock.patch('pg_view.models.clients.DBConnectionFinder.detect_with_proc_net', return_value=None)
Expand Down Expand Up @@ -217,11 +215,9 @@ def test_establish_user_defined_connection_should_raise_error_when_cant_connect(
with self.assertRaises(NotConnectedError):
client.establish_user_defined_connection('instance', [])

mocked_logger.error.assert_has_calls([
mock.call(
"failed to establish connection to instance via {'host': 'localhost', 'database': 'db', 'port': '5432', 'user': 'user'}"),
mock.call('PostgreSQL exception: ')]
)
expected_msg = "failed to establish connection to instance via {0}".format(
client.connection_builder.build_connection())
mocked_logger.error.assert_has_calls([mock.call(expected_msg), mock.call('PostgreSQL exception: ')])

@mock.patch('pg_view.models.clients.logger')
@mock.patch('pg_view.models.clients.psycopg2')
Expand All @@ -238,7 +234,8 @@ def test_establish_user_defined_connection_should_raise_error_when_not_pid_postm
with self.assertRaises(NoPidConnectionError):
client.establish_user_defined_connection('default', [])

expected_msg = "failed to read pid of the postmaster on {'host': 'localhost', 'database': 'db', 'port': '5432', 'user': 'user'}"
expected_msg = "failed to read pid of the postmaster on {0}".format(
client.connection_builder.build_connection())
mocked_logger.error.assert_called_once_with(expected_msg)

@mock.patch('pg_view.models.clients.logger')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def test__calculate_kb_left_until_limit_should_log_warn_when_non_optional_and_no

@unittest.skipUnless(psutil.LINUX, "Linux only")
@mock.patch('pg_view.models.collector_system.psutil._pslinux.open_binary')
def test_get_missing_cpu_stat_from_file_should_parse_data_from_proc_stat(self, mocked_open):
def test_get_missing_memory_stat_from_file_should_parse_data_from_proc_stat(self, mocked_open):
cpu_info_ok = os.path.join(TEST_DIR, 'proc_files', 'meminfo_ok')
mocked_open.return_value = open(cpu_info_ok, "rU")
mocked_open.return_value = open_universal(cpu_info_ok)
refreshed_data = self.collector.get_missing_memory_stat_from_file()
expected_data = {
'CommitLimit:': 250852 * KB_IN_MB, 'Committed_AS:': 329264 * KB_IN_MB, 'Dirty:': 36 * KB_IN_MB}
Expand Down
10 changes: 5 additions & 5 deletions tests/test_models_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test__read_proc_should_return_data_when_process_ok(self, mocked_psutil_proce

'guest_time': 0.0,
'starttime': datetime.datetime.fromtimestamp(1480777289.0),
'delayacct_blkio_ticks': 0L,
'delayacct_blkio_ticks': 0,
'cmdline': 'backend'
}
self.assertEqual(expected_proc_stats, proc_stats)
Expand Down Expand Up @@ -285,8 +285,8 @@ def test_refresh_should_return_results_when_ok(self, mocked__do_refresh, mocked_
'priority': 10,
'vsize': 252428288,
'guest_time': 0.0,
'starttime': 911L,
'delayacct_blkio_ticks': 1L,
'starttime': 911,
'delayacct_blkio_ticks': 1,
'cmdline': 'backend'
}

Expand All @@ -310,14 +310,14 @@ def test_refresh_should_return_results_when_ok(self, mocked__do_refresh, mocked_
'status': 'status',
'write_bytes': 1,
'vsize': 252428288,
'delayacct_blkio_ticks': 1L,
'delayacct_blkio_ticks': 1,
'pid': 1049,
'priority': 10,
'cmdline': 'backend',
'read_bytes': 655,
'uss': 10,
'stime': 0.0001,
'starttime': 911L,
'starttime': 911,
'utime': 0.0002,
'type': 'unknown',
'guest_time': 0.0,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def test_refresh_should_call_helpers_with_proper_data(self, mocked_read_cpu_time
@mock.patch('pg_view.models.collector_system.psutil._pslinux.open_binary')
def test_get_missing_cpu_stat_from_file_should_parse_data_from_proc_stat(self, mocked_open):
cpu_info_ok = os.path.join(TEST_DIR, 'proc_files', 'cpu_info_ok')
mocked_open.return_value = open(cpu_info_ok, "rU")
mocked_open.return_value = open(cpu_info_ok, "rb")
refreshed_data = self.collector.get_missing_cpu_stat_from_file()
self.assertEqual({'procs_blocked': 0, 'procs_running': 1}, refreshed_data)
self.assertEqual({b'procs_blocked': 0, b'procs_running': 1}, refreshed_data)

@mock.patch('pg_view.models.collector_system.psutil.cpu_times')
def test_read_cpu_data_should_transform_input_when_cpu_times_for_linux(self, mocked_cpu_times):
Expand Down

0 comments on commit b83d197

Please sign in to comment.