1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00
goreleaser/internal/pipe/s3/s3_test.go

237 lines
6.1 KiB
Go
Raw Normal View History

2018-05-13 13:08:14 -03:00
package s3
import (
2018-05-13 15:40:14 -03:00
"io/ioutil"
"net"
2018-05-13 15:40:14 -03:00
"os"
"os/exec"
"path/filepath"
2018-05-13 16:41:45 -03:00
"strings"
2018-05-13 13:08:14 -03:00
"testing"
2018-05-13 15:40:14 -03:00
"time"
2018-05-13 13:08:14 -03:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
2018-05-13 15:40:14 -03:00
"github.com/goreleaser/goreleaser/internal/artifact"
2018-09-04 09:34:09 -03:00
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2018-05-13 13:08:14 -03:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-05-13 13:08:14 -03:00
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
}
func TestNoS3(t *testing.T) {
2018-10-16 20:50:49 -03:00
testlib.AssertSkipped(t, Pipe{}.Publish(context.New(config.Project{})))
2018-05-13 13:08:14 -03:00
}
2018-05-13 14:44:49 -03:00
func TestDefaultsNoS3(t *testing.T) {
var assert = assert.New(t)
var ctx = context.New(config.Project{
S3: []config.S3{
{},
2018-05-13 14:44:49 -03:00
},
})
assert.NoError(Pipe{}.Default(ctx))
assert.Equal([]config.S3{{}}, ctx.Config.S3)
2018-05-13 14:44:49 -03:00
}
func TestDefaults(t *testing.T) {
var assert = assert.New(t)
var ctx = context.New(config.Project{
S3: []config.S3{
{
Bucket: "foo",
},
},
})
assert.NoError(Pipe{}.Default(ctx))
assert.Equal([]config.S3{{
2018-05-13 14:44:49 -03:00
Bucket: "foo",
Region: "us-east-1",
Folder: "{{ .ProjectName }}/{{ .Tag }}",
2018-07-26 10:04:25 -03:00
ACL: "private",
2018-05-13 14:44:49 -03:00
}}, ctx.Config.S3)
}
2018-05-13 15:40:14 -03:00
func TestUpload(t *testing.T) {
var listen = randomListen(t)
2018-05-13 15:40:14 -03:00
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
tgzpath := filepath.Join(folder, "bin.tar.gz")
debpath := filepath.Join(folder, "bin.deb")
assert.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
assert.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
var ctx = context.New(config.Project{
Dist: folder,
ProjectName: "testupload",
S3: []config.S3{
{
Bucket: "test",
Endpoint: "http://" + listen,
IDs: []string{"foo", "bar"},
2018-05-13 15:40:14 -03:00
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Artifacts.Add(&artifact.Artifact{
2018-05-13 15:40:14 -03:00
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tgzpath,
Extra: map[string]interface{}{
"ID": "foo",
},
2018-05-13 15:40:14 -03:00
})
ctx.Artifacts.Add(&artifact.Artifact{
2018-05-13 15:40:14 -03:00
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debpath,
Extra: map[string]interface{}{
"ID": "bar",
},
2018-05-13 15:40:14 -03:00
})
var name = "test_upload"
defer stop(t, name)
start(t, name, listen)
prepareEnv(t, listen)
2018-05-13 15:40:14 -03:00
assert.NoError(t, Pipe{}.Default(ctx))
2018-10-16 20:50:49 -03:00
assert.NoError(t, Pipe{}.Publish(ctx))
2018-05-13 15:40:14 -03:00
}
2019-01-21 17:34:26 +01:00
func TestUploadCustomBucketID(t *testing.T) {
var listen = randomListen(t)
2019-01-21 17:34:26 +01:00
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
tgzpath := filepath.Join(folder, "bin.tar.gz")
debpath := filepath.Join(folder, "bin.deb")
assert.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
assert.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
// Set custom BUCKET_ID env variable.
err = os.Setenv("BUCKET_ID", "test")
assert.NoError(t, err)
var ctx = context.New(config.Project{
Dist: folder,
ProjectName: "testupload",
S3: []config.S3{
{
Bucket: "{{.Env.BUCKET_ID}}",
Endpoint: "http://" + listen,
2019-01-21 17:34:26 +01:00
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Artifacts.Add(&artifact.Artifact{
2019-01-21 17:34:26 +01:00
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tgzpath,
})
ctx.Artifacts.Add(&artifact.Artifact{
2019-01-21 17:34:26 +01:00
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debpath,
})
var name = "custom_bucket_id"
defer stop(t, name)
start(t, name, listen)
prepareEnv(t, listen)
2019-01-21 17:34:26 +01:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.NoError(t, Pipe{}.Publish(ctx))
}
2019-01-21 21:10:35 +01:00
func TestUploadInvalidCustomBucketID(t *testing.T) {
var listen = randomListen(t)
2019-01-21 21:10:35 +01:00
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
tgzpath := filepath.Join(folder, "bin.tar.gz")
debpath := filepath.Join(folder, "bin.deb")
assert.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
assert.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
// Set custom BUCKET_ID env variable.
assert.NoError(t, err)
var ctx = context.New(config.Project{
Dist: folder,
ProjectName: "testupload",
S3: []config.S3{
{
Bucket: "{{.Bad}}",
Endpoint: "http://" + listen,
2019-01-21 21:10:35 +01:00
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Artifacts.Add(&artifact.Artifact{
2019-01-21 21:10:35 +01:00
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tgzpath,
})
ctx.Artifacts.Add(&artifact.Artifact{
2019-01-21 21:10:35 +01:00
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debpath,
})
var name = "invalid_bucket_id"
defer stop(t, name)
start(t, name, listen)
prepareEnv(t, listen)
2019-01-21 21:10:35 +01:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.Error(t, Pipe{}.Publish(ctx))
}
func randomListen(t *testing.T) string {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
listener.Close()
return listener.Addr().String()
}
func prepareEnv(t *testing.T, listen string) {
os.Setenv("AWS_ACCESS_KEY_ID", "minio")
os.Setenv("AWS_SECRET_ACCESS_KEY", "miniostorage")
2018-05-13 16:09:37 -03:00
os.Setenv("AWS_REGION", "us-east-1")
t.Log("creating test bucket")
_, err := newS3Svc(config.S3{
Endpoint: "http://" + listen,
Region: "us-east-1",
}).CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("test"),
})
require.NoError(t, err)
2018-05-13 16:09:37 -03:00
}
func start(t *testing.T, name, listen string) {
2018-05-13 15:40:14 -03:00
if out, err := exec.Command(
"docker", "run", "-d", "--rm",
"--name", name,
"-p", listen+":9000",
"-e", "MINIO_ACCESS_KEY=minio",
"-e", "MINIO_SECRET_KEY=miniostorage",
"--health-interval", "1s",
"minio/minio:RELEASE.2019-05-14T23-57-45Z",
2018-05-13 15:40:14 -03:00
"server", "/data",
).CombinedOutput(); err != nil {
t.Fatalf("failed to start minio: %s", string(out))
2018-05-13 15:40:14 -03:00
}
2018-05-13 16:41:45 -03:00
for range time.Tick(time.Second) {
out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", name).CombinedOutput()
2018-05-13 16:41:45 -03:00
if err != nil {
t.Fatalf("failed to check minio status: %s", string(out))
2018-05-13 16:41:45 -03:00
}
if strings.Contains(string(out), `"Status":"healthy"`) {
t.Log("minio is healthy")
2018-05-13 16:41:45 -03:00
break
}
t.Log("waiting for minio to be healthy")
2018-05-13 16:41:45 -03:00
}
2018-05-13 15:40:14 -03:00
}
func stop(t *testing.T, name string) {
if out, err := exec.Command("docker", "stop", name).CombinedOutput(); err != nil {
t.Fatalf("failed to stop minio: %s", string(out))
2018-05-13 15:40:14 -03:00
}
}