6
0
mirror of https://github.com/FirebirdSQL/firebird-qa.git synced 2025-01-22 13:33:07 +01:00
firebird-qa/tests/bugs/core_2940_test.py

282 lines
9.4 KiB
Python
Raw Normal View History

2021-04-26 20:07:00 +02:00
#coding:utf-8
#
# id: bugs.core_2940
# title: Trace output could contain garbage data left from filtered out statements
2021-11-16 19:44:53 +01:00
# decription:
2021-04-26 20:07:00 +02:00
# 1. Obtain engine_version from built-in context variable.
# 2. Make config for trace in proper format according to FB engine version, with 'exclude_filter' parameter from ticket.
# 3. Launch trace session in separate child process using 'FBSVCMGR action_trace_start'
# 4. Run ISQL with test commands. Only one of these command does not contain token that is specified in 'exclude_filter'
# 5. Stop trace session. Output its log with filtering only statistics info.
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# Checked on: WI-V2.5.5.26916 (SS, SC, CS); WI-V3.0.0.32008 (SS, SC, CS). Result: OK.
# Checked on: 3.0.1.32525, 4.0.0.238 // 07-jun-2016
# ::: NB :::
# Several delays (time.sleep) added in main thread because of OS buffering. Couldn't switch this buffering off.
2021-11-16 19:44:53 +01:00
#
#
2021-04-26 20:07:00 +02:00
# ::: NB ::: 07-jun-2016.
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# WI-T4.0.0.238 will issue in trace log following statement with its statistics ("1 records fetched"):
# ===
2021-11-16 19:44:53 +01:00
# with recursive role_tree as (
# select rdb$relation_name as nm, 0 as ur from rdb$user_privileges
# where
# rdb$privilege = 'M' and rdb$field_name = 'D'
# and rdb$user = ? and rdb$user_type = 8
# union all
# select rdb$role_name as nm, 1 as ur from rdb$roles
2021-04-26 20:07:00 +02:00
# where rdb$role_name =...
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# param0 = varchar(93), "SYSDBA"
# param1 = varchar(93), "NONE"
# ===
# We have to SKIP this statement statistics and start to check only "our" selects from rdb$database
# see usage of first_sttm_pattern and trace_stat_pattern.
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# tracker_id: CORE-2940
# min_versions: ['2.5.0']
# versions: 2.5
# qmid: None
import pytest
2021-11-16 19:44:53 +01:00
from firebird.qa import db_factory, python_act, Action
2021-04-26 20:07:00 +02:00
# version: 2.5
# resources: None
substitutions_1 = [('^((?!records fetched).)*$', '')]
init_script_1 = """"""
db_1 = db_factory(sql_dialect=3, init=init_script_1)
# test_script_1
#---
# import os
# import subprocess
# from subprocess import Popen
# import time
# import re
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# os.environ["ISC_USER"] = user_name
# os.environ["ISC_PASSWORD"] = user_password
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# engine=str(db_conn.engine_version)
# db_conn.close()
2021-11-16 19:44:53 +01:00
#
#
2021-04-26 20:07:00 +02:00
# #--------------------------------------------
2021-11-16 19:44:53 +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-16 19:44:53 +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-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# file_handle.flush()
# if file_handle.mode not in ('r', 'rb'):
# # otherwise: "OSError: [Errno 9] Bad file descriptor"!
# os.fsync(file_handle.fileno())
# file_handle.close()
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# #--------------------------------------------
2021-11-16 19:44:53 +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] )
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# #--------------------------------------------
2021-11-16 19:44:53 +01:00
#
#
2021-04-26 20:07:00 +02:00
# txt25 = '''# Trace config, format for 2.5. Generated auto, do not edit!
# <database %[\\\\\\\\/]bugs.core_2940.fdb>
# enabled true
# #include_filter
# exclude_filter %no_trace%
# log_connections true
# log_transactions true
# log_statement_finish true
# print_plan true
# print_perf true
2021-11-16 19:44:53 +01:00
# time_threshold 0
2021-04-26 20:07:00 +02:00
# </database>
# '''
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # NOTES ABOUT TRACE CONFIG FOR 3.0:
# # 1) Header contains `database` clause in different format vs FB 2.5: its data must be enclosed with '{' '}'
# # 2) Name and value must be separated by EQUALITY sign ('=') in FB-3 trace.conf, otherwise we get runtime error:
# # element "<. . .>" have no attribute value set
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# txt30 = '''# Trace config, format for 3.0. Generated auto, do not edit!
# database=%[\\\\\\\\/]bugs.core_2940.fdb
# {
# enabled = true
# #include_filter
# exclude_filter = %no_trace%
# log_connections = true
# log_transactions = true
# log_statement_finish = true
# print_plan = true
# print_perf = true
2021-11-16 19:44:53 +01:00
# time_threshold = 0
2021-04-26 20:07:00 +02:00
# }
# '''
# trccfg=open( os.path.join(context['temp_directory'],'tmp_trace_2940.cfg'), 'w')
# if engine.startswith('2.5'):
# trccfg.write(txt25)
# else:
# trccfg.write(txt30)
# trccfg.close()
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# trclog=open( os.path.join(context['temp_directory'],'tmp_trace_2940.log'), 'w')
# trclog.close()
# trclst=open( os.path.join(context['temp_directory'],'tmp_trace_2940.lst'), 'w')
# trclst.close()
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# #####################################################
# # Starting trace session in new child process (async.):
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# f_trclog=open(trclog.name,'w')
# # Execute a child program in a new process, redirecting STDERR to the same target as of STDOUT:
# p_trace=Popen([context['fbsvcmgr_path'], "localhost:service_mgr",
# "action_trace_start",
# "trc_cfg", trccfg.name],
# stdout=f_trclog, stderr=subprocess.STDOUT)
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # Wait! Trace session is initialized not instantly!
# time.sleep(1)
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# #####################################################
# # Running ISQL with test commands:
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# sqltxt='''
# set list on;
# -- statistics for this statement SHOULD appear in trace log:
2021-11-16 19:44:53 +01:00
# select 1 k1 from rdb$database;
2021-04-26 20:07:00 +02:00
# commit;
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# -- statistics for this statement should NOT appear in trace log:
2021-11-16 19:44:53 +01:00
# select 2 k2 from rdb$types rows 2 /* no_trace*/;
#
2021-04-26 20:07:00 +02:00
# -- statistics for this statement should NOT appear in trace log:
2021-11-16 19:44:53 +01:00
# select 3 no_trace from rdb$types rows 3;
#
2021-04-26 20:07:00 +02:00
# -- statistics for this statement should NOT appear in trace log:
# set term ^;
# execute block returns(k4 int) as
# begin
# for select 4 from rdb$types rows 4 into k4 do suspend;
# end -- no_trace
# ^
# set term ;^
# '''
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# runProgram('isql',[dsn,'-n'],sqltxt)
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # do NOT remove this otherwise trace log can contain only message about its start before being closed!
# time.sleep(3)
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# #####################################################
# # Getting ID of launched trace session and STOP it:
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # Save active trace session info into file for further parsing it and obtain session_id back (for stop):
# f_trclst=open(trclst.name,'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-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# trcssn=0
# with open( 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-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # Result: `trcssn` is ID of active trace session. Now we have to terminate it:
# f_trclst=open(trclst.name,'a')
# f_trclst.seek(0,2)
# subprocess.call([context['fbsvcmgr_path'], "localhost:service_mgr",
# "action_trace_stop",
# "trc_id",trcssn],
# stdout=f_trclst, stderr=subprocess.STDOUT
# )
# flush_and_close( f_trclst )
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # Terminate child process of launched trace session (though it should already be killed):
# p_trace.terminate()
# flush_and_close( f_trclog )
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# #####################################################
# # Output log of trace session, with filtering only info about statistics ('fetches'):
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# first_sttm_pattern=re.compile("select 1 k1")
# trace_stat_pattern=re.compile("1 records fetched")
# flag=0
# with open( trclog.name,'r') as f:
# for line in f:
# if first_sttm_pattern.match(line):
# flag=1
# if flag==1 and trace_stat_pattern.match(line):
# print(line)
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# # Cleanup:
# ##########
# time.sleep(1)
2021-11-16 19:44:53 +01:00
#
2021-04-26 20:07:00 +02:00
# cleanup([i.name for i in (trccfg,trclst,trclog)])
2021-11-16 19:44:53 +01:00
#
#
2021-04-26 20:07:00 +02:00
#---
2021-11-16 19:44:53 +01:00
act_1 = python_act('db_1', substitutions=substitutions_1)
2021-04-26 20:07:00 +02:00
expected_stdout_1 = """
1 records fetched
2021-11-16 19:44:53 +01:00
"""
test_script_1 = """
set list on;
-- statistics for this statement SHOULD appear in trace log:
select 1 k1 from rdb$database;
commit;
-- statistics for this statement should NOT appear in trace log:
select 2 k2 from rdb$types rows 2 /* no_trace*/;
-- statistics for this statement should NOT appear in trace log:
select 3 no_trace from rdb$types rows 3;
-- statistics for this statement should NOT appear in trace log:
set term ^;
execute block returns(k4 int) as
begin
for select 4 from rdb$types rows 4 into k4 do suspend;
end -- no_trace
^
set term ;^
"""
2021-12-14 20:56:34 +01:00
trace_1 = ['log_connections = true',
'log_transactions = true',
'log_statement_finish = true',
'print_plan = true',
'print_perf = true',
2021-12-14 20:56:34 +01:00
'time_threshold = 0',
'exclude_filter = %no_trace%',
]
2021-11-16 19:44:53 +01:00
@pytest.mark.version('>=3.0')
def test_1(act_1: Action):
with act_1.trace(db_events=trace_1):
act_1.isql(switches=['-n'], input=test_script_1)
2021-11-16 19:44:53 +01:00
act_1.expected_stdout = expected_stdout_1
act_1.trace_to_stdout()
2021-11-16 19:44:53 +01:00
assert act_1.clean_stdout == act_1.clean_expected_stdout