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

Dump controller logs after acceptance tests #335

Merged
merged 12 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/acctest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:

- name: TF acceptance tests
timeout-minutes: 20
run: make testacc UNIFI_VERSION=${{ matrix.unifi_version }}
run: make testacc UNIFI_STDOUT=true UNIFI_VERSION=${{ matrix.unifi_version }}
30 changes: 25 additions & 5 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"bytes"
"context"
"fmt"
"math"
Expand All @@ -14,7 +15,8 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/paultyng/go-unifi/unifi"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/compose"
)

var providerFactories = map[string]func() (*schema.Provider, error){
Expand All @@ -35,29 +37,47 @@ func TestMain(m *testing.M) {
}

func runAcceptanceTests(m *testing.M) int {
compose, err := tc.NewDockerCompose("../../docker-compose.yaml")
dc, err := compose.NewDockerCompose("../../docker-compose.yaml")
if err != nil {
panic(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err = compose.WithOsEnv().Up(ctx, tc.Wait(true)); err != nil {
if err = dc.WithOsEnv().Up(ctx, compose.Wait(true)); err != nil {
panic(err)
}

defer func() {
if err := compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal); err != nil {
if err := dc.Down(context.Background(), compose.RemoveOrphans(true), compose.RemoveImagesLocal); err != nil {
panic(err)
}
}()

container, err := compose.ServiceContainer(ctx, "unifi")
container, err := dc.ServiceContainer(ctx, "unifi")
if err != nil {
panic(err)
}

// Dump the container logs on exit.
//
// TODO: Use https://pkg.go.dev/github.com/testcontainers/testcontainers-go#LogConsumer instead.
defer func() {
if os.Getenv("UNIFI_STDOUT") == "" {
return
}

stream, err := container.Logs(ctx)
if err != nil {
panic(err)
}

buffer := new(bytes.Buffer)
buffer.ReadFrom(stream)
testcontainers.Logger.Printf("%s", buffer)
}()

endpoint, err := container.PortEndpoint(ctx, "8443/tcp", "https")
if err != nil {
panic(err)
Expand Down