mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-09 13:36:56 +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:
parent
89e5a4ebf1
commit
16f1304498
@ -3,7 +3,6 @@ package blob
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
@ -42,13 +41,3 @@ func (Pipe) Publish(ctx *context.Context) error {
|
||||
}
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
// errorContains check if error contains specific string.
|
||||
func errorContains(err error, subs ...string) bool {
|
||||
for _, sub := range subs {
|
||||
if strings.Contains(err.Error(), sub) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -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))
|
||||
|
@ -1,12 +1,9 @@
|
||||
package blob
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/goreleaser/goreleaser/pkg/config"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -16,6 +13,25 @@ func TestDescription(t *testing.T) {
|
||||
require.NotEmpty(t, Pipe{}.String())
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
for k, v := range map[string]string{
|
||||
"NoSuchBucket": "provided bucket does not exist: someurl: NoSuchBucket",
|
||||
"ContainerNotFound": "provided bucket does not exist: someurl: ContainerNotFound",
|
||||
"notFound": "provided bucket does not exist: someurl: notFound",
|
||||
"NoCredentialProviders": "check credentials and access to bucket: someurl: NoCredentialProviders",
|
||||
"InvalidAccessKeyId": "aws access key id you provided does not exist in our records: InvalidAccessKeyId",
|
||||
"AuthenticationFailed": "azure storage key you provided is not valid: AuthenticationFailed",
|
||||
"invalid_grant": "google app credentials you provided is not valid: invalid_grant",
|
||||
"no such host": "azure storage account you provided is not valid: no such host",
|
||||
"ServiceCode=ResourceNotFound": "missing azure storage key for provided bucket someurl: ServiceCode=ResourceNotFound",
|
||||
"other": "failed to write to bucket: other",
|
||||
} {
|
||||
t.Run(k, func(t *testing.T) {
|
||||
require.EqualError(t, handleError(fmt.Errorf(k), "someurl"), v)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultsNoConfig(t *testing.T) {
|
||||
errorString := "bucket or provider cannot be empty"
|
||||
ctx := context.New(config.Project{
|
||||
@ -98,150 +114,6 @@ func TestDefaultsWithProvider(t *testing.T) {
|
||||
require.Nil(t, Pipe{}.Default(ctx))
|
||||
}
|
||||
|
||||
func TestPipe_Publish(t *testing.T) {
|
||||
pipePublish(t, []config.ExtraFile{})
|
||||
}
|
||||
|
||||
func TestPipe_PublishExtraFiles(t *testing.T) {
|
||||
extra := []config.ExtraFile{
|
||||
{
|
||||
Glob: "./testdata/file.golden",
|
||||
},
|
||||
}
|
||||
pipePublish(t, extra)
|
||||
}
|
||||
|
||||
func pipePublish(t *testing.T, extra []config.ExtraFile) {
|
||||
t.Helper()
|
||||
gcloudCredentials, _ := filepath.Abs("./testdata/credentials.json")
|
||||
|
||||
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))
|
||||
|
||||
// Azure Blob Context
|
||||
azblobctx := context.New(config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: "testupload",
|
||||
Blobs: []config.Blob{
|
||||
{
|
||||
Bucket: "foo",
|
||||
Provider: "azblob",
|
||||
ExtraFiles: extra,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
azblobctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
||||
azblobctx.Artifacts.Add(&artifact.Artifact{
|
||||
Type: artifact.UploadableArchive,
|
||||
Name: "bin.tar.gz",
|
||||
Path: tgzpath,
|
||||
})
|
||||
azblobctx.Artifacts.Add(&artifact.Artifact{
|
||||
Type: artifact.LinuxPackage,
|
||||
Name: "bin.deb",
|
||||
Path: debpath,
|
||||
})
|
||||
|
||||
// Google Cloud Storage Context
|
||||
gsctx := context.New(config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: "testupload",
|
||||
Blobs: []config.Blob{
|
||||
{
|
||||
Bucket: "foo",
|
||||
Provider: "gs",
|
||||
ExtraFiles: extra,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
gsctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
||||
|
||||
gsctx.Artifacts.Add(&artifact.Artifact{
|
||||
Type: artifact.UploadableArchive,
|
||||
Name: "bin.tar.gz",
|
||||
Path: tgzpath,
|
||||
})
|
||||
gsctx.Artifacts.Add(&artifact.Artifact{
|
||||
Type: artifact.LinuxPackage,
|
||||
Name: "bin.deb",
|
||||
Path: debpath,
|
||||
})
|
||||
|
||||
// AWS S3 Context
|
||||
s3ctx := context.New(config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: "testupload",
|
||||
Blobs: []config.Blob{
|
||||
{
|
||||
Bucket: "foo",
|
||||
Provider: "s3",
|
||||
ExtraFiles: extra,
|
||||
},
|
||||
},
|
||||
})
|
||||
s3ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
||||
s3ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Type: artifact.UploadableArchive,
|
||||
Name: "bin.tar.gz",
|
||||
Path: tgzpath,
|
||||
})
|
||||
s3ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Type: artifact.LinuxPackage,
|
||||
Name: "bin.deb",
|
||||
Path: debpath,
|
||||
})
|
||||
|
||||
type args struct {
|
||||
ctx *context.Context
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
env map[string]string
|
||||
wantErr bool
|
||||
wantErrString string
|
||||
}{
|
||||
{
|
||||
name: "Azure Blob Bucket Test Publish",
|
||||
args: args{azblobctx},
|
||||
env: map[string]string{"AZURE_STORAGE_ACCOUNT": "hjsdhjsdhs", "AZURE_STORAGE_KEY": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2"},
|
||||
wantErr: false,
|
||||
wantErrString: "azure storage account you provided is not valid",
|
||||
},
|
||||
{
|
||||
name: "Google Cloud Storage Bucket Test Publish",
|
||||
args: args{gsctx},
|
||||
env: map[string]string{"GOOGLE_APPLICATION_CREDENTIALS": gcloudCredentials},
|
||||
wantErr: false,
|
||||
wantErrString: "google app credentials you provided is not valid",
|
||||
},
|
||||
{
|
||||
name: "AWS S3 Bucket Test Publish",
|
||||
args: args{s3ctx},
|
||||
env: map[string]string{"AWS_ACCESS_KEY": "WPXKJC7CZQCFPKY5727N", "AWS_SECRET_KEY": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2", "AWS_REGION": "us-east-1"},
|
||||
wantErr: false,
|
||||
wantErrString: "aws access key id you provided does not exist in our records",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := Pipe{}
|
||||
setEnv(tt.env)
|
||||
defer unsetEnv(tt.env)
|
||||
if err := p.Publish(tt.args.ctx); (err != nil) != tt.wantErr {
|
||||
if !strings.HasPrefix(err.Error(), tt.wantErrString) {
|
||||
t.Errorf("Pipe.Publish() error = %v, wantErr %v", err, tt.wantErrString)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestURL(t *testing.T) {
|
||||
t.Run("s3 with opts", func(t *testing.T) {
|
||||
url, err := urlFor(context.New(config.Project{}), config.Blob{
|
||||
@ -311,15 +183,3 @@ func TestSkip(t *testing.T) {
|
||||
require.False(t, Pipe{}.Skip(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func setEnv(env map[string]string) {
|
||||
for k, v := range env {
|
||||
os.Setenv(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func unsetEnv(env map[string]string) {
|
||||
for k := range env {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
}
|
||||
|
@ -139,6 +139,16 @@ func uploadData(ctx *context.Context, conf config.Blob, up uploader, dataFile, u
|
||||
return err
|
||||
}
|
||||
|
||||
// errorContains check if error contains specific string.
|
||||
func errorContains(err error, subs ...string) bool {
|
||||
for _, sub := range subs {
|
||||
if strings.Contains(err.Error(), sub) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func handleError(err error, url string) error {
|
||||
switch {
|
||||
case errorContains(err, "NoSuchBucket", "ContainerNotFound", "notFound"):
|
||||
@ -212,7 +222,7 @@ func (u *productionUploader) Open(ctx *context.Context, bucket string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *productionUploader) Upload(ctx *context.Context, filepath string, data []byte) (err error) {
|
||||
func (u *productionUploader) Upload(ctx *context.Context, filepath string, data []byte) error {
|
||||
log.WithField("path", filepath).Info("uploading")
|
||||
|
||||
opts := &blob.WriterOptions{
|
||||
@ -228,5 +238,5 @@ func (u *productionUploader) Upload(ctx *context.Context, filepath string, data
|
||||
}
|
||||
}()
|
||||
_, err = w.Write(data)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user