Skip to content

half0wl/mockgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

mockgres

A stupidly simple mock Postgres server for testing purposes.

# Run the server:
PORT=6666 go run ./main.go
starting mock server on port 6666
waiting for conn on port 6666...
conn from 127.0.0.1:56372
startup message length: 78
proto: 0x30000
regular startup message (protocol 3.0)
startup parameters: "user\x00test\x00database\x00foobar\x00application_name\x00psql\x00client_encoding\x00UTF8\x00\x00"
user: test, database: foobar
requesting password authentication
received password: "test"
Authentication successful
handshake complete, ready
conn established
received query: "select 1;"
received query: <....>
handling \dt

# From psql:
psql 'postgresql://test:[email protected]:6666/foobar?sslmode=disable'
psql (17.6, server 14.0)
Type "help" for help.

foobar=> select 1;
SELECT 0
foobar=> \dt
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+-------
 public | test_table | table | test
(1 row)

The main use case for this is the --intentionally-borked flag which simulates a Postgres server that unexpectedly closes connections with a TCP RST packet on any query:

# Run server with --intentionally-borked flag:
PORT=6666 go run ./main.go --intentionally-borked
starting mock server on port 6666
--intentionally-borked is on, will send RST on any query!
...
received query: "select 1;"
query recv, sending RST as --intentionally-borked is on
waiting 2 seconds before forcing RST close
forcing RST close on conn!
query error: conn forcibly closed with RST
...

# From psql:
psql 'postgresql://test:[email protected]:6666/foobar?sslmode=disable'
psql (17.6, server 14.0)
Type "help" for help.

foobar=> select 1;
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.

SSL Mode is not supported. You should also be able to connect to this with any Postgres client/library.

About

A simple server implementing basic Postgres protocol for testing purposes.

Topics

Resources

Stars

Watchers

Forks

Languages