Skip to content

Commit

Permalink
failfast
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermsi1337 committed Feb 14, 2025
1 parent bb6ff9a commit c06a009
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
42 changes: 34 additions & 8 deletions test/pkg/source/internal/testcommons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand All @@ -24,6 +25,25 @@ type TestContainerSetup struct {
Port string
}

func (t *TestContainerSetup) PrintLogs() {
reader, _ := t.Container.Logs(context.Background())

fmt.Printf("\n\n##### START CONTAINER LOGS #####\n\n")

buf := make([]byte, 4096)
for {
n, readErr := reader.Read(buf)
if readErr != nil {
if readErr == io.EOF {
break
}
}
fmt.Print(string(buf[:n]))
}

fmt.Printf("\n\n##### END CONTAINER LOGS #####\n\n")
}

// ResticReq is a testcontainers request for a restic container
var ResticReq = testcontainers.ContainerRequest{
Image: "restic/rest-server:latest",
Expand All @@ -37,10 +57,12 @@ var ResticReq = testcontainers.ContainerRequest{
// NewTestContainerSetup creates a TestContainerSetup which acts as a wrapper for the testcontainer specified by request
func NewTestContainerSetup(ctx context.Context, request *testcontainers.ContainerRequest, port nat.Port) (TestContainerSetup, error) {
result := TestContainerSetup{}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: *request,
Started: true,
})
container, err := testcontainers.GenericContainer(
ctx, testcontainers.GenericContainerRequest{
ContainerRequest: *request,
Started: true,
},
)
if err != nil {
return TestContainerSetup{}, err
}
Expand Down Expand Up @@ -70,10 +92,14 @@ func TestSetup() {

// DoResticRestore pulls the given backup from the given restic repo
func DoResticRestore(ctx context.Context, resticContainer TestContainerSetup, dataDir string) error {
cmd := exec.CommandContext(ctx, "restic", "restore", "-r", // nolint: gosec
fmt.Sprintf("rest:http://%s:%s/",
resticContainer.Address, resticContainer.Port),
"--target", dataDir, "latest")
cmd := exec.CommandContext(
ctx, "restic", "restore", "-r", // nolint: gosec
fmt.Sprintf(
"rest:http://%s:%s/",
resticContainer.Address, resticContainer.Port,
),
"--target", dataDir, "latest",
)
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Errorf("failed to execute restic restore: \n Output: %s \n Error: %s", out, err)
Expand Down
19 changes: 1 addition & 18 deletions test/pkg/source/mysqltest/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"database/sql"
"fmt"
"io"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -286,23 +285,7 @@ func mySQLDoRestore(
}

defer func() {
reader, _ := mySQLRestoreTarget.Container.Logs(context.Background())

fmt.Printf("\n\n##### START SQL CONTAINER LOGS #####\n\n")

buf := make([]byte, 4096)
for {
n, readErr := reader.Read(buf)
if readErr != nil {
if readErr == io.EOF {
break
}
log.Fatalf("Error reading logs: %v", err)
}
fmt.Print(string(buf[:n]))
}

fmt.Printf("\n\n##### END SQL CONTAINER LOGS #####\n\n")
mySQLRestoreTarget.PrintLogs()

restoreErr := mySQLRestoreTarget.Container.Terminate(ctx)
if restoreErr != nil {
Expand Down

0 comments on commit c06a009

Please sign in to comment.