forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikolay Kuznetsov
committed
Nov 21, 2019
1 parent
2b46560
commit 118fd09
Showing
5 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/docker/go-connections/nat" | ||
"github.com/pkg/errors" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
image = "postgres" | ||
tag = "9.6.15" | ||
port nat.Port = "5432/tcp" | ||
user = "user" | ||
password = "password" | ||
database = "database" | ||
logEntry = "database system is ready to accept connections" | ||
) | ||
|
||
type ContainerRequest struct { | ||
testcontainers.GenericContainerRequest | ||
User string | ||
Password string | ||
Database string | ||
} | ||
|
||
// should always be created via NewContainer | ||
type Container struct { | ||
Container testcontainers.Container | ||
req ContainerRequest | ||
} | ||
|
||
func (cont Container) ConnectURL(ctx context.Context) (string, error) { | ||
template := "postgres://%s:%s@localhost:%s/%s" | ||
|
||
mappedPort, err := cont.Container.MappedPort(ctx, port) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get mapper port for %s", port.Port()) | ||
} | ||
|
||
return fmt.Sprintf(template, cont.req.User, cont.req.Password, | ||
mappedPort.Port(), cont.req.Database), nil | ||
} | ||
|
||
func NewContainer(ctx context.Context, req ContainerRequest) (*Container, error) { | ||
req.ExposedPorts = []string{string(port)} | ||
|
||
// Set the default values if none were provided in the request | ||
if req.Image == "" && req.FromDockerfile.Context == "" { | ||
req.Image = fmt.Sprintf("%s:%s", image, tag) | ||
} | ||
|
||
if req.User == "" { | ||
req.User = user | ||
} | ||
|
||
if req.Password == "" { | ||
req.Password = password | ||
} | ||
|
||
if req.Database == "" { | ||
req.Database = database | ||
} | ||
|
||
if req.Env == nil { | ||
req.Env = map[string]string{} | ||
} | ||
req.Env["POSTGRES_USER"] = req.User | ||
req.Env["POSTGRES_PASSWORD"] = req.Password | ||
req.Env["POSTGRES_DB"] = req.Database | ||
|
||
if req.WaitingFor == nil { | ||
req.WaitingFor = wait.ForAll( | ||
wait.NewHostPortStrategy(port), | ||
wait.ForLog(logEntry).WithOccurrence(2), | ||
) | ||
} | ||
|
||
if req.Cmd == nil { | ||
req.Cmd = []string{"postgres", "-c", "fsync=off"} | ||
} | ||
|
||
provider, err := req.ProviderType.GetProvider() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c, err := provider.CreateContainer(ctx, req.ContainerRequest) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create container") | ||
} | ||
|
||
res := &Container{ | ||
Container: c, | ||
req: req, | ||
} | ||
|
||
if req.Started { | ||
if err := c.Start(ctx); err != nil { | ||
return res, errors.Wrap(err, "failed to start container") | ||
} | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/testcontainers/testcontainers-go" | ||
) | ||
|
||
func TestWriteIntoAPostgreSQLContainerViaDriver(t *testing.T) { | ||
|
||
ctx := context.Background() | ||
|
||
c, err := NewContainer(ctx, ContainerRequest{ | ||
Database: "hello", | ||
GenericContainerRequest: testcontainers.GenericContainerRequest{ | ||
Started: true, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
defer c.Container.Terminate(ctx) | ||
|
||
connectURL, err := c.ConnectURL(ctx) | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
connectURL += "?sslmode=disable" | ||
|
||
sqlC, err := sql.Open("postgres", connectURL) | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
defer sqlC.Close() | ||
|
||
_, err = sqlC.ExecContext(ctx, "CREATE TABLE example ( id integer, data varchar(32) )") | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
} | ||
|
||
func ExamplePostgreSQLContainerRequest() { | ||
|
||
// Optional | ||
containerRequest := testcontainers.ContainerRequest{ | ||
Image: "docker.io/library/postgres:11.5", | ||
} | ||
|
||
genericContainerRequest := testcontainers.GenericContainerRequest{ | ||
Started: true, | ||
ContainerRequest: containerRequest, | ||
} | ||
|
||
// Database, User, and Password are optional, | ||
// the driver will use default ones if not provided | ||
pgContainerRequest := ContainerRequest{ | ||
Database: "mycustomdatabase", | ||
User: "anyuser", | ||
Password: "yoursecurepassword", | ||
GenericContainerRequest: genericContainerRequest, | ||
} | ||
|
||
pgContainerRequest.Validate() | ||
} | ||
|
||
func ExampleNewPostgreSQLContainer() { | ||
ctx := context.Background() | ||
|
||
// Create your PostgreSQL database, | ||
// by setting Started this function will not return | ||
// until a test connection has been established | ||
c, _ := NewContainer(ctx, ContainerRequest{ | ||
Database: "hello", | ||
GenericContainerRequest: testcontainers.GenericContainerRequest{ | ||
Started: true, | ||
}, | ||
}) | ||
defer c.Container.Terminate(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters