File: test_no_database.py

package info (click to toggle)
pgbouncer 1.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,884 kB
  • sloc: ansic: 61,425; python: 5,703; sh: 4,527; makefile: 1,361; sed: 22
file content (122 lines) | stat: -rw-r--r-- 4,865 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import re

import psycopg
import pytest


def test_no_database(bouncer):
    with bouncer.log_contains(r"closing because: no such database: nosuchdb"):
        with pytest.raises(
            psycopg.OperationalError, match="no such database: nosuchdb"
        ):
            bouncer.test(dbname="nosuchdb")


def test_no_database_authfail(bouncer):
    bouncer.admin(f"set auth_type='md5'")
    with bouncer.log_contains(r"closing because: password authentication failed"):
        with pytest.raises(
            psycopg.OperationalError, match="password authentication failed"
        ):
            bouncer.test(dbname="nosuchdb", password="wrong")


def test_no_database_auth_user(bouncer):
    bouncer.admin(f"set auth_user='pswcheck'")
    bouncer.admin(f"set auth_type='md5'")
    with bouncer.log_contains(r"closing because: password authentication failed"):
        with pytest.raises(
            psycopg.OperationalError, match="password authentication failed"
        ):
            bouncer.test(dbname="nosuchdb", user="someuser", password="wrong")


def test_no_database_pg(bouncer):
    with bouncer.log_contains(
        r'server login failed: FATAL database "non_existing_pg_db" does not exist'
    ), bouncer.log_contains(
        r'closing because: database "non_existing_pg_db" does not exist'
    ):
        with pytest.raises(
            psycopg.OperationalError,
            match='database "non_existing_pg_db" does not exist',
        ):
            bouncer.test(dbname="non_existing_pg_db")


def test_no_database_auto_database(bouncer):
    with bouncer.ini_path.open() as f:
        original = f.read()
    with bouncer.ini_path.open("w") as f:
        # uncomment the auto-database line
        f.write(re.sub(r"^;\*", "*", original, flags=re.MULTILINE))

    bouncer.admin("reload")

    with bouncer.log_contains(
        r'server login failed: FATAL database "nosuchdb" does not exist'
    ), bouncer.log_contains(r'closing because: database "nosuchdb" does not exist'):
        with pytest.raises(
            psycopg.OperationalError, match='database "nosuchdb" does not exist'
        ):
            bouncer.test(dbname="nosuchdb")


def test_no_database_auto_database_auth_user(bouncer):
    with bouncer.ini_path.open() as f:
        original = f.read()
    with bouncer.ini_path.open("w") as f:
        # uncomment the auto-database line
        f.write(re.sub(r"^;\*", "*", original, flags=re.MULTILINE))

    bouncer.admin("reload")
    bouncer.admin(f"set auth_user='pswcheck'")
    bouncer.admin(f"set auth_type='md5'")

    with bouncer.log_contains(
        r'server login failed: FATAL database "nosuchdb" does not exist'
    ), bouncer.log_contains(r'closing because: database "nosuchdb" does not exist'):
        with pytest.raises(
            psycopg.OperationalError, match='database "nosuchdb" does not exist'
        ):
            bouncer.test(dbname="nosuchdb", user="nonexistinguser")


def test_no_database_md5_auth_scram_pw_success(bouncer):
    # Testing what happens on successful SCRAM auth connection to non-existent
    # DB Segfaults have been seen after mock authentication was put in place
    # with md5 auth and a scram PW when saving SCRAM credentials. Including
    # this test to check for the condition repeating.
    bouncer.admin(f"set auth_type='md5'")
    with bouncer.log_contains(r"closing because: no such database: nosuchdb"):
        with pytest.raises(
            psycopg.OperationalError, match="no such database: nosuchdb"
        ):
            bouncer.test(dbname="nosuchdb", user="scramuser1", password="foo")


def test_no_database_scram_auth_scram_pw_success(bouncer):
    # Testing what happens on successful SCRAM auth with a SCRAM PW connection
    # to non-existent DB. Segfaults have been seen after mock authentication
    # was put in place with md5 auth and a scram PW. Including this test for
    # completeness.
    bouncer.admin(f"set auth_type='scram-sha-256'")
    with bouncer.log_contains(r"closing because: no such database: nosuchdb"):
        with pytest.raises(
            psycopg.OperationalError, match="no such database: nosuchdb"
        ):
            bouncer.test(dbname="nosuchdb", user="scramuser1", password="foo")


@pytest.mark.md5
def test_no_database_md5_auth_md5_pw_success(bouncer):
    # Testing what happens on successful MD5 auth with a MD5 pw connection to
    # non-existent DB Segfaults have been seen after mock authentication was
    # put in place with md5 auth and a scram PW. Including this test for
    # completeness.
    bouncer.admin(f"set auth_type='md5'")
    with bouncer.log_contains(r"closing because: no such database: nosuchdb"):
        with pytest.raises(
            psycopg.OperationalError, match="no such database: nosuchdb"
        ):
            bouncer.test(dbname="nosuchdb", user="muser1", password="foo")