2021-04-26 20:07:00 +02:00
|
|
|
#coding:utf-8
|
|
|
|
#
|
|
|
|
# id: bugs.core_4451
|
|
|
|
# title: Allow output to trace explain plan form.
|
2021-11-26 19:20:43 +01:00
|
|
|
# decription:
|
2021-04-26 20:07:00 +02:00
|
|
|
# Checked on
|
|
|
|
# 4.0.0.1685 SS: 7.985s.
|
|
|
|
# 4.0.0.1685 CS: 8.711s.
|
|
|
|
# 3.0.5.33206 SS: 7.281s.
|
|
|
|
# 3.0.5.33206 CS: 8.278s.
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# tracker_id: CORE-4451
|
|
|
|
# min_versions: ['3.0.0']
|
|
|
|
# versions: 3.0
|
|
|
|
# qmid: None
|
|
|
|
|
|
|
|
import pytest
|
2021-11-26 19:20:43 +01:00
|
|
|
from firebird.qa import db_factory, python_act, Action
|
2021-04-26 20:07:00 +02:00
|
|
|
|
|
|
|
# version: 3.0
|
|
|
|
# resources: None
|
|
|
|
|
|
|
|
substitutions_1 = [('[ \t]+', ' '), ('[ \t]+[\\d]+[ \t]+ms', '')]
|
|
|
|
|
|
|
|
init_script_1 = """
|
|
|
|
recreate table test(x int);
|
2021-11-26 19:20:43 +01:00
|
|
|
"""
|
2021-04-26 20:07:00 +02:00
|
|
|
|
|
|
|
db_1 = db_factory(sql_dialect=3, init=init_script_1)
|
|
|
|
|
|
|
|
# test_script_1
|
|
|
|
#---
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# import os
|
|
|
|
# import subprocess
|
|
|
|
# from subprocess import Popen
|
|
|
|
# import time
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# os.environ["ISC_USER"] = user_name
|
|
|
|
# os.environ["ISC_PASSWORD"] = user_password
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# fdb_file=db_conn.database_name
|
|
|
|
# db_conn.close()
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #-----------------------------------
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# def flush_and_close(file_handle):
|
|
|
|
# # https://docs.python.org/2/library/os.html#os.fsync
|
2021-11-26 19:20:43 +01:00
|
|
|
# # If you're starting with a Python file object f,
|
|
|
|
# # first do f.flush(), and
|
2021-04-26 20:07:00 +02:00
|
|
|
# # then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.
|
|
|
|
# global os
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# file_handle.flush()
|
|
|
|
# os.fsync(file_handle.fileno())
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# file_handle.close()
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #--------------------------------------------
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# def cleanup( f_names_list ):
|
|
|
|
# global os
|
|
|
|
# for i in range(len( f_names_list )):
|
|
|
|
# if os.path.isfile( f_names_list[i]):
|
|
|
|
# os.remove( f_names_list[i] )
|
|
|
|
# if os.path.isfile( f_names_list[i]):
|
|
|
|
# print('ERROR: can not remove file ' + f_names_list[i])
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #--------------------------------------------
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #####################################################################
|
|
|
|
# # Prepare config for trace session that will be launched by call of FBSVCMGR:
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# txt = ''' database= # %[\\\\\\\\/]bugs.core_4451.fdb
|
|
|
|
# {
|
|
|
|
# enabled = true
|
2021-11-26 19:20:43 +01:00
|
|
|
# time_threshold = 0
|
2021-04-26 20:07:00 +02:00
|
|
|
# log_initfini = false
|
|
|
|
# print_plan = true
|
|
|
|
# explain_plan = true
|
|
|
|
# log_statement_prepare = true
|
|
|
|
# include_filter=%(from|join)[[:whitespace:]]test%
|
|
|
|
# }
|
|
|
|
# '''
|
|
|
|
# f_trccfg=open( os.path.join(context['temp_directory'],'tmp_trace_4451.cfg'), 'w')
|
|
|
|
# f_trccfg.write(txt)
|
|
|
|
# flush_and_close( f_trccfg )
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #####################################################################
|
|
|
|
# # Async. launch of trace session using FBSVCMGR action_trace_start:
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# f_trclog=open( os.path.join(context['temp_directory'],'tmp_trace_4451.log'), 'w')
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # Execute a child program in a new process, redirecting STDERR to the same target as of STDOUT:
|
|
|
|
# p_svcmgr = Popen( [ context['fbsvcmgr_path'], "localhost:service_mgr",
|
|
|
|
# "action_trace_start",
|
|
|
|
# "trc_cfg", f_trccfg.name
|
|
|
|
# ],
|
2021-11-26 19:20:43 +01:00
|
|
|
# stdout=f_trclog,
|
2021-04-26 20:07:00 +02:00
|
|
|
# stderr=subprocess.STDOUT
|
|
|
|
# )
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # Wait! Trace session is initialized not instantly!
|
|
|
|
# time.sleep(2)
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #####################################################################
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # Determine active trace session ID (for further stop):
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# f_trclst=open( os.path.join(context['temp_directory'],'tmp_trace_4451.lst'), 'w')
|
|
|
|
# subprocess.call([context['fbsvcmgr_path'], "localhost:service_mgr",
|
|
|
|
# "action_trace_list"],
|
|
|
|
# stdout=f_trclst, stderr=subprocess.STDOUT
|
|
|
|
# )
|
|
|
|
# flush_and_close( f_trclst )
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
|
|
|
# # Session ID: 5
|
|
|
|
# # user:
|
|
|
|
# # date: 2015-08-27 15:24:14
|
|
|
|
# # flags: active, trace
|
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# trcssn=0
|
|
|
|
# with open( f_trclst.name,'r') as f:
|
|
|
|
# for line in f:
|
|
|
|
# i=1
|
|
|
|
# if 'Session ID' in line:
|
|
|
|
# for word in line.split():
|
|
|
|
# if i==3:
|
|
|
|
# trcssn=word
|
|
|
|
# i=i+1
|
|
|
|
# break
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
|
|
|
# # Result: `trcssn` is ID of active trace session.
|
2021-04-26 20:07:00 +02:00
|
|
|
# # We have to terminate trace session that is running on server BEFORE we termitane process `p_svcmgr`
|
|
|
|
# if trcssn==0:
|
|
|
|
# print("Error parsing trace session ID.")
|
|
|
|
# flush_and_close( f_trclog )
|
|
|
|
# else:
|
|
|
|
# #####################################################################
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # Preparing script for ISQL:
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# sql_cmd='''select count(*) from test;'''
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# so=sys.stdout
|
|
|
|
# se=sys.stderr
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# sys.stdout = open(os.devnull, 'w')
|
|
|
|
# sys.stderr = sys.stdout
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# runProgram('isql',[dsn],sql_cmd)
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# sys.stdout = so
|
|
|
|
# sys.stderr = se
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # do NOT reduce this delay!
|
|
|
|
# time.sleep(2)
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# #####################################################################
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # Stop trace session:
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# f_trclst=open(f_trclst.name, "a")
|
|
|
|
# f_trclst.seek(0,2)
|
|
|
|
# subprocess.call( [ context['fbsvcmgr_path'], "localhost:service_mgr",
|
|
|
|
# "action_trace_stop",
|
|
|
|
# "trc_id",trcssn
|
|
|
|
# ],
|
2021-11-26 19:20:43 +01:00
|
|
|
# stdout=f_trclst,
|
2021-04-26 20:07:00 +02:00
|
|
|
# stderr=subprocess.STDOUT
|
|
|
|
# )
|
|
|
|
# flush_and_close( f_trclst )
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# p_svcmgr.terminate()
|
|
|
|
# flush_and_close( f_trclog )
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # do NOT remove this delay:
|
|
|
|
# time.sleep(1)
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# show_line = 0
|
|
|
|
# with open(f_trclog.name) as f:
|
|
|
|
# for line in f:
|
|
|
|
# show_line = ( show_line + 1 if ('^' * 79) in line or show_line>0 else show_line )
|
|
|
|
# if show_line > 1:
|
|
|
|
# print(line)
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
# # cleanup:
|
|
|
|
# ##########
|
|
|
|
# time.sleep(1)
|
|
|
|
# cleanup( [i.name for i in (f_trclst, f_trccfg, f_trclog) ] )
|
2021-11-26 19:20:43 +01:00
|
|
|
#
|
|
|
|
#
|
2021-04-26 20:07:00 +02:00
|
|
|
#---
|
2021-11-26 19:20:43 +01:00
|
|
|
|
|
|
|
act_1 = python_act('db_1', substitutions=substitutions_1)
|
2021-04-26 20:07:00 +02:00
|
|
|
|
|
|
|
expected_stdout_1 = """
|
|
|
|
Select Expression
|
|
|
|
-> Aggregate
|
|
|
|
-> Table "TEST" Full Scan
|
2021-11-26 19:20:43 +01:00
|
|
|
"""
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2021-12-07 20:09:36 +01:00
|
|
|
trace_1 = ['time_threshold = 0',
|
|
|
|
'log_initfini = false',
|
|
|
|
'print_plan = true',
|
|
|
|
'explain_plan = true',
|
|
|
|
'log_statement_prepare = true',
|
|
|
|
'include_filter=%(from|join)[[:whitespace:]]test%',
|
|
|
|
]
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2021-11-26 19:20:43 +01:00
|
|
|
@pytest.mark.version('>=3.0')
|
|
|
|
def test_1(act_1: Action, capsys):
|
2021-12-07 20:09:36 +01:00
|
|
|
with act_1.trace(db_events=trace_1):
|
|
|
|
act_1.isql(switches=[], input='select count(*) from test;')
|
|
|
|
# Process trace
|
2021-11-26 19:20:43 +01:00
|
|
|
show_line = 0
|
2021-12-07 20:09:36 +01:00
|
|
|
for line in act_1.trace_log:
|
2021-11-26 19:20:43 +01:00
|
|
|
show_line = (show_line + 1 if ('^' * 79) in line or show_line>0 else show_line)
|
|
|
|
if show_line > 1:
|
|
|
|
print(line)
|
2021-12-07 20:09:36 +01:00
|
|
|
# Check
|
2021-11-26 19:20:43 +01:00
|
|
|
act_1.expected_stdout = expected_stdout_1
|
|
|
|
act_1.stdout = capsys.readouterr().out
|
|
|
|
assert act_1.clean_stdout == act_1.clean_expected_stdout
|