You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-07-03 00:57:43 +02:00
test: speed up minio tests (#2550)
* test: speed up minio tests Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * test: speed up minio tests Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * test: speed up minio tests Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * fix: rm unused param Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * fix: use not-so-common contianer name Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * test: speed Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * test: inc coverage Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * test: inc coverage Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
committed by
GitHub
parent
89e5a4ebf1
commit
16f1304498
@ -1,12 +1,9 @@
|
||||
package blob
|
||||
|
||||
// this is pretty much copied from the s3 pipe to ensure both work the same way
|
||||
// only differences are that it sets `blobs` instead of `s3` on test cases and
|
||||
// the test setup and teardown
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -23,12 +20,39 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
minioUser = "minio"
|
||||
minioPwd = "miniostorage"
|
||||
minioUser = "minio"
|
||||
minioPwd = "miniostorage"
|
||||
containerName = "goreleaserTestMinio"
|
||||
)
|
||||
|
||||
var listen string
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
listener.Close()
|
||||
listen = listener.Addr().String()
|
||||
|
||||
cleanup, err := start(listen)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
prepareEnv()
|
||||
|
||||
code := m.Run()
|
||||
if err := cleanup(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestMinioUpload(t *testing.T) {
|
||||
listen := randomListen(t)
|
||||
name := "basic"
|
||||
folder := t.TempDir()
|
||||
srcpath := filepath.Join(folder, "source.tar.gz")
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
@ -44,10 +68,15 @@ func TestMinioUpload(t *testing.T) {
|
||||
Blobs: []config.Blob{
|
||||
{
|
||||
Provider: "s3",
|
||||
Bucket: "test",
|
||||
Bucket: name,
|
||||
Region: "us-east",
|
||||
Endpoint: "http://" + listen,
|
||||
IDs: []string{"foo", "bar"},
|
||||
ExtraFiles: []config.ExtraFile{
|
||||
{
|
||||
Glob: "./testdata/*.golden",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
@ -81,9 +110,8 @@ func TestMinioUpload(t *testing.T) {
|
||||
"ID": "bar",
|
||||
},
|
||||
})
|
||||
name := "test_upload"
|
||||
start(t, name, listen)
|
||||
prepareEnv()
|
||||
|
||||
setupBucket(t, name)
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
|
||||
@ -92,18 +120,19 @@ func TestMinioUpload(t *testing.T) {
|
||||
"testupload/v1.0.0/bin.tar.gz",
|
||||
"testupload/v1.0.0/checksum.txt",
|
||||
"testupload/v1.0.0/source.tar.gz",
|
||||
"testupload/v1.0.0/file.golden",
|
||||
})
|
||||
}
|
||||
|
||||
func TestMinioUploadCustomBucketID(t *testing.T) {
|
||||
listen := randomListen(t)
|
||||
name := "fromenv"
|
||||
folder := t.TempDir()
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
require.NoError(t, os.WriteFile(tgzpath, []byte("fake\ntargz"), 0o744))
|
||||
require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744))
|
||||
// Set custom BUCKET_ID env variable.
|
||||
require.NoError(t, os.Setenv("BUCKET_ID", "test"))
|
||||
require.NoError(t, os.Setenv("BUCKET_ID", name))
|
||||
ctx := context.New(config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: "testupload",
|
||||
@ -126,15 +155,14 @@ func TestMinioUploadCustomBucketID(t *testing.T) {
|
||||
Name: "bin.deb",
|
||||
Path: debpath,
|
||||
})
|
||||
name := "custom_bucket_id"
|
||||
start(t, name, listen)
|
||||
prepareEnv()
|
||||
|
||||
setupBucket(t, name)
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
}
|
||||
|
||||
func TestMinioUploadRootFolder(t *testing.T) {
|
||||
listen := randomListen(t)
|
||||
name := "rootdir"
|
||||
folder := t.TempDir()
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
@ -146,7 +174,7 @@ func TestMinioUploadRootFolder(t *testing.T) {
|
||||
Blobs: []config.Blob{
|
||||
{
|
||||
Provider: "s3",
|
||||
Bucket: "test",
|
||||
Bucket: name,
|
||||
Folder: "/",
|
||||
Endpoint: "http://" + listen,
|
||||
},
|
||||
@ -163,15 +191,13 @@ func TestMinioUploadRootFolder(t *testing.T) {
|
||||
Name: "bin.deb",
|
||||
Path: debpath,
|
||||
})
|
||||
name := "root_folder"
|
||||
start(t, name, listen)
|
||||
prepareEnv()
|
||||
|
||||
setupBucket(t, name)
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
}
|
||||
|
||||
func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
|
||||
listen := randomListen(t)
|
||||
folder := t.TempDir()
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
@ -199,45 +225,37 @@ func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
|
||||
Name: "bin.deb",
|
||||
Path: debpath,
|
||||
})
|
||||
name := "invalid_bucket_id"
|
||||
start(t, name, listen)
|
||||
prepareEnv()
|
||||
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.Error(t, Pipe{}.Publish(ctx))
|
||||
}
|
||||
|
||||
func randomListen(t *testing.T) string {
|
||||
t.Helper()
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
listener.Close()
|
||||
return listener.Addr().String()
|
||||
}
|
||||
|
||||
func prepareEnv() {
|
||||
os.Setenv("AWS_ACCESS_KEY_ID", minioUser)
|
||||
os.Setenv("AWS_SECRET_ACCESS_KEY", minioPwd)
|
||||
os.Setenv("AWS_REGION", "us-east-1")
|
||||
}
|
||||
|
||||
func start(tb testing.TB, name, listen string) {
|
||||
tb.Helper()
|
||||
func start(listen string) (func() error, error) {
|
||||
data := filepath.Join(os.TempDir(), containerName)
|
||||
|
||||
data := filepath.Join(os.TempDir(), name)
|
||||
tb.Cleanup(func() {
|
||||
mc(tb, name, "mc rb --force local/test")
|
||||
if out, err := exec.Command("docker", "stop", name).CombinedOutput(); err != nil {
|
||||
tb.Fatalf("failed to stop minio: %s", string(out))
|
||||
fn := func() error {
|
||||
if out, err := exec.Command("docker", "stop", containerName).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("failed to stop minio: %s: %w", out, err)
|
||||
}
|
||||
if err := os.RemoveAll(data); err != nil {
|
||||
tb.Logf("failed to remove %s", data)
|
||||
log.Println("failed to remove", data)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// stop container if it is running (likely from previous test)
|
||||
_, _ = exec.Command("docker", "stop", containerName).CombinedOutput()
|
||||
|
||||
if out, err := exec.Command(
|
||||
"docker", "run", "-d", "--rm",
|
||||
"-v", data+":/data",
|
||||
"--name", name,
|
||||
"--name", containerName,
|
||||
"-p", listen+":9000",
|
||||
"-e", "MINIO_ROOT_USER="+minioUser,
|
||||
"-e", "MINIO_ROOT_PASSWORD="+minioPwd,
|
||||
@ -246,35 +264,43 @@ func start(tb testing.TB, name, listen string) {
|
||||
"minio/minio",
|
||||
"server", "/data", "--console-address", ":9001",
|
||||
).CombinedOutput(); err != nil {
|
||||
tb.Fatalf("failed to start minio: %s", string(out))
|
||||
return fn, fmt.Errorf("failed to start minio: %s: %w", out, err)
|
||||
}
|
||||
|
||||
for range time.Tick(time.Second) {
|
||||
out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", name).CombinedOutput()
|
||||
out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", containerName).CombinedOutput()
|
||||
if err != nil {
|
||||
tb.Fatalf("failed to check minio status: %s", string(out))
|
||||
return fn, fmt.Errorf("failed to check minio status: %s: %w", string(out), err)
|
||||
}
|
||||
if strings.Contains(string(out), `"Status":"healthy"`) {
|
||||
tb.Log("minio is healthy")
|
||||
log.Println("minio is healthy")
|
||||
break
|
||||
}
|
||||
tb.Log("waiting for minio to be healthy")
|
||||
log.Println("waiting for minio to be healthy")
|
||||
}
|
||||
|
||||
mc(tb, name, "mc mb local/test")
|
||||
return fn, nil
|
||||
}
|
||||
|
||||
func mc(tb testing.TB, name, cmd string) {
|
||||
func setupBucket(tb testing.TB, name string) {
|
||||
tb.Helper()
|
||||
mc(tb, "mc mb local/"+name)
|
||||
tb.Cleanup(func() {
|
||||
mc(tb, "mc rb --force local/"+name)
|
||||
})
|
||||
}
|
||||
|
||||
func mc(tb testing.TB, cmd string) {
|
||||
tb.Helper()
|
||||
|
||||
if out, err := exec.Command(
|
||||
"docker", "run", "--rm",
|
||||
"--link", name,
|
||||
"--link", containerName,
|
||||
"--entrypoint", "sh",
|
||||
"minio/mc",
|
||||
"-c", fmt.Sprintf(
|
||||
"mc config host add local http://%s:9000 %s %s; %s",
|
||||
name, minioUser, minioPwd, cmd,
|
||||
containerName, minioUser, minioPwd, cmd,
|
||||
),
|
||||
).CombinedOutput(); err != nil {
|
||||
tb.Fatalf("failed to create test bucket: %s", string(out))
|
||||
|
Reference in New Issue
Block a user