Guest User

Untitled

a guest
Jul 30th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.62 KB | None | 0 0
  1. package postgres
  2.  
  3. import (
  4.     "context"
  5.     "fmt"
  6.     "log"
  7.     "os"
  8.     "testing"
  9.  
  10.     "github.com/docker/go-connections/nat"
  11.     "github.com/testcontainers/testcontainers-go"
  12.     "github.com/testcontainers/testcontainers-go/wait"
  13. )
  14.  
  15. func createTestContainer(ctx context.Context, dbname string) (testcontainers.Container, error) {
  16.  
  17.     var port = "5432/tcp"
  18.     //  dbURL := func(port nat.Port) string {
  19.     //  return fmt.Sprintf("postgres://postgres:password@localhost:%s/%s?sslmode=disable", port.Port(), dbname)
  20.     // }
  21.     req := testcontainers.ContainerRequest{
  22.         Image:        "postgres:11.7",
  23.         ExposedPorts: []string{port},
  24.         BindMounts:   map[string]string{"/home/user/Projects/typer-site/seedTestDB.psql": "/docker-entrypoint-initdb.d/init.sql"},
  25.         Env: map[string]string{
  26.             "POSTGRES_PASSWORD": "password",
  27.             "POSTGRES_USER":     "postgres",
  28.             "POSTGRES_DB":       dbname,
  29.         },
  30.         WaitingFor: wait.ForListeningPort("5432/tcp"),
  31.     }
  32.     container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
  33.         ContainerRequest: req,
  34.         Started:          true,
  35.     })
  36.     if err != nil {
  37.         return container, fmt.Errorf("failed to start container: %s", err.Error())
  38.     }
  39.     mappedPort, err := container.MappedPort(ctx, nat.Port(port))
  40.     if err != nil {
  41.         return container, fmt.Errorf("failed to get container external port: %s", err.Error())
  42.     }
  43.     log.Println("postgres container ready and running at " + mappedPort)
  44.     // url := fmt.Sprintf("postgres://postgres:password@localhost:%s/%s?sslmode=disable", mappedPort.Port(), dbname)
  45.     err = Connect("localhost", mappedPort.Port(), "postgres", "password", dbname)
  46.     if err != nil {
  47.         return container, fmt.Errorf("unable to connect to the database container: %s", err.Error())
  48.     }
  49.     return container, nil
  50. }
  51. func TestGetCustomTypeCount(t *testing.T) {
  52.     mountFrom := "/home/user/Projects/typer-site/seedTestDB.psql"
  53.     mountTo := "/docker-entrypoint-initdb.d/init.sql"
  54.     ctx := context.Background()
  55.     req := testcontainers.ContainerRequest{
  56.         Image:        "postgres:11.7",
  57.         ExposedPorts: []string{"5432/tcp"},
  58.         BindMounts:   map[string]string{mountFrom: mountTo},
  59.         Env: map[string]string{
  60.             "POSTGRES_DB": os.Getenv("DB_NAME"),
  61.         },
  62.         WaitingFor: wait.ForLog("database system is ready to accept connections"),
  63.     }
  64.     postgresC, err := testcontainers.GenericContainer(ctx,
  65.         testcontainers.GenericContainerRequest{
  66.             ContainerRequest: req,
  67.             Started:          true,
  68.         })
  69.     if err != nil {
  70.         panic(err)
  71.     }
  72.     defer postgresC.Terminate(ctx)
  73.  
  74.     p, _ := postgresC.MappedPort(ctx, "5432")
  75.     os.Setenv("DB_PORT", p.Port())
  76.  
  77.     Connect("localhost", p.Port(), "postgres", "", os.Getenv("DB_NAME"))
  78.  
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment