Skip to content
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

Add a testcontainers_wiremock#RunContainerAndStopOnCleanup() method to simplify automatic termination #30

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions examples/quickstart/quickstart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ package testcontainers_wiremock_quickstart

import (
"context"
. "github.com/wiremock/wiremock-testcontainers-go"
"testing"

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

func TestWireMock(t *testing.T) {
// Create Container
ctx := context.Background()
container, err := RunContainer(ctx,
WithMappingFile("hello", "hello-world.json"),
)
mappingFileName := "hello-world.json"

container, err := RunContainerAndStopOnCleanup(ctx, t, mappingFileName)
if err != nil {
t.Fatal(err)
}

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

// Send a simple HTTP GET request to the mocked API
statusCode, out, err := SendHttpGet(container, "/hello", nil)
if err != nil {
t.Fatal(err, "Failed to get a response")
Expand Down
19 changes: 19 additions & 0 deletions tc-wiremock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"path/filepath"
"testing"

"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -180,3 +181,21 @@ func addQueryParamsToURL(endpoint string, queryParams map[string]string) (string

return parsedURL.String(), nil
}

func RunContainerAndStopOnCleanup(ctx context.Context, t *testing.T, id string, mappingFileName string) (testcontainers.Container, error) {
container, err := RunContainer(ctx,
WithMappingFile(id, mappingFileName),
)
if err != nil {
t.Fatal(err)
return nil, err
}

t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

return container, nil
}