Skip to content

Commit

Permalink
integration: Remove dangling container and container names (#19)
Browse files Browse the repository at this point in the history
Fixes a flaky minio error when running `go test -v ./...` in rapid
succession due to a bucket already existing.

```
$ go test -v ./...
minio is up
=== RUN   TestIntegration
    integration_test.go:141: operation error S3: CreateBucket, https response error StatusCode: 409, RequestID: 1782341B6096C63C, HostID: dd9025bab4ad464b049177c95eb6ebf374d3b3fd1af9251148b658df7ac2e3e8, BucketAlreadyOwnedByYou: 
--- FAIL: TestIntegration (0.00s)
```
  • Loading branch information
pgporada authored Sep 7, 2023
1 parent 7c4dc15 commit 64a8481
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"reflect"
"strconv"
Expand All @@ -27,48 +25,48 @@ import (
"golang.org/x/sync/singleflight"
)

func TestMain(m *testing.M) {
cmd := exec.Command("podman", "run", "-p", "19085:9000", "quay.io/minio/minio", "server", "/data")
stderrPipe, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
const containerName string = "ctile_integration_test_minio"
const testLogSaysPastTheEnd string = "oh no! we fell off the end of the log!"

func startContainer(t *testing.T) {
_, err := exec.Command("podman", "run", "--rm", "--detach", "-p", "19085:9000", "--name", containerName, "quay.io/minio/minio", "server", "/data").Output()
if err != nil {
panic(err)
t.Fatalf("minio failed to come up: %v", err)
}
defer cmd.Process.Kill()
for i := 0; i < 1000; i++ {
_, err := net.Dial("tcp", "localhost:19085")
if errors.Is(err, syscall.ECONNREFUSED) {
t.Log("sleeping 10ms waiting for minio to come up")
time.Sleep(10 * time.Millisecond)
continue
}
if err != nil {
panic(err)
t.Fatalf("failed to connect to minio: %v", err)
}
fmt.Println("minio is up")
break
}
code := m.Run()
err = cmd.Process.Signal(os.Interrupt)
if err != nil {
panic(err)
t.Log("minio is up")
return
}
io.Copy(os.Stderr, stderrPipe)
processState, err := cmd.Process.Wait()
if err != nil {
panic(err)
}
if processState.ExitCode() != 0 {
panic(fmt.Errorf("minio exited with code %d", processState.ExitCode()))
}
os.Exit(code)
t.Fatalf("failed to connect to minio: %v", err)
}

const testLogSaysPastTheEnd = "oh no! we fell off the end of the log!"
// cleanupContainer stops a running named container and removes its assigned
// name. This is helpful in the event that a container wasn't properly killed
// during a previous test run or if manual testing was being performed and not
// cleaned up.
func cleanupContainer() {
// Unconditionally stop the container.
_, _ = exec.Command("podman", "stop", containerName).Output()

// Unconditionally remove the container name if the operator did manual
// container testing, but didn't clean up the name.
_, _ = exec.Command("podman", "rm", containerName).Output()
}

func TestIntegration(t *testing.T) {
cleanupContainer() // Clean up old containers and names just in case.
startContainer(t)
defer cleanupContainer()

// A test CT server that responds to get-entries requests with appropriately JSON-formatted
// data, where base64-decoding the LeafInput and ExtraData fields yields a binary encoding
// of the position of the given element.
Expand Down

0 comments on commit 64a8481

Please sign in to comment.