1
0
mirror of https://github.com/xorcare/testing-go-code-with-postgres.git synced 2025-06-30 23:23:40 +02:00
Go to file
Vasiliy Vasilyuk f8cba15b0d Add known issue with colima
This problem is caused by incorrect mounting of files.

```
/var/folders/3p/glp5vp4916n03wmjh_b0gf6m0000gn/T/tmp.ib2dU9TY
❯ git clone https://github.com/xorcare/testing-go-code-with-postgres
cd testing-go-code-with-postgres
make test-env-up test
Cloning into 'testing-go-code-with-postgres'...
remote: Enumerating objects: 124, done.
remote: Counting objects: 100% (66/66), done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 124 (delta 36), reused 51 (delta 30), pack-reused 58
Receiving objects: 100% (124/124), 31.92 KiB | 4.56 MiB/s, done.
Resolving deltas: 100% (50/50), done.
[+] Building 0.0s (0/0)                                                              docker:default
[+] Running 2/0
 ✔ Container testing-go-code-with-postgres-postgres-1  Recreated                               0.0s
 ✔ Container testing-go-code-with-postgres-migrate-1   Recreated                               0.0s
Attaching to testing-go-code-with-postgres-migrate-1
dependency failed to start: container testing-go-code-with-postgres-postgres-1 exited (126)
make: *** [test-env-up] Error 1
```

```
/var/folders/3p/glp5vp4916n03wmjh_b0gf6m0000gn/T/tmp.ib2dU9TY/testing-go-code-with-postgres
❯ docker-compose up
[+] Building 0.0s (0/0)                                                              docker:default
[+] Running 1/0
 ✔ Container testing-go-code-with-postgres-postgres-1  Created                                 0.0s
Attaching to testing-go-code-with-postgres-migrate-1, testing-go-code-with-postgres-postgres-1
testing-go-code-with-postgres-postgres-1  | The files belonging to this database system will be owned by user "postgres".
testing-go-code-with-postgres-postgres-1  | This user must also own the server process.
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  | The database cluster will be initialized with locale "en_US.utf8".
testing-go-code-with-postgres-postgres-1  | The default database encoding has accordingly been set to "UTF8".
testing-go-code-with-postgres-postgres-1  | The default text search configuration will be set to "english".
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  | Data page checksums are disabled.
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
testing-go-code-with-postgres-postgres-1  | creating subdirectories ... ok
testing-go-code-with-postgres-postgres-1  | selecting dynamic shared memory implementation ... posix
testing-go-code-with-postgres-postgres-1  | selecting default max_connections ... 100
testing-go-code-with-postgres-postgres-1  | selecting default shared_buffers ... 128MB
testing-go-code-with-postgres-postgres-1  | selecting default time zone ... UTC
testing-go-code-with-postgres-postgres-1  | creating configuration files ... ok
testing-go-code-with-postgres-postgres-1  | running bootstrap script ... ok
testing-go-code-with-postgres-postgres-1  | sh: locale: not found
testing-go-code-with-postgres-postgres-1  | 2023-11-18 13:34:16.419 UTC [31] WARNING:  no usable system locales were found
testing-go-code-with-postgres-postgres-1  | performing post-bootstrap initialization ... ok
testing-go-code-with-postgres-postgres-1  | syncing data to disk ... ok
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  | Success. You can now start the database server using:
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  | initdb: warning: enabling "trust" authentication for local connections
testing-go-code-with-postgres-postgres-1  | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
testing-go-code-with-postgres-postgres-1  | waiting for server to start....2023-11-18 13:34:16.617 UTC [37] LOG:  starting PostgreSQL 15.3 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
testing-go-code-with-postgres-postgres-1  | 2023-11-18 13:34:16.619 UTC [37] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
testing-go-code-with-postgres-postgres-1  | 2023-11-18 13:34:16.620 UTC [40] LOG:  database system was shut down at 2023-11-18 13:34:16 UTC
testing-go-code-with-postgres-postgres-1  | 2023-11-18 13:34:16.622 UTC [37] LOG:  database system is ready to accept connections
testing-go-code-with-postgres-postgres-1  |  done
testing-go-code-with-postgres-postgres-1  | server started
testing-go-code-with-postgres-postgres-1  |
testing-go-code-with-postgres-postgres-1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/docker-multiple-databases.sh
testing-go-code-with-postgres-postgres-1  | /usr/local/bin/docker-entrypoint.sh: line 170: /docker-entrypoint-initdb.d/docker-multiple-databases.sh: Is a directory
testing-go-code-with-postgres-postgres-1 exited with code 126
dependency failed to start: container testing-go-code-with-postgres-postgres-1 exited (126)
```
2023-11-18 16:36:40 +03:00
2023-10-11 20:26:28 +03:00
2023-09-22 22:16:15 +03:00
2023-11-14 22:35:32 +03:00
2023-06-17 16:29:50 +03:00
2023-11-14 22:35:25 +03:00
2023-11-18 16:36:40 +03:00

Example of testing Go code with Postgres

Go workflow status badge codecov Gitlab CI Pipeline Gitlab CI Coverage Go Report Card

The example suggests a solution to the problem of cleaning the database after running tests and the problem of running tests in parallel. It also shows how to organize integration testing of Go code with Postgres.

Quick start

For quickly try integration tests locally, use following commands.

git clone https://github.com/xorcare/testing-go-code-with-postgres
cd testing-go-code-with-postgres
make test-env-up test
Example of output
❯ git clone https://github.com/xorcare/testing-go-code-with-postgres
cd testing-go-code-with-postgres
make test-env-up test
Cloning into 'testing-go-code-with-postgres'...
remote: Enumerating objects: 103, done.
remote: Counting objects: 100% (45/45), done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 103 (delta 26), reused 29 (delta 20), pack-reused 58
Receiving objects: 100% (103/103), 27.58 KiB | 3.94 MiB/s, done.
Resolving deltas: 100% (40/40), done.
[+] Running 15/15
 ✔ migrate 5 layers [⣿⣿⣿⣿⣿]      0B/0B      Pulled                                             5.0s 
   ✔ 08409d417260 Pull complete                                                                1.5s 
   ✔ 2f9061c5186e Pull complete                                                                0.8s 
   ✔ de4eb1257b2b Pull complete                                                                2.2s 
   ✔ 750ec3989a15 Pull complete                                                                1.6s 
   ✔ 586322a68347 Pull complete                                                                2.2s 
 ✔ postgres 8 layers [⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                        15.2s 
   ✔ 9fda8d8052c6 Pull complete                                                                2.5s 
   ✔ b0d9bb38da5c Pull complete                                                                2.8s 
   ✔ a99f2e61e525 Pull complete                                                                2.8s 
   ✔ eb307cc1ffd3 Pull complete                                                               11.1s 
   ✔ 99aedaa309df Pull complete                                                                4.0s 
   ✔ 1d4087443ab6 Pull complete                                                                3.5s 
   ✔ 278b6fc01aef Pull complete                                                                4.2s 
   ✔ 024b1a6a5c4d Pull complete                                                                4.9s 
[+] Building 0.0s (0/0)                                                              docker:default
[+] Running 3/2
 ✔ Network testing-go-code-with-postgres_default       Created                                 0.0s 
 ✔ Container testing-go-code-with-postgres-postgres-1  Created                                 0.2s 
 ✔ Container testing-go-code-with-postgres-migrate-1   Created                                 0.0s 
Attaching to testing-go-code-with-postgres-migrate-1
testing-go-code-with-postgres-migrate-1  | 1/u create_users_table (4.481416ms)
testing-go-code-with-postgres-migrate-1 exited with code 0
Aborting on container exit...
[+] Stopping 1/0
 ✔ Container testing-go-code-with-postgres-migrate-1  Stopped                                  0.0s 
ok  	github.com/xorcare/testing-go-code-with-postgres	1.500s	coverage: 100.0% of statements
ok  	github.com/xorcare/testing-go-code-with-postgres/testingpg	1.764s	coverage: 100.0% of statements
total:	(statements)	100.0%

What's interesting here?

Generating human-readable database names from t.Name() to simplifying problem investigation. The last 8 characters are a short unique identifier needed to prevent name collision, its necessary because the maximum length of the name is 63 bytes, and the name must be unique.

Example of test names
TestNewPostgres-Changes-are-not-visible-in-different-inWirPQD7J
TestNewPostgres-Changes-are-not-visible-in-different-ineYp0ljjI
TestNewPostgres-Successfully-connect-by-URL-and-get-verzGq4pGza
TestNewPostgres-Successfully-obtained-a-version-using-a20YgZaMf
TestNewPostgres-URL-is-different-at-different-instancesIMDkJgoP
TestNewPostgres-URL-is-different-at-different-instancesjtSsjPR5
TestUserRepository-CreateUser-Cannot-create-a-user-withmgmHFdZe
TestUserRepository-CreateUser-Successfully-created-a-UspTBGNltW
TestUserRepository-ReadUser-Get-an-error-if-the-user-doRqS1GvYh

Known issues

When using colima on macos you may have problems if you clone this project to a temporary directory like this:

/var/folders/3p/glp5vp4916n03wmjh_b0gf6m0000gn/T/tmp.lbM4pbW2/testing-go-code-with-postgres

This problem is caused by incorrect mounting of files, and looks like this:

Example of output
/var/folders/3p/glp5vp4916n03wmjh_b0gf6m0000gn/T/tmp.lbM4pbW2/testing-go-code-with-postgres
❯ docker-compose up
...
testing-go-code-with-postgres-postgres-1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/docker-multiple-databases.sh
testing-go-code-with-postgres-postgres-1  | /usr/local/bin/docker-entrypoint.sh: line 170: /docker-entrypoint-initdb.d/docker-multiple-databases.sh: Is a directory
testing-go-code-with-postgres-postgres-1 exited with code 126
dependency failed to start: container testing-go-code-with-postgres-postgres-1 exited (126)

Disclaimer

This example is not an example of software architecture!

Description
The example suggests a solution to the problem of cleaning the database after running tests and the problem of running tests in parallel. It also shows how to organize integration testing of Go code with Postgres.
Readme BSD-3-Clause 339 KiB
Languages
Go 92.6%
Makefile 5%
Shell 2.4%