(ctx context.Context, image string)
| 113 | } |
| 114 | |
| 115 | func getPgContainerWithImage(ctx context.Context, image string) (retC *Container, retErr error) { |
| 116 | req := testcontainers.ContainerRequest{ |
| 117 | Image: image, |
| 118 | Env: map[string]string{ |
| 119 | "LANG": "en_US.UTF-8", |
| 120 | "POSTGRES_PASSWORD": "root-password", |
| 121 | }, |
| 122 | ExposedPorts: []string{"5432/tcp"}, |
| 123 | WaitingFor: wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute), |
| 124 | } |
| 125 | |
| 126 | c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 127 | ContainerRequest: req, |
| 128 | Started: true, |
| 129 | }) |
| 130 | if err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | |
| 134 | host, err := c.Host(ctx) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | port, err := c.MappedPort(ctx, "5432/tcp") |
| 139 | if err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 | |
| 143 | db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=postgres", host, port.Port())) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | defer func() { |
| 148 | if retErr != nil { |
| 149 | db.Close() |
| 150 | } |
| 151 | }() |
| 152 | |
| 153 | if err := waitDBPing(ctx, db); err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | |
| 157 | return &Container{ |
| 158 | container: c, |
| 159 | host: host, |
| 160 | port: port.Port(), |
| 161 | db: db, |
| 162 | }, nil |
| 163 | } |
| 164 | |
| 165 | func waitDBPing(ctx context.Context, db *sql.DB) error { |
| 166 | started := time.Now() |
no test coverage detected