简体   繁体   English

跨PG版本的Postgresql扩展pg_regress测试

[英]Postgresql Extension pg_regress tests across PG Version

I am writing a PostgreSQL extension which needs to work on PostgreSQL 9.4, 9.5, 9.6, and 10. I am using the standard pg_regress regression tests with make installcheck for the tests. 我正在编写一个需要在PostgreSQL pg_regress和10上运行的PostgreSQL扩展。我正在将标准pg_regress回归测试与make installcheck一起使用。 I am trying to set up tests across versions with Travis-CI. 我正在尝试使用Travis-CI建立跨版本的测试。

Because this is a security-critical extension, I have a lot of statements in my test cases that are supposed to throw errors, for example checks against whitelisted keywords. 因为这是一项对安全性至关重要的扩展,所以我的测试用例中有很多语句应该抛出错误,例如,对列入白名单的关键字进行检查。

pg_regress works by comparing the output of the test script with the expected output. pg_regress通过将测试脚本的输出与预期输出进行比较pg_regress工作。 For ordinary, successful queries this works fine, but for errors I have a problem. 对于普通的,成功的查询,这很好,但是对于错误,我有问题。

For 9.6, the output of an error seems to always have a CONTEXT line while for 9.4 it never does. 对于9.6,错误的输出似乎总是有一个CONTEXT行,而对于9.4,则从来没有。 In other words, 9.6 is sending troubleshooting information missing in 9.4, and this causes the tests to be incompatible across versions. 换句话说,9.6正在发送9.4中缺少的故障排除信息,这将导致测试在各个版本之间不兼容。

The relevant portions of my test script are: 我的测试脚本的相关部分是:

set client_min_messages to warning;
set log_error_verbosity to terse;
create extension roleman;

-- whitelist errors

-- no tables so pick a random number and cast to oid
select roleman.grant_table('postgres', 1::oid, array['execute', 'drop']);
select roleman.grant_schema('postgres', 'foo', array['execute']);
select roleman.grant_schema_all('postgres', 'foo', 'tables', array['execute', 'drop everything']);
select roleman.grant_database('postgres', 'foo', array['execute']);
select roleman.grant_function('postgres', 1::OID, array['wheeee']);
select roleman.grant_seq('postgres', 1::oid, array['execute']);

The output on 9.6 is: 9.6的输出为:

set client_min_messages to warning;
set log_error_verbosity to terse;
create extension roleman;
-- whitelist errors
-- no tables so pick a random number and cast to oid
select roleman.grant_table('postgres', 1::oid, array['execute', 'drop']);
ERROR:  bad database permissions for postgres,  table 1, perms execute, drop
CONTEXT:  PL/pgSQL function grant_table(text,regclass,text[]) line 4 at RAISE
select roleman.grant_schema('postgres', 'foo', array['execute']);
ERROR:  bad permissions for postgres,  schema foo, perms execute
CONTEXT:  PL/pgSQL function grant_schema(text,text,text[]) line 4 at RAISE
select roleman.grant_schema_all('postgres', 'foo', 'tables', array['execute', 'drop everything']);
ERROR:  bad database permissions for postgres,  schema foo, type tables, perms execute, drop everything
CONTEXT:  PL/pgSQL function grant_schema_all(text,text,text,text[]) line 5 at RAISE
select roleman.grant_database('postgres', 'foo', array['execute']);
ERROR:  bad database permissions for postgres, dbname foo, perms execute
CONTEXT:  PL/pgSQL function grant_database(text,text,text[]) line 4 at RAISE
select roleman.grant_function('postgres', 1::OID, array['wheeee']);
ERROR:  bad database permissions for postgres,  function 1, perms wheeee
CONTEXT:  PL/pgSQL function grant_function(text,regprocedure,text[]) line 4 at RAISE
select roleman.grant_seq('postgres', 1::oid, array['execute']);
ERROR:  bad database permissions for postgres,  sequence 1, perms execute
CONTEXT:  PL/pgSQL function grant_seq(text,regclass,text[]) line 4 at RAISE

On 9.4 the output is the same except the CONTEXT lines are missing. 在9.4上的输出是相同的,只是缺少CONTEXT行。 I tried to disable the lines by setting the error verbosity at the beginning of my script but that had no effect. 我试图通过在脚本的开头设置错误详细程度来禁用行,但这没有效果。 The lack of the CONTEXT line is treated by pg_regress a test failure. pg_regress处理pg_regress测试行的情况。

Is there any way around this? 有没有办法解决?

This sort of thing is a right pain. 这种事情是对的。 pg_regress supports alternate output files, but they're clumsy to use and maintain. pg_regress支持备用输出文件,但是使用和维护它们都很笨拙。

What BDR and pglogical does is \\set VERBOSITY terse , which helps with some such issues. BDR和pgologic所做的是\\set VERBOSITY terse ,这有助于解决某些此类问题。

In more complex cases sometimes you can use a DO block. 在更复杂的情况下,有时可以使用DO块。 Something like 就像是

DO LANGUAGE plpgsql $$
BEGIN
    BEGIN
        PERFORM the_statement;
    EXCEPTION WHEN ... THEN ...
        ... check exception matches expected here ...
    END;
END;
$$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM