Skip to content

Commit

Permalink
Merge branch 'main' into examples-work
Browse files Browse the repository at this point in the history
* main:
  chore: bump version in mkdocs (#630)
  docs: remove code snippets from main README (#631)
  docs: document replace directive for Docker Compose (#632)
  • Loading branch information
mdelapenya committed Nov 23, 2022
2 parents 58c00fa + ebc3dec commit bf5f4f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 82 deletions.
82 changes: 1 addition & 81 deletions README.md
Expand Up @@ -6,85 +6,5 @@ _Testcontainers for Go_ is a Go package that makes it simple to create and clean
automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers
that should be run as part of a test and clean up those resources when the test is done.

Here's an example of a test that spins up an NGINX container validates that it returns 200 for the status code:

```go
package main

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

type nginxContainer struct {
testcontainers.Container
URI string
}

func setupNginx(ctx context.Context) (*nginxContainer, error) {
req := testcontainers.ContainerRequest{
Image: "nginx",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForHTTP("/"),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}

ip, err := container.Host(ctx)
if err != nil {
return nil, err
}

mappedPort, err := container.MappedPort(ctx, "80")
if err != nil {
return nil, err
}

uri := fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())

return &nginxContainer{Container: container, URI: uri}, nil
}

func TestIntegrationNginxLatestReturn(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

ctx := context.Background()

nginxC, err := setupNginx(ctx)
if err != nil {
t.Fatal(err)
}

// Clean up the container after the test is complete
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %v", err)
}
}()

resp, err := http.Get(nginxC.URI)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
}
```

Cleaning up your environment after test completion should be accomplished by deferring the container termination, e.g
`defer nginxC.Terminate(ctx)`. Reaper (Ryuk) is also enabled by default to help clean up.

## Documentation

More information about _Testcontainers for Go_ can be found in [./docs](./docs), which is rendered at
You can find more information about _Testcontainers for Go_ in the [./docs](./docs) directory, which is rendered at
[golang.testcontainers.org](https://golang.testcontainers.org).
25 changes: 25 additions & 0 deletions docs/quickstart/gotest.md
Expand Up @@ -13,6 +13,31 @@ We use [gomod](https://blog.golang.org/using-go-modules) and you can get it inst
go get github.com/testcontainers/testcontainers-go
```

!!!warning

Given the version includes the Compose dependency, and the Docker folks added [a replace directive until the upcoming Docker 22.06 release is out](https://github.com/docker/compose/issues/9946#issuecomment-1288923912),
we were forced to add it too, causing consumers of _Testcontainers for Go_ to add the following replace directive to their `go.mod` files.
We expect this to be removed in the next releases of _Testcontainers for Go_.

```
replace (
github.com/docker/cli => github.com/docker/cli v20.10.3-0.20221013132413-1d6c6e2367e2+incompatible // 22.06 master branch
github.com/docker/docker => github.com/docker/docker v20.10.3-0.20221013203545-33ab36d6b304+incompatible // 22.06 branch
github.com/moby/buildkit => github.com/moby/buildkit v0.10.1-0.20220816171719-55ba9d14360a // same as buildx

github.com/opencontainers/runc => github.com/opencontainers/runc v1.1.2 // Can be removed on next bump of containerd to > 1.6.4

// For k8s dependencies, we use a replace directive, to prevent them being
// upgraded to the version specified in containerd, which is not relevant to the
// version needed.
// See https://github.com/docker/buildx/pull/948 for details.
// https://github.com/docker/buildx/blob/v0.8.1/go.mod#L62-L64
k8s.io/api => k8s.io/api v0.22.4
k8s.io/apimachinery => k8s.io/apimachinery v0.22.4
k8s.io/client-go => k8s.io/client-go v0.22.4
)
```

## 2. Spin up Redis

```go
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Expand Up @@ -62,4 +62,4 @@ nav:
- Getting help: getting_help.md
edit_uri: edit/main/docs/
extra:
latest_version: 0.15.0
latest_version: 0.16.0

0 comments on commit bf5f4f0

Please sign in to comment.