-
-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#98 canned postgres #117
Closed
Closed
#98 canned postgres #117
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,115 @@ | ||
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 | ||
Version string | ||
User string | ||
Password string | ||
Database string | ||
} | ||
|
||
// should always be created via NewContainer | ||
type Container struct { | ||
Container testcontainers.Container | ||
req ContainerRequest | ||
} | ||
|
||
func (c Container) ConnectURL(ctx context.Context) (string, error) { | ||
template := "postgres://%s:%s@%s:%d/%s" | ||
|
||
host, err := c.Container.Host(ctx) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get host") | ||
} | ||
|
||
mappedPort, err := c.Container.MappedPort(ctx, port) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get mapped port for %s", port.Port()) | ||
} | ||
|
||
return fmt.Sprintf(template, c.req.User, c.req.Password, host, | ||
mappedPort.Int(), c.req.Database), nil | ||
} | ||
|
||
func NewContainer(ctx context.Context, req ContainerRequest) (*Container, error) { | ||
req.ExposedPorts = []string{string(port)} | ||
|
||
if req.Version != "" && req.FromDockerfile.Context == "" { | ||
req.Image = fmt.Sprintf("%s:%s", image, req.Version) | ||
} | ||
|
||
// 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 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,81 @@ | ||
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{ | ||
Version: "9.6.15-alpine", | ||
Database: "hello", | ||
}) | ||
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 ExampleContainerRequest() { | ||
|
||
// 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 ExampleNewContainer() { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
image = "redis" | ||
tag = "5.0.7" | ||
port = "6379/tcp" | ||
) | ||
|
||
// RedisContainerRequest completes GenericContainerRequest | ||
type ContainerRequest struct { | ||
testcontainers.GenericContainerRequest | ||
Version string | ||
} | ||
|
||
// Container should always be created via NewContainer | ||
type Container struct { | ||
Container testcontainers.Container | ||
req ContainerRequest | ||
} | ||
|
||
func (c Container) ConnectURL(ctx context.Context) (string, error) { | ||
host, err := c.Container.Host(ctx) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get host") | ||
} | ||
|
||
mappedPort, err := c.Container.MappedPort(ctx, port) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get mapped port for %s", port) | ||
} | ||
|
||
return fmt.Sprintf("%s:%d", host, mappedPort.Int()), nil | ||
} | ||
|
||
// NewContainer creates and (optionally) starts a Redis instance. | ||
func NewContainer(ctx context.Context, req ContainerRequest) (*Container, error) { | ||
|
||
provider, err := req.ProviderType.GetProvider() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// With the current logic it's not really possible to allow other ports... | ||
req.ExposedPorts = []string{port} | ||
|
||
if req.Env == nil { | ||
req.Env = map[string]string{} | ||
} | ||
|
||
if req.Version != "" && req.FromDockerfile.Context == "" { | ||
req.Image = fmt.Sprintf("%s:%s", image, req.Version) | ||
} | ||
|
||
if req.Image == "" && req.FromDockerfile.Context == "" { | ||
req.Image = fmt.Sprintf("%s:%s", image, tag) | ||
} | ||
|
||
req.WaitingFor = wait.NewHostPortStrategy(port) | ||
|
||
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 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,55 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"github.com/go-redis/redis" | ||
"gotest.tools/assert" | ||
"testing" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
) | ||
|
||
func TestSetInRedis(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
c, err := NewContainer(ctx, ContainerRequest{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer c.Container.Terminate(ctx) | ||
|
||
addr, err := c.ConnectURL(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
client := redis.NewClient(&redis.Options{ | ||
Addr: addr, | ||
DB: 0, | ||
}) | ||
defer client.Close() | ||
|
||
err = client.Set("key", "value", 0).Err() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
got := client.Get("key") | ||
if got.Err() != nil { | ||
t.Fatal(got.Err()) | ||
} | ||
|
||
assert.Equal(t, got.Val(), "value") | ||
} | ||
|
||
func ExampleNewContainer() { | ||
ctx := context.Background() | ||
|
||
c, _ := NewContainer(ctx, ContainerRequest{ | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you think that it will be better if we use some fresher versions by default? Like 12.1-alpine, for example.